Skip to content

Commit c216aaa

Browse files
authored
Merge pull request #406 from containeroo/refactor
refactor: rename variables
2 parents 27f5b2f + c15baae commit c216aaa

9 files changed

+124
-92
lines changed

cmd/main.go

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"crypto/tls"
2121
"flag"
2222
"os"
23+
"time"
2324

2425
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2526
// to ensure that exec-entrypoint and run can make use of them.
@@ -55,11 +56,18 @@ func init() {
5556
}
5657

5758
func main() {
58-
var metricsAddr string
59-
var enableLeaderElection bool
60-
var probeAddr string
61-
var secureMetrics bool
62-
var enableHTTP2 bool
59+
var (
60+
metricsAddr string
61+
enableLeaderElection bool
62+
probeAddr string
63+
secureMetrics bool
64+
enableHTTP2 bool
65+
retryInterval time.Duration
66+
ipReconcilerHTTPClientTimeout time.Duration
67+
defaultReconcileInterval time.Duration
68+
cloudflareAPI cloudflare.API
69+
ctx = ctrl.SetupSignalHandler()
70+
)
6371
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
6472
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
6573
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
@@ -69,6 +77,11 @@ func main() {
6977
"If set the metrics endpoint is served securely")
7078
flag.BoolVar(&enableHTTP2, "enable-http2", false,
7179
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
80+
flag.DurationVar(&ipReconcilerHTTPClientTimeout, "ip-reconciler-http-client-timeout", 10*time.Second,
81+
"The HTTP client timeout for the IP reconciler")
82+
flag.DurationVar(&retryInterval, "retry-interval", 10*time.Second, "The interval at which to retry failed operations")
83+
flag.DurationVar(&defaultReconcileInterval, "default-reconcile-interval", 5*time.Minute,
84+
"The default interval at which to reconcile resources")
7285
opts := zap.Options{
7386
Development: true,
7487
}
@@ -115,43 +128,47 @@ func main() {
115128
os.Exit(1)
116129
}
117130

118-
cf := cloudflare.API{}
119-
ctx := ctrl.SetupSignalHandler()
120-
121131
if err = (&controller.AccountReconciler{
122-
Client: mgr.GetClient(),
123-
Scheme: mgr.GetScheme(),
124-
Cf: &cf,
132+
Client: mgr.GetClient(),
133+
Scheme: mgr.GetScheme(),
134+
CloudflareAPI: &cloudflareAPI,
135+
RetryInterval: retryInterval,
125136
}).SetupWithManager(mgr); err != nil {
126137
setupLog.Error(err, "unable to create controller", "controller", "Account")
127138
os.Exit(1)
128139
}
129140
if err = (&controller.ZoneReconciler{
130-
Client: mgr.GetClient(),
131-
Scheme: mgr.GetScheme(),
132-
Cf: &cf,
141+
Client: mgr.GetClient(),
142+
Scheme: mgr.GetScheme(),
143+
CloudflareAPI: &cloudflareAPI,
144+
RetryInterval: retryInterval,
133145
}).SetupWithManager(ctx, mgr); err != nil {
134146
setupLog.Error(err, "unable to create controller", "controller", "Zone")
135147
os.Exit(1)
136148
}
137149
if err = (&controller.IPReconciler{
138-
Client: mgr.GetClient(),
139-
Scheme: mgr.GetScheme(),
150+
Client: mgr.GetClient(),
151+
Scheme: mgr.GetScheme(),
152+
HTTPClientTimeout: ipReconcilerHTTPClientTimeout,
153+
DefaultReconcileInterval: defaultReconcileInterval,
140154
}).SetupWithManager(mgr); err != nil {
141155
setupLog.Error(err, "unable to create controller", "controller", "IP")
142156
os.Exit(1)
143157
}
144158
if err = (&controller.IngressReconciler{
145-
Client: mgr.GetClient(),
146-
Scheme: mgr.GetScheme(),
159+
Client: mgr.GetClient(),
160+
Scheme: mgr.GetScheme(),
161+
RetryInterval: retryInterval,
162+
DefaultReconcileInterval: defaultReconcileInterval,
147163
}).SetupWithManager(mgr); err != nil {
148164
setupLog.Error(err, "unable to create controller", "controller", "Ingress")
149165
os.Exit(1)
150166
}
151167
if err = (&controller.DNSRecordReconciler{
152-
Client: mgr.GetClient(),
153-
Scheme: mgr.GetScheme(),
154-
Cf: &cf,
168+
Client: mgr.GetClient(),
169+
Scheme: mgr.GetScheme(),
170+
CloudflareAPI: &cloudflareAPI,
171+
RetryInterval: retryInterval,
155172
}).SetupWithManager(ctx, mgr); err != nil {
156173
setupLog.Error(err, "unable to create controller", "controller", "DNSRecord")
157174
os.Exit(1)

internal/controller/account_controller.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ import (
4545
type AccountReconciler struct {
4646
client.Client
4747
Scheme *runtime.Scheme
48-
Cf *cloudflare.API
48+
49+
RetryInterval time.Duration
50+
51+
CloudflareAPI *cloudflare.API
4952
}
5053

5154
// SetupWithManager sets up the controller with the Manager.
@@ -103,22 +106,22 @@ func (r *AccountReconciler) reconcileAccount(ctx context.Context, account *cloud
103106
secret := &corev1.Secret{}
104107
if err := r.Get(ctx, client.ObjectKey{Namespace: account.Spec.ApiToken.SecretRef.Namespace, Name: account.Spec.ApiToken.SecretRef.Name}, secret); err != nil {
105108
intconditions.MarkFalse(account, err)
106-
return ctrl.Result{RequeueAfter: time.Second * 30}
109+
return ctrl.Result{RequeueAfter: r.RetryInterval}
107110
}
108111

109-
cfApiToken := string(secret.Data["apiToken"])
110-
if cfApiToken == "" {
112+
cloudflareAPIToken := string(secret.Data["apiToken"])
113+
if cloudflareAPIToken == "" {
111114
intconditions.MarkFalse(account, errors.New("Secret has no key named \"apiToken\""))
112-
return ctrl.Result{RequeueAfter: time.Second * 30}
115+
return ctrl.Result{RequeueAfter: r.RetryInterval}
113116
}
114117

115-
if r.Cf.APIToken != cfApiToken {
116-
cf, err := cloudflare.NewWithAPIToken(cfApiToken)
118+
if r.CloudflareAPI.APIToken != cloudflareAPIToken {
119+
cloudflareAPI, err := cloudflare.NewWithAPIToken(cloudflareAPIToken)
117120
if err != nil {
118121
intconditions.MarkFalse(account, err)
119122
return ctrl.Result{RequeueAfter: time.Second * 30}
120123
}
121-
*r.Cf = *cf
124+
*r.CloudflareAPI = *cloudflareAPI
122125
}
123126

124127
intconditions.MarkTrue(account, "Account is ready")

internal/controller/account_controller_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func NewTestScheme() *runtime.Scheme {
4444
return s
4545
}
4646

47-
var cf cloudflare.API
47+
var cloudflareAPI cloudflare.API
4848

4949
func TestAccountReconciler_reconcileAccount(t *testing.T) {
5050
t.Run("reconcile account", func(t *testing.T) {
@@ -79,7 +79,7 @@ func TestAccountReconciler_reconcileAccount(t *testing.T) {
7979
WithScheme(NewTestScheme()).
8080
WithObjects(secret, account).
8181
Build(),
82-
Cf: &cf,
82+
CloudflareAPI: &cloudflareAPI,
8383
}
8484

8585
_ = r.reconcileAccount(context.TODO(), account)
@@ -88,7 +88,7 @@ func TestAccountReconciler_reconcileAccount(t *testing.T) {
8888
*conditions.TrueCondition(cloudflareoperatoriov1.ConditionTypeReady, cloudflareoperatoriov1.ConditionReasonReady, "Account is ready"),
8989
}))
9090

91-
g.Expect(cf.APIToken).To(Equal(string(secret.Data["apiToken"])))
91+
g.Expect(cloudflareAPI.APIToken).To(Equal(string(secret.Data["apiToken"])))
9292
})
9393

9494
t.Run("econcile account error secret not found", func(t *testing.T) {
@@ -113,7 +113,7 @@ func TestAccountReconciler_reconcileAccount(t *testing.T) {
113113
WithScheme(NewTestScheme()).
114114
WithObjects(account).
115115
Build(),
116-
Cf: &cf,
116+
CloudflareAPI: &cloudflareAPI,
117117
}
118118

119119
_ = r.reconcileAccount(context.TODO(), account)
@@ -155,7 +155,7 @@ func TestAccountReconciler_reconcileAccount(t *testing.T) {
155155
WithScheme(NewTestScheme()).
156156
WithObjects(secret, account).
157157
Build(),
158-
Cf: &cf,
158+
CloudflareAPI: &cloudflareAPI,
159159
}
160160

161161
_ = r.reconcileAccount(context.TODO(), account)

internal/controller/dnsrecord_controller.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ import (
5050
type DNSRecordReconciler struct {
5151
client.Client
5252
Scheme *runtime.Scheme
53-
Cf *cloudflare.API
53+
54+
RetryInterval time.Duration
55+
56+
CloudflareAPI *cloudflare.API
5457
}
5558

5659
// SetupWithManager sets up the controller with the Manager.
@@ -122,7 +125,7 @@ func (r *DNSRecordReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
122125
zones := &cloudflareoperatoriov1.ZoneList{}
123126
if err := r.List(ctx, zones, client.MatchingFields{cloudflareoperatoriov1.ZoneNameIndexKey: zoneName}); err != nil {
124127
log.Error(err, "Failed to list zones")
125-
return ctrl.Result{RequeueAfter: time.Second * 30}, nil
128+
return ctrl.Result{RequeueAfter: r.RetryInterval}, nil
126129
}
127130

128131
if len(zones.Items) == 0 {
@@ -150,26 +153,26 @@ func (r *DNSRecordReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
150153

151154
// reconcileDNSRecord reconciles the dnsrecord
152155
func (r *DNSRecordReconciler) reconcileDNSRecord(ctx context.Context, dnsrecord *cloudflareoperatoriov1.DNSRecord, zone *cloudflareoperatoriov1.Zone) ctrl.Result {
153-
if r.Cf.APIToken == "" {
156+
if r.CloudflareAPI.APIToken == "" {
154157
intconditions.MarkUnknown(dnsrecord, "Cloudflare account is not ready")
155-
return ctrl.Result{RequeueAfter: time.Second * 5}
158+
return ctrl.Result{RequeueAfter: r.RetryInterval}
156159
}
157160

158161
if !conditions.IsTrue(zone, cloudflareoperatoriov1.ConditionTypeReady) {
159162
intconditions.MarkUnknown(dnsrecord, "Zone is not ready")
160-
return ctrl.Result{RequeueAfter: time.Second * 5}
163+
return ctrl.Result{RequeueAfter: r.RetryInterval}
161164
}
162165

163166
var existingRecord cloudflare.DNSRecord
164167
if dnsrecord.Status.RecordID != "" {
165168
var err error
166-
existingRecord, err = r.Cf.GetDNSRecord(ctx, cloudflare.ZoneIdentifier(zone.Status.ID), dnsrecord.Status.RecordID)
169+
existingRecord, err = r.CloudflareAPI.GetDNSRecord(ctx, cloudflare.ZoneIdentifier(zone.Status.ID), dnsrecord.Status.RecordID)
167170
if err != nil {
168171
intconditions.MarkFalse(dnsrecord, err)
169172
return ctrl.Result{}
170173
}
171174
} else {
172-
cfExistingRecords, _, err := r.Cf.ListDNSRecords(ctx, cloudflare.ZoneIdentifier(zone.Status.ID), cloudflare.ListDNSRecordsParams{
175+
cloudflareExistingRecord, _, err := r.CloudflareAPI.ListDNSRecords(ctx, cloudflare.ZoneIdentifier(zone.Status.ID), cloudflare.ListDNSRecordsParams{
173176
Type: dnsrecord.Spec.Type,
174177
Name: dnsrecord.Spec.Name,
175178
Content: dnsrecord.Spec.Content,
@@ -178,8 +181,8 @@ func (r *DNSRecordReconciler) reconcileDNSRecord(ctx context.Context, dnsrecord
178181
intconditions.MarkFalse(dnsrecord, err)
179182
return ctrl.Result{}
180183
}
181-
if len(cfExistingRecords) > 0 {
182-
existingRecord = cfExistingRecords[0]
184+
if len(cloudflareExistingRecord) > 0 {
185+
existingRecord = cloudflareExistingRecord[0]
183186
}
184187
dnsrecord.Status.RecordID = existingRecord.ID
185188
}
@@ -188,7 +191,7 @@ func (r *DNSRecordReconciler) reconcileDNSRecord(ctx context.Context, dnsrecord
188191
ip := &cloudflareoperatoriov1.IP{}
189192
if err := r.Get(ctx, client.ObjectKey{Name: dnsrecord.Spec.IPRef.Name}, ip); err != nil {
190193
intconditions.MarkFalse(dnsrecord, err)
191-
return ctrl.Result{RequeueAfter: time.Second * 30}
194+
return ctrl.Result{RequeueAfter: r.RetryInterval}
192195
}
193196
dnsrecord.Spec.Content = ip.Spec.Address
194197
}
@@ -199,7 +202,7 @@ func (r *DNSRecordReconciler) reconcileDNSRecord(ctx context.Context, dnsrecord
199202
}
200203

201204
if existingRecord.ID == "" {
202-
newDNSRecord, err := r.Cf.CreateDNSRecord(ctx, cloudflare.ZoneIdentifier(zone.Status.ID), cloudflare.CreateDNSRecordParams{
205+
newDNSRecord, err := r.CloudflareAPI.CreateDNSRecord(ctx, cloudflare.ZoneIdentifier(zone.Status.ID), cloudflare.CreateDNSRecordParams{
203206
Name: dnsrecord.Spec.Name,
204207
Type: dnsrecord.Spec.Type,
205208
Content: dnsrecord.Spec.Content,
@@ -210,11 +213,11 @@ func (r *DNSRecordReconciler) reconcileDNSRecord(ctx context.Context, dnsrecord
210213
})
211214
if err != nil {
212215
intconditions.MarkFalse(dnsrecord, err)
213-
return ctrl.Result{RequeueAfter: time.Second * 30}
216+
return ctrl.Result{RequeueAfter: r.RetryInterval}
214217
}
215218
dnsrecord.Status.RecordID = newDNSRecord.ID
216219
} else if !r.compareDNSRecord(dnsrecord.Spec, existingRecord) {
217-
if _, err := r.Cf.UpdateDNSRecord(ctx, cloudflare.ZoneIdentifier(zone.Status.ID), cloudflare.UpdateDNSRecordParams{
220+
if _, err := r.CloudflareAPI.UpdateDNSRecord(ctx, cloudflare.ZoneIdentifier(zone.Status.ID), cloudflare.UpdateDNSRecordParams{
218221
ID: dnsrecord.Status.RecordID,
219222
Name: dnsrecord.Spec.Name,
220223
Type: dnsrecord.Spec.Type,
@@ -225,7 +228,7 @@ func (r *DNSRecordReconciler) reconcileDNSRecord(ctx context.Context, dnsrecord
225228
Data: dnsrecord.Spec.Data,
226229
}); err != nil {
227230
intconditions.MarkFalse(dnsrecord, err)
228-
return ctrl.Result{RequeueAfter: time.Second * 30}
231+
return ctrl.Result{RequeueAfter: r.RetryInterval}
229232
}
230233
}
231234

@@ -319,7 +322,7 @@ func (r *DNSRecordReconciler) requestsForIPChange(ctx context.Context, o client.
319322

320323
// reconcileDelete reconciles the deletion of the dnsrecord
321324
func (r *DNSRecordReconciler) reconcileDelete(ctx context.Context, zoneID string, dnsrecord *cloudflareoperatoriov1.DNSRecord) error {
322-
if err := r.Cf.DeleteDNSRecord(ctx, cloudflare.ZoneIdentifier(zoneID), dnsrecord.Status.RecordID); err != nil && err.Error() != "Record does not exist. (81044)" && dnsrecord.Status.RecordID != "" {
325+
if err := r.CloudflareAPI.DeleteDNSRecord(ctx, cloudflare.ZoneIdentifier(zoneID), dnsrecord.Status.RecordID); err != nil && err.Error() != "Record does not exist. (81044)" && dnsrecord.Status.RecordID != "" {
323326
return err
324327
}
325328
metrics.DnsRecordFailureCounter.DeleteLabelValues(dnsrecord.Namespace, dnsrecord.Name, dnsrecord.Spec.Name)

0 commit comments

Comments
 (0)