This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
/
generic.go
61 lines (52 loc) · 1.87 KB
/
generic.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
package validation
import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
"github.com/coreos/go-semver/semver"
"github.com/jetstack/navigator/pkg/apis/navigator"
)
var supportedPullPolicies = []string{
string(corev1.PullNever),
string(corev1.PullIfNotPresent),
string(corev1.PullAlways),
}
var emptySemver = semver.Version{}
func ValidateImageSpec(img *navigator.ImageSpec, fldPath *field.Path) field.ErrorList {
el := field.ErrorList{}
if img.Tag == "" {
el = append(el, field.Required(fldPath.Child("tag"), ""))
}
if img.Repository == "" {
el = append(el, field.Required(fldPath.Child("repository"), ""))
}
if img.PullPolicy != corev1.PullNever &&
img.PullPolicy != corev1.PullIfNotPresent &&
img.PullPolicy != corev1.PullAlways {
el = append(el, field.NotSupported(fldPath.Child("pullPolicy"), img.PullPolicy, supportedPullPolicies))
}
return el
}
func ValidateNavigatorClusterConfig(cfg *navigator.NavigatorClusterConfig, fldPath *field.Path) field.ErrorList {
allErrs := ValidateImageSpec(&cfg.PilotImage, fldPath.Child("pilotImage"))
allErrs = append(allErrs, ValidateNavigatorSecurityContext(&cfg.SecurityContext, fldPath.Child("securityContext"))...)
return allErrs
}
func ValidateNavigatorSecurityContext(ctx *navigator.NavigatorSecurityContext, fldPath *field.Path) field.ErrorList {
el := field.ErrorList{}
if ctx.RunAsUser != nil {
if *ctx.RunAsUser < 0 {
el = append(el, field.Invalid(fldPath.Child("runAsUser"), *ctx.RunAsUser, "must be non-negative"))
}
}
return el
}
func ValidatePersistenceConfig(cfg *navigator.PersistenceConfig, fldPath *field.Path) field.ErrorList {
el := field.ErrorList{}
if cfg.Size.IsZero() {
el = append(el, field.Required(fldPath.Child("size"), ""))
}
if cfg.Size.Sign() == -1 {
el = append(el, field.Invalid(fldPath.Child("size"), cfg.Size, "must be greater than zero"))
}
return el
}