Skip to content

Commit

Permalink
Make --ignore-hostname-annotation flag more consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
johngmyers committed Sep 29, 2023
1 parent 5225d99 commit 1280e0d
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 65 deletions.
4 changes: 2 additions & 2 deletions docs/annotations/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ The following table documents which sources support which annotations:
| Gloo | | | | Yes | Yes[^5] | Yes[^5] |
| Ingress | Yes | Yes[^1] | | Yes | Yes | Yes |
| Istio | Yes | Yes[^1] | | Yes | Yes | Yes |
| Kong | | Yes | | Yes | Yes | Yes |
| Kong | | Yes[^1] | | Yes | Yes | Yes |
| Node | Yes | | | Yes | Yes | |
| OpenShift | Yes | Yes[^1] | | Yes | Yes | Yes |
| Pod | | Yes | Yes | Yes | | |
| Service | Yes | Yes[^1] | Yes[^1][^2] | Yes[^3] | Yes | Yes |
| Skipper | Yes | Yes[^1] | | Yes | Yes | Yes |
| Traefik | | Yes | | Yes | Yes | Yes |
| Traefik | | Yes[^1] | | Yes | Yes | Yes |

[^1]: Unless the `--ignore-hostname-annotation` flag is specified.
[^2]: Only behaves differently than `hostname` for `Service`s of type `ClusterIP` or `LoadBalancer`.
Expand Down
36 changes: 20 additions & 16 deletions source/kong_tcpingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,17 @@ var kongGroupdVersionResource = schema.GroupVersionResource{

// kongTCPIngressSource is an implementation of Source for Kong TCPIngress objects.
type kongTCPIngressSource struct {
annotationFilter string
dynamicKubeClient dynamic.Interface
kongTCPIngressInformer informers.GenericInformer
kubeClient kubernetes.Interface
namespace string
unstructuredConverter *unstructuredConverter
annotationFilter string
ignoreHostnameAnnotation bool
dynamicKubeClient dynamic.Interface
kongTCPIngressInformer informers.GenericInformer
kubeClient kubernetes.Interface
namespace string
unstructuredConverter *unstructuredConverter
}

// NewKongTCPIngressSource creates a new kongTCPIngressSource with the given config.
func NewKongTCPIngressSource(ctx context.Context, dynamicKubeClient dynamic.Interface, kubeClient kubernetes.Interface, namespace string, annotationFilter string) (Source, error) {
func NewKongTCPIngressSource(ctx context.Context, dynamicKubeClient dynamic.Interface, kubeClient kubernetes.Interface, namespace string, annotationFilter string, ignoreHostnameAnnotation bool) (Source, error) {
var err error

// Use shared informer to listen for add/update/delete of Host in the specified namespace.
Expand Down Expand Up @@ -85,12 +86,13 @@ func NewKongTCPIngressSource(ctx context.Context, dynamicKubeClient dynamic.Inte
}

return &kongTCPIngressSource{
annotationFilter: annotationFilter,
dynamicKubeClient: dynamicKubeClient,
kongTCPIngressInformer: kongTCPIngressInformer,
kubeClient: kubeClient,
namespace: namespace,
unstructuredConverter: uc,
annotationFilter: annotationFilter,
ignoreHostnameAnnotation: ignoreHostnameAnnotation,
dynamicKubeClient: dynamicKubeClient,
kongTCPIngressInformer: kongTCPIngressInformer,
kubeClient: kubeClient,
namespace: namespace,
unstructuredConverter: uc,
}, nil
}

Expand Down Expand Up @@ -213,9 +215,11 @@ func (sc *kongTCPIngressSource) endpointsFromTCPIngress(tcpIngress *TCPIngress,

resource := fmt.Sprintf("tcpingress/%s/%s", tcpIngress.Namespace, tcpIngress.Name)

hostnameList := getHostnamesFromAnnotations(tcpIngress.Annotations)
for _, hostname := range hostnameList {
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
if !sc.ignoreHostnameAnnotation {
hostnameList := getHostnamesFromAnnotations(tcpIngress.Annotations)
for _, hostname := range hostnameList {
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
}
}

if tcpIngress.Spec.Rules != nil {
Expand Down
72 changes: 67 additions & 5 deletions source/kong_tcpingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ func TestKongTCPIngressEndpoints(t *testing.T) {
t.Parallel()

for _, ti := range []struct {
title string
tcpProxy TCPIngress
expected []*endpoint.Endpoint
title string
tcpProxy TCPIngress
ignoreHostnameAnnotation bool
expected []*endpoint.Endpoint
}{
{
title: "TCPIngress with hostname annotation",
Expand Down Expand Up @@ -220,6 +221,67 @@ func TestKongTCPIngressEndpoints(t *testing.T) {
},
},
},
{
title: "TCPIngress ignoring hostname annotation",
tcpProxy: TCPIngress{
TypeMeta: metav1.TypeMeta{
APIVersion: kongGroupdVersionResource.GroupVersion().String(),
Kind: "TCPIngress",
},
ObjectMeta: metav1.ObjectMeta{
Name: "tcp-ingress-both",
Namespace: defaultKongNamespace,
Annotations: map[string]string{
"external-dns.alpha.kubernetes.io/hostname": "d.example.com",
"kubernetes.io/ingress.class": "kong",
},
},
Spec: tcpIngressSpec{
Rules: []tcpIngressRule{
{
Port: 30004,
Host: "e.example.com",
},
{
Port: 30005,
Host: "f.example.com",
},
},
},
Status: tcpIngressStatus{
LoadBalancer: corev1.LoadBalancerStatus{
Ingress: []corev1.LoadBalancerIngress{
{
Hostname: "a12e71861a4303f063456769a314a3bd-1291189659.us-east-1.elb.amazonaws.com",
},
},
},
},
},
ignoreHostnameAnnotation: true,
expected: []*endpoint.Endpoint{
{
DNSName: "e.example.com",
Targets: []string{"a12e71861a4303f063456769a314a3bd-1291189659.us-east-1.elb.amazonaws.com"},
RecordType: endpoint.RecordTypeCNAME,
RecordTTL: 0,
Labels: endpoint.Labels{
"resource": "tcpingress/kong/tcp-ingress-both",
},
ProviderSpecific: endpoint.ProviderSpecific{},
},
{
DNSName: "f.example.com",
Targets: []string{"a12e71861a4303f063456769a314a3bd-1291189659.us-east-1.elb.amazonaws.com"},
RecordType: endpoint.RecordTypeCNAME,
RecordTTL: 0,
Labels: endpoint.Labels{
"resource": "tcpingress/kong/tcp-ingress-both",
},
ProviderSpecific: endpoint.ProviderSpecific{},
},
},
},
{
title: "TCPIngress with target annotation",
tcpProxy: TCPIngress{
Expand Down Expand Up @@ -300,7 +362,7 @@ func TestKongTCPIngressEndpoints(t *testing.T) {
_, err = fakeDynamicClient.Resource(kongGroupdVersionResource).Namespace(defaultKongNamespace).Create(context.Background(), &tcpi, metav1.CreateOptions{})
assert.NoError(t, err)

source, err := NewKongTCPIngressSource(context.TODO(), fakeDynamicClient, fakeKubernetesClient, defaultKongNamespace, "kubernetes.io/ingress.class=kong")
source, err := NewKongTCPIngressSource(context.TODO(), fakeDynamicClient, fakeKubernetesClient, defaultKongNamespace, "kubernetes.io/ingress.class=kong", ti.ignoreHostnameAnnotation)
assert.NoError(t, err)
assert.NotNil(t, source)

Expand All @@ -312,7 +374,7 @@ func TestKongTCPIngressEndpoints(t *testing.T) {
endpoints, err := source.Endpoints(context.Background())
assert.NoError(t, err)
assert.Len(t, endpoints, len(ti.expected))
assert.Equal(t, endpoints, ti.expected)
assert.Equal(t, ti.expected, endpoints)
})
}
}
4 changes: 2 additions & 2 deletions source/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func BuildWithConfig(ctx context.Context, source string, p ClientGenerator, cfg
if err != nil {
return nil, err
}
return NewTraefikSource(ctx, dynamicClient, kubernetesClient, cfg.Namespace, cfg.AnnotationFilter)
return NewTraefikSource(ctx, dynamicClient, kubernetesClient, cfg.Namespace, cfg.AnnotationFilter, cfg.IgnoreHostnameAnnotation)
case "openshift-route":
ocpClient, err := p.OpenShiftClient()
if err != nil {
Expand Down Expand Up @@ -341,7 +341,7 @@ func BuildWithConfig(ctx context.Context, source string, p ClientGenerator, cfg
if err != nil {
return nil, err
}
return NewKongTCPIngressSource(ctx, dynamicClient, kubernetesClient, cfg.Namespace, cfg.AnnotationFilter)
return NewKongTCPIngressSource(ctx, dynamicClient, kubernetesClient, cfg.Namespace, cfg.AnnotationFilter, cfg.IgnoreHostnameAnnotation)
case "f5-virtualserver":
kubernetesClient, err := p.KubeClient()
if err != nil {
Expand Down
28 changes: 18 additions & 10 deletions source/traefik_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ var (

type traefikSource struct {
annotationFilter string
ignoreHostnameAnnotation bool
dynamicKubeClient dynamic.Interface
ingressRouteInformer informers.GenericInformer
ingressRouteTcpInformer informers.GenericInformer
Expand All @@ -92,7 +93,7 @@ type traefikSource struct {
unstructuredConverter *unstructuredConverter
}

func NewTraefikSource(ctx context.Context, dynamicKubeClient dynamic.Interface, kubeClient kubernetes.Interface, namespace string, annotationFilter string) (Source, error) {
func NewTraefikSource(ctx context.Context, dynamicKubeClient dynamic.Interface, kubeClient kubernetes.Interface, namespace string, annotationFilter string, ignoreHostnameAnnotation bool) (Source, error) {
// Use shared informer to listen for add/update/delete of Host in the specified namespace.
// Set resync period to 0, to prevent processing when nothing has changed.
informerFactory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(dynamicKubeClient, 0, namespace, nil)
Expand Down Expand Up @@ -149,6 +150,7 @@ func NewTraefikSource(ctx context.Context, dynamicKubeClient dynamic.Interface,

return &traefikSource{
annotationFilter: annotationFilter,
ignoreHostnameAnnotation: ignoreHostnameAnnotation,
dynamicKubeClient: dynamicKubeClient,
ingressRouteInformer: ingressRouteInformer,
ingressRouteTcpInformer: ingressRouteTcpInformer,
Expand Down Expand Up @@ -656,9 +658,11 @@ func (ts *traefikSource) endpointsFromIngressRoute(ingressRoute *IngressRoute, t

resource := fmt.Sprintf("ingressroute/%s/%s", ingressRoute.Namespace, ingressRoute.Name)

hostnameList := getHostnamesFromAnnotations(ingressRoute.Annotations)
for _, hostname := range hostnameList {
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
if !ts.ignoreHostnameAnnotation {
hostnameList := getHostnamesFromAnnotations(ingressRoute.Annotations)
for _, hostname := range hostnameList {
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
}
}

for _, route := range ingressRoute.Spec.Routes {
Expand Down Expand Up @@ -693,9 +697,11 @@ func (ts *traefikSource) endpointsFromIngressRouteTCP(ingressRoute *IngressRoute

resource := fmt.Sprintf("ingressroutetcp/%s/%s", ingressRoute.Namespace, ingressRoute.Name)

hostnameList := getHostnamesFromAnnotations(ingressRoute.Annotations)
for _, hostname := range hostnameList {
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
if !ts.ignoreHostnameAnnotation {
hostnameList := getHostnamesFromAnnotations(ingressRoute.Annotations)
for _, hostname := range hostnameList {
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
}
}

for _, route := range ingressRoute.Spec.Routes {
Expand Down Expand Up @@ -731,9 +737,11 @@ func (ts *traefikSource) endpointsFromIngressRouteUDP(ingressRoute *IngressRoute

resource := fmt.Sprintf("ingressrouteudp/%s/%s", ingressRoute.Namespace, ingressRoute.Name)

hostnameList := getHostnamesFromAnnotations(ingressRoute.Annotations)
for _, hostname := range hostnameList {
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
if !ts.ignoreHostnameAnnotation {
hostnameList := getHostnamesFromAnnotations(ingressRoute.Annotations)
for _, hostname := range hostnameList {
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier, resource)...)
}
}

return endpoints, nil
Expand Down
Loading

0 comments on commit 1280e0d

Please sign in to comment.