Merge pull request #115447 from kidddddddddddddddddddddd/ingress

[ingress] Create with ingressClass annotation and IngressClassName both set
This commit is contained in:
Kubernetes Prow Robot 2023-03-15 02:02:16 -07:00 committed by GitHub
commit 8decaf3ae7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 5 deletions

View File

@ -277,9 +277,9 @@ func ValidateIngressCreate(ingress *networking.Ingress) field.ErrorList {
} }
allErrs = append(allErrs, validateIngress(ingress, opts)...) allErrs = append(allErrs, validateIngress(ingress, opts)...)
annotationVal, annotationIsSet := ingress.Annotations[annotationIngressClass] 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) 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 return allErrs
} }

View File

@ -993,12 +993,19 @@ func TestValidateIngressCreate(t *testing.T) {
}, },
expectedErrs: field.ErrorList{}, 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) { tweakIngress: func(ingress *networking.Ingress) {
ingress.Spec.IngressClassName = utilpointer.String("bar") ingress.Spec.IngressClassName = utilpointer.String("bar")
ingress.Annotations = map[string]string{annotationIngressClass: "foo"} 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": { "valid regex path": {
tweakIngress: func(ingress *networking.Ingress) { tweakIngress: func(ingress *networking.Ingress) {

View File

@ -18,6 +18,7 @@ package ingress
import ( import (
"context" "context"
"fmt"
apiequality "k8s.io/apimachinery/pkg/api/equality" apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
@ -29,6 +30,10 @@ import (
"sigs.k8s.io/structured-merge-diff/v4/fieldpath" "sigs.k8s.io/structured-merge-diff/v4/fieldpath"
) )
const (
annotationIngressClass = "kubernetes.io/ingress.class"
)
// ingressStrategy implements verification logic for Replication Ingress. // ingressStrategy implements verification logic for Replication Ingress.
type ingressStrategy struct { type ingressStrategy struct {
runtime.ObjectTyper runtime.ObjectTyper
@ -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. // 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. // Canonicalize normalizes the object after validation.
func (ingressStrategy) Canonicalize(obj runtime.Object) { func (ingressStrategy) Canonicalize(obj runtime.Object) {