generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 32
/
validate.go
296 lines (274 loc) · 9.49 KB
/
validate.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package cmd
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"github.com/spf13/cobra"
"k8s.io/apiextensions-apiserver/pkg/apiserver"
"k8s.io/apiextensions-apiserver/pkg/registry/customresourcedefinition"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/kubectl-validate/pkg/openapiclient"
"sigs.k8s.io/kubectl-validate/pkg/utils"
"sigs.k8s.io/kubectl-validate/pkg/validator"
yamlv2 "gopkg.in/yaml.v2"
)
type OutputFormat string
const (
OutputHuman OutputFormat = "human"
OutputJSON OutputFormat = "json"
)
// String is used both by fmt.Print and by Cobra in help text
func (e *OutputFormat) String() string {
return string(*e)
}
// Set must have pointer receiver so it doesn't change the value of a copy
func (e *OutputFormat) Set(v string) error {
switch v {
case "human", "json":
*e = OutputFormat(v)
return nil
default:
return fmt.Errorf(`must be one of "human", or "json"`)
}
}
// Type is only used in help text
func (e *OutputFormat) Type() string {
return "OutputFormat"
}
type commandFlags struct {
kubeConfigOverrides clientcmd.ConfigOverrides
version string
localSchemasDir string
localCRDsDir string
schemaPatchesDir string
outputFormat OutputFormat
}
func NewRootCommand() *cobra.Command {
invoked := &commandFlags{
outputFormat: OutputHuman,
version: "1.27",
}
res := &cobra.Command{
Use: "kubectl-validate [manifests to validate]",
Short: "kubectl-validate",
Long: "kubectl-validate is a CLI tool to validate Kubernetes manifests against their schemas",
Args: cobra.MinimumNArgs(1),
RunE: invoked.Run,
SilenceUsage: true,
}
res.Flags().StringVarP(&invoked.version, "version", "", invoked.version, "Kubernetes version to validate native resources against. Required if not connected directly to cluster")
res.Flags().StringVarP(&invoked.localSchemasDir, "local-schemas", "", "", "--local-schemas=./path/to/schemas/dir. Path to a directory with format: /apis/<group>/<version>.json for each group-version's schema.")
res.Flags().StringVarP(&invoked.localCRDsDir, "local-crds", "", "", "--local-crds=./path/to/crds/dir. Path to a directory containing .yaml or .yml files for CRD definitions.")
res.Flags().StringVarP(&invoked.schemaPatchesDir, "schema-patches", "", "", "Path to a directory with format: /apis/<group>/<version>.json for each group-version's schema you wish to jsonpatch to the groupversion's final schema. Patches only apply if the schema exists")
res.Flags().VarP(&invoked.outputFormat, "output", "o", "Output format. Choice of: \"human\" or \"json\"")
clientcmd.BindOverrideFlags(&invoked.kubeConfigOverrides, res.Flags(), clientcmd.RecommendedConfigOverrideFlags("kube-"))
return res
}
type joinedErrors interface {
Unwrap() []error
}
func errorToStatus(err error) metav1.Status {
var statusErr *k8serrors.StatusError
var fieldError *field.Error
var errorList utilerrors.Aggregate
var errorList2 joinedErrors
if errors.As(err, &errorList2) {
errs := errorList2.Unwrap()
if len(errs) == 0 {
return metav1.Status{Status: metav1.StatusSuccess}
}
var fieldErrors field.ErrorList
var otherErrors []error
var yamlErrors []metav1.StatusCause
for _, e := range errs {
var fieldError *field.Error
var yamlError *yamlv2.TypeError
if errors.As(e, &fieldError) {
fieldErrors = append(fieldErrors, fieldError)
} else if errors.As(e, &yamlError) {
for _, sub := range yamlError.Errors {
yamlErrors = append(yamlErrors, metav1.StatusCause{
Message: sub,
})
}
} else {
otherErrors = append(otherErrors, e)
}
}
if len(otherErrors) > 0 {
return k8serrors.NewInternalError(err).ErrStatus
} else if len(yamlErrors) > 0 && len(fieldErrors) == 0 {
// YAML type errors are raised during decoding
return metav1.Status{
Status: metav1.StatusFailure,
Code: http.StatusBadRequest,
Reason: metav1.StatusReasonBadRequest,
Details: &metav1.StatusDetails{
Causes: yamlErrors,
},
Message: "failed to unmarshal document to YAML",
}
}
return k8serrors.NewInvalid(schema.GroupKind{}, "", fieldErrors).ErrStatus
} else if errors.As(err, &statusErr) {
return statusErr.ErrStatus
} else if errors.As(err, &fieldError) {
return k8serrors.NewInvalid(schema.GroupKind{}, "", field.ErrorList{fieldError}).ErrStatus
} else if errors.As(err, &errorList) {
errs := errorList.Errors()
var fieldErrs []*field.Error
var otherErrs []error
for _, e := range errs {
fieldError = nil
if errors.As(e, &fieldError) {
fieldErrs = append(fieldErrs, fieldError)
} else {
otherErrs = append(otherErrs, e)
}
}
if len(otherErrs) == 0 {
return k8serrors.NewInvalid(schema.GroupKind{}, "", fieldErrs).ErrStatus
} else {
return k8serrors.NewInternalError(err).ErrStatus
}
} else if err != nil {
return k8serrors.NewInternalError(err).ErrStatus
}
return metav1.Status{Status: metav1.StatusSuccess}
}
func (c *commandFlags) Run(cmd *cobra.Command, args []string) error {
// tool fetches openapi in the following priority order:
factory, err := validator.New(
openapiclient.NewOverlay(
// apply user defined patches on top of the final schema
openapiclient.PatchLoaderFromDirectory(nil, c.schemaPatchesDir),
openapiclient.NewComposite(
// consult local OpenAPI
openapiclient.NewLocalSchemaFiles(nil, c.localSchemasDir),
// consult local CRDs
openapiclient.NewLocalCRDFiles(nil, c.localCRDsDir),
openapiclient.NewOverlay(
// Hand-written hardcoded patches.
openapiclient.HardcodedPatchLoader(c.version),
// try cluster for schemas first, if they are not available
// then fallback to hardcoded or builtin schemas
openapiclient.NewFallback(
// contact connected cluster for any schemas. (should this be opt-in?)
openapiclient.NewKubeConfig(c.kubeConfigOverrides),
// try hardcoded builtins first, if they are not available
// fall back to GitHub builtins
openapiclient.NewFallback(
// schemas for known k8s versions are scraped from GH and placed here
openapiclient.NewHardcodedBuiltins(c.version),
// check github for builtins not hardcoded.
// subject to rate limiting. should use a diskcache
// since etag requests are not limited
openapiclient.NewGitHubBuiltins(c.version),
)),
),
),
),
)
if err != nil {
return ArgumentError{err}
}
files, err := utils.FindFiles(args...)
if err != nil {
return ArgumentError{err}
}
hasError := false
if c.outputFormat == OutputHuman {
for _, path := range files {
fmt.Fprintf(cmd.OutOrStdout(), "\n\033[1m%v\033[0m...", path)
var errs []error
for _, err := range ValidateFile(path, factory) {
if err != nil {
errs = append(errs, err)
}
}
if len(errs) != 0 {
fmt.Fprintln(cmd.OutOrStdout(), "\033[31mERROR\033[0m")
for _, err := range errs {
fmt.Fprintln(cmd.ErrOrStderr(), err.Error())
}
hasError = true
} else {
fmt.Fprintln(cmd.OutOrStdout(), "\033[32mOK\033[0m")
}
}
} else {
res := map[string][]metav1.Status{}
for _, path := range files {
for _, err := range ValidateFile(path, factory) {
res[path] = append(res[path], errorToStatus(err))
hasError = hasError || err != nil
}
}
data, e := json.MarshalIndent(res, "", " ")
if e != nil {
return InternalError{fmt.Errorf("failed to render results into JSON: %w", e)}
}
fmt.Fprintln(cmd.OutOrStdout(), string(data))
}
if hasError {
return ValidationError{errors.New("validation failed")}
}
return nil
}
func ValidateFile(filePath string, resolver *validator.Validator) []error {
fileBytes, err := os.ReadFile(filePath)
if err != nil {
return []error{fmt.Errorf("error reading file: %w", err)}
}
if utils.IsYaml(filePath) {
documents, err := utils.SplitYamlDocuments(fileBytes)
if err != nil {
return []error{err}
}
var errs []error
for _, document := range documents {
if utils.IsEmptyYamlDocument(document) {
errs = append(errs, nil)
} else {
errs = append(errs, ValidateDocument(document, resolver))
}
}
return errs
} else {
return []error{
ValidateDocument(fileBytes, resolver),
}
}
}
func ValidateDocument(document []byte, resolver *validator.Validator) error {
gvk, parsed, err := resolver.Parse(document)
if gvk.Group == "apiextensions.k8s.io" && gvk.Kind == "CustomResourceDefinition" {
// CRD spec contains an infinite loop which is not supported by K8s
// OpenAPI-based validator. Use the handwritten validation based upon
// native types for CRD files. There are no other recursive schemas to my
// knowledge, and any schema defined in CRD cannot be recursive.
// Long term goal is to remove this once k8s upstream has better
// support for validating against spec.Schema for native types.
obj, _, err := serializer.NewCodecFactory(apiserver.Scheme).UniversalDecoder().Decode(document, nil, nil)
if err != nil {
return err
}
strat := customresourcedefinition.NewStrategy(apiserver.Scheme)
rest.FillObjectMetaSystemFields(obj.(metav1.Object))
return rest.BeforeCreate(strat, request.WithNamespace(context.TODO(), ""), obj)
} else if err != nil {
return err
}
return resolver.Validate(parsed)
}