fix: empty IngressClassName, Error handling
This commit is contained in:
parent
bbd8073c89
commit
e5fa90db9b
2 changed files with 34 additions and 33 deletions
|
@ -41,30 +41,24 @@ var (
|
|||
// IsValid returns true if the given Ingress specify the ingress.class
|
||||
// annotation or IngressClassName resource for Kubernetes >= v1.18
|
||||
func IsValid(ing *networking.Ingress) bool {
|
||||
// 1. with annotation
|
||||
// 1. with annotation or IngressClass
|
||||
ingress, ok := ing.GetAnnotations()[IngressKey]
|
||||
if ok {
|
||||
// empty annotation and same annotation on ingress
|
||||
if ingress == "" && IngressClass == DefaultClass {
|
||||
return true
|
||||
}
|
||||
|
||||
return ingress == IngressClass
|
||||
if !ok && ing.Spec.IngressClassName != nil {
|
||||
ingress = *ing.Spec.IngressClassName
|
||||
}
|
||||
|
||||
// 2. k8s < v1.18. Check default annotation
|
||||
if !k8s.IsIngressV1Beta1Ready {
|
||||
return IngressClass == DefaultClass
|
||||
// empty ingress and IngressClass equal default
|
||||
if len(ingress) == 0 && IngressClass == DefaultClass {
|
||||
return true
|
||||
}
|
||||
|
||||
// 3. without annotation and IngressClass. Check default annotation
|
||||
if k8s.IngressClass == nil {
|
||||
return IngressClass == DefaultClass
|
||||
// k8s > v1.18.
|
||||
// Processing may be redundant because k8s.IngressClass is obtained by IngressClass
|
||||
// 3. without annotation and IngressClass. Check IngressClass
|
||||
if k8s.IngressClass != nil {
|
||||
return ingress == k8s.IngressClass.Name
|
||||
}
|
||||
|
||||
// 4. with IngressClass
|
||||
if ing.Spec.IngressClassName != nil {
|
||||
return k8s.IngressClass.Name == *ing.Spec.IngressClassName
|
||||
}
|
||||
return false
|
||||
return ingress == IngressClass
|
||||
}
|
||||
|
|
|
@ -39,28 +39,32 @@ func TestIsValidClass(t *testing.T) {
|
|||
}()
|
||||
|
||||
tests := []struct {
|
||||
ingress string
|
||||
controller string
|
||||
defClass string
|
||||
annotation bool
|
||||
k8sClass *networking.IngressClass
|
||||
v1Ready bool
|
||||
isValid bool
|
||||
ingress string
|
||||
controller string
|
||||
defClass string
|
||||
annotation bool
|
||||
ingressClassName bool
|
||||
k8sClass *networking.IngressClass
|
||||
v1Ready bool
|
||||
isValid bool
|
||||
}{
|
||||
{"", "", "nginx", true, nil, false, true},
|
||||
{"", "nginx", "nginx", true, nil, false, true},
|
||||
{"nginx", "nginx", "nginx", true, nil, false, true},
|
||||
{"custom", "custom", "nginx", true, nil, false, true},
|
||||
{"", "killer", "nginx", true, nil, false, false},
|
||||
{"custom", "nginx", "nginx", true, nil, false, false},
|
||||
{"", "custom", "nginx", false,
|
||||
{"", "", "nginx", true, false, nil, false, true},
|
||||
{"", "nginx", "nginx", true, false, nil, false, true},
|
||||
{"nginx", "nginx", "nginx", true, false, nil, false, true},
|
||||
{"custom", "custom", "nginx", true, false, nil, false, true},
|
||||
{"", "killer", "nginx", true, false, nil, false, false},
|
||||
{"custom", "nginx", "nginx", true, false, nil, false, false},
|
||||
{"nginx", "nginx", "nginx", false, true, nil, false, true},
|
||||
{"custom", "nginx", "nginx", false, true, nil, true, false},
|
||||
{"nginx", "nginx", "nginx", false, true, nil, true, true},
|
||||
{"", "custom", "nginx", false, false,
|
||||
&networking.IngressClass{
|
||||
ObjectMeta: meta_v1.ObjectMeta{
|
||||
Name: "custom",
|
||||
},
|
||||
},
|
||||
false, false},
|
||||
{"", "custom", "nginx", false,
|
||||
{"", "custom", "nginx", false, false,
|
||||
&networking.IngressClass{
|
||||
ObjectMeta: meta_v1.ObjectMeta{
|
||||
Name: "custom",
|
||||
|
@ -82,6 +86,9 @@ func TestIsValidClass(t *testing.T) {
|
|||
if test.annotation {
|
||||
ing.Annotations[IngressKey] = test.ingress
|
||||
}
|
||||
if test.ingressClassName {
|
||||
ing.Spec.IngressClassName = &[]string{test.ingress}[0]
|
||||
}
|
||||
|
||||
IngressClass = test.controller
|
||||
DefaultClass = test.defClass
|
||||
|
|
Loading…
Reference in a new issue