Skip to content

Commit

Permalink
feat(controller): append custom hostnames in ExternalDNS annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
nettoclaudio committed Sep 10, 2021
1 parent 9702ed2 commit 7450c95
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 10 deletions.
37 changes: 27 additions & 10 deletions controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -966,18 +966,35 @@ func newConfigMap(instance *v1alpha1.RpaasInstance, renderedTemplate string) *co
}

func mergeServiceWithDNS(instance *v1alpha1.RpaasInstance) *nginxv1alpha1.NginxService {
service := instance.Spec.Service
if instance.Spec.DNS != nil && service != nil {
if service.Annotations == nil {
service.Annotations = make(map[string]string)
}
service.Annotations[externalDNSHostnameLabel] = fmt.Sprintf("%s.%s", instance.Name, instance.Spec.DNS.Zone)
if instance.Spec.DNS.TTL != nil {
service.Annotations[externalDNSTTLLabel] = strconv.Itoa(int(*instance.Spec.DNS.TTL))
}
if instance == nil {
return nil
}

s := instance.Spec.Service
if s == nil {
return nil
}

if instance.Spec.DNS == nil {
return s
}

if s.Annotations == nil {
s.Annotations = make(map[string]string)
}

hostname := fmt.Sprintf("%s.%s", instance.Name, instance.Spec.DNS.Zone)
if custom, found := s.Annotations[externalDNSHostnameLabel]; found {
hostname = strings.Join([]string{hostname, custom}, ",")
}

s.Annotations[externalDNSHostnameLabel] = hostname

if instance.Spec.DNS.TTL != nil {
s.Annotations[externalDNSTTLLabel] = strconv.Itoa(int(*instance.Spec.DNS.TTL))
}

return service
return s
}

func newNginx(instanceMergedWithFlavors *v1alpha1.RpaasInstance, plan *v1alpha1.RpaasPlan, configMap *corev1.ConfigMap) *nginxv1alpha1.Nginx {
Expand Down
81 changes: 81 additions & 0 deletions controllers/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2222,6 +2222,87 @@ func Test_nameForCronJob(t *testing.T) {
}
}

func Test_mergeServiceWithDNS(t *testing.T) {
tests := []struct {
instance *v1alpha1.RpaasInstance
expected *nginxv1alpha1.NginxService
}{
{},

{
instance: &v1alpha1.RpaasInstance{},
},

{
instance: &v1alpha1.RpaasInstance{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
},
Spec: v1alpha1.RpaasInstanceSpec{
DNS: &v1alpha1.DNSConfig{
Zone: "apps.example.com",
},
},
},
},

{
instance: &v1alpha1.RpaasInstance{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
},
Spec: v1alpha1.RpaasInstanceSpec{
Service: &nginxv1alpha1.NginxService{},
DNS: &v1alpha1.DNSConfig{
Zone: "apps.example.com",
TTL: func(n int32) *int32 { return &n }(int32(600)),
},
},
},

expected: &nginxv1alpha1.NginxService{
Annotations: map[string]string{
"external-dns.alpha.kubernetes.io/hostname": "my-instance.apps.example.com",
"external-dns.alpha.kubernetes.io/ttl": "600",
},
},
},

{
instance: &v1alpha1.RpaasInstance{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
},
Spec: v1alpha1.RpaasInstanceSpec{
Service: &nginxv1alpha1.NginxService{
Annotations: map[string]string{
"external-dns.alpha.kubernetes.io/hostname": "www.example.com,www.example.org",
},
},

DNS: &v1alpha1.DNSConfig{
Zone: "apps.example.com",
TTL: func(n int32) *int32 { return &n }(int32(600)),
},
},
},

expected: &nginxv1alpha1.NginxService{
Annotations: map[string]string{
"external-dns.alpha.kubernetes.io/hostname": "my-instance.apps.example.com,www.example.com,www.example.org",
"external-dns.alpha.kubernetes.io/ttl": "600",
},
},
},
}

for _, tt := range tests {
t.Run("", func(t *testing.T) {
assert.Equal(t, tt.expected, mergeServiceWithDNS(tt.instance))
})
}
}

type fakeImageMetadata struct{}

func (i *fakeImageMetadata) Modules(img string) ([]string, error) {
Expand Down

0 comments on commit 7450c95

Please sign in to comment.