-
Notifications
You must be signed in to change notification settings - Fork 685
/
resource_validator.go
44 lines (38 loc) · 1.12 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
package entrypoint
import (
"context"
getambassadorio "github.com/datawire/ambassador/v2/pkg/api/getambassador.io"
"github.com/datawire/ambassador/v2/pkg/kates"
"github.com/datawire/dlib/dlog"
)
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 {
key := string(un.GetUID())
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())
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
}