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
|
// 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 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
return ingress == IngressClass
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. k8s < v1.18. Check default annotation
|
// empty ingress and IngressClass equal default
|
||||||
if !k8s.IsIngressV1Beta1Ready {
|
if len(ingress) == 0 && IngressClass == DefaultClass {
|
||||||
return IngressClass == DefaultClass
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. without annotation and IngressClass. Check default annotation
|
// k8s > v1.18.
|
||||||
if k8s.IngressClass == nil {
|
// Processing may be redundant because k8s.IngressClass is obtained by IngressClass
|
||||||
return IngressClass == DefaultClass
|
// 3. without annotation and IngressClass. Check IngressClass
|
||||||
|
if k8s.IngressClass != nil {
|
||||||
|
return ingress == k8s.IngressClass.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. with IngressClass
|
// 4. with IngressClass
|
||||||
if ing.Spec.IngressClassName != nil {
|
return ingress == IngressClass
|
||||||
return k8s.IngressClass.Name == *ing.Spec.IngressClassName
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,28 +39,32 @@ func TestIsValidClass(t *testing.T) {
|
||||||
}()
|
}()
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
ingress string
|
ingress string
|
||||||
controller string
|
controller string
|
||||||
defClass string
|
defClass string
|
||||||
annotation bool
|
annotation bool
|
||||||
k8sClass *networking.IngressClass
|
ingressClassName bool
|
||||||
v1Ready bool
|
k8sClass *networking.IngressClass
|
||||||
isValid bool
|
v1Ready 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
|
||||||
|
|
Loading…
Reference in a new issue