diff --git a/internal/backupcontroller/default_objects_gate.go b/internal/backupcontroller/default_objects_gate.go index efcd970d5..d0ed29954 100644 --- a/internal/backupcontroller/default_objects_gate.go +++ b/internal/backupcontroller/default_objects_gate.go @@ -51,15 +51,16 @@ var defaultObjectsMissing = prometheus.NewGaugeVec( ) // defaultObjectsForceReconciles counts the forced Helm upgrades the gate -// issued to materialise those objects. A counter that keeps climbing means -// the forced render is not producing the objects (e.g. a CRD is missing), +// issued to materialise those objects or to refresh their S3 endpoint. A +// counter that keeps climbing means the forced render is not producing the +// objects (e.g. a CRD is missing) or not changing their endpoint, // which is a different failure than the install-time race this gate closes. // A suspended release is NOT counted here: it is skipped before the patch, // so a paused release cannot masquerade as a render that keeps failing. var defaultObjectsForceReconciles = prometheus.NewCounterVec( prometheus.CounterOpts{ Name: "cozystack_backup_default_objects_force_reconciles_total", - Help: "Number of forced HelmRelease reconciles issued to create missing platform backup objects.", + Help: "Number of forced HelmRelease reconciles issued to create missing platform backup objects or refresh their S3 endpoint.", }, []string{"namespace", "name"}, ) @@ -125,8 +126,9 @@ var backupStorageLocationGVR = schema.GroupVersionResource{ // is resolvable — read from the same platform credentials Secret the // projector already consumes, so no new dependency and no `lookup` — it // checks that every object the platform BackupClass routes to actually -// exists, and forces one Helm upgrade when any is missing. A forced -// upgrade re-runs the lookups, so the gated templates materialise. +// exists, and forces one Helm upgrade when any is missing or carries a stale +// S3 endpoint. A forced upgrade re-runs the lookups, so the gated templates +// materialise with the current endpoint. // // It deliberately does not create the objects itself: their bodies are // values-driven (endpoint, region, forcePathStyle, the Altinity strategy's @@ -317,8 +319,9 @@ func (g *DefaultObjectsGate) checkAndLog(ctx context.Context, logger logr.Logger } // forcedReleaseFor reports which release renders the given missing set, for -// logging only. reconcileCredentials returns the credentials Secret as the -// sole missing object, and it is never mixed with the routed objects — +// logging only. reconcileCredentials and staleCredentials return the +// credentials Secret as the sole entry, and it is never mixed with the routed +// objects — // resolving the bucket name is a precondition for checking those at all. func (g *DefaultObjectsGate) forcedReleaseFor(missing []string) types.NamespacedName { credsKey := fmt.Sprintf("Secret/%s", g.Config.SourceSecretName) @@ -329,9 +332,10 @@ func (g *DefaultObjectsGate) forcedReleaseFor(missing []string) types.Namespaced } // Check resolves the platform bucket name, verifies that every object the -// BackupClass routes to exists, and forces one Helm upgrade when any is -// missing. It returns the missing objects (formatted for logs) and whether -// a forced upgrade was issued on this call. +// BackupClass routes to exists and carries the current S3 endpoint, and +// forces one Helm upgrade when any is missing or stale. It returns those +// objects (formatted for logs) and whether a forced upgrade was issued on this +// call. // // While the source Secret is absent or carries no bucket name, the gate // cannot evaluate the routed objects — their templates gate on the same @@ -412,11 +416,16 @@ func (g *DefaultObjectsGate) Check(ctx context.Context) ([]string, bool, error) // its endpoint differs from the one in the COSI Secret it is rendered from, // comparing against what packages/system/bucket/templates/user-credentials.yaml // writes for it. The COSI Secret being absent (the BucketAccess is being -// re-granted) leaves nothing to compare against and is not staleness. +// re-granted) leaves nothing to compare against and is not staleness, and +// neither is a source Secret the bucket chart does not render: the COSI Secret +// itself (the projector accepts one) or one without the endpoint key. func (g *DefaultObjectsGate) staleCredentials(ctx context.Context, src *corev1.Secret) (string, error) { if g.CredentialsCOSISecretName == "" || g.CredentialsHelmRelease.Name == "" || g.CredentialsHelmRelease.Namespace == "" { return "", nil } + if _, ok := src.Data["endpoint"]; !ok || g.CredentialsCOSISecretName == g.Config.SourceSecretName { + return "", nil + } cosi := &corev1.Secret{} key := types.NamespacedName{Namespace: g.Config.SourceNamespace, Name: g.CredentialsCOSISecretName} if err := g.Client.Get(ctx, key, cosi); err != nil { @@ -446,13 +455,13 @@ func (g *DefaultObjectsGate) staleCredentials(ctx context.Context, src *corev1.S // renderedEndpointFor mirrors the "backupstrategy-controller.endpoint" helper // for a provisioned bucket: the credentials Secret's endpoint, trimmed, with // any scheme replaced by https://. It is the value the chart stamps as -// endpointAnnotation. An empty endpoint makes the helper fall back to -// backupStorage.endpoint, which the gate cannot see, so it returns "". +// endpointAnnotation. An absent or empty endpoint makes the helper fall back +// to backupStorage.endpoint, which the gate cannot see, so it returns "". func renderedEndpointFor(raw []byte) string { - host := strings.TrimSpace(string(raw)) - if host == "" { + if len(raw) == 0 { return "" } + host := strings.TrimSpace(string(raw)) return "https://" + strings.TrimPrefix(strings.TrimPrefix(host, "https://"), "http://") } @@ -511,7 +520,7 @@ func (g *DefaultObjectsGate) force(ctx context.Context, target types.NamespacedN // annotations would be ignored, so skip the patch entirely and // leave the force counter alone — a climbing counter must keep // meaning "the forced render is not producing the objects". - log.FromContext(ctx).Info("skipped forcing a suspended HelmRelease; missing default backup objects will not be repaired until it is resumed", + log.FromContext(ctx).Info("skipped forcing a suspended HelmRelease; missing or stale default backup objects will not be repaired until it is resumed", "helmRelease", target.String(), "missing", missing) *last = now return missing, false, nil diff --git a/internal/backupcontroller/default_objects_gate_test.go b/internal/backupcontroller/default_objects_gate_test.go index dc1c0c9f0..cff6187cd 100644 --- a/internal/backupcontroller/default_objects_gate_test.go +++ b/internal/backupcontroller/default_objects_gate_test.go @@ -1099,6 +1099,9 @@ func TestRenderedEndpointMatchesTheChart(t *testing.T) { // No endpoint in the Secret: the chart renders its values fallback, // which the gate cannot reproduce, so it compares nothing. {"", ""}, + // Present but blank: the helper's lookup sees a non-empty value and + // renders the bare scheme. + {"\n", "https://"}, } { if got := renderedEndpointFor([]byte(tc.in)); got != tc.want { t.Errorf("renderedEndpointFor(%q) = %q, want %q", tc.in, got, tc.want) @@ -1125,3 +1128,49 @@ func TestForcedReleaseForNamesTheStaleCredentialsRelease(t *testing.T) { } } } + +// TestCheckSkipsCredentialsComparisonWithoutDerivedSecret covers a +// systemSecretName the bucket chart does not render: the same name as the COSI +// Secret (the projector also reads a raw COSI Secret) or a Secret without the +// endpoint key. Comparing it would report it stale on every tick, or fail the +// check, and the missing objects would never be repaired. +func TestCheckSkipsCredentialsComparisonWithoutDerivedSecret(t *testing.T) { + for _, tc := range []struct { + name string + apply func(g *DefaultObjectsGate, src *corev1.Secret) + }{ + {"COSI Secret is the source", func(g *DefaultObjectsGate, src *corev1.Secret) { + g.CredentialsCOSISecretName = g.Config.SourceSecretName + src.Data["BucketInfo"] = cosiSecret("https://s3.new.example.com").Data["BucketInfo"] + }}, + {"source without endpoint", func(_ *DefaultObjectsGate, src *corev1.Secret) { + delete(src.Data, "endpoint") + }}, + } { + t.Run(tc.name, func(t *testing.T) { + src := sourceSecret("bucket-1a2b") + var objs []runtime.Object + for _, o := range allDefaultObjects() { + if u := o.(*unstructured.Unstructured); u.GetName() != "cozy-default-cnpg" { + objs = append(objs, u) + } + } + objs = append(objs, helmReleaseObject(), credentialsHelmReleaseObject()) + g, dyn := newGate(t, nil, objs...) + tc.apply(g, src) + g.Client = fake.NewClientBuilder().WithScheme(gateScheme()). + WithObjects(src, cosiSecret("https://s3.new.example.com"), cozyDefaultBackupClass()).Build() + + missing, forced, err := g.Check(context.Background()) + if err != nil { + t.Fatalf("Check: %v", err) + } + if !forced || len(missing) != 1 || missing[0] != "CNPG/cozy-default-cnpg" { + t.Fatalf("missing = %v, forced = %v; want the missing strategy repaired", missing, forced) + } + if ann := credentialsForceAnnotations(t, dyn); len(ann) != 0 { + t.Errorf("bucket release forced: %v", ann) + } + }) + } +} diff --git a/packages/system/backupstrategy-controller/templates/deployment.yaml b/packages/system/backupstrategy-controller/templates/deployment.yaml index adc09fa2a..1278bec8a 100644 --- a/packages/system/backupstrategy-controller/templates/deployment.yaml +++ b/packages/system/backupstrategy-controller/templates/deployment.yaml @@ -92,9 +92,11 @@ spec: # "-credentials" to its name). The COSI sidecar writes it once per # BucketAccess, so the gate compares the two endpoints and forces the # release when the credentials Secret lags behind. + {{- if hasSuffix "-credentials" .Values.backupStorage.systemSecretName }} - name: BACKUP_CREDENTIALS_COSI_SECRET_NAME value: {{ trimSuffix "-credentials" .Values.backupStorage.systemSecretName | quote }} {{- end }} + {{- end }} {{- if .Values.velero.bslEnabled }} - name: BACKUP_DEFAULT_OBJECTS_VELERO_NAMESPACE value: {{ .Values.velero.namespace | quote }} diff --git a/packages/system/backupstrategy-controller/tests/default_objects_gate_test.yaml b/packages/system/backupstrategy-controller/tests/default_objects_gate_test.yaml index ed616ce79..446bbdcfa 100644 --- a/packages/system/backupstrategy-controller/tests/default_objects_gate_test.yaml +++ b/packages/system/backupstrategy-controller/tests/default_objects_gate_test.yaml @@ -128,6 +128,19 @@ tests: name: BACKUP_CREDENTIALS_COSI_SECRET_NAME value: bucket-cozy-backups-system + # A systemSecretName that is not -credentials is not rendered by + # the bucket chart from a COSI Secret, so there is nothing to compare it to. + - it: "systemSecretName without the -credentials suffix: no COSI Secret to compare against" + template: templates/deployment.yaml + set: + backupStorage.systemSecretName: bucket-cozy-backups-system + asserts: + - notContains: + path: spec.template.spec.containers[0].env + any: true + content: + name: BACKUP_CREDENTIALS_COSI_SECRET_NAME + - it: "velero.bslEnabled=false: the BSL is not rendered, so the gate must not look for it" template: templates/deployment.yaml set: diff --git a/packages/system/backupstrategy-controller/values.yaml b/packages/system/backupstrategy-controller/values.yaml index cdca60ff8..740672a44 100644 --- a/packages/system/backupstrategy-controller/values.yaml +++ b/packages/system/backupstrategy-controller/values.yaml @@ -208,9 +208,9 @@ backupStorage: # release. The COSI Secret itself keeps the endpoint it was granted with, so # after a publishing.host change that endpoint follows only once the # BucketAccess is re-granted. Both are throttled, independently, and a no-op - # in the steady state. A suspended release is skipped rather than re-stamped forever. The - # gate does NOT create the objects itself: their bodies are values-driven - # and Helm stays their single owner. + # in the steady state. A suspended release is skipped rather than + # re-stamped forever. The gate does NOT create the objects itself: their + # bodies are values-driven and Helm stays their single owner. # # Turn this off only if you manage the default Strategy CRs out of band and # accept that a missed install-time render is never repaired. Watch