@@ -13,7 +13,9 @@ import (
1313 "github.com/linode/linodego"
1414 "golang.org/x/exp/slices"
1515 "sigs.k8s.io/cluster-api/api/v1beta1"
16+ kutil "sigs.k8s.io/cluster-api/util"
1617
18+ "github.com/linode/cluster-api-provider-linode/api/v1alpha2"
1719 "github.com/linode/cluster-api-provider-linode/clients"
1820 "github.com/linode/cluster-api-provider-linode/cloud/scope"
1921 rutil "github.com/linode/cluster-api-provider-linode/util/reconciler"
@@ -35,7 +37,7 @@ type DNSOptions struct {
3537func EnsureDNSEntries (ctx context.Context , cscope * scope.ClusterScope , operation string ) error {
3638 // Get the public IP that was assigned
3739 var dnss DNSEntries
38- dnsEntries , err := dnss .getDNSEntriesToEnsure (cscope )
40+ dnsEntries , err := dnss .getDNSEntriesToEnsure (ctx , cscope )
3941 if err != nil {
4042 return err
4143 }
@@ -172,7 +174,6 @@ func EnsureLinodeDNSEntries(ctx context.Context, cscope *scope.ClusterScope, ope
172174 if err != nil {
173175 return err
174176 }
175-
176177 domainRecords , err := cscope .LinodeDomainsClient .ListDomainRecords (ctx , domainID , linodego .NewListOptions (0 , string (filter )))
177178 if err != nil {
178179 return err
@@ -295,8 +296,38 @@ func removeElement(stringList []string, elemToRemove string) []string {
295296 return stringList
296297}
297298
299+ func processLinodeMachine (ctx context.Context , cscope * scope.ClusterScope , eachMachine v1alpha2.LinodeMachine , dnsTTLSec int ) ([]DNSOptions , error ) {
300+ // Look up the corresponding CAPI machine, see if its deleted.
301+ capiMachine , err := kutil .GetOwnerMachine (ctx , cscope .Client , eachMachine .ObjectMeta )
302+ if err != nil {
303+ return nil , fmt .Errorf ("failed to get CAPI machine for LinodeMachine %s: %w" , eachMachine .Name , err )
304+ }
305+
306+ if capiMachine == nil || capiMachine .DeletionTimestamp != nil {
307+ // If the CAPI machine is deleted, we don't need to create DNS entries for it.
308+ return nil , nil
309+ }
310+
311+ options := []DNSOptions {}
312+ for _ , IPs := range eachMachine .Status .Addresses {
313+ recordType := linodego .RecordTypeA
314+ if IPs .Type != v1beta1 .MachineExternalIP {
315+ continue
316+ }
317+ addr , err := netip .ParseAddr (IPs .Address )
318+ if err != nil {
319+ return nil , fmt .Errorf ("not a valid IP %w" , err )
320+ }
321+ if ! addr .Is4 () {
322+ recordType = linodego .RecordTypeAAAA
323+ }
324+ options = append (options , DNSOptions {getSubDomain (cscope ), IPs .Address , recordType , dnsTTLSec })
325+ }
326+ return options , nil
327+ }
328+
298329// getDNSEntriesToEnsure return DNS entries to create/delete
299- func (d * DNSEntries ) getDNSEntriesToEnsure (cscope * scope.ClusterScope ) ([]DNSOptions , error ) {
330+ func (d * DNSEntries ) getDNSEntriesToEnsure (ctx context. Context , cscope * scope.ClusterScope ) ([]DNSOptions , error ) {
300331 d .mux .Lock ()
301332 defer d .mux .Unlock ()
302333 dnsTTLSec := rutil .DefaultDNSTTLSec
@@ -305,25 +336,12 @@ func (d *DNSEntries) getDNSEntriesToEnsure(cscope *scope.ClusterScope) ([]DNSOpt
305336 }
306337
307338 subDomain := getSubDomain (cscope )
308-
309339 for _ , eachMachine := range cscope .LinodeMachines .Items {
310- for _ , IPs := range eachMachine .Status .Addresses {
311- recordType := linodego .RecordTypeA
312- if IPs .Type != v1beta1 .MachineExternalIP {
313- continue
314- }
315- addr , err := netip .ParseAddr (IPs .Address )
316- if err != nil {
317- return nil , fmt .Errorf ("not a valid IP %w" , err )
318- }
319- if ! addr .Is4 () {
320- recordType = linodego .RecordTypeAAAA
321- }
322- d .options = append (d .options , DNSOptions {subDomain , IPs .Address , recordType , dnsTTLSec })
323- }
324- if len (d .options ) == 0 {
325- continue
340+ options , err := processLinodeMachine (ctx , cscope , eachMachine , dnsTTLSec )
341+ if err != nil {
342+ return nil , fmt .Errorf ("failed to process LinodeMachine %s: %w" , eachMachine .Name , err )
326343 }
344+ d .options = append (d .options , options ... )
327345 }
328346 d .options = append (d .options , DNSOptions {subDomain , cscope .LinodeCluster .Name , linodego .RecordTypeTXT , dnsTTLSec })
329347
0 commit comments