Skip to content

Commit eeca44c

Browse files
committed
Fix tests
1 parent ae8119f commit eeca44c

File tree

2 files changed

+168
-9
lines changed

2 files changed

+168
-9
lines changed

cloud/services/domains.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ func EnsureLinodeDNSEntries(ctx context.Context, cscope *scope.ClusterScope, ope
174174
if err != nil {
175175
return err
176176
}
177-
178177
domainRecords, err := cscope.LinodeDomainsClient.ListDomainRecords(ctx, domainID, linodego.NewListOptions(0, string(filter)))
179178
if err != nil {
180179
return err
@@ -297,33 +296,34 @@ func removeElement(stringList []string, elemToRemove string) []string {
297296
return stringList
298297
}
299298

300-
func (d *DNSEntries) processLinodeMachine(ctx context.Context, cscope *scope.ClusterScope, eachMachine v1alpha2.LinodeMachine, dnsTTLSec int) error {
299+
func processLinodeMachine(ctx context.Context, cscope *scope.ClusterScope, eachMachine v1alpha2.LinodeMachine, dnsTTLSec int) ([]DNSOptions, error) {
301300
// Look up the corresponding CAPI machine, see if its deleted.
302301
capiMachine, err := kutil.GetOwnerMachine(ctx, cscope.Client, eachMachine.ObjectMeta)
303302
if err != nil {
304-
return fmt.Errorf("failed to get CAPI machine for LinodeMachine %s: %w", eachMachine.Name, err)
303+
return nil, fmt.Errorf("failed to get CAPI machine for LinodeMachine %s: %w", eachMachine.Name, err)
305304
}
306305

307306
if capiMachine == nil || capiMachine.DeletionTimestamp != nil {
308307
// If the CAPI machine is deleted, we don't need to create DNS entries for it.
309-
return nil
308+
return nil, nil
310309
}
311310

311+
options := []DNSOptions{}
312312
for _, IPs := range eachMachine.Status.Addresses {
313313
recordType := linodego.RecordTypeA
314314
if IPs.Type != v1beta1.MachineExternalIP {
315315
continue
316316
}
317317
addr, err := netip.ParseAddr(IPs.Address)
318318
if err != nil {
319-
return fmt.Errorf("not a valid IP %w", err)
319+
return nil, fmt.Errorf("not a valid IP %w", err)
320320
}
321321
if !addr.Is4() {
322322
recordType = linodego.RecordTypeAAAA
323323
}
324-
d.options = append(d.options, DNSOptions{getSubDomain(cscope), IPs.Address, recordType, dnsTTLSec})
324+
options = append(options, DNSOptions{getSubDomain(cscope), IPs.Address, recordType, dnsTTLSec})
325325
}
326-
return nil
326+
return options, nil
327327
}
328328

329329
// getDNSEntriesToEnsure return DNS entries to create/delete
@@ -336,12 +336,12 @@ func (d *DNSEntries) getDNSEntriesToEnsure(ctx context.Context, cscope *scope.Cl
336336
}
337337

338338
subDomain := getSubDomain(cscope)
339-
340339
for _, eachMachine := range cscope.LinodeMachines.Items {
341-
err := d.processLinodeMachine(ctx, cscope, eachMachine, dnsTTLSec)
340+
options, err := processLinodeMachine(ctx, cscope, eachMachine, dnsTTLSec)
342341
if err != nil {
343342
return nil, fmt.Errorf("failed to process LinodeMachine %s: %w", eachMachine.Name, err)
344343
}
344+
d.options = append(d.options, options...)
345345
}
346346
d.options = append(d.options, DNSOptions{subDomain, cscope.LinodeCluster.Name, linodego.RecordTypeTXT, dnsTTLSec})
347347

cloud/services/domains_test.go

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ func TestAddIPToEdgeDNS(t *testing.T) {
5858
ObjectMeta: metav1.ObjectMeta{
5959
Name: "test-machine",
6060
UID: "test-uid",
61+
OwnerReferences: []metav1.OwnerReference{
62+
{
63+
APIVersion: "cluster.x-k8s.io/v1beta1",
64+
Kind: "Machine",
65+
Name: "test-machine",
66+
UID: "test-uid",
67+
},
68+
},
6169
},
6270
Spec: infrav1alpha2.LinodeMachineSpec{
6371
ProviderID: ptr.To("linode://123"),
@@ -117,6 +125,14 @@ func TestAddIPToEdgeDNS(t *testing.T) {
117125
ObjectMeta: metav1.ObjectMeta{
118126
Name: "test-machine",
119127
UID: "test-uid",
128+
OwnerReferences: []metav1.OwnerReference{
129+
{
130+
APIVersion: "cluster.x-k8s.io/v1beta1",
131+
Kind: "Machine",
132+
Name: "test-machine",
133+
UID: "test-uid",
134+
},
135+
},
120136
},
121137
Spec: infrav1alpha2.LinodeMachineSpec{
122138
ProviderID: ptr.To("linode://123"),
@@ -214,6 +230,14 @@ func TestRemoveIPFromEdgeDNS(t *testing.T) {
214230
ObjectMeta: metav1.ObjectMeta{
215231
Name: "test-machine",
216232
UID: "test-uid",
233+
OwnerReferences: []metav1.OwnerReference{
234+
{
235+
APIVersion: "cluster.x-k8s.io/v1beta1",
236+
Kind: "Machine",
237+
Name: "test-machine",
238+
UID: "test-uid",
239+
},
240+
},
217241
},
218242
Spec: infrav1alpha2.LinodeMachineSpec{
219243
ProviderID: ptr.To("linode://123"),
@@ -281,6 +305,14 @@ func TestRemoveIPFromEdgeDNS(t *testing.T) {
281305
ObjectMeta: metav1.ObjectMeta{
282306
Name: "test-machine",
283307
UID: "test-uid",
308+
OwnerReferences: []metav1.OwnerReference{
309+
{
310+
APIVersion: "cluster.x-k8s.io/v1beta1",
311+
Kind: "Machine",
312+
Name: "test-machine",
313+
UID: "test-uid",
314+
},
315+
},
284316
},
285317
Spec: infrav1alpha2.LinodeMachineSpec{
286318
ProviderID: ptr.To("linode://123"),
@@ -377,6 +409,14 @@ func TestAddIPToDNS(t *testing.T) {
377409
ObjectMeta: metav1.ObjectMeta{
378410
Name: "test-deleted-machine",
379411
UID: "test-uid-1",
412+
OwnerReferences: []metav1.OwnerReference{
413+
{
414+
APIVersion: "cluster.x-k8s.io/v1beta1",
415+
Kind: "Machine",
416+
Name: "test-deleted-machine",
417+
UID: "test-uid-1",
418+
},
419+
},
380420
},
381421
Spec: infrav1alpha2.LinodeMachineSpec{
382422
ProviderID: ptr.To("linode://123"),
@@ -396,6 +436,14 @@ func TestAddIPToDNS(t *testing.T) {
396436
ObjectMeta: metav1.ObjectMeta{
397437
Name: "test-active-machine",
398438
UID: "test-uid-2",
439+
OwnerReferences: []metav1.OwnerReference{
440+
{
441+
APIVersion: "cluster.x-k8s.io/v1beta1",
442+
Kind: "Machine",
443+
Name: "test-active-machine",
444+
UID: "test-uid-2",
445+
},
446+
},
399447
},
400448
Spec: infrav1alpha2.LinodeMachineSpec{
401449
ProviderID: ptr.To("linode://456"),
@@ -439,6 +487,18 @@ func TestAddIPToDNS(t *testing.T) {
439487
Target: "10.20.20.20",
440488
TTLSec: 30,
441489
}, nil).AnyTimes()
490+
mockClient.EXPECT().CreateDomainRecord(gomock.Any(), gomock.Any(), gomock.Eq(linodego.DomainRecordCreateOptions{
491+
Type: "TXT",
492+
Name: "test-cluster-test-hash",
493+
Target: "test-cluster",
494+
TTLSec: 30,
495+
})).Return(&linodego.DomainRecord{
496+
ID: 1234,
497+
Type: "TXT",
498+
Name: "test-cluster",
499+
Target: "test-cluster",
500+
TTLSec: 30,
501+
}, nil).AnyTimes()
442502

443503
// Make sure there's no expectation for the deleted machine's IP
444504
// We don't need an explicit negative expectation since the mock
@@ -462,10 +522,12 @@ func TestAddIPToDNS(t *testing.T) {
462522
// Set DeletionTimestamp to indicate the machine is being deleted
463523
deletionTime := metav1.Now()
464524
machine.DeletionTimestamp = &deletionTime
525+
machine.UID = "test-uid-1"
465526
case "test-active-machine":
466527
// Set up as an active machine
467528
machine.Name = "test-active-machine"
468529
machine.Namespace = "default"
530+
machine.UID = "test-uid-2"
469531
machine.DeletionTimestamp = nil
470532
}
471533
}
@@ -501,6 +563,14 @@ func TestAddIPToDNS(t *testing.T) {
501563
ObjectMeta: metav1.ObjectMeta{
502564
Name: "test-machine",
503565
UID: "test-uid",
566+
OwnerReferences: []metav1.OwnerReference{
567+
{
568+
APIVersion: "cluster.x-k8s.io/v1beta1",
569+
Kind: "Machine",
570+
Name: "test-machine",
571+
UID: "test-uid",
572+
},
573+
},
504574
},
505575
Spec: infrav1alpha2.LinodeMachineSpec{
506576
ProviderID: ptr.To("linode://123"),
@@ -571,6 +641,14 @@ func TestAddIPToDNS(t *testing.T) {
571641
ObjectMeta: metav1.ObjectMeta{
572642
Name: "test-machine",
573643
UID: "test-uid",
644+
OwnerReferences: []metav1.OwnerReference{
645+
{
646+
APIVersion: "cluster.x-k8s.io/v1beta1",
647+
Kind: "Machine",
648+
Name: "test-machine",
649+
UID: "test-uid",
650+
},
651+
},
574652
},
575653
Spec: infrav1alpha2.LinodeMachineSpec{
576654
ProviderID: ptr.To("linode://123"),
@@ -640,6 +718,14 @@ func TestAddIPToDNS(t *testing.T) {
640718
ObjectMeta: metav1.ObjectMeta{
641719
Name: "test-machine",
642720
UID: "test-uid",
721+
OwnerReferences: []metav1.OwnerReference{
722+
{
723+
APIVersion: "cluster.x-k8s.io/v1beta1",
724+
Kind: "Machine",
725+
Name: "test-machine",
726+
UID: "test-uid",
727+
},
728+
},
643729
},
644730
Spec: infrav1alpha2.LinodeMachineSpec{
645731
ProviderID: ptr.To("linode://123"),
@@ -704,6 +790,14 @@ func TestAddIPToDNS(t *testing.T) {
704790
ObjectMeta: metav1.ObjectMeta{
705791
Name: "test-machine",
706792
UID: "test-uid",
793+
OwnerReferences: []metav1.OwnerReference{
794+
{
795+
APIVersion: "cluster.x-k8s.io/v1beta1",
796+
Kind: "Machine",
797+
Name: "test-machine",
798+
UID: "test-uid",
799+
},
800+
},
707801
},
708802
Spec: infrav1alpha2.LinodeMachineSpec{
709803
ProviderID: ptr.To("linode://123"),
@@ -775,6 +869,14 @@ func TestAddIPToDNS(t *testing.T) {
775869
ObjectMeta: metav1.ObjectMeta{
776870
Name: "test-machine",
777871
UID: "test-uid",
872+
OwnerReferences: []metav1.OwnerReference{
873+
{
874+
APIVersion: "cluster.x-k8s.io/v1beta1",
875+
Kind: "Machine",
876+
Name: "test-machine",
877+
UID: "test-uid",
878+
},
879+
},
778880
},
779881
Spec: infrav1alpha2.LinodeMachineSpec{
780882
ProviderID: ptr.To("linode://123"),
@@ -838,6 +940,14 @@ func TestAddIPToDNS(t *testing.T) {
838940
ObjectMeta: metav1.ObjectMeta{
839941
Name: "test-machine",
840942
UID: "test-uid",
943+
OwnerReferences: []metav1.OwnerReference{
944+
{
945+
APIVersion: "cluster.x-k8s.io/v1beta1",
946+
Kind: "Machine",
947+
Name: "test-machine",
948+
UID: "test-uid",
949+
},
950+
},
841951
},
842952
Spec: infrav1alpha2.LinodeMachineSpec{
843953
ProviderID: ptr.To("linode://123"),
@@ -900,6 +1010,14 @@ func TestAddIPToDNS(t *testing.T) {
9001010
ObjectMeta: metav1.ObjectMeta{
9011011
Name: "test-machine",
9021012
UID: "test-uid",
1013+
OwnerReferences: []metav1.OwnerReference{
1014+
{
1015+
APIVersion: "cluster.x-k8s.io/v1beta1",
1016+
Kind: "Machine",
1017+
Name: "test-machine",
1018+
UID: "test-uid",
1019+
},
1020+
},
9031021
},
9041022
Spec: infrav1alpha2.LinodeMachineSpec{
9051023
ProviderID: ptr.To("linode://123"),
@@ -1001,6 +1119,14 @@ func TestDeleteIPFromDNS(t *testing.T) {
10011119
ObjectMeta: metav1.ObjectMeta{
10021120
Name: "test-machine",
10031121
UID: "test-uid",
1122+
OwnerReferences: []metav1.OwnerReference{
1123+
{
1124+
APIVersion: "cluster.x-k8s.io/v1beta1",
1125+
Kind: "Machine",
1126+
Name: "test-machine",
1127+
UID: "test-uid",
1128+
},
1129+
},
10041130
},
10051131
Spec: infrav1alpha2.LinodeMachineSpec{
10061132
ProviderID: ptr.To("linode://123"),
@@ -1072,6 +1198,14 @@ func TestDeleteIPFromDNS(t *testing.T) {
10721198
ObjectMeta: metav1.ObjectMeta{
10731199
Name: "test-machine",
10741200
UID: "test-uid",
1201+
OwnerReferences: []metav1.OwnerReference{
1202+
{
1203+
APIVersion: "cluster.x-k8s.io/v1beta1",
1204+
Kind: "Machine",
1205+
Name: "test-machine",
1206+
UID: "test-uid",
1207+
},
1208+
},
10751209
},
10761210
Spec: infrav1alpha2.LinodeMachineSpec{
10771211
ProviderID: ptr.To("linode://123"),
@@ -1188,6 +1322,14 @@ func TestDeleteIPFromDNS(t *testing.T) {
11881322
ObjectMeta: metav1.ObjectMeta{
11891323
Name: "test-machine",
11901324
UID: "test-uid",
1325+
OwnerReferences: []metav1.OwnerReference{
1326+
{
1327+
APIVersion: "cluster.x-k8s.io/v1beta1",
1328+
Kind: "Machine",
1329+
Name: "test-machine",
1330+
UID: "test-uid",
1331+
},
1332+
},
11911333
},
11921334
Spec: infrav1alpha2.LinodeMachineSpec{
11931335
ProviderID: ptr.To("linode://123"),
@@ -1245,6 +1387,14 @@ func TestDeleteIPFromDNS(t *testing.T) {
12451387
ObjectMeta: metav1.ObjectMeta{
12461388
Name: "test-machine",
12471389
UID: "test-uid",
1390+
OwnerReferences: []metav1.OwnerReference{
1391+
{
1392+
APIVersion: "cluster.x-k8s.io/v1beta1",
1393+
Kind: "Machine",
1394+
Name: "test-machine",
1395+
UID: "test-uid",
1396+
},
1397+
},
12481398
},
12491399
Spec: infrav1alpha2.LinodeMachineSpec{
12501400
ProviderID: ptr.To("linode://123"),
@@ -1307,6 +1457,14 @@ func TestDeleteIPFromDNS(t *testing.T) {
13071457
ObjectMeta: metav1.ObjectMeta{
13081458
Name: "test-machine",
13091459
UID: "test-uid",
1460+
OwnerReferences: []metav1.OwnerReference{
1461+
{
1462+
APIVersion: "cluster.x-k8s.io/v1beta1",
1463+
Kind: "Machine",
1464+
Name: "test-machine",
1465+
UID: "test-uid",
1466+
},
1467+
},
13101468
},
13111469
Spec: infrav1alpha2.LinodeMachineSpec{
13121470
ProviderID: ptr.To("linode://123"),
@@ -1384,6 +1542,7 @@ func mockCAPIMachine(mockK8sClient *mock.MockK8sClient) {
13841542
machine.Name = "test-machine"
13851543
machine.Namespace = "default"
13861544
machine.DeletionTimestamp = nil
1545+
machine.UID = "test-uid"
13871546
}
13881547
return nil
13891548
}).AnyTimes()

0 commit comments

Comments
 (0)