@@ -21,6 +21,7 @@ import (
21
21
22
22
"github.com/golang/glog"
23
23
24
+ "github.com/hashicorp/go-multierror"
24
25
"k8s.io/api/admission/v1beta1"
25
26
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
26
27
corev1 "k8s.io/api/core/v1"
@@ -61,27 +62,49 @@ func ToAdmissionResponse(err error) *v1beta1.AdmissionResponse {
61
62
}
62
63
}
63
64
64
- func CheckPolicyDuplicate (policies []v1alpha1.LifecyclePolicy ) ( string , bool ) {
65
- policyEvents := map [v1alpha1. Event ]v1alpha1. Event {}
66
- hasDuplicate := false
67
- var duplicateInfo string
65
+ func ValidatePolicies (policies []v1alpha1.LifecyclePolicy ) error {
66
+ var err error
67
+ policyEvents := map [v1alpha1. Event ] struct {}{}
68
+ exitCodes := map [ int32 ] struct {}{}
68
69
69
70
for _ , policy := range policies {
70
- if _ , found := policyEvents [policy .Event ]; found {
71
- hasDuplicate = true
72
- duplicateInfo = fmt .Sprintf ("%v" , policy .Event )
71
+ if policy .Event != "" && policy .ExitCode != nil {
72
+ err = multierror .Append (err , fmt .Errorf ("must not specify event and exitCode simultaneously" ))
73
73
break
74
+ }
75
+
76
+ if policy .Event == "" && policy .ExitCode == nil {
77
+ err = multierror .Append (err , fmt .Errorf ("either event and exitCode should be specified" ))
78
+ break
79
+ }
80
+
81
+ if policy .Event != "" {
82
+ // TODO: check event is in supported Event
83
+ if _ , found := policyEvents [policy .Event ]; found {
84
+ err = multierror .Append (err , fmt .Errorf ("duplicate event %v" , policy .Event ))
85
+ break
86
+ } else {
87
+ policyEvents [policy .Event ] = struct {}{}
88
+ }
74
89
} else {
75
- policyEvents [policy .Event ] = policy .Event
90
+ if * policy .ExitCode == 0 {
91
+ err = multierror .Append (err , fmt .Errorf ("0 is not a valid error code" ))
92
+ break
93
+ }
94
+ if _ , found := exitCodes [* policy .ExitCode ]; found {
95
+ err = multierror .Append (err , fmt .Errorf ("duplicate exitCode %v" , * policy .ExitCode ))
96
+ break
97
+ } else {
98
+ exitCodes [* policy .ExitCode ] = struct {}{}
99
+ }
76
100
}
77
101
}
78
102
79
103
if _ , found := policyEvents [v1alpha1 .AnyEvent ]; found && len (policyEvents ) > 1 {
80
- hasDuplicate = true
81
- duplicateInfo = "if there's * here, no other policy should be here"
104
+ err = multierror .Append (err , fmt .Errorf ("if there's * here, no other policy should be here" ))
82
105
}
83
106
84
- return duplicateInfo , hasDuplicate
107
+ return err
85
108
}
86
109
87
110
func DecodeJob (object runtime.RawExtension , resource metav1.GroupVersionResource ) (v1alpha1.Job , error ) {
0 commit comments