@@ -32,6 +32,10 @@ import (
32
32
type LabelSelectorValidationOptions struct {
33
33
// Allow invalid label value in selector
34
34
AllowInvalidLabelValueInSelector bool
35
+
36
+ // Allows an operator that is not interpretable to pass validation. This is useful for cases where a broader check
37
+ // can be performed, as in a *SubjectAccessReview
38
+ AllowUnknownOperatorInRequirement bool
35
39
}
36
40
37
41
// LabelSelectorHasInvalidLabelValue returns true if the given selector contains an invalid label value in a match expression.
@@ -79,7 +83,9 @@ func ValidateLabelSelectorRequirement(sr metav1.LabelSelectorRequirement, opts L
79
83
allErrs = append (allErrs , field .Forbidden (fldPath .Child ("values" ), "may not be specified when `operator` is 'Exists' or 'DoesNotExist'" ))
80
84
}
81
85
default :
82
- allErrs = append (allErrs , field .Invalid (fldPath .Child ("operator" ), sr .Operator , "not a valid selector operator" ))
86
+ if ! opts .AllowUnknownOperatorInRequirement {
87
+ allErrs = append (allErrs , field .Invalid (fldPath .Child ("operator" ), sr .Operator , "not a valid selector operator" ))
88
+ }
83
89
}
84
90
allErrs = append (allErrs , ValidateLabelName (sr .Key , fldPath .Child ("key" ))... )
85
91
if ! opts .AllowInvalidLabelValueInSelector {
@@ -113,6 +119,39 @@ func ValidateLabels(labels map[string]string, fldPath *field.Path) field.ErrorLi
113
119
return allErrs
114
120
}
115
121
122
+ // FieldSelectorValidationOptions is a struct that can be passed to ValidateFieldSelectorRequirement to record the validate options
123
+ type FieldSelectorValidationOptions struct {
124
+ // Allows an operator that is not interpretable to pass validation. This is useful for cases where a broader check
125
+ // can be performed, as in a *SubjectAccessReview
126
+ AllowUnknownOperatorInRequirement bool
127
+ }
128
+
129
+ // ValidateLabelSelectorRequirement validates the requirement according to the opts and returns any validation errors.
130
+ func ValidateFieldSelectorRequirement (requirement metav1.FieldSelectorRequirement , opts FieldSelectorValidationOptions , fldPath * field.Path ) field.ErrorList {
131
+ allErrs := field.ErrorList {}
132
+
133
+ if len (requirement .Key ) == 0 {
134
+ allErrs = append (allErrs , field .Required (fldPath .Child ("key" ), "must be specified" ))
135
+ }
136
+
137
+ switch requirement .Operator {
138
+ case metav1 .FieldSelectorOpIn , metav1 .FieldSelectorOpNotIn :
139
+ if len (requirement .Values ) == 0 {
140
+ allErrs = append (allErrs , field .Required (fldPath .Child ("values" ), "must be specified when `operator` is 'In' or 'NotIn'" ))
141
+ }
142
+ case metav1 .FieldSelectorOpExists , metav1 .FieldSelectorOpDoesNotExist :
143
+ if len (requirement .Values ) > 0 {
144
+ allErrs = append (allErrs , field .Forbidden (fldPath .Child ("values" ), "may not be specified when `operator` is 'Exists' or 'DoesNotExist'" ))
145
+ }
146
+ default :
147
+ if ! opts .AllowUnknownOperatorInRequirement {
148
+ allErrs = append (allErrs , field .Invalid (fldPath .Child ("operator" ), requirement .Operator , "not a valid selector operator" ))
149
+ }
150
+ }
151
+
152
+ return allErrs
153
+ }
154
+
116
155
func ValidateDeleteOptions (options * metav1.DeleteOptions ) field.ErrorList {
117
156
allErrs := field.ErrorList {}
118
157
//lint:file-ignore SA1019 Keep validation for deprecated OrphanDependents option until it's being removed
0 commit comments