From b8641e170434f38dfac7e84fcdfb699eb78feee2 Mon Sep 17 00:00:00 2001 From: "yasin.lachiny" Date: Sun, 8 Jan 2023 23:01:55 +0100 Subject: [PATCH] fix: check default ingclass when ingclass is nill --- d | 0 pkg/ingress/class_loader.go | 36 +++++++++++++++++++++++++++++++++++- pkg/ingress/group_loader.go | 21 +++++++++------------ 3 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 d diff --git a/d b/d new file mode 100644 index 000000000..e69de29bb diff --git a/pkg/ingress/class_loader.go b/pkg/ingress/class_loader.go index 7006a08ff..7c20b898e 100644 --- a/pkg/ingress/class_loader.go +++ b/pkg/ingress/class_loader.go @@ -3,6 +3,7 @@ package ingress import ( "context" "fmt" + "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" networking "k8s.io/api/networking/v1" @@ -20,6 +21,8 @@ const ( ingressClassControllerALB = "ingress.k8s.aws/alb" // the Kind for IngressClassParams CRD. ingressClassParamsKind = "IngressClassParams" + // default class from ingressClass + defaultClassAnnotation = "ingressclass.kubernetes.io/is-default-class" ) // ErrInvalidIngressClass is an sentinel error that represents the IngressClass configuration for Ingress is invalid. @@ -43,9 +46,40 @@ type defaultClassLoader struct { client client.Client } +// GetDefaultIngressClass returns the default IngressClass from the list of IngressClasses. +// If multiple IngressClasses are marked as the default, it returns an error. +func (l *defaultClassLoader) GetDefaultIngressClass(ctx context.Context) (string, error) { + var defaultClass string + var defaultClassFound bool + ingClassList := &networking.IngressClassList{} + if err := l.client.List(ctx, ingClassList); err != nil { + return "", fmt.Errorf("failed to fetch ingressClasses, %v", err.Error()) + } + for _, ingressClass := range ingClassList.Items { + if ingressClass.Annotations[defaultClassAnnotation] == "true" { + if defaultClassFound { + return "", errors.Errorf("multiple default IngressClasses found") + } + defaultClass = ingressClass.GetName() + defaultClassFound = true + } + } + + return defaultClass, nil +} + func (l *defaultClassLoader) Load(ctx context.Context, ing *networking.Ingress) (ClassConfiguration, error) { + if ing.Spec.IngressClassName == nil { - return ClassConfiguration{}, fmt.Errorf("%w: %v", ErrInvalidIngressClass, "spec.ingressClassName is nil") + defaultClass, err := l.GetDefaultIngressClass(ctx) + if err != nil { + return ClassConfiguration{}, err + } + if defaultClass != "" { + ing.Spec.IngressClassName = &defaultClass + } else { + return ClassConfiguration{}, err + } } ingClassKey := types.NamespacedName{Name: *ing.Spec.IngressClassName} diff --git a/pkg/ingress/group_loader.go b/pkg/ingress/group_loader.go index 8ca77bb70..7433799fd 100644 --- a/pkg/ingress/group_loader.go +++ b/pkg/ingress/group_loader.go @@ -81,7 +81,6 @@ func (m *defaultGroupLoader) Load(ctx context.Context, groupID GroupID) (Group, if err := m.client.List(ctx, ingList); err != nil { return Group{}, err } - var members []ClassifiedIngress var inactiveMembers []*networking.Ingress for index := range ingList.Items { @@ -207,17 +206,15 @@ func (m *defaultGroupLoader) classifyIngress(ctx context.Context, ing *networkin IngClassConfig: ClassConfiguration{}, }, false, nil } - - if ing.Spec.IngressClassName != nil { - ingClassConfig, err := m.classLoader.Load(ctx, ing) - if err != nil { - return ClassifiedIngress{ - Ing: ing, - IngClassConfig: ClassConfiguration{}, - }, false, err - } - - if matchesIngressClass := ingClassConfig.IngClass != nil && ingClassConfig.IngClass.Spec.Controller == ingressClassControllerALB; matchesIngressClass { + ingClassConfig, err := m.classLoader.Load(ctx, ing) + if err != nil { + return ClassifiedIngress{ + Ing: ing, + IngClassConfig: ClassConfiguration{}, + }, false, err + } + if matchesIngressClass := ingClassConfig.IngClass != nil; matchesIngressClass { + if ingClassConfig.IngClass.Spec.Controller == ingressClassControllerALB { return ClassifiedIngress{ Ing: ing, IngClassConfig: ingClassConfig,