-
Notifications
You must be signed in to change notification settings - Fork 24
/
horizonsource_validation.go
65 lines (53 loc) · 1.5 KB
/
horizonsource_validation.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
/*
Copyright 2022 VMware, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package v1alpha1
import (
"context"
"knative.dev/pkg/apis"
"knative.dev/pkg/kmp"
)
// Validate validates HorizonSource.
func (src *HorizonSource) Validate(ctx context.Context) *apis.FieldError {
var errs *apis.FieldError
if apis.IsInUpdate(ctx) {
original := apis.GetBaseline(ctx).(*HorizonSource)
// all fields immutable
if diff, err := kmp.ShortDiff(original.Spec, src.Spec); err != nil {
return &apis.FieldError{
Message: "Failed to diff HorizonSource",
Paths: []string{"spec"},
Details: err.Error(),
}
} else if diff != "" {
return &apis.FieldError{
Message: "Immutable fields changed (-old +new)",
Paths: []string{"spec"},
Details: diff,
}
}
}
errs = errs.Also(src.Spec.Validate(ctx).ViaField("spec"))
return errs
}
// Validate validates HorizonSourceSpec.
func (spec *HorizonSourceSpec) Validate(ctx context.Context) *apis.FieldError {
var errs *apis.FieldError
errs = spec.Sink.Validate(ctx).ViaField("sink").
Also(spec.HorizonAuthSpec.Validate(ctx))
if spec.ServiceAccountName == "" {
errs = errs.Also(apis.ErrMissingField("serviceAccountName"))
}
return errs
}
// Validate implements apis.Validatable
func (auth *HorizonAuthSpec) Validate(ctx context.Context) (err *apis.FieldError) {
if auth.Address.Host == "" {
err = err.Also(apis.ErrMissingField("address.host"))
}
if auth.SecretRef.Name == "" {
err = err.Also(apis.ErrMissingField("secretRef.name"))
}
return err
}