Skip to content

fix: createRecord for alibabacloud provider #5432

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

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
13 changes: 13 additions & 0 deletions provider/alibabacloud/alibaba_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,20 @@ func (p *AlibabaCloudProvider) unescapeTXTRecordValue(value string) string {
}

func (p *AlibabaCloudProvider) createRecord(endpoint *endpoint.Endpoint, target string, hostedZoneDomains []string) error {
if len(hostedZoneDomains) == 0 {
log.Errorf("Failed to create %s record named '%s' to '%s' for Alibaba Cloud DNS: zone not found",
endpoint.RecordType, endpoint.DNSName, target)
return fmt.Errorf("zone not found")
}

rr, domain := p.splitDNSName(endpoint.DNSName, hostedZoneDomains)

if domain == "" {
log.Errorf("Failed to create %s record named '%s' to '%s' for Alibaba Cloud DNS: no corresponding DNS zone found for this domain '%s'",
endpoint.RecordType, endpoint.DNSName, target, endpoint.DNSName)
return fmt.Errorf("no corresponding DNS zone found for this domain")
}

request := alidns.CreateAddDomainRecordRequest()
request.DomainName = domain
request.Type = endpoint.RecordType
Expand Down
68 changes: 68 additions & 0 deletions provider/alibabacloud/alibaba_cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,64 @@ func TestAlibabaCloudProvider_ApplyChanges(t *testing.T) {
}
}

func TestAlibabaCloudProvider_ApplyChanges_HaveNoDefinedZoneDomain(t *testing.T) {
p := newTestAlibabaCloudProvider(false)
defaultTtlPlan := &endpoint.Endpoint{
DNSName: "ttl.container-service.top",
RecordType: "A",
RecordTTL: defaultTTL,
Targets: endpoint.NewTargets("4.3.2.1"),
}
changes := plan.Changes{
Create: []*endpoint.Endpoint{
{
DNSName: "www.example.com", //no found this zone by API: DescribeDomains
RecordType: "A",
RecordTTL: 300,
Targets: endpoint.NewTargets("9.9.9.9"),
},
defaultTtlPlan,
},
UpdateNew: []*endpoint.Endpoint{
{
DNSName: "abc.container-service.top",
RecordType: "A",
RecordTTL: 500,
Targets: endpoint.NewTargets("1.2.3.4", "5.6.7.8"),
},
},
Delete: []*endpoint.Endpoint{
{
DNSName: "abc.container-service.top",
RecordType: "TXT",
RecordTTL: 300,
Targets: endpoint.NewTargets("\"heritage=external-dns,external-dns/owner=default\""),
},
},
}
ctx := context.Background()
err := p.ApplyChanges(ctx, &changes)
assert.NoError(t, err)
endpoints, err := p.Records(ctx)
if err != nil {
t.Errorf("Failed to get records: %v", err)
} else {
if len(endpoints) != 2 {
t.Errorf("Incorrect number of records: %d", len(endpoints))
}
for _, ep := range endpoints {
t.Logf("Endpoint for %++v", *ep)
}
}
for _, ep := range endpoints {
if ep.DNSName == defaultTtlPlan.DNSName {
if ep.RecordTTL != defaultTtlPlan.RecordTTL {
t.Error("default ttl execute error")
}
}
}
}

func TestAlibabaCloudProvider_Records_PrivateZone(t *testing.T) {
p := newTestAlibabaCloudProvider(true)
endpoints, err := p.Records(context.Background())
Expand Down Expand Up @@ -389,6 +447,8 @@ func TestAlibabaCloudProvider_splitDNSName(t *testing.T) {
endpoint := &endpoint.Endpoint{}
hostedZoneDomains := []string{"container-service.top", "example.org"}

var emptyZoneDomains []string

endpoint.DNSName = "www.example.org"
rr, domain := p.splitDNSName(endpoint.DNSName, hostedZoneDomains)
if rr != "www" || domain != "example.org" {
Expand Down Expand Up @@ -440,6 +500,14 @@ func TestAlibabaCloudProvider_splitDNSName(t *testing.T) {
if rr != "a.b" || domain != "c.container-service.top" {
t.Errorf("Failed to splitDNSName for %s: rr=%s, domain=%s", endpoint.DNSName, rr, domain)
}
rr, domain = p.splitDNSName(endpoint.DNSName, emptyZoneDomains)
if rr != "@" || domain != "" {
t.Errorf("Failed to splitDNSName with emptyZoneDomains for %s: rr=%s, domain=%s", endpoint.DNSName, rr, domain)
}
rr, domain = p.splitDNSName(endpoint.DNSName, []string{"example.com"})
if rr != "@" || domain != "" {
t.Errorf("Failed to splitDNSName for %s: rr=%s, domain=%s", endpoint.DNSName, rr, domain)
}
}

func TestAlibabaCloudProvider_TXTEndpoint(t *testing.T) {
Expand Down
Loading