/
validation.go
59 lines (47 loc) · 1.92 KB
/
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
package controlplane
import (
"errors"
"sigs.k8s.io/controller-runtime/pkg/client"
operatorv1beta1 "github.com/kong/gateway-operator/apis/v1beta1"
"github.com/kong/gateway-operator/pkg/consts"
k8sutils "github.com/kong/gateway-operator/pkg/utils/kubernetes"
)
// Validator validates ControlPlane objects.
type Validator struct{}
// NewValidator creates a ControlPlane validator.
func NewValidator(c client.Client) *Validator {
return &Validator{}
}
// Validate validates a ControlPlane object and return the first validation error found.
func (v *Validator) Validate(controlplane *operatorv1beta1.ControlPlane) error {
if err := v.ValidateDeploymentOptions(&controlplane.Spec.Deployment); err != nil {
return err
}
// prepared for more validations
return nil
}
// ValidateDeploymentOptions validates the DeploymentOptions field of ControlPlane object.
func (v *Validator) ValidateDeploymentOptions(opts *operatorv1beta1.ControlPlaneDeploymentOptions) error {
if opts == nil || opts.PodTemplateSpec == nil {
// Can't use empty DeploymentOptions because we still require users
// to provide an image
// Related: https://github.com/Kong/gateway-operator/issues/754.
return errors.New("ControlPlane requires an image")
}
// Ref: https://github.com/Kong/gateway-operator/issues/736
if opts.Replicas != nil && *opts.Replicas != 1 {
return errors.New("ControlPlane only supports replicas of 1")
}
container := k8sutils.GetPodContainerByName(&opts.PodTemplateSpec.Spec, consts.ControlPlaneControllerContainerName)
if container == nil {
// We need the controller container for e.g. specifying an image which
// is still required.
// Ref: https://github.com/Kong/gateway-operator/issues/754.
return errors.New("no controller container found in ControlPlane spec")
}
// Ref: https://github.com/Kong/gateway-operator/issues/754.
if container.Image == "" {
return errors.New("ControlPlane requires an image")
}
return nil
}