Add support for IngressClass and ingress.class annotation

This commit is contained in:
Manuel Alejandro de Brito Fontes 2020-04-20 17:38:50 -04:00
parent d4e0657991
commit efbb3f9fc8
17 changed files with 350 additions and 53 deletions

View file

@ -18,6 +18,7 @@ package class
import (
networking "k8s.io/api/networking/v1beta1"
"k8s.io/ingress-nginx/internal/k8s"
)
const (
@ -37,30 +38,30 @@ var (
IngressClass = "nginx"
)
// IsValid returns true if the given Ingress either doesn't specify
// the ingress.class annotation, or it's set to the configured in the
// ingress controller.
// 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 {
className := ing.Spec.IngressClassName
// 1. with annotation
ingress, ok := ing.GetAnnotations()[IngressKey]
if ok {
// empty annotation and same annotation on ingress
if ingress == "" && IngressClass == DefaultClass {
return true
}
// we have 2 valid combinations
// 1 - ingress with default class | blank annotation on ingress
// 2 - ingress with specific class | same annotation on ingress
//
// and 2 invalid combinations
// 3 - ingress with default class | fixed annotation on ingress
// 4 - ingress with specific class | different annotation on ingress
if className != nil {
return *className == IngressClass
return ingress == IngressClass
}
if IngressClass == DefaultClass {
return true
// 2. k8s < v1.18. Check default annotation
if !k8s.IsIngressV1Ready {
return IngressClass == DefaultClass
}
if IngressClass == "" {
return true
// 3. without annotation and IngressClass. Check default annotation
if k8s.IngressClass == nil {
return IngressClass == DefaultClass
}
return false
// 4. with IngressClass
return k8s.IngressClass.Name == *ing.Spec.IngressClassName
}

View file

@ -54,10 +54,10 @@ func TestIsValidClass(t *testing.T) {
},
}
data := map[string]string{}
ing.SetAnnotations(data)
for _, test := range tests {
if test.ingress != "" {
ing.Spec.IngressClassName = &test.ingress
}
ing.Annotations[IngressKey] = test.ingress
IngressClass = test.controller
DefaultClass = test.defClass

View file

@ -190,8 +190,7 @@ func TestCheckIngress(t *testing.T) {
}
t.Run("When the ingress class differs from nginx", func(t *testing.T) {
class := "different"
ing.Spec.IngressClassName = &class
ing.ObjectMeta.Annotations["kubernetes.io/ingress.class"] = "different"
nginx.command = testNginxTestCommand{
t: t,
err: fmt.Errorf("test error"),
@ -202,8 +201,7 @@ func TestCheckIngress(t *testing.T) {
})
t.Run("when the class is the nginx one", func(t *testing.T) {
class := "nginx"
ing.Spec.IngressClassName = &class
ing.ObjectMeta.Annotations["kubernetes.io/ingress.class"] = "nginx"
nginx.command = testNginxTestCommand{
t: t,
err: nil,

View file

@ -949,30 +949,18 @@ func toIngress(obj interface{}) (*networkingv1beta1.Ingress, bool) {
return nil, false
}
ing.Spec.IngressClassName = extractClassName(ing)
setDefaultPathTypeIfEmpty(ing)
return ing, true
}
if ing, ok := obj.(*networkingv1beta1.Ingress); ok {
ing.Spec.IngressClassName = extractClassName(ing)
setDefaultPathTypeIfEmpty(ing)
return ing, true
}
return nil, false
}
func extractClassName(ing *networkingv1beta1.Ingress) *string {
if c, ok := ing.Annotations[class.IngressKey]; ok {
return &c
}
return nil
}
// Default path type is Prefix to not break existing definitions
var defaultPathType = networkingv1beta1.PathTypePrefix

View file

@ -25,6 +25,7 @@ import (
"k8s.io/klog"
apiv1 "k8s.io/api/core/v1"
networkingv1beta1 "k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/version"
clientset "k8s.io/client-go/kubernetes"
@ -121,6 +122,13 @@ var IsNetworkingIngressAvailable bool
// IsIngressV1Ready indicates if the running Kubernetes version is at least v1.18.0
var IsIngressV1Ready bool
// IngressClass indicates the class of the Ingress to use as filter
var IngressClass *networkingv1beta1.IngressClass
// IngressNGINXController defines the valid value of IngressClass
// Controller field for ingress-nginx
const IngressNGINXController = "k8s.io/ingress-nginx"
// NetworkingIngressAvailable checks if the package "k8s.io/api/networking/v1beta1"
// is available or not and if Ingress V1 is supported (k8s >= v1.18.0)
func NetworkingIngressAvailable(client clientset.Interface) (bool, bool) {