Skip to content

Commit b4559c7

Browse files
committed
BUG/MINOR: Fix IngressClass filtering
The controller should follow the followig rules in order to accept or not an Ingress Resource: - If the "--ingress.class" argument of the controller is not configured: - Accept Ingress resource when neither "ingress.class" annotation nor "ingressClassName" fields are set. - Accept Ingress resource when "ingress.class" annotation is not set but "ingressClassName" field matches. - If the "--ingress.class" argument of the controller is configured: - Accept Ingress resource when neither "ingress.class" annotation nor "ingressClassName" fields are set but controller argument "--EmptyIngressClass" is enabled. - Accept Ingress resource when "--ingress.class" argument is equal to "ingress.class" annotation. - Accept Ingress resource when "ingressClassName" field matches. - Ignore Ingress resource otherwise. - "ingressClassName" field (of Ingress resource) matches means that the corresponding IngressClass resource should have the Spec.Controller field equal to "haproxy.org/ingress-controller/<ingress.class>" where <ingress.class> is the value of the "--ingress.class" argument of the controller. In addition to rewriting Ingress eligibility to make it clearer this commit fixes "ingressClassName" field matching as discussed in github issure #354
1 parent 26349eb commit b4559c7

File tree

5 files changed

+46
-26
lines changed

5 files changed

+46
-26
lines changed

controller/ingress/ingress.go

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package ingress
1616

1717
import (
1818
"fmt"
19+
"path/filepath"
1920

2021
"github.com/haproxytech/kubernetes-ingress/controller/annotations"
2122
"github.com/haproxytech/kubernetes-ingress/controller/configuration"
@@ -28,18 +29,18 @@ import (
2829
)
2930

3031
type Ingress struct {
31-
resource *store.Ingress
32-
ruleIDs []rules.RuleID
33-
class string
34-
emptyClass bool
35-
sslPassthrough bool
32+
resource *store.Ingress
33+
ruleIDs []rules.RuleID
34+
controllerClass string
35+
allowEmptyClass bool
36+
sslPassthrough bool
3637
}
3738

3839
// New returns an Ingress instance to handle the k8s ingress resource given in params.
3940
// If the k8s ingress resource is not assigned to the controller (no matching IngressClass)
4041
// then New will return nil
4142
func New(k store.K8s, resource *store.Ingress, class string, emptyClass bool) *Ingress {
42-
i := &Ingress{resource: resource, class: class, emptyClass: emptyClass}
43+
i := &Ingress{resource: resource, controllerClass: class, allowEmptyClass: emptyClass}
4344
if i.resource == nil || !i.supported(k) {
4445
return nil
4546
}
@@ -52,28 +53,45 @@ func New(k store.K8s, resource *store.Ingress, class string, emptyClass bool) *I
5253
// According to https://github.com/kubernetes/api/blob/master/networking/v1/types.go#L257
5354
// ingress.class annotation should have precedence over the IngressClass mechanism implemented
5455
// in "networking.k8s.io".
55-
func (i Ingress) supported(k8s store.K8s) bool {
56-
var igClass *store.IngressClass
57-
igClassAnn := annotations.String("ingress.class", i.resource.Annotations)
58-
59-
// If ingress class is unassigned and the controller is controlling any resource without explicit ingress class then support it.
60-
if igClassAnn == i.class {
61-
return true
62-
}
63-
if igClassAnn == "" && i.emptyClass {
64-
return true
56+
func (i Ingress) supported(k8s store.K8s) (supported bool) {
57+
var igClassAnn, igClassSpec string
58+
igClassAnn = annotations.String("ingress.class", i.resource.Annotations)
59+
if igClassResource := k8s.IngressClasses[i.resource.Class]; igClassResource != nil && igClassResource.Status != store.DELETED {
60+
igClassSpec = igClassResource.Controller
6561
}
6662

67-
igClass = k8s.IngressClasses[i.resource.Class]
68-
if igClass != nil && igClass.Status != store.DELETED && igClass.Controller == CONTROLLER_CLASS {
69-
// Corresponding IngresClass was updated so Ingress resource should be re-processed
70-
// This is particularly important if the Ingress was skipped due to mismatching ingrssClass
71-
if igClass.Status != store.EMPTY {
72-
i.resource.Status = store.MODIFIED
63+
defer func() {
64+
if supported && i.resource.Ignored {
65+
i.resource.Status = store.ADDED
66+
i.resource.Ignored = false
67+
}
68+
}()
69+
70+
if i.controllerClass == "" {
71+
if igClassAnn == "" && igClassSpec == "" {
72+
supported = true
73+
return
74+
}
75+
if igClassSpec == CONTROLLER {
76+
supported = true
77+
return
78+
}
79+
} else {
80+
if igClassAnn == "" && igClassSpec == "" && i.allowEmptyClass {
81+
supported = true
82+
return
83+
}
84+
if igClassAnn == i.controllerClass {
85+
supported = true
86+
return
87+
}
88+
if igClassSpec == filepath.Join(CONTROLLER, i.controllerClass) {
89+
supported = true
90+
return
7391
}
74-
return true
7592
}
76-
return false
93+
i.resource.Ignored = true
94+
return
7795
}
7896

7997
func (i *Ingress) handlePath(k store.K8s, cfg *configuration.ControllerCfg, api api.HAProxyClient, host string, path *store.IngressPath) (reload bool, err error) {

controller/ingress/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
//nolint:golint,stylecheck
10-
const CONTROLLER_CLASS = "haproxy.org/ingress-controller"
10+
const CONTROLLER = "haproxy.org/ingress-controller"
1111

1212
var logger = utils.GetLogger()
1313

controller/store/events.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func (k *K8s) EventIngress(ns *Namespace, data *Ingress, controllerClass string)
8080
newIngress.Status = ADDED
8181
return k.EventIngress(ns, newIngress, controllerClass)
8282
}
83+
newIngress.Ignored = oldIngress.Ignored
8384
if oldIngress.Equal(data) {
8485
return false
8586
}

controller/store/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ type Ingress struct {
132132
Rules map[string]*IngressRule
133133
DefaultBackend *IngressPath
134134
TLS map[string]*IngressTLS
135+
Ignored bool // true if resource ignored because of non matching Controller Class
135136
Status Status
136137
}
137138

deploy/tests/e2e/ingressclass/config/deploy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ kind: IngressClass
4646
metadata:
4747
name: haproxy
4848
spec:
49-
controller: haproxy.org/ingress-controller
49+
controller: haproxy.org/ingress-controller/haproxy

0 commit comments

Comments
 (0)