Skip to content

Commit

Permalink
ingress source: ingressClassNames now feed into annotation filter
Browse files Browse the repository at this point in the history
  • Loading branch information
dsalisbury committed Oct 2, 2021
1 parent 71a672f commit 901effb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
27 changes: 19 additions & 8 deletions source/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}

Expand Down
13 changes: 13 additions & 0 deletions source/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand All @@ -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"},
},
},
},
} {
Expand Down

0 comments on commit 901effb

Please sign in to comment.