-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidator.go
41 lines (34 loc) · 994 Bytes
/
validator.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
package main
import (
"k8s.io/client-go/discovery"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi"
"k8s.io/kubernetes/pkg/kubectl/cmd/util/openapi/validation"
)
type validator struct {
validator *validation.SchemaValidation
}
func newValidator() (*validator, error) {
config := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{},
)
clientConfig, err := config.ClientConfig()
if err != nil {
return nil, err
}
discoveryClient, err := discovery.NewDiscoveryClientForConfig(clientConfig)
if err != nil {
return nil, err
}
getter := openapi.NewOpenAPIGetter(discoveryClient)
resources, err := getter.Get()
if err != nil {
return nil, err
}
return &validator{validation.NewSchemaValidation(resources)}, nil
}
func (v *validator) validate(data []byte) error {
return v.validator.ValidateBytes(data)
}