Skip to content

Commit

Permalink
fix: check default ingclass when ingclass is nill
Browse files Browse the repository at this point in the history
  • Loading branch information
yasinlachiny committed Jan 9, 2023
1 parent 9c3f5a2 commit b8641e1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
Empty file added d
Empty file.
36 changes: 35 additions & 1 deletion pkg/ingress/class_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.
Expand All @@ -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}
Expand Down
21 changes: 9 additions & 12 deletions pkg/ingress/group_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b8641e1

Please sign in to comment.