@@ -349,6 +349,129 @@ func TestAddIPToDNS(t *testing.T) {
349349 expectedDomainRecord * linodego.DomainRecord
350350 expectedError error
351351 }{
352+ {name : "Skip - If a CAPI machine is deleted, don't add its IP to the Domain but include other machines" ,
353+ clusterScope : & scope.ClusterScope {
354+ Cluster : & clusterv1.Cluster {
355+ ObjectMeta : metav1.ObjectMeta {
356+ Name : "test-cluster" ,
357+ UID : "test-uid" ,
358+ },
359+ },
360+ LinodeCluster : & infrav1alpha2.LinodeCluster {
361+ ObjectMeta : metav1.ObjectMeta {
362+ Name : "test-cluster" ,
363+ UID : "test-uid" ,
364+ },
365+ Spec : infrav1alpha2.LinodeClusterSpec {
366+ Network : infrav1alpha2.NetworkSpec {
367+ LoadBalancerType : "dns" ,
368+ DNSRootDomain : "lkedevs.net" ,
369+ DNSUniqueIdentifier : "test-hash" ,
370+ },
371+ },
372+ },
373+ LinodeMachines : infrav1alpha2.LinodeMachineList {
374+ Items : []infrav1alpha2.LinodeMachine {
375+ {
376+ // This machine's CAPI owner is deleted
377+ ObjectMeta : metav1.ObjectMeta {
378+ Name : "test-deleted-machine" ,
379+ UID : "test-uid-1" ,
380+ },
381+ Spec : infrav1alpha2.LinodeMachineSpec {
382+ ProviderID : ptr .To ("linode://123" ),
383+ InstanceID : ptr .To (123 ),
384+ },
385+ Status : infrav1alpha2.LinodeMachineStatus {
386+ Addresses : []clusterv1.MachineAddress {
387+ {
388+ Type : "ExternalIP" ,
389+ Address : "10.10.10.10" ,
390+ },
391+ },
392+ },
393+ },
394+ {
395+ // This machine's CAPI owner is NOT deleted and should have DNS entries
396+ ObjectMeta : metav1.ObjectMeta {
397+ Name : "test-active-machine" ,
398+ UID : "test-uid-2" ,
399+ },
400+ Spec : infrav1alpha2.LinodeMachineSpec {
401+ ProviderID : ptr .To ("linode://456" ),
402+ InstanceID : ptr .To (456 ),
403+ },
404+ Status : infrav1alpha2.LinodeMachineStatus {
405+ Addresses : []clusterv1.MachineAddress {
406+ {
407+ Type : "ExternalIP" ,
408+ Address : "10.20.20.20" ,
409+ },
410+ },
411+ },
412+ },
413+ },
414+ },
415+ },
416+ expects : func (mockClient * mock.MockLinodeClient ) {
417+ // The code path should still call ListDomains
418+ mockClient .EXPECT ().ListDomains (gomock .Any (), gomock .Any ()).Return ([]linodego.Domain {
419+ {
420+ ID : 1 ,
421+ Domain : "lkedevs.net" ,
422+ },
423+ }, nil ).AnyTimes ()
424+
425+ // Must mock ListDomainRecords
426+ mockClient .EXPECT ().ListDomainRecords (gomock .Any (), gomock .Any (), gomock .Any ()).Return ([]linodego.DomainRecord {}, nil ).AnyTimes ()
427+
428+ // Expect CreateDomainRecord to be called for the active machine's IP (10.20.20.20)
429+ // but NOT for the deleted machine's IP (10.10.10.10)
430+ mockClient .EXPECT ().CreateDomainRecord (gomock .Any (), gomock .Any (), gomock .Eq (linodego.DomainRecordCreateOptions {
431+ Type : "A" ,
432+ Name : "test-cluster-test-hash" ,
433+ Target : "10.20.20.20" ,
434+ TTLSec : 30 ,
435+ })).Return (& linodego.DomainRecord {
436+ ID : 1234 ,
437+ Type : "A" ,
438+ Name : "test-cluster" ,
439+ Target : "10.20.20.20" ,
440+ TTLSec : 30 ,
441+ }, nil ).AnyTimes ()
442+
443+ // Make sure there's no expectation for the deleted machine's IP
444+ // We don't need an explicit negative expectation since the mock
445+ // will fail if any unexpected calls are made
446+ },
447+ expectedError : nil ,
448+ expectK8sClient : func (mockK8sClient * mock.MockK8sClient ) {
449+ mockK8sClient .EXPECT ().Scheme ().Return (nil ).AnyTimes ()
450+
451+ // Mock the Get call for GetOwnerMachine to handle both machines
452+ mockK8sClient .EXPECT ().Get (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).DoAndReturn (
453+ func (ctx context.Context , key client.ObjectKey , obj client.Object , opts ... client.GetOption ) error {
454+ // Set the Machine fields based on the machine name
455+ machine , ok := obj .(* clusterv1.Machine )
456+ if ok {
457+ if key .Name == "test-deleted-machine" {
458+ // Set up as a deleted machine
459+ machine .Name = "test-deleted-machine"
460+ machine .Namespace = "default"
461+ // Set DeletionTimestamp to indicate the machine is being deleted
462+ deletionTime := metav1 .Now ()
463+ machine .DeletionTimestamp = & deletionTime
464+ } else if key .Name == "test-active-machine" {
465+ // Set up as an active machine
466+ machine .Name = "test-active-machine"
467+ machine .Namespace = "default"
468+ machine .DeletionTimestamp = nil
469+ }
470+ }
471+ return nil
472+ }).AnyTimes ()
473+ },
474+ },
352475 {
353476 name : "Success - If the machine is a control plane node, add the IP to the Domain" ,
354477 clusterScope : & scope.ClusterScope {
0 commit comments