forked from kubernetes/ingress-nginx
/
main.go
234 lines (197 loc) · 5.9 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package lint
import (
"fmt"
"github.com/spf13/cobra"
appsv1 "k8s.io/api/apps/v1"
networking "k8s.io/api/networking/v1"
kmeta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/ingress-nginx/cmd/plugin/lints"
"k8s.io/ingress-nginx/cmd/plugin/request"
"k8s.io/ingress-nginx/cmd/plugin/util"
"k8s.io/ingress-nginx/version"
)
// CreateCommand creates and returns this cobra subcommand
func CreateCommand(flags *genericclioptions.ConfigFlags) *cobra.Command {
var opts *lintOptions
cmd := &cobra.Command{
Use: "lint",
Short: "Inspect kubernetes resources for possible issues",
RunE: func(_ *cobra.Command, _ []string) error {
err := opts.Validate()
if err != nil {
return err
}
fmt.Println("Checking ingresses...")
err = ingresses(*opts)
if err != nil {
util.PrintError(err)
}
fmt.Println("Checking deployments...")
err = deployments(*opts)
if err != nil {
util.PrintError(err)
}
return nil
},
}
opts = addCommonOptions(flags, cmd)
cmd.AddCommand(createSubcommand(flags, []string{"ingresses", "ingress", "ing"}, "Check ingresses for possible issues", ingresses))
cmd.AddCommand(createSubcommand(flags, []string{"deployments", "deployment", "dep"}, "Check deployments for possible issues", deployments))
return cmd
}
func createSubcommand(flags *genericclioptions.ConfigFlags, names []string, short string, f func(opts lintOptions) error) *cobra.Command {
var opts *lintOptions
cmd := &cobra.Command{
Use: names[0],
Aliases: names[1:],
Short: short,
RunE: func(_ *cobra.Command, _ []string) error {
err := opts.Validate()
if err != nil {
return err
}
util.PrintError(f(*opts))
return nil
},
}
opts = addCommonOptions(flags, cmd)
return cmd
}
func addCommonOptions(flags *genericclioptions.ConfigFlags, cmd *cobra.Command) *lintOptions {
out := lintOptions{
flags: flags,
}
cmd.Flags().BoolVar(&out.allNamespaces, "all-namespaces", false, "Check resources in all namespaces")
cmd.Flags().BoolVar(&out.showAll, "show-all", false, "Show all resources, not just the ones with problems")
cmd.Flags().BoolVarP(&out.verbose, "verbose", "v", false, "Show extra information about the lints")
cmd.Flags().StringVarP(&out.versionFrom, "from-version", "f", "0.0.0", "Use lints added for versions starting with this one")
cmd.Flags().StringVarP(&out.versionTo, "to-version", "t", version.RELEASE, "Use lints added for versions up to and including this one")
return &out
}
type lintOptions struct {
flags *genericclioptions.ConfigFlags
allNamespaces bool
showAll bool
verbose bool
versionFrom string
versionTo string
}
func (opts *lintOptions) Validate() error {
//nolint:dogsled // Ignore 3 blank identifiers
_, _, _, err := util.ParseVersionString(opts.versionFrom)
if err != nil {
return err
}
//nolint:dogsled // Ignore 3 blank identifiers
_, _, _, err = util.ParseVersionString(opts.versionTo)
if err != nil {
return err
}
return nil
}
type lint interface {
Check(obj kmeta.Object) bool
Message() string
Link() string
Version() string
}
func checkObjectArray(allLints []lint, objects []kmeta.Object, opts lintOptions) {
usedLints := make([]lint, 0)
for _, lint := range allLints {
lintVersion := lint.Version()
if lint.Version() == "" {
lintVersion = "0.0.0"
}
if util.InVersionRangeInclusive(opts.versionFrom, lintVersion, opts.versionTo) {
usedLints = append(usedLints, lint)
}
}
for _, obj := range objects {
objName := obj.GetName()
if opts.allNamespaces {
objName = obj.GetNamespace() + "/" + obj.GetName()
}
failedLints := make([]lint, 0)
for _, lint := range usedLints {
if lint.Check(obj) {
failedLints = append(failedLints, lint)
}
}
if len(failedLints) != 0 {
fmt.Printf("✗ %v\n", objName)
for _, lint := range failedLints {
fmt.Printf(" - %v\n", lint.Message())
if opts.verbose && lint.Version() != "" {
fmt.Printf(" Lint added for version %v\n", lint.Version())
}
if opts.verbose && lint.Link() != "" {
fmt.Printf(" %v\n", lint.Link())
}
}
fmt.Println("")
continue
}
if opts.showAll {
fmt.Printf("✓ %v\n", objName)
}
}
}
func ingresses(opts lintOptions) error {
var ings []networking.Ingress
var err error
if opts.allNamespaces {
ings, err = request.GetIngressDefinitions(opts.flags, "")
} else {
ings, err = request.GetIngressDefinitions(opts.flags, util.GetNamespace(opts.flags))
}
if err != nil {
return err
}
iLints := lints.GetIngressLints()
genericLints := make([]lint, len(iLints))
for i := range iLints {
genericLints[i] = iLints[i]
}
objects := make([]kmeta.Object, 0)
for i := range ings {
objects = append(objects, &ings[i])
}
checkObjectArray(genericLints, objects, opts)
return nil
}
func deployments(opts lintOptions) error {
var deps []appsv1.Deployment
var err error
if opts.allNamespaces {
deps, err = request.GetDeployments(opts.flags, "")
} else {
deps, err = request.GetDeployments(opts.flags, util.GetNamespace(opts.flags))
}
if err != nil {
return err
}
iLints := lints.GetDeploymentLints()
genericLints := make([]lint, len(iLints))
for i := range iLints {
genericLints[i] = iLints[i]
}
objects := make([]kmeta.Object, 0)
for i := range deps {
objects = append(objects, &deps[i])
}
checkObjectArray(genericLints, objects, opts)
return nil
}