Skip to content

Commit

Permalink
chore(account): Clean up API key lookup func
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Apr 9, 2024
1 parent eb394c1 commit 93d0d02
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ issues:
linters:
- dupl
- lll

linters-settings:
gocyclo:
min-complexity: 35

linters:
disable-all: true
enable:
Expand Down
15 changes: 5 additions & 10 deletions internal/controller/account_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (r *AccountReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{}, client.IgnoreNotFound(err)
}

account, apiKey, err := GetApiKey(ctx, r.Client, req.Name)
apiKey, err := GetApiKey(ctx, r.Client, account)
if err != nil {
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -132,25 +132,20 @@ func GetAccount(ctx context.Context, c client.Client, account *uptimerobotv1.Acc
return nil
}

func GetApiKey(ctx context.Context, c client.Client, name string) (*uptimerobotv1.Account, string, error) {
account := &uptimerobotv1.Account{}
if err := GetAccount(ctx, c, account, name); err != nil {
return account, "", err
}

func GetApiKey(ctx context.Context, c client.Client, account *uptimerobotv1.Account) (string, error) {
secret := &corev1.Secret{}
err := c.Get(ctx, client.ObjectKey{
Namespace: ClusterResourceNamespace,
Name: account.Spec.ApiKeySecretRef.Name,
}, secret)
if err != nil {
return account, "", err
return "", err
}

apiKey, ok := secret.Data[account.Spec.ApiKeySecretRef.Key]
if !ok {
return account, "", fmt.Errorf("%w: %s", ErrKeyNotFound, account.Spec.ApiKeySecretRef.Key)
return "", fmt.Errorf("%w: %s", ErrKeyNotFound, account.Spec.ApiKeySecretRef.Key)
}

return account, string(apiKey), nil
return string(apiKey), nil
}
7 changes: 6 additions & 1 deletion internal/controller/contact_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ func (r *ContactReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{}, client.IgnoreNotFound(err)
}

_, apiKey, err := GetApiKey(ctx, r.Client, contact.Spec.Account.Name)
account := &uptimerobotv1.Account{}
if err := GetAccount(ctx, r.Client, account, contact.Spec.Account.Name); err != nil {
return ctrl.Result{}, err
}

apiKey, err := GetApiKey(ctx, r.Client, account)
if err != nil {
return ctrl.Result{}, err
}
Expand Down
7 changes: 6 additions & 1 deletion internal/controller/monitor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ func (r *MonitorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return ctrl.Result{}, client.IgnoreNotFound(err)
}

_, apiKey, err := GetApiKey(ctx, r.Client, monitor.Spec.Account.Name)
account := &uptimerobotv1.Account{}
if err := GetAccount(ctx, r.Client, account, monitor.Spec.Account.Name); err != nil {
return ctrl.Result{}, err
}

apiKey, err := GetApiKey(ctx, r.Client, account)
if err != nil {
return ctrl.Result{}, err
}
Expand Down

0 comments on commit 93d0d02

Please sign in to comment.