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

feat(cloudflare): support cloudflare region #4646

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
a38476b
feat: add cloudflare host name
AndrewCharlesHay Jul 31, 2024
a6590de
add test
AndrewCharlesHay Aug 1, 2024
bbe914b
add flag declaration
AndrewCharlesHay Aug 5, 2024
d8ec935
fix: add flag declaration
AndrewCharlesHay Aug 5, 2024
6ef25bf
fix: add RegionalHostname
AndrewCharlesHay Aug 6, 2024
4386cbb
cloudflare: changes to plan
AndrewCharlesHay Aug 6, 2024
2227d22
Merge branch 'master' into cloudflare/region
AndrewCharlesHay Sep 6, 2024
70406fc
fix: resolve syntax errors
AndrewCharlesHay Sep 6, 2024
e0cc262
fix: add region to changes function
AndrewCharlesHay Sep 9, 2024
c18bb8b
test: add tests for cloudflare region
AndrewCharlesHay Sep 9, 2024
797629f
chore: remove extra comments
AndrewCharlesHay Sep 13, 2024
e49c40d
chore: remove extra comments
AndrewCharlesHay Sep 13, 2024
fe7f6f0
feat: add codespace definition
AndrewCharlesHay Sep 13, 2024
e8d55ba
ci: move go to vscode setings
AndrewCharlesHay Sep 13, 2024
d704d7d
chore: remove devcontainer
AndrewCharlesHay Sep 14, 2024
15d0813
style: tabs vs spaces
AndrewCharlesHay Sep 14, 2024
3b4568e
fix: update failing tests
AndrewCharlesHay Oct 7, 2024
438b0bc
fix: add UpdateDataLocalizationRegionalHostname to test framework
AndrewCharlesHay Oct 11, 2024
1e35134
fix: add remove default from minimal config
AndrewCharlesHay Oct 11, 2024
806cf81
fix: update function name to resemble crud
AndrewCharlesHay Oct 29, 2024
3b5ef97
Merge branch 'master' into cloudflare/region
AndrewCharlesHay Nov 1, 2024
6f6e714
docs: notes on how to use cloudflare-region-key
AndrewCharlesHay Nov 1, 2024
10d383c
Update docs/tutorials/cloudflare.md
AndrewCharlesHay Nov 2, 2024
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
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func main() {
case "civo":
p, err = civo.NewCivoProvider(domainFilter, cfg.DryRun)
case "cloudflare":
p, err = cloudflare.NewCloudFlareProvider(domainFilter, zoneIDFilter, cfg.CloudflareProxied, cfg.DryRun, cfg.CloudflareDNSRecordsPerPage)
p, err = cloudflare.NewCloudFlareProvider(domainFilter, zoneIDFilter, cfg.CloudflareProxied, cfg.DryRun, cfg.CloudflareDNSRecordsPerPage, cfg.CloudflareRegionKey)
case "google":
p, err = google.NewGoogleProvider(ctx, cfg.GoogleProject, domainFilter, zoneIDFilter, cfg.GoogleBatchChangeSize, cfg.GoogleBatchChangeInterval, cfg.GoogleZoneVisibility, cfg.DryRun)
case "digitalocean":
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/externaldns/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ type Config struct {
AzureActiveDirectoryAuthorityHost string
CloudflareProxied bool
CloudflareDNSRecordsPerPage int
CloudflareRegionKey string
CoreDNSPrefix string
AkamaiServiceConsumerDomain string
AkamaiClientToken string
Expand Down Expand Up @@ -262,6 +263,7 @@ var defaultConfig = &Config{
AzureSubscriptionID: "",
CloudflareProxied: false,
CloudflareDNSRecordsPerPage: 100,
CloudflareRegionKey: "earth",
CoreDNSPrefix: "/skydns/",
AkamaiServiceConsumerDomain: "",
AkamaiClientToken: "",
Expand Down Expand Up @@ -482,6 +484,7 @@ func (cfg *Config) ParseFlags(args []string) error {

app.Flag("cloudflare-proxied", "When using the Cloudflare provider, specify if the proxy mode must be enabled (default: disabled)").BoolVar(&cfg.CloudflareProxied)
app.Flag("cloudflare-dns-records-per-page", "When using the Cloudflare provider, specify how many DNS records listed per page, max possible 5,000 (default: 100)").Default(strconv.Itoa(defaultConfig.CloudflareDNSRecordsPerPage)).IntVar(&cfg.CloudflareDNSRecordsPerPage)
app.Flag("cloudflare-region-key", "When using the Cloudflare provider, specify the region (default: earth)").StringVar(&cfg.CloudflareRegionKey)
app.Flag("coredns-prefix", "When using the CoreDNS provider, specify the prefix name").Default(defaultConfig.CoreDNSPrefix).StringVar(&cfg.CoreDNSPrefix)
app.Flag("akamai-serviceconsumerdomain", "When using the Akamai provider, specify the base URL (required when --provider=akamai and edgerc-path not specified)").Default(defaultConfig.AkamaiServiceConsumerDomain).StringVar(&cfg.AkamaiServiceConsumerDomain)
app.Flag("akamai-client-token", "When using the Akamai provider, specify the client token (required when --provider=akamai and edgerc-path not specified)").Default(defaultConfig.AkamaiClientToken).StringVar(&cfg.AkamaiClientToken)
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/externaldns/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ var (
AzureSubscriptionID: "",
CloudflareProxied: false,
CloudflareDNSRecordsPerPage: 100,
CloudflareRegionKey: "earth",
CoreDNSPrefix: "/skydns/",
AkamaiServiceConsumerDomain: "",
AkamaiClientToken: "",
Expand Down Expand Up @@ -172,6 +173,7 @@ var (
AzureSubscriptionID: "arg",
CloudflareProxied: true,
CloudflareDNSRecordsPerPage: 5000,
CloudflareRegionKey: "us",
CoreDNSPrefix: "/coredns/",
AkamaiServiceConsumerDomain: "oooo-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx.luna.akamaiapis.net",
AkamaiClientToken: "o184671d5307a388180fbf7f11dbdf46",
Expand Down Expand Up @@ -273,6 +275,7 @@ func TestParseFlags(t *testing.T) {
"--azure-subscription-id=arg",
"--cloudflare-proxied",
"--cloudflare-dns-records-per-page=5000",
"--cloudflare-region-key=us",
"--coredns-prefix=/coredns/",
"--akamai-serviceconsumerdomain=oooo-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx.luna.akamaiapis.net",
"--akamai-client-token=o184671d5307a388180fbf7f11dbdf46",
Expand Down Expand Up @@ -389,6 +392,7 @@ func TestParseFlags(t *testing.T) {
"EXTERNAL_DNS_AZURE_SUBSCRIPTION_ID": "arg",
"EXTERNAL_DNS_CLOUDFLARE_PROXIED": "1",
"EXTERNAL_DNS_CLOUDFLARE_DNS_RECORDS_PER_PAGE": "5000",
"EXTERNAL_DNS_CLOUDFLARE_REGION_KEY": "us",
"EXTERNAL_DNS_COREDNS_PREFIX": "/coredns/",
"EXTERNAL_DNS_AKAMAI_SERVICECONSUMERDOMAIN": "oooo-xxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxx.luna.akamaiapis.net",
"EXTERNAL_DNS_AKAMAI_CLIENT_TOKEN": "o184671d5307a388180fbf7f11dbdf46",
Expand Down
56 changes: 52 additions & 4 deletions provider/cloudflare/cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"strconv"
"strings"
"time"

cloudflare "github.com/cloudflare/cloudflare-go"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -73,6 +74,7 @@ type cloudFlareDNS interface {
CreateDNSRecord(ctx context.Context, rc *cloudflare.ResourceContainer, rp cloudflare.CreateDNSRecordParams) (cloudflare.DNSRecord, error)
DeleteDNSRecord(ctx context.Context, rc *cloudflare.ResourceContainer, recordID string) error
UpdateDNSRecord(ctx context.Context, rc *cloudflare.ResourceContainer, rp cloudflare.UpdateDNSRecordParams) error
UpdateDataLocalizationRegionalHostname(ctx context.Context, rc *cloudflare.ResourceContainer, rp cloudflare.UpdateDataLocalizationRegionalHostnameParams) error
}

type zoneService struct {
Expand Down Expand Up @@ -104,6 +106,11 @@ func (z zoneService) UpdateDNSRecord(ctx context.Context, rc *cloudflare.Resourc
return err
}

func (z zoneService) UpdateDataLocalizationRegionalHostname(ctx context.Context, rc *cloudflare.ResourceContainer, rp cloudflare.UpdateDataLocalizationRegionalHostnameParams) error {
_, err := z.service.UpdateDataLocalizationRegionalHostname(ctx, rc, rp)
return err
}

func (z zoneService) DeleteDNSRecord(ctx context.Context, rc *cloudflare.ResourceContainer, recordID string) error {
return z.service.DeleteDNSRecord(ctx, rc, recordID)
}
Expand All @@ -126,12 +133,30 @@ type CloudFlareProvider struct {
proxiedByDefault bool
DryRun bool
DNSRecordsPerPage int
RegionKey string
}

// func (p *CloudflareProvider) CreateDomainRecord(zoneID, recordType, name string, content string, ttl int) error {
// rec := cloudflare.DNSRecord{
// Type: recordType,
// Name: name,
// Content: content,
// TTL: ttl,
// Proxied: p.proxied,
// // add the region key if provided
// Meta: map[string]interface{}{
// "region": p.config.CloudflareRegion, // Add this line
// },
// }

// // existing code
// }
AndrewCharlesHay marked this conversation as resolved.
Show resolved Hide resolved

// cloudFlareChange differentiates between ChangActions
type cloudFlareChange struct {
Action string
ResourceRecord cloudflare.DNSRecord
Action string
ResourceRecord cloudflare.DNSRecord
RegionalHostname cloudflare.RegionalHostname
}

// RecordParamsTypes is a typeset of the possible Record Params that can be passed to cloudflare-go library
Expand All @@ -150,6 +175,14 @@ func getUpdateDNSRecordParam(cfc cloudFlareChange) cloudflare.UpdateDNSRecordPar
}
}

// getUpdateDataLocalizationRegionalHostnameParams is a function that returns the appropriate RegionalHostname Param based on the cloudFlareChange passed in
func getUpdateDataLocalizationRegionalHostnameParams(cfc cloudFlareChange) cloudflare.UpdateDataLocalizationRegionalHostnameParams {
return cloudflare.UpdateDataLocalizationRegionalHostnameParams{
Hostname: cfc.RegionalHostname.Hostname,
RegionKey: cfc.RegionalHostname.RegionKey,
}
}

AndrewCharlesHay marked this conversation as resolved.
Show resolved Hide resolved
// getCreateDNSRecordParam is a function that returns the appropriate Record Param based on the cloudFlareChange passed in
func getCreateDNSRecordParam(cfc cloudFlareChange) cloudflare.CreateDNSRecordParams {
return cloudflare.CreateDNSRecordParams{
Expand All @@ -162,7 +195,7 @@ func getCreateDNSRecordParam(cfc cloudFlareChange) cloudflare.CreateDNSRecordPar
}

// NewCloudFlareProvider initializes a new CloudFlare DNS based Provider.
func NewCloudFlareProvider(domainFilter endpoint.DomainFilter, zoneIDFilter provider.ZoneIDFilter, proxiedByDefault bool, dryRun bool, dnsRecordsPerPage int) (*CloudFlareProvider, error) {
func NewCloudFlareProvider(domainFilter endpoint.DomainFilter, zoneIDFilter provider.ZoneIDFilter, proxiedByDefault bool, dryRun bool, dnsRecordsPerPage int, regionKey string) (*CloudFlareProvider, error) {
// initialize via chosen auth method and returns new API object
var (
config *cloudflare.API
Expand Down Expand Up @@ -192,6 +225,7 @@ func NewCloudFlareProvider(domainFilter endpoint.DomainFilter, zoneIDFilter prov
proxiedByDefault: proxiedByDefault,
DryRun: dryRun,
DNSRecordsPerPage: dnsRecordsPerPage,
RegionKey: regionKey,
}
return provider, nil
}
Expand Down Expand Up @@ -351,12 +385,18 @@ func (p *CloudFlareProvider) submitChanges(ctx context.Context, changes []*cloud
continue
}
recordParam := getUpdateDNSRecordParam(*change)
regionalHostnameParam := getUpdateDataLocalizationRegionalHostnameParams(*change)
recordParam.ID = recordID
err := p.Client.UpdateDNSRecord(ctx, resourceContainer, recordParam)
if err != nil {
failedChange = true
log.WithFields(logFields).Errorf("failed to update record: %v", err)
}
regionalHostnameErr := p.Client.UpdateDataLocalizationRegionalHostname(ctx, resourceContainer, regionalHostnameParam)
if regionalHostnameErr != nil {
failedChange = true
log.WithFields(logFields).Errorf("failed to update record: %v", regionalHostnameErr)
}
} else if change.Action == cloudFlareDelete {
recordID := p.getRecordID(records, change.ResourceRecord)
if recordID == "" {
Expand Down Expand Up @@ -443,7 +483,7 @@ func (p *CloudFlareProvider) newCloudFlareChange(action string, endpoint *endpoi
if endpoint.RecordTTL.IsConfigured() {
ttl = int(endpoint.RecordTTL)
}

dt := time.Now()
return &cloudFlareChange{
Action: action,
ResourceRecord: cloudflare.DNSRecord{
Expand All @@ -452,6 +492,14 @@ func (p *CloudFlareProvider) newCloudFlareChange(action string, endpoint *endpoi
Proxied: &proxied,
Type: endpoint.RecordType,
Content: target,
Meta: map[string]interface{}{
"region": p.RegionKey,
},
},
RegionalHostname: cloudflare.RegionalHostname{
Hostname: endpoint.DNSName,
RegionKey: endpoint.SetIdentifier,
CreatedOn: &dt,
},
}
}
Expand Down
91 changes: 87 additions & 4 deletions provider/cloudflare/cloudflare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,20 @@ func (m *mockCloudFlareClient) UpdateDNSRecord(ctx context.Context, rc *cloudfla
return nil
}

func (m *mockCloudFlareClient) UpdateDataLocalizationRegionalHostname(ctx context.Context, rc *cloudflare.ResourceContainer, rp cloudflare.UpdateDataLocalizationRegionalHostnameParams) error {
// Add logic here to mock the UpdateDataLocalizationRegionalHostname method
// For example, you could add a MockAction to the Actions slice
m.Actions = append(m.Actions, MockAction{
Name: "UpdateDataLocalizationRegionalHostname",
ZoneId: rc.Identifier,
RecordId: "", // You can leave this empty or use a default value
RecordData: cloudflare.DNSRecord{
Name: rp.Hostname,
AndrewCharlesHay marked this conversation as resolved.
Show resolved Hide resolved
},
})
return nil
}

func (m *mockCloudFlareClient) DeleteDNSRecord(ctx context.Context, rc *cloudflare.ResourceContainer, recordID string) error {
m.Actions = append(m.Actions, MockAction{
Name: "Delete",
Expand Down Expand Up @@ -710,7 +724,8 @@ func TestCloudflareProvider(t *testing.T) {
provider.NewZoneIDFilter([]string{""}),
false,
true,
5000)
5000,
"")
if err != nil {
t.Errorf("should not fail, %s", err)
}
Expand All @@ -726,7 +741,8 @@ func TestCloudflareProvider(t *testing.T) {
provider.NewZoneIDFilter([]string{""}),
false,
true,
5000)
5000,
"")
if err != nil {
t.Errorf("should not fail, %s", err)
}
Expand All @@ -739,7 +755,8 @@ func TestCloudflareProvider(t *testing.T) {
provider.NewZoneIDFilter([]string{""}),
false,
true,
5000)
5000,
"")
if err != nil {
t.Errorf("should not fail, %s", err)
}
Expand All @@ -751,7 +768,8 @@ func TestCloudflareProvider(t *testing.T) {
provider.NewZoneIDFilter([]string{""}),
false,
true,
5000)
5000,
"")
if err == nil {
t.Errorf("expected to fail")
}
Expand Down Expand Up @@ -1362,3 +1380,68 @@ func TestCustomTTLWithEnabledProxyNotChanged(t *testing.T) {
assert.Equal(t, 0, len(planned.Changes.UpdateOld), "no new changes should be here")
assert.Equal(t, 0, len(planned.Changes.Delete), "no new changes should be here")
}
func TestCloudFlareProvider_Region(t *testing.T) {
provider, err := NewCloudFlareProvider(endpoint.NewDomainFilter([]string{"example.com"}), provider.ZoneIDFilter{}, true, false, 50, "us")
if err != nil {
t.Fatal(err)
}

if provider.RegionKey != "us" {
t.Errorf("expected region key to be 'us', but got '%s'", provider.RegionKey)
}
}

func TestCloudFlareProvider_UpdateDataLocalizationRegionalHostname(t *testing.T) {
provider, err := NewCloudFlareProvider(endpoint.NewDomainFilter([]string{"example.com"}), provider.ZoneIDFilter{}, true, false, 50, "us")
if err != nil {
t.Fatal(err)
}

ctx := context.Background()
zoneID := "example.com"
resourceContainer := cloudflare.ZoneIdentifier(zoneID)
regionalHostnameParam := cloudflare.UpdateDataLocalizationRegionalHostnameParams{
Hostname: "example.com",
RegionKey: "us",
}

err = provider.Client.UpdateDataLocalizationRegionalHostname(ctx, resourceContainer, regionalHostnameParam)
if err != nil {
t.Errorf("expected no error, but got '%v'", err)
}
}

func TestCloudFlareProvider_getUpdateDataLocalizationRegionalHostnameParams(t *testing.T) {
change := &cloudFlareChange{
RegionalHostname: cloudflare.RegionalHostname{
Hostname: "example.com",
RegionKey: "us",
},
}

params := getUpdateDataLocalizationRegionalHostnameParams(*change)
if params.Hostname != "example.com" {
t.Errorf("expected hostname to be 'example.com', but got '%s'", params.Hostname)
}

if params.RegionKey != "us" {
t.Errorf("expected region key to be 'us', but got '%s'", params.RegionKey)
}
}

func TestCloudFlareProvider_newCloudFlareChange(t *testing.T) {
provider, err := NewCloudFlareProvider(endpoint.NewDomainFilter([]string{"example.com"}), provider.ZoneIDFilter{}, true, false, 50, "us")
if err != nil {
t.Fatal(err)
}

endpoint := &endpoint.Endpoint{
DNSName: "example.com",
Targets: []string{"192.0.2.1"},
}

change := provider.newCloudFlareChange(cloudFlareCreate, endpoint, endpoint.Targets[0])
if change.RegionalHostname.RegionKey != "us" {
t.Errorf("expected region key to be 'us', but got '%s'", change.RegionalHostname.RegionKey)
}
}