diff --git a/source/ingress.go b/source/ingress.go index bb0bf2961e..58fb119be2 100644 --- a/source/ingress.go +++ b/source/ingress.go @@ -27,6 +27,7 @@ import ( log "github.com/sirupsen/logrus" networkv1 "k8s.io/api/networking/v1" "k8s.io/apimachinery/pkg/labels" + "k8s.io/apimachinery/pkg/selection" "k8s.io/apimachinery/pkg/util/wait" kubeinformers "k8s.io/client-go/informers" netinformers "k8s.io/client-go/informers/networking/v1" @@ -241,24 +242,34 @@ func (sc *ingressSource) filterByIngressClass(ingresses []*networkv1.Ingress) ([ return ingresses, nil } + classNameReq, err := labels.NewRequirement("kubernetes.io/ingress.class", selection.In, sc.ingressClassNames) + if err != nil { + return nil, errors.New("Failed to create selector requirement from ingress class names") + } + + selector := labels.NewSelector() + selector = selector.Add(*classNameReq) + filteredList := []*networkv1.Ingress{} for _, ingress := range ingresses { - // we have a filter class but this ingress doesn't have its class set - if ingress.Spec.IngressClassName == nil { - log.Debugf("Ignoring ingress %s/%s because ingressClassName is not set", ingress.Namespace, ingress.Name) - } - var matched = false; + for _, nameFilter := range sc.ingressClassNames { - if nameFilter == *ingress.Spec.IngressClassName { - filteredList = append(filteredList, ingress) + if ingress.Spec.IngressClassName != nil && nameFilter == *ingress.Spec.IngressClassName { + matched = true; + } else if matchLabelSelector(selector, ingress.Annotations) { matched = true; + } + + if matched == true { + filteredList = append(filteredList, ingress) break } } + if matched == false { - log.Debugf("Ignoring ingress %s/%s because ingressClassName '%s' is not in specified list", ingress.Namespace, ingress.Name, *ingress.Spec.IngressClassName) + log.Debugf("Discarding ingress %s/%s because it does not match required ingress classes %v", ingress.Namespace, ingress.Name, sc.ingressClassNames) } } diff --git a/source/ingress_test.go b/source/ingress_test.go index 9f1590a4c0..9cf60d9630 100644 --- a/source/ingress_test.go +++ b/source/ingress_test.go @@ -1198,6 +1198,15 @@ func testIngressEndpoints(t *testing.T) { ips: []string{"3.4.5.6"}, ingressClassName: "dmz", }, + { + name: "annotated-dmz", + namespace: namespace, + tlsdnsnames: [][]string{{"annodmz.example.org"}}, + ips: []string{"4.5.6.7"}, + annotations: map[string]string{ + "kubernetes.io/ingress.class": "dmz", + }, + }, }, expected: []*endpoint.Endpoint{ { @@ -1208,6 +1217,10 @@ func testIngressEndpoints(t *testing.T) { DNSName: "dmz.example.org", Targets: endpoint.Targets{"3.4.5.6"}, }, + { + DNSName: "annodmz.example.org", + Targets: endpoint.Targets{"4.5.6.7"}, + }, }, }, } {