Fix panic in ingress class validation
If an ingress had no class annotation, nor IngressClassName at all, and an IngressClass resource was created for the ingress-nginx there was a panic when the controller tried to check the IngressClassName of the Ingress.
This commit is contained in:
parent
56a1e82125
commit
7d82903ce9
2 changed files with 46 additions and 17 deletions
|
@ -63,5 +63,8 @@ func IsValid(ing *networking.Ingress) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. with IngressClass
|
// 4. with IngressClass
|
||||||
return k8s.IngressClass.Name == *ing.Spec.IngressClassName
|
if ing.Spec.IngressClassName != nil {
|
||||||
|
return k8s.IngressClass.Name == *ing.Spec.IngressClassName
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,45 +22,71 @@ import (
|
||||||
api "k8s.io/api/core/v1"
|
api "k8s.io/api/core/v1"
|
||||||
networking "k8s.io/api/networking/v1beta1"
|
networking "k8s.io/api/networking/v1beta1"
|
||||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/ingress-nginx/internal/k8s"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIsValidClass(t *testing.T) {
|
func TestIsValidClass(t *testing.T) {
|
||||||
dc := DefaultClass
|
dc := DefaultClass
|
||||||
ic := IngressClass
|
ic := IngressClass
|
||||||
|
k8sic := k8s.IngressClass
|
||||||
|
v1Ready := k8s.IsIngressV1Ready
|
||||||
// restore original values after the tests
|
// restore original values after the tests
|
||||||
defer func() {
|
defer func() {
|
||||||
DefaultClass = dc
|
DefaultClass = dc
|
||||||
IngressClass = ic
|
IngressClass = ic
|
||||||
|
k8s.IngressClass = k8sic
|
||||||
|
k8s.IsIngressV1Ready = v1Ready
|
||||||
}()
|
}()
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
ingress string
|
ingress string
|
||||||
controller string
|
controller string
|
||||||
defClass string
|
defClass string
|
||||||
|
annotation bool
|
||||||
|
k8sClass *networking.IngressClass
|
||||||
|
v1Ready bool
|
||||||
isValid bool
|
isValid bool
|
||||||
}{
|
}{
|
||||||
{"", "", "nginx", true},
|
{"", "", "nginx", true, nil, false, true},
|
||||||
{"", "nginx", "nginx", true},
|
{"", "nginx", "nginx", true, nil, false, true},
|
||||||
{"nginx", "nginx", "nginx", true},
|
{"nginx", "nginx", "nginx", true, nil, false, true},
|
||||||
{"custom", "custom", "nginx", true},
|
{"custom", "custom", "nginx", true, nil, false, true},
|
||||||
{"", "killer", "nginx", false},
|
{"", "killer", "nginx", true, nil, false, false},
|
||||||
{"custom", "nginx", "nginx", false},
|
{"custom", "nginx", "nginx", true, nil, false, false},
|
||||||
|
{"", "custom", "nginx", false,
|
||||||
|
&networking.IngressClass{
|
||||||
|
ObjectMeta: meta_v1.ObjectMeta{
|
||||||
|
Name: "custom",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
false, false},
|
||||||
|
{"", "custom", "nginx", false,
|
||||||
|
&networking.IngressClass{
|
||||||
|
ObjectMeta: meta_v1.ObjectMeta{
|
||||||
|
Name: "custom",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
true, false},
|
||||||
}
|
}
|
||||||
|
|
||||||
ing := &networking.Ingress{
|
|
||||||
ObjectMeta: meta_v1.ObjectMeta{
|
|
||||||
Name: "foo",
|
|
||||||
Namespace: api.NamespaceDefault,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
data := map[string]string{}
|
|
||||||
ing.SetAnnotations(data)
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
ing.Annotations[IngressKey] = test.ingress
|
ing := &networking.Ingress{
|
||||||
|
ObjectMeta: meta_v1.ObjectMeta{
|
||||||
|
Name: "foo",
|
||||||
|
Namespace: api.NamespaceDefault,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
data := map[string]string{}
|
||||||
|
ing.SetAnnotations(data)
|
||||||
|
if test.annotation {
|
||||||
|
ing.Annotations[IngressKey] = test.ingress
|
||||||
|
}
|
||||||
|
|
||||||
IngressClass = test.controller
|
IngressClass = test.controller
|
||||||
DefaultClass = test.defClass
|
DefaultClass = test.defClass
|
||||||
|
k8s.IngressClass = test.k8sClass
|
||||||
|
k8s.IsIngressV1Ready = test.v1Ready
|
||||||
|
|
||||||
b := IsValid(ing)
|
b := IsValid(ing)
|
||||||
if b != test.isValid {
|
if b != test.isValid {
|
||||||
|
|
Loading…
Reference in a new issue