Skip to content

Commit 4294b8c

Browse files
dns record use uuid in domain name (#10)
* add uuid to dns name * go mod update fix test * select last 12 characters * use 6 char suffix for dns Co-authored-by: jzhoucliqr <[email protected]>
1 parent 23acd16 commit 4294b8c

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.15
55
require (
66
github.com/go-logr/logr v0.3.0
77
github.com/golang/mock v1.2.0
8+
github.com/google/uuid v1.2.0
89
github.com/onsi/gomega v1.10.1
910
github.com/pkg/errors v0.9.1
1011
github.com/prometheus/common v0.10.0

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,6 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1
357357
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
358358
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
359359
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
360-
github.com/spectrocloud/maas-client-go v0.0.1-beta1 h1:OZGxJl5AR+4mM2w+ylJewYIORBGBCMy2kMh/iMCkZsw=
361-
github.com/spectrocloud/maas-client-go v0.0.1-beta1/go.mod h1:ih7QvZPySD8fiIo56wO+W40w1ojiqNnpLP0Zb6nKvKk=
362360
github.com/spectrocloud/maas-client-go v0.0.1-beta1.0.20210805102600-28f250f3bdc7 h1:4HsJjvyBXs8co1ss78oEh7OQbBG+h8E7cRx1WRaRtE0=
363361
github.com/spectrocloud/maas-client-go v0.0.1-beta1.0.20210805102600-28f250f3bdc7/go.mod h1:ih7QvZPySD8fiIo56wO+W40w1ojiqNnpLP0Zb6nKvKk=
364362
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=

pkg/maas/dns/dns_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ func TestDNS(t *testing.T) {
4646
mockDNSResources.EXPECT().List(context.Background(), gomock.Any()).Return(nil, nil)
4747
mockClientSetInterface.EXPECT().DNSResources().Return(mockDNSResources)
4848
mockDNSResources.EXPECT().Builder().Return(mockDNSResourceBuilder)
49-
mockDNSResourceBuilder.EXPECT().WithFQDN("a.b.com").Return(mockDNSResourceBuilder)
49+
mockDNSResourceBuilder.EXPECT().WithFQDN(gomock.Any()).Return(mockDNSResourceBuilder)
5050
mockDNSResourceBuilder.EXPECT().WithAddressTTL("10").Return(mockDNSResourceBuilder)
5151
mockDNSResourceBuilder.EXPECT().WithIPAddresses(nil).Return(mockDNSResourceBuilder)
5252
mockDNSResourceBuilder.EXPECT().Create(context.Background())
5353
err := s.ReconcileDNS()
5454

5555
g.Expect(err).ToNot(HaveOccurred())
56-
g.Expect(s.scope.GetDNSName()).To(BeEquivalentTo("a.b.com"))
56+
g.Expect(s.scope.GetDNSName()).To(ContainSubstring(cluster.Name))
57+
g.Expect(s.scope.GetDNSName()).To(ContainSubstring(maasCluster.Spec.DNSDomain))
5758
})
5859

5960
t.Run("update dns attachment", func(t *testing.T) {

pkg/maas/scope/cluster.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"github.com/go-logr/logr"
7+
"github.com/google/uuid"
78
"github.com/pkg/errors"
89
infrav1 "github.com/spectrocloud/cluster-api-provider-maas/api/v1alpha3"
910
v1 "k8s.io/api/core/v1"
@@ -21,6 +22,10 @@ import (
2122
"time"
2223
)
2324

25+
const (
26+
DnsSuffixLength = 6
27+
)
28+
2429
// ClusterScopeParams defines the input parameters used to create a new Scope.
2530
type ClusterScopeParams struct {
2631
Client client.Client
@@ -118,7 +123,19 @@ func (s *ClusterScope) SetDNSName(dnsName string) {
118123
// GetDNSName sets the Network systemID in spec.
119124
// This can't do a lookup on Status.Network.DNSDomain name since it's derviced from here
120125
func (s *ClusterScope) GetDNSName() string {
121-
return fmt.Sprintf("%s.%s", s.Cluster.Name, s.MaasCluster.Spec.DNSDomain)
126+
if !s.Cluster.Spec.ControlPlaneEndpoint.IsZero() {
127+
return s.Cluster.Spec.ControlPlaneEndpoint.Host
128+
}
129+
130+
if s.MaasCluster.Status.Network.DNSName != "" {
131+
return s.MaasCluster.Status.Network.DNSName
132+
}
133+
134+
uid := uuid.New().String()
135+
dnsName := fmt.Sprintf("%s-%s.%s", s.Cluster.Name, uid[len(uid)-DnsSuffixLength:], s.MaasCluster.Spec.DNSDomain)
136+
137+
s.SetDNSName(dnsName)
138+
return dnsName
122139
}
123140

124141
// GetActiveMaasMachines all MaaS machines NOT being deleted

pkg/maas/scope/cluster_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package scope
22

33
import (
4+
"testing"
5+
46
"github.com/onsi/gomega"
57
infrav1 "github.com/spectrocloud/cluster-api-provider-maas/api/v1alpha3"
68
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
79
"k8s.io/apimachinery/pkg/runtime"
810
"k8s.io/klog/klogr"
911
"sigs.k8s.io/cluster-api/api/v1alpha3"
1012
"sigs.k8s.io/controller-runtime/pkg/client/fake"
11-
"testing"
1213
)
1314

1415
func TestNewCluster(t *testing.T) {
@@ -43,4 +44,28 @@ func TestNewCluster(t *testing.T) {
4344
g.Expect(scope).ToNot(gomega.BeNil())
4445

4546
})
47+
48+
t.Run("new dns name", func(t *testing.T) {
49+
g := gomega.NewGomegaWithT(t)
50+
scheme := runtime.NewScheme()
51+
client := fake.NewFakeClientWithScheme(scheme)
52+
clusterCopy := cluster.DeepCopy()
53+
clusterCopy.Name = "dns-test"
54+
maasClusterCopy := maasCluster.DeepCopy()
55+
maasClusterCopy.Spec.DNSDomain = "maas.com"
56+
log := klogr.New()
57+
scope, err := NewClusterScope(ClusterScopeParams{
58+
Client: client,
59+
Logger: log,
60+
Cluster: clusterCopy,
61+
MaasCluster: maasClusterCopy,
62+
})
63+
64+
g.Expect(err).ToNot(gomega.HaveOccurred())
65+
g.Expect(scope.GetDNSName()).ToNot(gomega.BeNil())
66+
g.Expect(scope.GetDNSName()).To(gomega.ContainSubstring(clusterCopy.Name))
67+
g.Expect(scope.GetDNSName()).To(gomega.ContainSubstring(maasClusterCopy.Spec.DNSDomain))
68+
dnsLengh := len("dns-test-") + DnsSuffixLength + len(".maas.com")
69+
g.Expect(len(scope.GetDNSName())).To(gomega.Equal(dnsLengh))
70+
})
4671
}

0 commit comments

Comments
 (0)