Skip to content

Commit

Permalink
Merge pull request kubernetes#115447 from kidddddddddddddddddddddd/in…
Browse files Browse the repository at this point in the history
…gress

[ingress] Create with ingressClass annotation and IngressClassName both set
  • Loading branch information
k8s-ci-robot committed Mar 15, 2023
2 parents 37937bb + ac626f8 commit 8decaf3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pkg/apis/networking/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,9 @@ func ValidateIngressCreate(ingress *networking.Ingress) field.ErrorList {
}
allErrs = append(allErrs, validateIngress(ingress, opts)...)
annotationVal, annotationIsSet := ingress.Annotations[annotationIngressClass]
if annotationIsSet && ingress.Spec.IngressClassName != nil {
if annotationIsSet && ingress.Spec.IngressClassName != nil && annotationVal != *ingress.Spec.IngressClassName {
annotationPath := field.NewPath("annotations").Child(annotationIngressClass)
allErrs = append(allErrs, field.Invalid(annotationPath, annotationVal, "can not be set when the class field is also set"))
allErrs = append(allErrs, field.Invalid(annotationPath, annotationVal, "must match `ingressClassName` when both are specified"))
}
return allErrs
}
Expand Down
11 changes: 9 additions & 2 deletions pkg/apis/networking/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -993,12 +993,19 @@ func TestValidateIngressCreate(t *testing.T) {
},
expectedErrs: field.ErrorList{},
},
"class field and annotation set": {
"class field and annotation set with same value": {
tweakIngress: func(ingress *networking.Ingress) {
ingress.Spec.IngressClassName = utilpointer.String("foo")
ingress.Annotations = map[string]string{annotationIngressClass: "foo"}
},
expectedErrs: field.ErrorList{},
},
"class field and annotation set with different value": {
tweakIngress: func(ingress *networking.Ingress) {
ingress.Spec.IngressClassName = utilpointer.String("bar")
ingress.Annotations = map[string]string{annotationIngressClass: "foo"}
},
expectedErrs: field.ErrorList{field.Invalid(field.NewPath("annotations").Child(annotationIngressClass), "foo", "can not be set when the class field is also set")},
expectedErrs: field.ErrorList{field.Invalid(field.NewPath("annotations").Child(annotationIngressClass), "foo", "must match `ingressClassName` when both are specified")},
},
"valid regex path": {
tweakIngress: func(ingress *networking.Ingress) {
Expand Down
15 changes: 14 additions & 1 deletion pkg/registry/networking/ingress/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package ingress

import (
"context"
"fmt"

apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -29,6 +30,10 @@ import (
"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
)

const (
annotationIngressClass = "kubernetes.io/ingress.class"
)

// ingressStrategy implements verification logic for Replication Ingress.
type ingressStrategy struct {
runtime.ObjectTyper
Expand Down Expand Up @@ -93,7 +98,15 @@ func (ingressStrategy) Validate(ctx context.Context, obj runtime.Object) field.E
}

// WarningsOnCreate returns warnings for the creation of the given object.
func (ingressStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { return nil }
func (ingressStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
var warnings []string
ingress := obj.(*networking.Ingress)
_, annotationIsSet := ingress.Annotations[annotationIngressClass]
if annotationIsSet && ingress.Spec.IngressClassName == nil {
warnings = append(warnings, fmt.Sprintf("annotation %q is deprecated, please use 'spec.ingressClassName' instead", annotationIngressClass))
}
return warnings
}

// Canonicalize normalizes the object after validation.
func (ingressStrategy) Canonicalize(obj runtime.Object) {
Expand Down

0 comments on commit 8decaf3

Please sign in to comment.