Skip to content
This repository was archived by the owner on Apr 19, 2025. It is now read-only.

Commit 93d0d02

Browse files
committed
chore(account): Clean up API key lookup func
1 parent eb394c1 commit 93d0d02

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

.golangci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ issues:
1616
linters:
1717
- dupl
1818
- lll
19+
20+
linters-settings:
21+
gocyclo:
22+
min-complexity: 35
23+
1924
linters:
2025
disable-all: true
2126
enable:

internal/controller/account_controller.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (r *AccountReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
6262
return ctrl.Result{}, client.IgnoreNotFound(err)
6363
}
6464

65-
account, apiKey, err := GetApiKey(ctx, r.Client, req.Name)
65+
apiKey, err := GetApiKey(ctx, r.Client, account)
6666
if err != nil {
6767
return ctrl.Result{}, err
6868
}
@@ -132,25 +132,20 @@ func GetAccount(ctx context.Context, c client.Client, account *uptimerobotv1.Acc
132132
return nil
133133
}
134134

135-
func GetApiKey(ctx context.Context, c client.Client, name string) (*uptimerobotv1.Account, string, error) {
136-
account := &uptimerobotv1.Account{}
137-
if err := GetAccount(ctx, c, account, name); err != nil {
138-
return account, "", err
139-
}
140-
135+
func GetApiKey(ctx context.Context, c client.Client, account *uptimerobotv1.Account) (string, error) {
141136
secret := &corev1.Secret{}
142137
err := c.Get(ctx, client.ObjectKey{
143138
Namespace: ClusterResourceNamespace,
144139
Name: account.Spec.ApiKeySecretRef.Name,
145140
}, secret)
146141
if err != nil {
147-
return account, "", err
142+
return "", err
148143
}
149144

150145
apiKey, ok := secret.Data[account.Spec.ApiKeySecretRef.Key]
151146
if !ok {
152-
return account, "", fmt.Errorf("%w: %s", ErrKeyNotFound, account.Spec.ApiKeySecretRef.Key)
147+
return "", fmt.Errorf("%w: %s", ErrKeyNotFound, account.Spec.ApiKeySecretRef.Key)
153148
}
154149

155-
return account, string(apiKey), nil
150+
return string(apiKey), nil
156151
}

internal/controller/contact_controller.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ func (r *ContactReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
5555
return ctrl.Result{}, client.IgnoreNotFound(err)
5656
}
5757

58-
_, apiKey, err := GetApiKey(ctx, r.Client, contact.Spec.Account.Name)
58+
account := &uptimerobotv1.Account{}
59+
if err := GetAccount(ctx, r.Client, account, contact.Spec.Account.Name); err != nil {
60+
return ctrl.Result{}, err
61+
}
62+
63+
apiKey, err := GetApiKey(ctx, r.Client, account)
5964
if err != nil {
6065
return ctrl.Result{}, err
6166
}

internal/controller/monitor_controller.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ func (r *MonitorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
6464
return ctrl.Result{}, client.IgnoreNotFound(err)
6565
}
6666

67-
_, apiKey, err := GetApiKey(ctx, r.Client, monitor.Spec.Account.Name)
67+
account := &uptimerobotv1.Account{}
68+
if err := GetAccount(ctx, r.Client, account, monitor.Spec.Account.Name); err != nil {
69+
return ctrl.Result{}, err
70+
}
71+
72+
apiKey, err := GetApiKey(ctx, r.Client, account)
6873
if err != nil {
6974
return ctrl.Result{}, err
7075
}

0 commit comments

Comments
 (0)