...
1 package entrypoint
2
3 import (
4 "context"
5
6 getambassadorio "github.com/datawire/ambassador/v2/pkg/api/getambassador.io"
7 "github.com/datawire/ambassador/v2/pkg/kates"
8 "github.com/datawire/dlib/dlog"
9 )
10
11 type resourceValidator struct {
12 invalid map[string]*kates.Unstructured
13 katesValidator *kates.Validator
14 }
15
16 func newResourceValidator() (*resourceValidator, error) {
17 return &resourceValidator{
18 katesValidator: getambassadorio.NewValidator(),
19 invalid: map[string]*kates.Unstructured{},
20 }, nil
21 }
22
23 func (v *resourceValidator) isValid(ctx context.Context, un *kates.Unstructured) bool {
24 err := v.katesValidator.Validate(ctx, un)
25
26 if err != nil {
27 dlog.Errorf(ctx, "validation error: %s %s/%s -- %s", un.GetKind(), un.GetNamespace(), un.GetName(), err.Error())
28 v.addInvalid(ctx, un, err.Error())
29 return false
30 } else {
31 v.removeInvalid(ctx, un)
32 return true
33 }
34 }
35
36 func (v *resourceValidator) getInvalid() []*kates.Unstructured {
37 var result []*kates.Unstructured
38 for _, inv := range v.invalid {
39 result = append(result, inv)
40 }
41 return result
42 }
43
44
45
46 func (v *resourceValidator) addInvalid(ctx context.Context, un *kates.Unstructured, errorMessage string) {
47 key := string(un.GetUID())
48
49 copy := un.DeepCopy()
50 copy.Object["errors"] = errorMessage
51 v.invalid[key] = copy
52 }
53
54
55
56 func (v *resourceValidator) removeInvalid(ctx context.Context, un *kates.Unstructured) {
57 key := string(un.GetUID())
58 delete(v.invalid, key)
59 }
60
View as plain text