@@ -57,22 +57,60 @@ var proxyEnabled *bool = boolPtr(true)
57
57
// proxyDisabled is a pointer to a bool false showing the record should not be proxied through cloudflare
58
58
var proxyDisabled * bool = boolPtr (false )
59
59
60
- // for faster getRecordID () lookup
60
+ // for faster GetRecordID () lookup
61
61
type DNSRecordIndex struct {
62
62
Name string
63
63
Type string
64
64
Content string
65
65
}
66
66
67
+ func newDNSRecordIndex (r cloudflare.DNSRecord ) DNSRecordIndex {
68
+ return DNSRecordIndex {Name : r .Name , Type : r .Type , Content : r .Content }
69
+ }
70
+
67
71
type DNSRecordsMap map [DNSRecordIndex ]cloudflare.DNSRecord
68
72
69
- // for faster getCustomHostname() lookup
73
+ func (m DNSRecordsMap ) GetRecordID (record cloudflare.DNSRecord ) string {
74
+ return m [newDNSRecordIndex (record )].ID
75
+ }
76
+
77
+ func (m DNSRecordsMap ) Set (record cloudflare.DNSRecord ) {
78
+ m [newDNSRecordIndex (record )] = record
79
+ }
80
+
81
+ func (m DNSRecordsMap ) Delete (record cloudflare.DNSRecord ) {
82
+ delete (m , newDNSRecordIndex (record ))
83
+ }
84
+
85
+ // for faster GetCustomHostname() lookup
70
86
type CustomHostnameIndex struct {
71
87
Hostname string
72
88
}
73
89
90
+ func newCustomHostnameIndex (ch cloudflare.CustomHostname ) CustomHostnameIndex {
91
+ return CustomHostnameIndex {Hostname : ch .Hostname }
92
+ }
93
+
74
94
type CustomHostnamesMap map [CustomHostnameIndex ]cloudflare.CustomHostname
75
95
96
+ func (m CustomHostnamesMap ) Get (hostname string ) (cloudflare.CustomHostname , error ) {
97
+ if hostname == "" {
98
+ return cloudflare.CustomHostname {}, fmt .Errorf ("failed to get custom hostname: %q is empty" , hostname )
99
+ }
100
+ if ch , ok := m [CustomHostnameIndex {Hostname : hostname }]; ok {
101
+ return ch , nil
102
+ }
103
+ return cloudflare.CustomHostname {}, fmt .Errorf ("failed to get custom hostname: %q not found" , hostname )
104
+ }
105
+
106
+ func (m CustomHostnamesMap ) Set (ch cloudflare.CustomHostname ) {
107
+ m [newCustomHostnameIndex (ch )] = ch
108
+ }
109
+
110
+ func (m CustomHostnamesMap ) Delete (ch cloudflare.CustomHostname ) {
111
+ delete (m , newCustomHostnameIndex (ch ))
112
+ }
113
+
76
114
type DataLocalizationRegionalHostnameChange struct {
77
115
Action string
78
116
cloudflare.RegionalHostname
@@ -434,7 +472,7 @@ func (p *CloudFlareProvider) submitCustomHostnameChanges(ctx context.Context, zo
434
472
add , remove , _ := provider .Difference (change .CustomHostnamesPrev , slices .Collect (maps .Keys (change .CustomHostnames )))
435
473
436
474
for _ , changeCH := range remove {
437
- if prevCh , err := getCustomHostname ( chs , changeCH ); err == nil {
475
+ if prevCh , err := chs . Get ( changeCH ); err == nil {
438
476
prevChID := prevCh .ID
439
477
if prevChID != "" {
440
478
log .WithFields (logFields ).Infof ("Removing previous custom hostname %q/%q" , prevChID , changeCH )
@@ -443,29 +481,33 @@ func (p *CloudFlareProvider) submitCustomHostnameChanges(ctx context.Context, zo
443
481
failedChange = true
444
482
log .WithFields (logFields ).Errorf ("failed to remove previous custom hostname %q/%q: %v" , prevChID , changeCH , chErr )
445
483
}
484
+ chs .Delete (prevCh )
446
485
}
447
486
}
448
487
}
449
488
for _ , changeCH := range add {
450
489
log .WithFields (logFields ).Infof ("Adding custom hostname %q" , changeCH )
451
- _ , chErr := p .Client .CreateCustomHostname (ctx , zoneID , change .CustomHostnames [changeCH ])
490
+ ch := change .CustomHostnames [changeCH ]
491
+ _ , chErr := p .Client .CreateCustomHostname (ctx , zoneID , ch )
452
492
if chErr != nil {
453
493
failedChange = true
454
494
log .WithFields (logFields ).Errorf ("failed to add custom hostname %q: %v" , changeCH , chErr )
455
495
}
496
+ chs .Set (ch )
456
497
}
457
498
}
458
499
case cloudFlareDelete :
459
500
for _ , changeCH := range change .CustomHostnames {
460
501
if recordTypeCustomHostnameSupported [change .ResourceRecord .Type ] && changeCH .Hostname != "" {
461
502
log .WithFields (logFields ).Infof ("Deleting custom hostname %q" , changeCH .Hostname )
462
- if ch , err := getCustomHostname ( chs , changeCH .Hostname ); err == nil {
503
+ if ch , err := chs . Get ( changeCH .Hostname ); err == nil {
463
504
chID := ch .ID
464
505
chErr := p .Client .DeleteCustomHostname (ctx , zoneID , chID )
465
506
if chErr != nil {
466
507
failedChange = true
467
508
log .WithFields (logFields ).Errorf ("failed to delete custom hostname %q/%q: %v" , chID , changeCH .Hostname , chErr )
468
509
}
510
+ chs .Delete (changeCH )
469
511
} else {
470
512
log .WithFields (logFields ).Warnf ("failed to delete custom hostname %q: %v" , changeCH .Hostname , err )
471
513
}
@@ -475,7 +517,7 @@ func (p *CloudFlareProvider) submitCustomHostnameChanges(ctx context.Context, zo
475
517
for _ , changeCH := range change .CustomHostnames {
476
518
if recordTypeCustomHostnameSupported [change .ResourceRecord .Type ] && changeCH .Hostname != "" {
477
519
log .WithFields (logFields ).Infof ("Creating custom hostname %q" , changeCH .Hostname )
478
- if ch , err := getCustomHostname ( chs , changeCH .Hostname ); err == nil {
520
+ if ch , err := chs . Get ( changeCH .Hostname ); err == nil {
479
521
if changeCH .CustomOriginServer == ch .CustomOriginServer {
480
522
log .WithFields (logFields ).Warnf ("custom hostname %q already exists with the same origin %q, continue" , changeCH .Hostname , ch .CustomOriginServer )
481
523
} else {
@@ -489,6 +531,7 @@ func (p *CloudFlareProvider) submitCustomHostnameChanges(ctx context.Context, zo
489
531
log .WithFields (logFields ).Errorf ("failed to create custom hostname %q: %v" , changeCH .Hostname , chErr )
490
532
}
491
533
}
534
+ chs .Set (changeCH )
492
535
}
493
536
}
494
537
}
@@ -636,8 +679,22 @@ func (p *CloudFlareProvider) submitChanges(ctx context.Context, changes []*cloud
636
679
var failedZones []string
637
680
for zoneID , zoneChanges := range changesByZone {
638
681
var failedChange bool
682
+ var records DNSRecordsMap
683
+ var chs CustomHostnamesMap
639
684
resourceContainer := cloudflare .ZoneIdentifier (zoneID )
640
685
686
+ if ! p .DryRun {
687
+ records , err = p .listDNSRecordsWithAutoPagination (ctx , zoneID )
688
+ if err != nil {
689
+ return fmt .Errorf ("could not fetch records from zone, %w" , err )
690
+ }
691
+
692
+ chs , err = p .listCustomHostnamesWithPagination (ctx , zoneID )
693
+ if err != nil {
694
+ return fmt .Errorf ("could not fetch custom hostnames from zone, %v" , err )
695
+ }
696
+ }
697
+
641
698
for _ , change := range zoneChanges {
642
699
logFields := log.Fields {
643
700
"record" : change .ResourceRecord .Name ,
@@ -653,19 +710,11 @@ func (p *CloudFlareProvider) submitChanges(ctx context.Context, changes []*cloud
653
710
continue
654
711
}
655
712
656
- records , err := p .listDNSRecordsWithAutoPagination (ctx , zoneID )
657
- if err != nil {
658
- return fmt .Errorf ("could not fetch records from zone, %w" , err )
659
- }
660
- chs , chErr := p .listCustomHostnamesWithPagination (ctx , zoneID )
661
- if chErr != nil {
662
- return fmt .Errorf ("could not fetch custom hostnames from zone, %v" , chErr )
663
- }
664
713
if change .Action == cloudFlareUpdate {
665
714
if ! p .submitCustomHostnameChanges (ctx , zoneID , change , chs , logFields ) {
666
715
failedChange = true
667
716
}
668
- recordID := p . getRecordID ( records , change .ResourceRecord )
717
+ recordID := records . GetRecordID ( change .ResourceRecord )
669
718
if recordID == "" {
670
719
log .WithFields (logFields ).Errorf ("failed to find previous record: %v" , change .ResourceRecord )
671
720
continue
@@ -678,7 +727,7 @@ func (p *CloudFlareProvider) submitChanges(ctx context.Context, changes []*cloud
678
727
log .WithFields (logFields ).Errorf ("failed to update record: %v" , err )
679
728
}
680
729
} else if change .Action == cloudFlareDelete {
681
- recordID := p . getRecordID ( records , change .ResourceRecord )
730
+ recordID := records . GetRecordID ( change .ResourceRecord )
682
731
if recordID == "" {
683
732
log .WithFields (logFields ).Errorf ("failed to find previous record: %v" , change .ResourceRecord )
684
733
continue
@@ -688,16 +737,18 @@ func (p *CloudFlareProvider) submitChanges(ctx context.Context, changes []*cloud
688
737
failedChange = true
689
738
log .WithFields (logFields ).Errorf ("failed to delete record: %v" , err )
690
739
}
740
+ records .Delete (change .ResourceRecord )
691
741
if ! p .submitCustomHostnameChanges (ctx , zoneID , change , chs , logFields ) {
692
742
failedChange = true
693
743
}
694
744
} else if change .Action == cloudFlareCreate {
695
745
recordParam := getCreateDNSRecordParam (* change )
696
- _ , err := p .Client .CreateDNSRecord (ctx , resourceContainer , recordParam )
746
+ record , err := p .Client .CreateDNSRecord (ctx , resourceContainer , recordParam )
697
747
if err != nil {
698
748
failedChange = true
699
749
log .WithFields (logFields ).Errorf ("failed to create record: %v" , err )
700
750
}
751
+ records .Set (record )
701
752
if ! p .submitCustomHostnameChanges (ctx , zoneID , change , chs , logFields ) {
702
753
failedChange = true
703
754
}
@@ -776,23 +827,6 @@ func (p *CloudFlareProvider) changesByZone(zones []cloudflare.Zone, changeSet []
776
827
return changes
777
828
}
778
829
779
- func (p * CloudFlareProvider ) getRecordID (records DNSRecordsMap , record cloudflare.DNSRecord ) string {
780
- if zoneRecord , ok := records [DNSRecordIndex {Name : record .Name , Type : record .Type , Content : record .Content }]; ok {
781
- return zoneRecord .ID
782
- }
783
- return ""
784
- }
785
-
786
- func getCustomHostname (chs CustomHostnamesMap , chName string ) (cloudflare.CustomHostname , error ) {
787
- if chName == "" {
788
- return cloudflare.CustomHostname {}, fmt .Errorf ("failed to get custom hostname: %q is empty" , chName )
789
- }
790
- if ch , ok := chs [CustomHostnameIndex {Hostname : chName }]; ok {
791
- return ch , nil
792
- }
793
- return cloudflare.CustomHostname {}, fmt .Errorf ("failed to get custom hostname: %q not found" , chName )
794
- }
795
-
796
830
func (p * CloudFlareProvider ) newCustomHostname (customHostname string , origin string ) cloudflare.CustomHostname {
797
831
return cloudflare.CustomHostname {
798
832
Hostname : customHostname ,
@@ -843,10 +877,6 @@ func (p *CloudFlareProvider) newCloudFlareChange(action string, ep *endpoint.End
843
877
}
844
878
}
845
879
846
- func newDNSRecordIndex (r cloudflare.DNSRecord ) DNSRecordIndex {
847
- return DNSRecordIndex {Name : r .Name , Type : r .Type , Content : r .Content }
848
- }
849
-
850
880
// listDNSRecordsWithAutoPagination performs automatic pagination of results on requests to cloudflare.ListDNSRecords with custom per_page values
851
881
func (p * CloudFlareProvider ) listDNSRecordsWithAutoPagination (ctx context.Context , zoneID string ) (DNSRecordsMap , error ) {
852
882
// for faster getRecordID lookup
@@ -877,10 +907,6 @@ func (p *CloudFlareProvider) listDNSRecordsWithAutoPagination(ctx context.Contex
877
907
return records , nil
878
908
}
879
909
880
- func newCustomHostnameIndex (ch cloudflare.CustomHostname ) CustomHostnameIndex {
881
- return CustomHostnameIndex {Hostname : ch .Hostname }
882
- }
883
-
884
910
// listCustomHostnamesWithPagination performs automatic pagination of results on requests to cloudflare.CustomHostnames
885
911
func (p * CloudFlareProvider ) listCustomHostnamesWithPagination (ctx context.Context , zoneID string ) (CustomHostnamesMap , error ) {
886
912
if ! p .CustomHostnamesConfig .Enabled {
0 commit comments