-
Notifications
You must be signed in to change notification settings - Fork 686
/
resource_validator.go
59 lines (49 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
package entrypoint
import (
"context"
"github.com/datawire/dlib/dlog"
getambassadorio "github.com/emissary-ingress/emissary/v3/pkg/api/getambassador.io"
"github.com/emissary-ingress/emissary/v3/pkg/kates"
)
type resourceValidator struct {
invalid map[string]*kates.Unstructured
katesValidator *kates.Validator
}
func newResourceValidator() (*resourceValidator, error) {
return &resourceValidator{
katesValidator: getambassadorio.NewValidator(),
invalid: map[string]*kates.Unstructured{},
}, nil
}
func (v *resourceValidator) isValid(ctx context.Context, un *kates.Unstructured) bool {
err := v.katesValidator.Validate(ctx, un)
if err != nil {
dlog.Errorf(ctx, "validation error: %s %s/%s -- %s", un.GetKind(), un.GetNamespace(), un.GetName(), err.Error())
v.addInvalid(ctx, un, err.Error())
return false
} else {
v.removeInvalid(ctx, un)
return true
}
}
func (v *resourceValidator) getInvalid() []*kates.Unstructured {
var result []*kates.Unstructured
for _, inv := range v.invalid {
result = append(result, inv)
}
return result
}
// The addInvalid method adds a resource to the Validator's list of invalid
// resources.
func (v *resourceValidator) addInvalid(ctx context.Context, un *kates.Unstructured, errorMessage string) {
key := string(un.GetUID())
copy := un.DeepCopy()
copy.Object["errors"] = errorMessage
v.invalid[key] = copy
}
// The removeInvalid method removes a resource from the Validator's list of
// invalid resources.
func (v *resourceValidator) removeInvalid(ctx context.Context, un *kates.Unstructured) {
key := string(un.GetUID())
delete(v.invalid, key)
}