Skip to content

Commit a343c76

Browse files
authored
[fix] Fix create/update opertions if leftover DNS entries are found (#424)
* if DNS A Record found with exact match during create, no need to update it
1 parent a417bbc commit a343c76

File tree

2 files changed

+3
-109
lines changed

2 files changed

+3
-109
lines changed

cloud/services/domains.go

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func EnsureLinodeDNSEntries(ctx context.Context, mscope *scope.MachineScope, ope
6666
}
6767
continue
6868
}
69-
if err := CreateUpdateDomainRecord(ctx, mscope, domainID, dnsEntry); err != nil {
69+
if err := CreateDomainRecord(ctx, mscope, domainID, dnsEntry); err != nil {
7070
return err
7171
}
7272
}
@@ -189,7 +189,7 @@ func GetDomainID(ctx context.Context, mscope *scope.MachineScope) (int, error) {
189189
return domains[0].ID, nil
190190
}
191191

192-
func CreateUpdateDomainRecord(ctx context.Context, mscope *scope.MachineScope, domainID int, dnsEntry DNSOptions) error {
192+
func CreateDomainRecord(ctx context.Context, mscope *scope.MachineScope, domainID int, dnsEntry DNSOptions) error {
193193
// Check if domain record exists for this IP and name combo
194194
filter, err := json.Marshal(map[string]interface{}{"name": dnsEntry.Hostname, "target": dnsEntry.Target, "type": dnsEntry.DNSRecordType})
195195
if err != nil {
@@ -215,32 +215,6 @@ func CreateUpdateDomainRecord(ctx context.Context, mscope *scope.MachineScope, d
215215
); err != nil {
216216
return err
217217
}
218-
return nil
219-
}
220-
221-
// If record exists, update it
222-
if len(domainRecords) != 0 && dnsEntry.DNSRecordType != linodego.RecordTypeTXT {
223-
isOwner, err := IsDomainRecordOwner(ctx, mscope, dnsEntry.Hostname, domainID)
224-
if err != nil {
225-
return err
226-
}
227-
if !isOwner {
228-
return fmt.Errorf("the domain record is not owned by this entity. wont update")
229-
}
230-
}
231-
232-
if _, err := mscope.LinodeDomainsClient.UpdateDomainRecord(
233-
ctx,
234-
domainID,
235-
domainRecords[0].ID,
236-
linodego.DomainRecordUpdateOptions{
237-
Type: dnsEntry.DNSRecordType,
238-
Name: dnsEntry.Hostname,
239-
Target: dnsEntry.Target,
240-
TTLSec: dnsEntry.DNSTTLSec,
241-
},
242-
); err != nil {
243-
return err
244218
}
245219
return nil
246220
}

cloud/services/domains_test.go

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ func TestAddIPToDNS(t *testing.T) {
545545
expectedError: fmt.Errorf("failed to create domain record of type A"),
546546
},
547547
{
548-
name: "Success - If the machine is a control plane node and record already exists, update it",
548+
name: "Success - If the machine is a control plane node and record already exists, leave it alone",
549549
machineScope: &scope.MachineScope{
550550
Machine: &clusterv1.Machine{
551551
ObjectMeta: metav1.ObjectMeta{
@@ -612,13 +612,6 @@ func TestAddIPToDNS(t *testing.T) {
612612
TTLSec: 30,
613613
},
614614
}, nil).AnyTimes()
615-
mockClient.EXPECT().CreateDomainRecord(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
616-
mockClient.EXPECT().UpdateDomainRecord(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(&linodego.DomainRecord{
617-
ID: 1234,
618-
Type: "A",
619-
Name: "test-cluster",
620-
TTLSec: 30,
621-
}, nil).AnyTimes()
622615
},
623616
expectedError: nil,
624617
},
@@ -686,79 +679,6 @@ func TestAddIPToDNS(t *testing.T) {
686679
},
687680
expectedError: fmt.Errorf("api error"),
688681
},
689-
{
690-
name: "Error - UpdateDomainRecord fails",
691-
machineScope: &scope.MachineScope{
692-
Machine: &clusterv1.Machine{
693-
ObjectMeta: metav1.ObjectMeta{
694-
Name: "test-machine",
695-
UID: "test-uid",
696-
Labels: map[string]string{
697-
clusterv1.MachineControlPlaneLabel: "true",
698-
},
699-
},
700-
},
701-
Cluster: &clusterv1.Cluster{
702-
ObjectMeta: metav1.ObjectMeta{
703-
Name: "test-cluster",
704-
UID: "test-uid",
705-
},
706-
},
707-
LinodeCluster: &infrav1alpha2.LinodeCluster{
708-
ObjectMeta: metav1.ObjectMeta{
709-
Name: "test-cluster",
710-
UID: "test-uid",
711-
},
712-
Spec: infrav1alpha2.LinodeClusterSpec{
713-
Network: infrav1alpha2.NetworkSpec{
714-
LoadBalancerType: "dns",
715-
DNSRootDomain: "lkedevs.net",
716-
DNSUniqueIdentifier: "test-hash",
717-
},
718-
},
719-
},
720-
LinodeMachine: &infrav1alpha2.LinodeMachine{
721-
ObjectMeta: metav1.ObjectMeta{
722-
Name: "test-machine",
723-
UID: "test-uid",
724-
},
725-
Spec: infrav1alpha2.LinodeMachineSpec{
726-
InstanceID: ptr.To(123),
727-
},
728-
Status: infrav1alpha2.LinodeMachineStatus{
729-
Addresses: []clusterv1.MachineAddress{
730-
{
731-
Type: "ExternalIP",
732-
Address: "10.10.10.10",
733-
},
734-
{
735-
Type: "ExternalIP",
736-
Address: "fd00::",
737-
},
738-
},
739-
},
740-
},
741-
},
742-
expects: func(mockClient *mock.MockLinodeClient) {
743-
mockClient.EXPECT().ListDomains(gomock.Any(), gomock.Any()).Return([]linodego.Domain{
744-
{
745-
ID: 1,
746-
Domain: "lkedevs.net",
747-
},
748-
}, nil).AnyTimes()
749-
mockClient.EXPECT().ListDomainRecords(gomock.Any(), gomock.Any(), gomock.Any()).Return([]linodego.DomainRecord{
750-
{
751-
ID: 1234,
752-
Type: "A",
753-
Name: "test-cluster",
754-
TTLSec: 30,
755-
},
756-
}, nil).AnyTimes()
757-
mockClient.EXPECT().CreateDomainRecord(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
758-
mockClient.EXPECT().UpdateDomainRecord(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("failed to update domain record of type A")).AnyTimes()
759-
},
760-
expectedError: fmt.Errorf("failed to update domain record of type A"),
761-
},
762682
{
763683
name: "Error - no public ip set",
764684
machineScope: &scope.MachineScope{

0 commit comments

Comments
 (0)