fix: empty IngressClassName, Error handling

This commit is contained in:
aimuz 2020-11-09 11:36:00 +08:00
parent bbd8073c89
commit e5fa90db9b
2 changed files with 34 additions and 33 deletions

View file

@ -41,30 +41,24 @@ var (
// IsValid returns true if the given Ingress specify the ingress.class // IsValid returns true if the given Ingress specify the ingress.class
// annotation or IngressClassName resource for Kubernetes >= v1.18 // annotation or IngressClassName resource for Kubernetes >= v1.18
func IsValid(ing *networking.Ingress) bool { func IsValid(ing *networking.Ingress) bool {
// 1. with annotation // 1. with annotation or IngressClass
ingress, ok := ing.GetAnnotations()[IngressKey] ingress, ok := ing.GetAnnotations()[IngressKey]
if ok { if !ok && ing.Spec.IngressClassName != nil {
// empty annotation and same annotation on ingress ingress = *ing.Spec.IngressClassName
if ingress == "" && IngressClass == DefaultClass { }
// empty ingress and IngressClass equal default
if len(ingress) == 0 && IngressClass == DefaultClass {
return true return true
} }
return ingress == IngressClass // k8s > v1.18.
} // Processing may be redundant because k8s.IngressClass is obtained by IngressClass
// 3. without annotation and IngressClass. Check IngressClass
// 2. k8s < v1.18. Check default annotation if k8s.IngressClass != nil {
if !k8s.IsIngressV1Beta1Ready { return ingress == k8s.IngressClass.Name
return IngressClass == DefaultClass
}
// 3. without annotation and IngressClass. Check default annotation
if k8s.IngressClass == nil {
return IngressClass == DefaultClass
} }
// 4. with IngressClass // 4. with IngressClass
if ing.Spec.IngressClassName != nil { return ingress == IngressClass
return k8s.IngressClass.Name == *ing.Spec.IngressClassName
}
return false
} }

View file

@ -43,24 +43,28 @@ func TestIsValidClass(t *testing.T) {
controller string controller string
defClass string defClass string
annotation bool annotation bool
ingressClassName bool
k8sClass *networking.IngressClass k8sClass *networking.IngressClass
v1Ready bool v1Ready bool
isValid bool isValid bool
}{ }{
{"", "", "nginx", true, nil, false, true}, {"", "", "nginx", true, false, nil, false, true},
{"", "nginx", "nginx", true, nil, false, true}, {"", "nginx", "nginx", true, false, nil, false, true},
{"nginx", "nginx", "nginx", true, nil, false, true}, {"nginx", "nginx", "nginx", true, false, nil, false, true},
{"custom", "custom", "nginx", true, nil, false, true}, {"custom", "custom", "nginx", true, false, nil, false, true},
{"", "killer", "nginx", true, nil, false, false}, {"", "killer", "nginx", true, false, nil, false, false},
{"custom", "nginx", "nginx", true, nil, false, false}, {"custom", "nginx", "nginx", true, false, nil, false, false},
{"", "custom", "nginx", 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{ &networking.IngressClass{
ObjectMeta: meta_v1.ObjectMeta{ ObjectMeta: meta_v1.ObjectMeta{
Name: "custom", Name: "custom",
}, },
}, },
false, false}, false, false},
{"", "custom", "nginx", false, {"", "custom", "nginx", false, false,
&networking.IngressClass{ &networking.IngressClass{
ObjectMeta: meta_v1.ObjectMeta{ ObjectMeta: meta_v1.ObjectMeta{
Name: "custom", Name: "custom",
@ -82,6 +86,9 @@ func TestIsValidClass(t *testing.T) {
if test.annotation { if test.annotation {
ing.Annotations[IngressKey] = test.ingress ing.Annotations[IngressKey] = test.ingress
} }
if test.ingressClassName {
ing.Spec.IngressClassName = &[]string{test.ingress}[0]
}
IngressClass = test.controller IngressClass = test.controller
DefaultClass = test.defClass DefaultClass = test.defClass