@@ -3,6 +3,7 @@ package services
33import (
44 "context"
55 "fmt"
6+ "strings"
67 "testing"
78
89 "github.com/akamai/AkamaiOPEN-edgegrid-golang/v12/pkg/dns"
@@ -1121,6 +1122,136 @@ func TestAddIPToDNS(t *testing.T) {
11211122 }
11221123}
11231124
1125+ func TestEnsureLinodeDNSDeletesRecordForDeletingCAPIMachine (t * testing.T ) {
1126+ t .Parallel ()
1127+
1128+ clusterScope := & scope.ClusterScope {
1129+ Cluster : & clusterv1.Cluster {
1130+ ObjectMeta : metav1.ObjectMeta {
1131+ Name : "test-cluster" ,
1132+ UID : "test-uid" ,
1133+ },
1134+ },
1135+ LinodeCluster : & infrav1alpha2.LinodeCluster {
1136+ ObjectMeta : metav1.ObjectMeta {
1137+ Name : "test-cluster" ,
1138+ UID : "test-uid" ,
1139+ },
1140+ Spec : infrav1alpha2.LinodeClusterSpec {
1141+ Network : infrav1alpha2.NetworkSpec {
1142+ LoadBalancerType : "dns" ,
1143+ DNSRootDomain : "lkedevs.net" ,
1144+ DNSUniqueIdentifier : "test-hash" ,
1145+ },
1146+ },
1147+ },
1148+ LinodeMachines : infrav1alpha2.LinodeMachineList {
1149+ Items : []infrav1alpha2.LinodeMachine {
1150+ {
1151+ ObjectMeta : metav1.ObjectMeta {
1152+ Name : "deleting-machine" ,
1153+ OwnerReferences : []metav1.OwnerReference {{
1154+ APIVersion : "cluster.x-k8s.io/v1beta1" ,
1155+ Kind : "Machine" ,
1156+ Name : "deleting-machine" ,
1157+ UID : "deleting-machine-uid" ,
1158+ }},
1159+ },
1160+ Status : infrav1alpha2.LinodeMachineStatus {
1161+ Addresses : []clusterv1.MachineAddress {{
1162+ Type : clusterv1 .MachineExternalIP ,
1163+ Address : "10.10.10.10" ,
1164+ }},
1165+ },
1166+ },
1167+ {
1168+ ObjectMeta : metav1.ObjectMeta {
1169+ Name : "active-machine" ,
1170+ OwnerReferences : []metav1.OwnerReference {{
1171+ APIVersion : "cluster.x-k8s.io/v1beta1" ,
1172+ Kind : "Machine" ,
1173+ Name : "active-machine" ,
1174+ UID : "active-machine-uid" ,
1175+ }},
1176+ },
1177+ Status : infrav1alpha2.LinodeMachineStatus {
1178+ Addresses : []clusterv1.MachineAddress {{
1179+ Type : clusterv1 .MachineExternalIP ,
1180+ Address : "10.20.20.20" ,
1181+ }},
1182+ },
1183+ },
1184+ },
1185+ },
1186+ }
1187+
1188+ ctrl := gomock .NewController (t )
1189+ defer ctrl .Finish ()
1190+
1191+ mockDNSClient := mock .NewMockLinodeClient (ctrl )
1192+ clusterScope .LinodeDomainsClient = mockDNSClient
1193+ mockDNSClient .EXPECT ().ListDomains (gomock .Any (), gomock .Any ()).Return ([]linodego.Domain {{
1194+ ID : 1 ,
1195+ Domain : "lkedevs.net" ,
1196+ }}, nil )
1197+ mockDNSClient .EXPECT ().ListDomainRecords (gomock .Any (), 1 , gomock .Any ()).DoAndReturn (
1198+ func (_ context.Context , _ int , opts * linodego.ListOptions ) ([]linodego.DomainRecord , error ) {
1199+ if strings .Contains (opts .Filter , `"name":"test-cluster-test-hash"` ) && ! strings .Contains (opts .Filter , `"target"` ) {
1200+ return []linodego.DomainRecord {
1201+ {
1202+ ID : 100 ,
1203+ Type : linodego .RecordTypeA ,
1204+ Name : "test-cluster-test-hash" ,
1205+ Target : "10.10.10.10" ,
1206+ },
1207+ {
1208+ ID : 101 ,
1209+ Type : linodego .RecordTypeA ,
1210+ Name : "test-cluster-test-hash" ,
1211+ Target : "10.20.20.20" ,
1212+ },
1213+ {
1214+ ID : 102 ,
1215+ Type : linodego .RecordTypeTXT ,
1216+ Name : "test-cluster-test-hash" ,
1217+ Target : "test-cluster" ,
1218+ },
1219+ }, nil
1220+ }
1221+ return []linodego.DomainRecord {{ID : 101 }}, nil
1222+ }).AnyTimes ()
1223+ mockDNSClient .EXPECT ().DeleteDomainRecord (gomock .Any (), 1 , 100 ).Return (nil )
1224+
1225+ mockK8sClient := mock .NewMockK8sClient (ctrl )
1226+ clusterScope .Client = mockK8sClient
1227+ mockK8sClient .EXPECT ().Scheme ().Return (nil ).AnyTimes ()
1228+ mockK8sClient .EXPECT ().Get (gomock .Any (), gomock .Any (), gomock .Any (), gomock .Any ()).DoAndReturn (
1229+ func (_ context.Context , key client.ObjectKey , obj client.Object , _ ... client.GetOption ) error {
1230+ machine , ok := obj .(* clusterv1.Machine )
1231+ if ! ok {
1232+ return nil
1233+ }
1234+
1235+ machine .Name = key .Name
1236+ machine .Namespace = key .Namespace
1237+ switch key .Name {
1238+ case "deleting-machine" :
1239+ deletionTime := metav1 .Now ()
1240+ machine .DeletionTimestamp = & deletionTime
1241+ machine .UID = "deleting-machine-uid"
1242+ case "active-machine" :
1243+ machine .UID = "active-machine-uid"
1244+ machine .Status .Conditions = []metav1.Condition {{
1245+ Type : clusterv1 .ReadyCondition ,
1246+ Status : metav1 .ConditionTrue ,
1247+ }}
1248+ }
1249+ return nil
1250+ }).AnyTimes ()
1251+
1252+ require .NoError (t , EnsureDNSEntries (t .Context (), clusterScope , "create" ))
1253+ }
1254+
11241255func TestDeleteIPFromDNS (t * testing.T ) {
11251256 t .Parallel ()
11261257 tests := []struct {
0 commit comments