Skip to content

Commit

Permalink
Update workload selector validation
Browse files Browse the repository at this point in the history
Kubernetes-commit: fc69084bf1fd32e55256795a6db7a74d75679d8b
  • Loading branch information
liggitt authored and k8s-publishing-bot committed Nov 7, 2022
1 parent fe82462 commit dad8cd8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
21 changes: 21 additions & 0 deletions pkg/apis/meta/v1/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ type LabelSelectorValidationOptions struct {
AllowInvalidLabelValueInSelector bool
}

// LabelSelectorHasInvalidLabelValue returns true if the given selector contains an invalid label value in a match expression.
// This is useful for determining whether AllowInvalidLabelValueInSelector should be set to true when validating an update
// based on existing persisted invalid values.
func LabelSelectorHasInvalidLabelValue(ps *metav1.LabelSelector) bool {
if ps == nil {
return false
}
for _, e := range ps.MatchExpressions {
for _, v := range e.Values {
if len(validation.IsValidLabelValue(v)) > 0 {
return true
}
}
}
return false
}

// ValidateLabelSelector validate the LabelSelector according to the opts and returns any validation errors.
// opts.AllowInvalidLabelValueInSelector is only expected to be set to true when required for backwards compatibility with existing invalid data.
func ValidateLabelSelector(ps *metav1.LabelSelector, opts LabelSelectorValidationOptions, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if ps == nil {
Expand All @@ -46,6 +65,8 @@ func ValidateLabelSelector(ps *metav1.LabelSelector, opts LabelSelectorValidatio
return allErrs
}

// ValidateLabelSelectorRequirement validate the requirement according to the opts and returns any validation errors.
// opts.AllowInvalidLabelValueInSelector is only expected to be set to true when required for backwards compatibility with existing invalid data.
func ValidateLabelSelectorRequirement(sr metav1.LabelSelectorRequirement, opts LabelSelectorValidationOptions, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
switch sr.Operator {
Expand Down
1 change: 0 additions & 1 deletion pkg/apis/meta/v1/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,6 @@ func TestValidateConditions(t *testing.T) {
}

func TestLabelSelectorMatchExpression(t *testing.T) {
// Success case
testCases := []struct {
name string
labelSelector *metav1.LabelSelector
Expand Down

0 comments on commit dad8cd8

Please sign in to comment.