Skip to content

Commit 6acdb65

Browse files
[fix] check machine readiness before adding to DNS (#979)
* fix: check machine readiness before adding to DNS * Update cloud/services/domains.go Co-authored-by: Ashley Dumaine <[email protected]> --------- Co-authored-by: Ashley Dumaine <[email protected]>
1 parent 27dc205 commit 6acdb65

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

cloud/services/domains.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
"sync"
1212

1313
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/dns"
14+
"github.com/go-logr/logr"
1415
"github.com/linode/linodego"
16+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1517
"sigs.k8s.io/cluster-api/api/core/v1beta2"
1618
kutil "sigs.k8s.io/cluster-api/util"
1719

@@ -300,7 +302,16 @@ func removeElement(stringList []string, elemToRemove string) []string {
300302
return stringList
301303
}
302304

303-
func processLinodeMachine(ctx context.Context, cscope *scope.ClusterScope, machine v1alpha2.LinodeMachine, dnsTTLSec int, subdomain string) ([]DNSOptions, error) {
305+
func isCapiMachineReady(capiMachine *v1beta2.Machine) bool {
306+
for _, condition := range capiMachine.Status.Conditions {
307+
if condition.Type == v1beta2.ReadyCondition {
308+
return condition.Status == metav1.ConditionTrue
309+
}
310+
}
311+
return false
312+
}
313+
314+
func processLinodeMachine(ctx context.Context, cscope *scope.ClusterScope, machine v1alpha2.LinodeMachine, dnsTTLSec int, subdomain string, firstMachine bool) ([]DNSOptions, error) {
304315
// Look up the corresponding CAPI machine, see if it is marked for deletion
305316
capiMachine, err := kutil.GetOwnerMachine(ctx, cscope.Client, machine.ObjectMeta)
306317
if err != nil {
@@ -312,6 +323,14 @@ func processLinodeMachine(ctx context.Context, cscope *scope.ClusterScope, machi
312323
return nil, nil
313324
}
314325

326+
if !firstMachine && !isCapiMachineReady(capiMachine) {
327+
// always process the first linodeMachine, and add its IP to the DNS entries.
328+
// For other linodeMachine, only process them if the CAPI machine is ready
329+
logger := logr.FromContextOrDiscard(ctx)
330+
logger.Info("skipping DNS entry creation for LinodeMachine as the CAPI machine is not ready", "LinodeMachine", machine.Name)
331+
return nil, nil
332+
}
333+
315334
options := []DNSOptions{}
316335
for _, IPs := range machine.Status.Addresses {
317336
recordType := linodego.RecordTypeA
@@ -340,8 +359,10 @@ func (d *DNSEntries) getDNSEntriesToEnsure(ctx context.Context, cscope *scope.Cl
340359
}
341360

342361
subDomain := getSubDomain(cscope)
362+
firstMachine := true
343363
for _, eachMachine := range cscope.LinodeMachines.Items {
344-
options, err := processLinodeMachine(ctx, cscope, eachMachine, dnsTTLSec, subDomain)
364+
options, err := processLinodeMachine(ctx, cscope, eachMachine, dnsTTLSec, subDomain, firstMachine)
365+
firstMachine = false
345366
if err != nil {
346367
return nil, fmt.Errorf("failed to process LinodeMachine %s: %w", eachMachine.Name, err)
347368
}

cloud/services/domains_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,33 @@ func TestAddIPToDNS(t *testing.T) {
458458
},
459459
},
460460
},
461+
{
462+
// This machine's CAPI owner is NOT ready, and should NOT have DNS entries
463+
ObjectMeta: metav1.ObjectMeta{
464+
Name: "test-not-ready-machine",
465+
UID: "test-uid-2",
466+
OwnerReferences: []metav1.OwnerReference{
467+
{
468+
APIVersion: "cluster.x-k8s.io/v1beta1",
469+
Kind: "Machine",
470+
Name: "test-not-ready-machine",
471+
UID: "test-uid-3",
472+
},
473+
},
474+
},
475+
Spec: infrav1alpha2.LinodeMachineSpec{
476+
ProviderID: ptr.To("linode://4567"),
477+
InstanceID: ptr.To(456),
478+
},
479+
Status: infrav1alpha2.LinodeMachineStatus{
480+
Addresses: []clusterv1.MachineAddress{
481+
{
482+
Type: "ExternalIP",
483+
Address: "10.20.20.22",
484+
},
485+
},
486+
},
487+
},
461488
},
462489
},
463490
},
@@ -529,6 +556,18 @@ func TestAddIPToDNS(t *testing.T) {
529556
machine.Namespace = "default"
530557
machine.UID = "test-uid-2"
531558
machine.DeletionTimestamp = nil
559+
machine.Status.Conditions = []metav1.Condition{
560+
{
561+
Type: clusterv1.ReadyCondition,
562+
Status: metav1.ConditionTrue,
563+
},
564+
}
565+
case "test-not-ready-machine":
566+
// Set up as a not-ready machine (skipping conditions)
567+
machine.Name = "test-not-ready-machine"
568+
machine.Namespace = "default"
569+
machine.UID = "test-uid-3"
570+
machine.DeletionTimestamp = nil
532571
}
533572
}
534573
return nil

0 commit comments

Comments
 (0)