Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] Fix create/update opertions if leftover DNS entries are found #424

Merged
merged 6 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 2 additions & 28 deletions cloud/services/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func EnsureLinodeDNSEntries(ctx context.Context, mscope *scope.MachineScope, ope
}
continue
}
if err := CreateUpdateDomainRecord(ctx, mscope, domainID, dnsEntry); err != nil {
if err := CreateDomainRecord(ctx, mscope, domainID, dnsEntry); err != nil {
return err
}
}
Expand Down Expand Up @@ -189,7 +189,7 @@ func GetDomainID(ctx context.Context, mscope *scope.MachineScope) (int, error) {
return domains[0].ID, nil
}

func CreateUpdateDomainRecord(ctx context.Context, mscope *scope.MachineScope, domainID int, dnsEntry DNSOptions) error {
func CreateDomainRecord(ctx context.Context, mscope *scope.MachineScope, domainID int, dnsEntry DNSOptions) error {
// Check if domain record exists for this IP and name combo
filter, err := json.Marshal(map[string]interface{}{"name": dnsEntry.Hostname, "target": dnsEntry.Target, "type": dnsEntry.DNSRecordType})
if err != nil {
Expand All @@ -215,32 +215,6 @@ func CreateUpdateDomainRecord(ctx context.Context, mscope *scope.MachineScope, d
); err != nil {
return err
}
return nil
}

// If record exists, update it
if len(domainRecords) != 0 && dnsEntry.DNSRecordType != linodego.RecordTypeTXT {
isOwner, err := IsDomainRecordOwner(ctx, mscope, dnsEntry.Hostname, domainID)
if err != nil {
return err
}
if !isOwner {
return fmt.Errorf("the domain record is not owned by this entity. wont update")
}
}

if _, err := mscope.LinodeDomainsClient.UpdateDomainRecord(
ctx,
domainID,
domainRecords[0].ID,
linodego.DomainRecordUpdateOptions{
Type: dnsEntry.DNSRecordType,
Name: dnsEntry.Hostname,
Target: dnsEntry.Target,
TTLSec: dnsEntry.DNSTTLSec,
},
); err != nil {
return err
}
return nil
}
Expand Down
82 changes: 1 addition & 81 deletions cloud/services/domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ func TestAddIPToDNS(t *testing.T) {
expectedError: fmt.Errorf("failed to create domain record of type A"),
},
{
name: "Success - If the machine is a control plane node and record already exists, update it",
name: "Success - If the machine is a control plane node and record already exists, leave it alone",
machineScope: &scope.MachineScope{
Machine: &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -612,13 +612,6 @@ func TestAddIPToDNS(t *testing.T) {
TTLSec: 30,
},
}, nil).AnyTimes()
mockClient.EXPECT().CreateDomainRecord(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
mockClient.EXPECT().UpdateDomainRecord(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(&linodego.DomainRecord{
ID: 1234,
Type: "A",
Name: "test-cluster",
TTLSec: 30,
}, nil).AnyTimes()
},
expectedError: nil,
},
Expand Down Expand Up @@ -686,79 +679,6 @@ func TestAddIPToDNS(t *testing.T) {
},
expectedError: fmt.Errorf("api error"),
},
{
name: "Error - UpdateDomainRecord fails",
machineScope: &scope.MachineScope{
Machine: &clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine",
UID: "test-uid",
Labels: map[string]string{
clusterv1.MachineControlPlaneLabel: "true",
},
},
},
Cluster: &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
UID: "test-uid",
},
},
LinodeCluster: &infrav1alpha2.LinodeCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cluster",
UID: "test-uid",
},
Spec: infrav1alpha2.LinodeClusterSpec{
Network: infrav1alpha2.NetworkSpec{
LoadBalancerType: "dns",
DNSRootDomain: "lkedevs.net",
DNSUniqueIdentifier: "test-hash",
},
},
},
LinodeMachine: &infrav1alpha2.LinodeMachine{
ObjectMeta: metav1.ObjectMeta{
Name: "test-machine",
UID: "test-uid",
},
Spec: infrav1alpha2.LinodeMachineSpec{
InstanceID: ptr.To(123),
},
Status: infrav1alpha2.LinodeMachineStatus{
Addresses: []clusterv1.MachineAddress{
{
Type: "ExternalIP",
Address: "10.10.10.10",
},
{
Type: "ExternalIP",
Address: "fd00::",
},
},
},
},
},
expects: func(mockClient *mock.MockLinodeClient) {
mockClient.EXPECT().ListDomains(gomock.Any(), gomock.Any()).Return([]linodego.Domain{
{
ID: 1,
Domain: "lkedevs.net",
},
}, nil).AnyTimes()
mockClient.EXPECT().ListDomainRecords(gomock.Any(), gomock.Any(), gomock.Any()).Return([]linodego.DomainRecord{
{
ID: 1234,
Type: "A",
Name: "test-cluster",
TTLSec: 30,
},
}, nil).AnyTimes()
mockClient.EXPECT().CreateDomainRecord(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
mockClient.EXPECT().UpdateDomainRecord(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("failed to update domain record of type A")).AnyTimes()
},
expectedError: fmt.Errorf("failed to update domain record of type A"),
},
{
name: "Error - no public ip set",
machineScope: &scope.MachineScope{
Expand Down
Loading