Skip to content

Commit

Permalink
ingress: getAddresses algo returning CNames
Browse files Browse the repository at this point in the history
  • Loading branch information
morpheu committed Jul 8, 2022
1 parent fa0f5d5 commit b51de88
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
30 changes: 26 additions & 4 deletions kubernetes/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,20 @@ func (k *IngressService) GetAddresses(ctx context.Context, id router.InstanceID)
}
return nil, err
}
urls := k.getAddressesURLs(ingress)
if cnames, found := ingress.Annotations[AnnotationsCNames]; found && cnames != "" {
for _, cname := range strings.Split(cnames, ",") {
ingressCName, err := k.getCName(ctx, id, cname)
if err != nil {
return nil, err
}
urls = append(urls, k.getAddressesURLs(ingressCName)...)
}
}
return urls, nil
}

func (k *IngressService) getAddressesURLs(ingress *networkingV1.Ingress) []string {
hosts := []string{}
urls := []string{}
for _, rule := range ingress.Spec.Rules {
Expand All @@ -420,9 +434,9 @@ func (k *IngressService) GetAddresses(ctx context.Context, id router.InstanceID)
}
}
if len(urls) > 0 {
return urls, nil
return urls
}
return hosts, nil
return hosts
}
func (k *IngressService) GetStatus(ctx context.Context, id router.InstanceID) (router.BackendStatus, string, error) {
ingress, err := k.get(ctx, id)
Expand Down Expand Up @@ -452,11 +466,19 @@ func (k *IngressService) get(ctx context.Context, id router.InstanceID) (*networ
if err != nil {
return nil, err
}
ingress, err := client.Get(ctx, k.ingressName(id), metav1.GetOptions{})
return client.Get(ctx, k.ingressName(id), metav1.GetOptions{})
}

func (k *IngressService) getCName(ctx context.Context, id router.InstanceID, cname string) (*networkingV1.Ingress, error) {
ns, err := k.getAppNamespace(ctx, id.AppName)
if err != nil {
return nil, err
}
client, err := k.ingressClient(ns)
if err != nil {
return nil, err
}
return ingress, nil
return client.Get(ctx, k.ingressCName(id, cname), metav1.GetOptions{})
}

func (k *IngressService) ingressClient(namespace string) (networkingTypedV1.IngressInterface, error) {
Expand Down
21 changes: 21 additions & 0 deletions kubernetes/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,28 @@ func TestIngressGetAddressTLS(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, []string{"https://test.apps.example.org"}, addrs)
}
func TestIngressGetAddressAndCNames(t *testing.T) {
svc := createFakeService()
svc.DomainSuffix = "apps.example.org"
svc.Labels = map[string]string{"controller": "my-controller", "XPTO": "true"}
svc.Annotations = map[string]string{"ann1": "val1", "ann2": "val2"}
err := svc.Ensure(ctx, idForApp("test"), router.EnsureBackendOpts{
Prefixes: []router.BackendPrefix{
{
Target: router.BackendTarget{
Service: "test-web",
Namespace: "default",
},
},
},
CNames: []string{"cname.test.com"},
})
require.NoError(t, err)

addrs, err := svc.GetAddresses(ctx, idForApp("test"))
require.NoError(t, err)
assert.Equal(t, []string{"test.apps.example.org", "cname.test.com"}, addrs)
}
func TestIngressGetMultipleAddresses(t *testing.T) {
client := fake.NewSimpleClientset()
err := createAppWebService(client, "default", "test")
Expand Down

0 comments on commit b51de88

Please sign in to comment.