-
Notifications
You must be signed in to change notification settings - Fork 683
/
resource_validator.go
75 lines (64 loc) · 1.6 KB
/
resource_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
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
package entrypoint
import (
"context"
"fmt"
"io/ioutil"
"strings"
"github.com/datawire/ambassador/v2/pkg/kates"
)
type resourceValidator struct {
invalid map[string]*kates.Unstructured
katesValidator *kates.Validator
}
func newResourceValidator() *resourceValidator {
crdYAML, err := ioutil.ReadFile(findCRDFilename())
if err != nil {
panic(err)
}
crdObjs, err := kates.ParseManifests(string(crdYAML))
if err != nil {
panic(err)
}
katesValidator, err := kates.NewValidator(nil, crdObjs)
if err != nil {
panic(err)
}
return &resourceValidator{
katesValidator: katesValidator,
invalid: map[string]*kates.Unstructured{},
}
}
func (v *resourceValidator) isValid(ctx context.Context, un *kates.Unstructured) bool {
key := string(un.GetUID())
err := v.katesValidator.Validate(ctx, un)
if err != nil {
copy := un.DeepCopy()
copy.Object["errors"] = err.Error()
v.invalid[key] = copy
return false
} else {
delete(v.invalid, key)
return true
}
}
func (v *resourceValidator) getInvalid() []*kates.Unstructured {
var result []*kates.Unstructured
for _, inv := range v.invalid {
result = append(result, inv)
}
return result
}
func findCRDFilename() string {
searchPath := []string{
"/opt/ambassador/etc/crds.yaml",
"manifests/emissary/emissary-crds.yaml",
"ambassador/manifests/emissary/emissary-crds.yaml",
"../../manifests/emissary/emissary-crds.yaml",
}
for _, candidate := range searchPath {
if fileExists(candidate) {
return candidate
}
}
panic(fmt.Sprintf("couldn't find CRDs at any of the following locations: %s", strings.Join(searchPath, ", ")))
}