Skip to content

Commit 5ee620d

Browse files
committed
Unconditional Grafana service accoutn update
1 parent ceaca80 commit 5ee620d

File tree

1 file changed

+18
-61
lines changed

1 file changed

+18
-61
lines changed

controllers/serviceaccount_controller.go

Lines changed: 18 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -283,23 +283,6 @@ func (r *GrafanaServiceAccountReconciler) reconcileWithInstance(
283283
return fmt.Errorf("upserting service account: %w", err)
284284
}
285285

286-
updateForm := buildUpdateFormIfNeeded(&cr.Spec, cr.Status.Account)
287-
if updateForm != nil {
288-
update, err := gClient.ServiceAccounts.UpdateServiceAccount(
289-
service_accounts.
290-
NewUpdateServiceAccountParamsWithContext(ctx).
291-
WithServiceAccountID(cr.Status.Account.ID).
292-
WithBody(updateForm),
293-
)
294-
if err != nil {
295-
return fmt.Errorf("updating service account: %w", err)
296-
}
297-
298-
cr.Status.Account.IsDisabled = update.Payload.Serviceaccount.IsDisabled
299-
cr.Status.Account.Role = update.Payload.Serviceaccount.Role
300-
cr.Status.Account.Name = update.Payload.Serviceaccount.Name
301-
}
302-
303286
// Ensure tokens are always sorted for stable ordering
304287
defer func() {
305288
sort.Slice(cr.Status.Account.Tokens, func(i, j int) bool {
@@ -351,37 +334,6 @@ func convertGrafanaExpiration(expiration strfmt.DateTime) *metav1.Time {
351334
return ptr.To(metav1.NewTime(time.Time(expiration)))
352335
}
353336

354-
// buildUpdateFormIfNeeded compares spec with current status and builds an update form if changes are needed.
355-
// Returns nil if the account is already in the desired state.
356-
func buildUpdateFormIfNeeded(spec *v1beta1.GrafanaServiceAccountSpec, status *v1beta1.GrafanaServiceAccountInfo) *models.UpdateServiceAccountForm {
357-
form := &models.UpdateServiceAccountForm{
358-
// The form contains a ServiceAccountID field which is unused in Grafana, so it's ignored here.
359-
}
360-
361-
hasChanges := false
362-
363-
if status.Name != spec.Name {
364-
hasChanges = true
365-
form.Name = spec.Name
366-
}
367-
368-
if status.Role != spec.Role {
369-
hasChanges = true
370-
form.Role = spec.Role
371-
}
372-
373-
if status.IsDisabled != spec.IsDisabled {
374-
hasChanges = true
375-
form.IsDisabled = ptr.To(spec.IsDisabled)
376-
}
377-
378-
if !hasChanges {
379-
return nil
380-
}
381-
382-
return form
383-
}
384-
385337
// removeOutdatedTokens removes tokens that are not in the desired spec or have mismatched expiration times.
386338
// These tokens will be recreated later with the correct configuration.
387339
func (r *GrafanaServiceAccountReconciler) removeOutdatedTokens(
@@ -646,18 +598,24 @@ func (r *GrafanaServiceAccountReconciler) upsertAccount(
646598
cr *v1beta1.GrafanaServiceAccount,
647599
) error {
648600
if cr.Status.Account != nil {
649-
retrieve, err := gClient.ServiceAccounts.RetrieveServiceAccountWithParams(
601+
update, err := gClient.ServiceAccounts.UpdateServiceAccount(
650602
service_accounts.
651-
NewRetrieveServiceAccountParamsWithContext(ctx).
652-
WithServiceAccountID(cr.Status.Account.ID),
603+
NewUpdateServiceAccountParamsWithContext(ctx).
604+
WithServiceAccountID(cr.Status.Account.ID).
605+
WithBody(&models.UpdateServiceAccountForm{
606+
// The form contains a ServiceAccountID field which is unused in Grafana, so it's ignored here.
607+
Name: cr.Spec.Name,
608+
Role: cr.Spec.Role,
609+
IsDisabled: ptr.To(cr.Spec.IsDisabled),
610+
}),
653611
)
654612
if err == nil {
655613
cr.Status.Account = &v1beta1.GrafanaServiceAccountInfo{
656-
ID: retrieve.Payload.ID,
657-
Role: retrieve.Payload.Role,
658-
IsDisabled: retrieve.Payload.IsDisabled,
659-
Name: retrieve.Payload.Name,
660-
Login: retrieve.Payload.Login,
614+
ID: update.Payload.Serviceaccount.ID,
615+
Role: update.Payload.Serviceaccount.Role,
616+
IsDisabled: update.Payload.Serviceaccount.IsDisabled,
617+
Name: update.Payload.Serviceaccount.Name,
618+
Login: update.Payload.Serviceaccount.Login,
661619
}
662620

663621
// Load existing tokens from Grafana
@@ -684,14 +642,13 @@ func (r *GrafanaServiceAccountReconciler) upsertAccount(
684642
return nil
685643
}
686644

687-
// ATM, service_accounts.RetrieveServiceAccountNotFound doesn't have Is, Unwrap, Unwrap.
645+
// ATM, service_accounts.UpdateServiceAccountNotFound doesn't have Is, Unwrap, Unwrap.
688646
// So, we cannot rely only on errors.Is().
689-
_, notFound := err.(*service_accounts.RetrieveServiceAccountNotFound) // nolint:errorlint
690-
if !notFound && !errors.Is(err, service_accounts.NewRetrieveServiceAccountNotFound()) {
691-
return fmt.Errorf("retrieving service account by ID %d: %w", cr.Status.Account.ID, err)
647+
_, ok := err.(*service_accounts.UpdateServiceAccountNotFound) // nolint:errorlint
648+
if !ok && !errors.Is(err, service_accounts.NewUpdateServiceAccountNotFound()) {
649+
return fmt.Errorf("updating service account: %w", err)
692650
}
693651

694-
// Service account not found, clear the status and create a new one
695652
cr.Status.Account = nil
696653
}
697654

0 commit comments

Comments
 (0)