Skip to content

chore(controllers): error handling and metrics regestring improvements - #259

Merged
isindir merged 7 commits into
masterfrom
01/improve
Nov 1, 2026
Merged

isindir merged 7 commits into
masterfrom
01/improve

Conversation

@isindir

@isindir isindir commented Oct 20, 2026

Copy link
Copy Markdown
Owner

No description provided.

Three corrections identified in the 100 Go Mistakes audit:

1. Error wrapping (mistake #49): cloneTemplateData used %v to format
   the base64 decode error, discarding the original error from the
   chain. Changed to %w so callers can inspect the root cause via
   errors.Is / errors.As / errors.Unwrap.

2. Map pre-sizing (mistake #21): garbageCollectOrphanedSecrets
   allocated make(map[string]bool) with no capacity hint on every
   reconciliation. Changed to make(map[string]bool,
   len(sopsSecret.Spec.SecretsTemplate)) to avoid repeated rehashing
   for secrets with many templates.

3. UpdateSopsSecretStatus doc comment + rationale: added a Go doc
   comment explaining the function's purpose and an inline comment on
   the intentionally ignored status-update error, making the design
   decision explicit for future contributors.

Also adds:
- TestCloneTemplateDataErrorWrapping unit test (TDD red→green for fix 1)
- .scratch/ to .gitignore (local ticket scratch directory)
#48)

## What

Two logging correctness fixes in sopssecret_controller.go:

1. garbageCollectOrphanedSecrets: move the "Garbage collected an orphaned
   secret" info log into the else{} branch of the Delete error check so it
   only fires when Delete returns nil. Previously it fired unconditionally,
   meaning a failed delete would still log success.

2. getSecretFromClusterOrCreateFromTemplate: change r.Log.Error → r.Log.V(0).Info
   on the errors.IsNotFound path. Secret-not-found is the normal creation
   path, not a failure; logging it at error level causes false alerts.
   Message updated from "Creating a new Secret" to "Secret not found, creating"
   to be accurate about what triggered the log.

## Tests

- TestIsNotFoundDoesNotLogError: spyLogSink verifies no r.Log.Error call on
  the IsNotFound path.
- TestGarbageCollectSuccessLogOnlyOnSuccess: two sub-tests verify success log
  fires only when deleteErr == nil.
- Full envtest integration suite passes (8 Ginkgo specs).

Addresses Go Mistakes #48 (error vs non-error log levels).
…stakes #62)

## What

Replace the package-level init() in custom_metrics.go with an explicit
RegisterMetrics(registry prometheus.Registerer) function, wired into
SetupWithManager. This removes the global side-effect from package import,
making the controllers package safe to import in unit tests without touching
the global Prometheus registry.

Changes:
- custom_metrics.go: remove func init(); add exported RegisterMetrics that
  accepts any prometheus.Registerer and calls MustRegister on the three
  existing counters.
- sopssecret_controller.go: import ctrlmetrics and call
  RegisterMetrics(ctrlmetrics.Registry) in SetupWithManager before returning,
  preserving existing registration behaviour in production.
- enforce_ownership_test.go: add TestRegisterMetrics that calls
  RegisterMetrics(prometheus.NewRegistry()) and asserts no panic, using a
  fresh registry to avoid double-registration against the global one.

Note: mgr.GetMetricsServer().GetRegistry() does not exist in the vendored
controller-runtime version; ctrlmetrics.Registry is the canonical pattern
used by all controller-runtime internal packages (workqueue, certwatcher,
webhook).

Addresses Go Mistakes #62 (init() side-effects / dependency injection).
@isindir
isindir merged commit 8468005 into master Nov 1, 2026
5 checks passed
@isindir
isindir deleted the 01/improve branch November 1, 2026 09:39

This branch was previously deployed

1 inactive deployment
ci-cd — 5e7af8a9 Deployed Oct 31, 2026 by isindir via CI #172
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant