mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-05 06:34:12 +09:00
fix(backups): re-render the default backup objects when the S3 endpoint changes
The S3 endpoint the platform backups use is copied twice and neither
copy is refreshed. The COSI sidecar writes BucketInfo once per
BucketAccess, so the credentials Secret rendered from it keeps its
endpoint, and the Velero BSL and the default Strategy CRs keep whatever
endpoint the last render of backupstrategy-controller saw. The default
objects gate only acted on objects that were missing, so after a
publishing.host change every backup kept going to the old host with
nothing reporting it.
The gate now also forces a render when a copy is stale: the bucket's
<bucket>-system release when the credentials Secret's endpoint differs
from the COSI Secret it is rendered from, and backupstrategy-controller
when the endpoint an object was rendered with differs from the
credentials Secret's. That endpoint sits at a different path in every
kind, in some only as a bare host, so the chart stamps the rendered
value on each object as backups.cozystack.io/endpoint and the gate
compares that. Objects rendered before the annotation existed count as
stale until one forced render has gone through, which stamps it.
The COSI Secret itself still holds the endpoint the BucketAccess was
granted with; it follows a host change only once the BucketAccess is
re-granted.
Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
(cherry picked from commit cd91138f33)
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
This commit is contained in:
1 parent
a14ad50b1e
commit
e8fef61fa1
21 files changed
+678
-36
No files matched your search
@@ -2,6 +2,8 @@ package backupcontroller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -18,6 +20,7 @@ import (
|
||||
k8stesting "k8s.io/client-go/testing"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
|
||||
|
||||
backupsv1alpha1 "github.com/cozystack/cozystack/api/backups/v1alpha1"
|
||||
)
|
||||
@@ -110,14 +113,60 @@ func cozyDefaultBackupClass() *backupsv1alpha1.BackupClass {
|
||||
}
|
||||
}
|
||||
|
||||
// renderedEndpoint is what the chart stamps on every default object for the
|
||||
// endpoint sourceSecret carries: the steady state after a render that saw it.
|
||||
const renderedEndpoint = "https://s3.example.com"
|
||||
|
||||
func strategyObject(kind, name string) *unstructured.Unstructured {
|
||||
u := &unstructured.Unstructured{}
|
||||
u.SetAPIVersion(strategyAPIGroup + "/v1alpha1")
|
||||
u.SetKind(kind)
|
||||
u.SetName(name)
|
||||
u.SetAnnotations(map[string]string{endpointAnnotation: renderedEndpoint})
|
||||
return u
|
||||
}
|
||||
|
||||
func withEndpoint(u *unstructured.Unstructured, endpoint string) *unstructured.Unstructured {
|
||||
u.SetAnnotations(map[string]string{endpointAnnotation: endpoint})
|
||||
return u
|
||||
}
|
||||
|
||||
// unannotated is an object rendered by a chart that predates the endpoint
|
||||
// annotation.
|
||||
func unannotated(u *unstructured.Unstructured) *unstructured.Unstructured {
|
||||
u.SetAnnotations(nil)
|
||||
return u
|
||||
}
|
||||
|
||||
// allDefaultObjects is every object cozy-default routes to, plus the BSL, as
|
||||
// the chart renders them for sourceSecret's endpoint.
|
||||
func allDefaultObjects() []runtime.Object {
|
||||
return []runtime.Object{
|
||||
strategyObject("CNPG", "cozy-default-cnpg"),
|
||||
strategyObject("MariaDB", "cozy-default-mariadb"),
|
||||
strategyObject("Etcd", "cozy-default-etcd"),
|
||||
strategyObject("Altinity", "cozy-default-altinity"),
|
||||
strategyObject("Velero", "cozy-default-velero-vminstance"),
|
||||
strategyObject("Velero", "cozy-default-velero-vmdisk"),
|
||||
bslObject(),
|
||||
}
|
||||
}
|
||||
|
||||
// cosiSecret is the Secret the COSI sidecar writes for the platform bucket's
|
||||
// BucketAccess, which user-credentials.yaml renders the credentials Secret
|
||||
// from. The BucketInfo document is one read off a live cluster, with the keys
|
||||
// and the host replaced.
|
||||
func cosiSecret(endpoint string) *corev1.Secret {
|
||||
bucketInfo := `{"metadata": {"name": "bc-b8e30337-1821-418f-a4dd-163e8016aaee", "creationTimestamp": null}, ` +
|
||||
`"spec": {"bucketName": "bucket-1a2b", "authenticationType": "KEY", ` +
|
||||
`"secretS3": {"endpoint": "` + endpoint + `", "region": "", "accessKeyID": "AK", "accessSecretKey": "SK"}, ` +
|
||||
`"secretAzure": null, "protocols": ["s3"]}}`
|
||||
return &corev1.Secret{
|
||||
ObjectMeta: metav1.ObjectMeta{Namespace: "tenant-root", Name: "bucket-cozy-backups-system"},
|
||||
Data: map[string][]byte{"BucketInfo": []byte(bucketInfo)},
|
||||
}
|
||||
}
|
||||
|
||||
func helmReleaseObject() *unstructured.Unstructured {
|
||||
u := &unstructured.Unstructured{}
|
||||
u.SetAPIVersion("helm.toolkit.fluxcd.io/v2")
|
||||
@@ -163,9 +212,10 @@ func newGate(t *testing.T, ctrlObjs []client.Object, dynObjs ...runtime.Object)
|
||||
Namespace: "tenant-root",
|
||||
Name: "bucket-cozy-backups-system",
|
||||
},
|
||||
VeleroNamespace: "cozy-velero",
|
||||
MinForceInterval: 5 * time.Minute,
|
||||
now: func() time.Time { return time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC) },
|
||||
CredentialsCOSISecretName: "bucket-cozy-backups-system",
|
||||
VeleroNamespace: "cozy-velero",
|
||||
MinForceInterval: 5 * time.Minute,
|
||||
now: func() time.Time { return time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC) },
|
||||
}
|
||||
g.Interface = dyn
|
||||
g.RESTMapper = gateRESTMapper()
|
||||
@@ -193,6 +243,7 @@ func bslObject() *unstructured.Unstructured {
|
||||
u.SetKind("BackupStorageLocation")
|
||||
u.SetNamespace("cozy-velero")
|
||||
u.SetName(defaultBackupStorageLocationName)
|
||||
u.SetAnnotations(map[string]string{endpointAnnotation: renderedEndpoint})
|
||||
return u
|
||||
}
|
||||
|
||||
@@ -259,7 +310,7 @@ func TestCheckForcesWhenObjectsMissing(t *testing.T) {
|
||||
// the cluster.
|
||||
func TestCheckNoopWhenAllPresent(t *testing.T) {
|
||||
g, dyn := newGate(t,
|
||||
[]client.Object{sourceSecret("bucket-1a2b"), cozyDefaultBackupClass()},
|
||||
[]client.Object{sourceSecret("bucket-1a2b"), cosiSecret("https://s3.example.com"), cozyDefaultBackupClass()},
|
||||
helmReleaseObject(),
|
||||
strategyObject("CNPG", "cozy-default-cnpg"),
|
||||
strategyObject("MariaDB", "cozy-default-mariadb"),
|
||||
@@ -550,10 +601,11 @@ func TestMissingObjectsIgnoresUnmappedKinds(t *testing.T) {
|
||||
bslObject(),
|
||||
)
|
||||
|
||||
missing, err := g.missingObjects(context.Background(), bc)
|
||||
state, err := g.inspectObjects(context.Background(), bc, "")
|
||||
if err != nil {
|
||||
t.Fatalf("missingObjects: %v", err)
|
||||
t.Fatalf("inspectObjects: %v", err)
|
||||
}
|
||||
missing := state.missing
|
||||
if len(missing) != 0 {
|
||||
t.Fatalf("missing = %v, want none (unmapped kind must be ignored)", missing)
|
||||
}
|
||||
@@ -579,10 +631,11 @@ func TestMissingObjectsSkipsVeleroWhenNamespaceEmpty(t *testing.T) {
|
||||
)
|
||||
g.VeleroNamespace = ""
|
||||
|
||||
missing, err := g.missingObjects(context.Background(), bc)
|
||||
state, err := g.inspectObjects(context.Background(), bc, "")
|
||||
if err != nil {
|
||||
t.Fatalf("missingObjects: %v", err)
|
||||
t.Fatalf("inspectObjects: %v", err)
|
||||
}
|
||||
missing := state.missing
|
||||
if len(missing) != 0 {
|
||||
t.Fatalf("missing = %v, want none when Velero is disabled", missing)
|
||||
}
|
||||
@@ -630,10 +683,11 @@ func TestMissingObjectsSkipsBSLWhenVeleroAPIAbsent(t *testing.T) {
|
||||
}
|
||||
g.RESTMapper = m
|
||||
|
||||
missing, err := g.missingObjects(context.Background(), bc)
|
||||
state, err := g.inspectObjects(context.Background(), bc, "")
|
||||
if err != nil {
|
||||
t.Fatalf("missingObjects: %v", err)
|
||||
t.Fatalf("inspectObjects: %v", err)
|
||||
}
|
||||
missing := state.missing
|
||||
if len(missing) != 0 {
|
||||
t.Fatalf("missing = %v, want none when the Velero API is absent", missing)
|
||||
}
|
||||
@@ -744,3 +798,330 @@ func TestCheckToleratesAbsentHelmRelease(t *testing.T) {
|
||||
t.Error("objects should still be reported as missing")
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckForcesCredentialsReleaseWhenEndpointStale covers the credentials
|
||||
// Secret outliving the endpoint it was rendered from. The COSI sidecar writes
|
||||
// BucketInfo once per BucketAccess, so a re-granted BucketAccess (after a
|
||||
// publishing.host change) carries the new endpoint while the credentials
|
||||
// Secret <bucket>-system rendered earlier keeps the old one: the Secret
|
||||
// exists, so the missing-Secret path never fires, and every consumer keeps
|
||||
// the dead host.
|
||||
func TestCheckForcesCredentialsReleaseWhenEndpointStale(t *testing.T) {
|
||||
objs := append([]runtime.Object{helmReleaseObject(), credentialsHelmReleaseObject()}, allDefaultObjects()...)
|
||||
g, dyn := newGate(t,
|
||||
[]client.Object{sourceSecret("bucket-1a2b"), cosiSecret("https://s3.new.example.com"), cozyDefaultBackupClass()},
|
||||
objs...,
|
||||
)
|
||||
|
||||
missing, forced, err := g.Check(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("Check: %v", err)
|
||||
}
|
||||
if !forced {
|
||||
t.Fatal("expected the bucket release forced while its credentials Secret carries a stale endpoint")
|
||||
}
|
||||
if len(missing) != 1 || !strings.HasPrefix(missing[0], "Secret/bucket-cozy-backups-system-credentials") {
|
||||
t.Fatalf("missing = %v, want the credentials Secret", missing)
|
||||
}
|
||||
if ann := credentialsForceAnnotations(t, dyn); ann["reconcile.fluxcd.io/forceAt"] == "" {
|
||||
t.Errorf("bucket release not forced: %v", ann)
|
||||
}
|
||||
// The objects were rendered from the stale Secret and agree with it;
|
||||
// re-rendering them before the Secret is fixed would bake the old
|
||||
// endpoint in again.
|
||||
if ann := forceAnnotations(t, dyn); len(ann) != 0 {
|
||||
t.Errorf("own HelmRelease forced while the credentials Secret is stale: %v", ann)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckCredentialsEndpointComparison pins the comparison against what
|
||||
// user-credentials.yaml writes for a given BucketInfo endpoint (the scheme
|
||||
// stripped, nothing else), so a Secret the chart rendered from the current
|
||||
// BucketInfo never counts as stale and the bucket release is not forced on
|
||||
// every throttle window.
|
||||
func TestCheckCredentialsEndpointComparison(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
cosi string
|
||||
wantForce bool
|
||||
}{
|
||||
{"https scheme stripped", "https://s3.example.com", false},
|
||||
{"http scheme stripped", "http://s3.example.com", false},
|
||||
{"bare host", "s3.example.com", false},
|
||||
{"other host", "https://s3.new.example.com", true},
|
||||
{"other port", "https://s3.example.com:8443", true},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
objs := append([]runtime.Object{helmReleaseObject(), credentialsHelmReleaseObject()}, allDefaultObjects()...)
|
||||
g, dyn := newGate(t,
|
||||
[]client.Object{sourceSecret("bucket-1a2b"), cosiSecret(tc.cosi), cozyDefaultBackupClass()},
|
||||
objs...,
|
||||
)
|
||||
if _, _, err := g.Check(context.Background()); err != nil {
|
||||
t.Fatalf("Check: %v", err)
|
||||
}
|
||||
forced := credentialsForceAnnotations(t, dyn)["reconcile.fluxcd.io/forceAt"] != ""
|
||||
if forced != tc.wantForce {
|
||||
t.Fatalf("bucket release forced = %v, want %v", forced, tc.wantForce)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckSkipsCredentialsComparisonWithoutCOSISecret covers the window in
|
||||
// which the BucketAccess is being re-granted: the COSI Secret is gone and
|
||||
// there is nothing to compare against, which is not evidence of staleness.
|
||||
func TestCheckSkipsCredentialsComparisonWithoutCOSISecret(t *testing.T) {
|
||||
objs := append([]runtime.Object{helmReleaseObject(), credentialsHelmReleaseObject()}, allDefaultObjects()...)
|
||||
g, dyn := newGate(t,
|
||||
[]client.Object{sourceSecret("bucket-1a2b"), cozyDefaultBackupClass()},
|
||||
objs...,
|
||||
)
|
||||
|
||||
missing, forced, err := g.Check(context.Background())
|
||||
if err != nil || forced || len(missing) != 0 {
|
||||
t.Fatalf("missing = %v, forced = %v, err = %v; want a no-op", missing, forced, err)
|
||||
}
|
||||
if ann := credentialsForceAnnotations(t, dyn); len(ann) != 0 {
|
||||
t.Errorf("bucket release forced with no COSI Secret to compare against: %v", ann)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckSkipsCredentialsComparisonWhenUnconfigured pins both switches that
|
||||
// turn the comparison off: no COSI Secret name, and no bucket release (external
|
||||
// S3, where the Secret is admin-managed and no BucketInfo backs it).
|
||||
func TestCheckSkipsCredentialsComparisonWhenUnconfigured(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
apply func(g *DefaultObjectsGate)
|
||||
}{
|
||||
{"no COSI Secret name", func(g *DefaultObjectsGate) { g.CredentialsCOSISecretName = "" }},
|
||||
{"no bucket release", func(g *DefaultObjectsGate) { g.CredentialsHelmRelease = types.NamespacedName{} }},
|
||||
{"no bucket release name", func(g *DefaultObjectsGate) { g.CredentialsHelmRelease.Name = "" }},
|
||||
{"no bucket release namespace", func(g *DefaultObjectsGate) { g.CredentialsHelmRelease.Namespace = "" }},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
objs := append([]runtime.Object{helmReleaseObject(), credentialsHelmReleaseObject()}, allDefaultObjects()...)
|
||||
g, dyn := newGate(t,
|
||||
[]client.Object{sourceSecret("bucket-1a2b"), cosiSecret("https://s3.new.example.com"), cozyDefaultBackupClass()},
|
||||
objs...,
|
||||
)
|
||||
tc.apply(g)
|
||||
// The real API server rejects a Get with an empty name; the fake
|
||||
// answers NotFound, which would hide a lookup of an unset name.
|
||||
g.Client = interceptor.NewClient(g.Client.(client.WithWatch), interceptor.Funcs{
|
||||
Get: func(ctx context.Context, c client.WithWatch, key client.ObjectKey, obj client.Object, opts ...client.GetOption) error {
|
||||
if key.Name == "" {
|
||||
return errors.New("resource name may not be empty")
|
||||
}
|
||||
return c.Get(ctx, key, obj, opts...)
|
||||
},
|
||||
})
|
||||
missing, forced, err := g.Check(context.Background())
|
||||
if err != nil || forced || len(missing) != 0 {
|
||||
t.Fatalf("missing = %v, forced = %v, err = %v; want a no-op", missing, forced, err)
|
||||
}
|
||||
if ann := credentialsForceAnnotations(t, dyn); len(ann) != 0 {
|
||||
t.Errorf("bucket release forced with the comparison disabled: %v", ann)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckReportsMalformedCOSISecret pins that a COSI Secret the gate cannot
|
||||
// read is an error the check counts, not a silent "not stale".
|
||||
func TestCheckReportsMalformedCOSISecret(t *testing.T) {
|
||||
defaultObjectsCheckErrors.Reset()
|
||||
bad := cosiSecret("")
|
||||
bad.Data["BucketInfo"] = []byte("{not json")
|
||||
objs := append([]runtime.Object{helmReleaseObject(), credentialsHelmReleaseObject()}, allDefaultObjects()...)
|
||||
g, _ := newGate(t, []client.Object{sourceSecret("bucket-1a2b"), bad, cozyDefaultBackupClass()}, objs...)
|
||||
|
||||
if _, forced, err := g.Check(context.Background()); err == nil || forced {
|
||||
t.Fatalf("forced = %v, err = %v; want an error", forced, err)
|
||||
}
|
||||
if got := testutil.ToFloat64(defaultObjectsCheckErrors.WithLabelValues("cozy-default")); got != 1 {
|
||||
t.Fatalf("check errors = %v, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckForcesOwnReleaseWhenObjectEndpointStale is the second half of a
|
||||
// publishing.host change: the credentials Secret now carries the new
|
||||
// endpoint, but the BSL and the default Strategies baked the old one at the
|
||||
// last render of this chart and nothing re-renders them, so every backup
|
||||
// keeps going to the dead host.
|
||||
func TestCheckForcesOwnReleaseWhenObjectEndpointStale(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
stale *unstructured.Unstructured
|
||||
key string
|
||||
}{
|
||||
{"strategy", strategyObject("CNPG", "cozy-default-cnpg"), "CNPG/cozy-default-cnpg"},
|
||||
{"BSL", bslObject(), "BackupStorageLocation/cozy-default"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var objs []runtime.Object
|
||||
for _, o := range allDefaultObjects() {
|
||||
u := o.(*unstructured.Unstructured)
|
||||
if u.GetKind() == tc.stale.GetKind() && u.GetName() == tc.stale.GetName() {
|
||||
withEndpoint(u, "https://s3.old.example.com")
|
||||
}
|
||||
objs = append(objs, u)
|
||||
}
|
||||
objs = append(objs, helmReleaseObject(), credentialsHelmReleaseObject())
|
||||
g, dyn := newGate(t,
|
||||
[]client.Object{sourceSecret("bucket-1a2b"), cosiSecret("https://s3.example.com"), cozyDefaultBackupClass()},
|
||||
objs...,
|
||||
)
|
||||
|
||||
missing, forced, err := g.Check(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("Check: %v", err)
|
||||
}
|
||||
if !forced {
|
||||
t.Fatal("expected this chart's release forced while an object carries a stale endpoint")
|
||||
}
|
||||
if len(missing) != 1 || !strings.HasPrefix(missing[0], tc.key+" ") {
|
||||
t.Fatalf("missing = %v, want only %s", missing, tc.key)
|
||||
}
|
||||
if ann := forceAnnotations(t, dyn); ann["reconcile.fluxcd.io/forceAt"] == "" {
|
||||
t.Errorf("own HelmRelease not forced: %v", ann)
|
||||
}
|
||||
if ann := credentialsForceAnnotations(t, dyn); len(ann) != 0 {
|
||||
t.Errorf("bucket release forced for a stale object: %v", ann)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckIgnoresObjectEndpointOnExternalS3 pins that the comparison is off
|
||||
// when no bucket release is configured: with provisionBucket=false the chart
|
||||
// renders backupStorage.endpoint, not the Secret's, so the two legitimately
|
||||
// differ and forcing would loop every MinForceInterval.
|
||||
func TestCheckIgnoresObjectEndpointOnExternalS3(t *testing.T) {
|
||||
objs := []runtime.Object{helmReleaseObject()}
|
||||
for _, o := range allDefaultObjects() {
|
||||
objs = append(objs, withEndpoint(o.(*unstructured.Unstructured), "https://minio.example.internal:9000"))
|
||||
}
|
||||
g, dyn := newGate(t, []client.Object{sourceSecret("bucket-1a2b"), cozyDefaultBackupClass()}, objs...)
|
||||
g.CredentialsHelmRelease = types.NamespacedName{}
|
||||
|
||||
missing, forced, err := g.Check(context.Background())
|
||||
if err != nil || forced || len(missing) != 0 {
|
||||
t.Fatalf("missing = %v, forced = %v, err = %v; want a no-op", missing, forced, err)
|
||||
}
|
||||
if ann := forceAnnotations(t, dyn); len(ann) != 0 {
|
||||
t.Errorf("own HelmRelease forced on external S3: %v", ann)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckIgnoresObjectEndpointWithoutSecretEndpoint covers a credentials
|
||||
// Secret with no endpoint key: the chart then renders its values fallback,
|
||||
// which the gate cannot reproduce, so there is nothing to compare against.
|
||||
func TestCheckIgnoresObjectEndpointWithoutSecretEndpoint(t *testing.T) {
|
||||
src := sourceSecret("bucket-1a2b")
|
||||
delete(src.Data, "endpoint")
|
||||
objs := append([]runtime.Object{helmReleaseObject(), credentialsHelmReleaseObject()}, allDefaultObjects()...)
|
||||
g, dyn := newGate(t, []client.Object{src, cozyDefaultBackupClass()}, objs...)
|
||||
|
||||
if _, forced, err := g.Check(context.Background()); err != nil || forced {
|
||||
t.Fatalf("forced = %v, err = %v; want a no-op", forced, err)
|
||||
}
|
||||
if ann := forceAnnotations(t, dyn); len(ann) != 0 {
|
||||
t.Errorf("own HelmRelease forced with no endpoint to compare against: %v", ann)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckForcesOnceForUnannotatedObjects covers objects rendered by a chart
|
||||
// that predates the endpoint annotation. Their endpoint is unknown, so they
|
||||
// are treated as stale until one forced render has gone through, which stamps
|
||||
// the annotation. A force that did not go through (throttled) does not count,
|
||||
// and once one has, an object still without the annotation (one the chart
|
||||
// does not render) must not force again every MinForceInterval.
|
||||
func TestCheckForcesOnceForUnannotatedObjects(t *testing.T) {
|
||||
base := time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)
|
||||
objs := []runtime.Object{helmReleaseObject(), credentialsHelmReleaseObject()}
|
||||
for _, o := range allDefaultObjects() {
|
||||
objs = append(objs, unannotated(o.(*unstructured.Unstructured)))
|
||||
}
|
||||
g, dyn := newGate(t, []client.Object{sourceSecret("bucket-1a2b"), cozyDefaultBackupClass()}, objs...)
|
||||
g.lastForce = base.Add(-time.Minute)
|
||||
g.now = func() time.Time { return base }
|
||||
|
||||
if missing, forced, err := g.Check(context.Background()); err != nil || forced || len(missing) != 7 {
|
||||
t.Fatalf("throttled Check: missing = %v, forced = %v, err = %v; want 7 reported, none forced", missing, forced, err)
|
||||
}
|
||||
|
||||
g.now = func() time.Time { return base.Add(5 * time.Minute) }
|
||||
if _, forced, err := g.Check(context.Background()); err != nil || !forced {
|
||||
t.Fatalf("first unthrottled Check: forced = %v, err = %v; want forced", forced, err)
|
||||
}
|
||||
first := forceAnnotations(t, dyn)["reconcile.fluxcd.io/forceAt"]
|
||||
|
||||
g.now = func() time.Time { return base.Add(time.Hour) }
|
||||
missing, forced, err := g.Check(context.Background())
|
||||
if err != nil || forced || len(missing) != 0 {
|
||||
t.Fatalf("after the forced render: missing = %v, forced = %v, err = %v; want a no-op", missing, forced, err)
|
||||
}
|
||||
if again := forceAnnotations(t, dyn)["reconcile.fluxcd.io/forceAt"]; again != first {
|
||||
t.Errorf("forced again for unannotated objects: %q, then %q", first, again)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckStillForcesStaleAfterUnannotatedForce pins that the once-only
|
||||
// treatment covers the missing annotation alone: an annotation that names the
|
||||
// wrong endpoint still forces after that.
|
||||
func TestCheckStillForcesStaleAfterUnannotatedForce(t *testing.T) {
|
||||
objs := []runtime.Object{helmReleaseObject(), credentialsHelmReleaseObject()}
|
||||
for _, o := range allDefaultObjects() {
|
||||
objs = append(objs, withEndpoint(o.(*unstructured.Unstructured), "https://s3.old.example.com"))
|
||||
}
|
||||
g, _ := newGate(t, []client.Object{sourceSecret("bucket-1a2b"), cozyDefaultBackupClass()}, objs...)
|
||||
g.renderForced = true
|
||||
|
||||
if _, forced, err := g.Check(context.Background()); err != nil || !forced {
|
||||
t.Fatalf("forced = %v, err = %v; want forced", forced, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRenderedEndpointMatchesTheChart pins renderedEndpointFor to the
|
||||
// "backupstrategy-controller.endpoint" helper, which the chart stamps as the
|
||||
// annotation: trim, strip a scheme, force https://. Any divergence makes the
|
||||
// gate see every object as stale and force this release forever.
|
||||
func TestRenderedEndpointMatchesTheChart(t *testing.T) {
|
||||
for _, tc := range []struct{ in, want string }{
|
||||
{"s3.example.com", "https://s3.example.com"},
|
||||
{"s3.example.com\n", "https://s3.example.com"},
|
||||
{" s3.example.com ", "https://s3.example.com"},
|
||||
{"https://s3.example.com", "https://s3.example.com"},
|
||||
{"http://s3.example.com:8333", "https://s3.example.com:8333"},
|
||||
{"s3.example.com/prefix/", "https://s3.example.com/prefix/"},
|
||||
// No endpoint in the Secret: the chart renders its values fallback,
|
||||
// which the gate cannot reproduce, so it compares nothing.
|
||||
{"", ""},
|
||||
} {
|
||||
if got := renderedEndpointFor([]byte(tc.in)); got != tc.want {
|
||||
t.Errorf("renderedEndpointFor(%q) = %q, want %q", tc.in, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestForcedReleaseForNamesTheStaleCredentialsRelease pins the release the
|
||||
// log line names: a stale credentials Secret is repaired by the bucket
|
||||
// release, like a missing one, and everything else by this chart's own.
|
||||
func TestForcedReleaseForNamesTheStaleCredentialsRelease(t *testing.T) {
|
||||
g, _ := newGate(t, nil)
|
||||
for _, tc := range []struct {
|
||||
missing []string
|
||||
want types.NamespacedName
|
||||
}{
|
||||
{[]string{"Secret/bucket-cozy-backups-system-credentials"}, g.CredentialsHelmRelease},
|
||||
{[]string{`Secret/bucket-cozy-backups-system-credentials (endpoint "s3.old", COSI Secret has "s3.new")`}, g.CredentialsHelmRelease},
|
||||
{[]string{"Secret/bucket-cozy-backups-system-credentials-other"}, g.HelmRelease},
|
||||
{[]string{`CNPG/cozy-default-cnpg (endpoint "https://s3.old", want "https://s3.new")`}, g.HelmRelease},
|
||||
} {
|
||||
if got := g.forcedReleaseFor(tc.missing); got != tc.want {
|
||||
t.Errorf("forcedReleaseFor(%v) = %v, want %v", tc.missing, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user