@@ -13,7 +13,9 @@ import (
13
13
"github.com/linode/linodego"
14
14
"golang.org/x/exp/slices"
15
15
"sigs.k8s.io/cluster-api/api/v1beta1"
16
+ kutil "sigs.k8s.io/cluster-api/util"
16
17
18
+ "github.com/linode/cluster-api-provider-linode/api/v1alpha2"
17
19
"github.com/linode/cluster-api-provider-linode/clients"
18
20
"github.com/linode/cluster-api-provider-linode/cloud/scope"
19
21
rutil "github.com/linode/cluster-api-provider-linode/util/reconciler"
@@ -35,7 +37,7 @@ type DNSOptions struct {
35
37
func EnsureDNSEntries (ctx context.Context , cscope * scope.ClusterScope , operation string ) error {
36
38
// Get the public IP that was assigned
37
39
var dnss DNSEntries
38
- dnsEntries , err := dnss .getDNSEntriesToEnsure (cscope )
40
+ dnsEntries , err := dnss .getDNSEntriesToEnsure (ctx , cscope )
39
41
if err != nil {
40
42
return err
41
43
}
@@ -172,7 +174,6 @@ func EnsureLinodeDNSEntries(ctx context.Context, cscope *scope.ClusterScope, ope
172
174
if err != nil {
173
175
return err
174
176
}
175
-
176
177
domainRecords , err := cscope .LinodeDomainsClient .ListDomainRecords (ctx , domainID , linodego .NewListOptions (0 , string (filter )))
177
178
if err != nil {
178
179
return err
@@ -295,8 +296,38 @@ func removeElement(stringList []string, elemToRemove string) []string {
295
296
return stringList
296
297
}
297
298
299
+ func processLinodeMachine (ctx context.Context , cscope * scope.ClusterScope , machine v1alpha2.LinodeMachine , dnsTTLSec int , subdomain string ) ([]DNSOptions , error ) {
300
+ // Look up the corresponding CAPI machine, see if it is marked for deletion
301
+ capiMachine , err := kutil .GetOwnerMachine (ctx , cscope .Client , machine .ObjectMeta )
302
+ if err != nil {
303
+ return nil , fmt .Errorf ("failed to get CAPI machine for LinodeMachine %s: %w" , machine .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 machine .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 {subdomain , IPs .Address , recordType , dnsTTLSec })
325
+ }
326
+ return options , nil
327
+ }
328
+
298
329
// 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 ) {
300
331
d .mux .Lock ()
301
332
defer d .mux .Unlock ()
302
333
dnsTTLSec := rutil .DefaultDNSTTLSec
@@ -305,25 +336,12 @@ func (d *DNSEntries) getDNSEntriesToEnsure(cscope *scope.ClusterScope) ([]DNSOpt
305
336
}
306
337
307
338
subDomain := getSubDomain (cscope )
308
-
309
339
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 , subDomain )
341
+ if err != nil {
342
+ return nil , fmt .Errorf ("failed to process LinodeMachine %s: %w" , eachMachine .Name , err )
326
343
}
344
+ d .options = append (d .options , options ... )
327
345
}
328
346
d .options = append (d .options , DNSOptions {subDomain , cscope .LinodeCluster .Name , linodego .RecordTypeTXT , dnsTTLSec })
329
347
0 commit comments