Skip to content

Commit 39879ba

Browse files
committed
fixup! fix: fall back to canonical backup auth secret name on restore
fixup\! fix: fall back to canonical backup auth secret name on restore Address review feedback: - Use canonical DevWorkspaceBackupAuthSecretName for workspace NS lookup directly instead of falling back (dkwon17) - Remove unused secretGR variable and schema import (rohanKanojia) Assisted-by: Claude Opus 4.6 (1M context) Signed-off-by: Oleksii Kurinnyi <okurinny@redhat.com>
1 parent c9f7339 commit 39879ba

File tree

2 files changed

+13
-69
lines changed

2 files changed

+13
-69
lines changed

pkg/secrets/backup.go

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,42 +54,23 @@ func HandleRegistryAuthSecret(ctx context.Context, c client.Client, workspace *d
5454
return nil, nil
5555
}
5656

57-
// First check the workspace namespace for the secret
57+
// First check the workspace namespace for the secret.
58+
// CopySecret always writes the copy under DevWorkspaceBackupAuthSecretName,
59+
// so look it up by that canonical name.
5860
registryAuthSecret := &corev1.Secret{}
5961
err := c.Get(ctx, client.ObjectKey{
60-
Name: secretName,
62+
Name: constants.DevWorkspaceBackupAuthSecretName,
6163
Namespace: workspace.Namespace}, registryAuthSecret)
6264
if err == nil {
63-
log.Info("Successfully retrieved registry auth secret for backup from workspace namespace", "secretName", secretName)
65+
log.Info("Successfully retrieved registry auth secret for backup from workspace namespace",
66+
"secretName", constants.DevWorkspaceBackupAuthSecretName)
6467
return registryAuthSecret, nil
6568
}
6669
if client.IgnoreNotFound(err) != nil {
6770
return nil, err
6871
}
6972
// If we don't provide an operator namespace, don't attempt to look there.
70-
// However, CopySecret always writes the secret under DevWorkspaceBackupAuthSecretName,
71-
// regardless of the configured name. If the configured name differs, attempt a fallback
72-
// lookup under the canonical name so that the restore path can find it.
7373
if operatorConfigNamespace == "" {
74-
if secretName == constants.DevWorkspaceBackupAuthSecretName {
75-
// The configured name IS the canonical name; it was already looked up and not
76-
// found above, so there is nothing else to try.
77-
return nil, nil
78-
}
79-
fallbackSecret := &corev1.Secret{}
80-
fallbackErr := c.Get(ctx, client.ObjectKey{
81-
Name: constants.DevWorkspaceBackupAuthSecretName,
82-
Namespace: workspace.Namespace,
83-
}, fallbackSecret)
84-
if fallbackErr == nil {
85-
log.Info("Registry auth secret not found under configured name in workspace namespace; using canonical backup auth secret as fallback",
86-
"configuredName", secretName,
87-
"fallbackName", constants.DevWorkspaceBackupAuthSecretName)
88-
return fallbackSecret, nil
89-
}
90-
if client.IgnoreNotFound(fallbackErr) != nil {
91-
return nil, fallbackErr
92-
}
9374
return nil, nil
9475
}
9576
log.Info("Registry auth secret not found in workspace namespace, checking operator namespace", "secretName", secretName)

pkg/secrets/backup_test.go

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131
"k8s.io/apimachinery/pkg/runtime"
32-
"k8s.io/apimachinery/pkg/runtime/schema"
3332
"sigs.k8s.io/controller-runtime/pkg/client"
3433
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3534
"sigs.k8s.io/controller-runtime/pkg/log/zap"
@@ -102,38 +101,22 @@ var _ = Describe("HandleRegistryAuthSecret (restore path: operatorConfigNamespac
102101
scheme = buildScheme()
103102
})
104103

105-
It("returns the secret directly when the configured name is present in the workspace namespace", func() {
106-
By("creating a workspace-namespace secret whose name matches the configured auth secret name")
107-
configuredName := "quay-backup-auth"
108-
secret := makeSecret(configuredName, workspaceNS)
109-
110-
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(secret).Build()
111-
workspace := makeWorkspace(workspaceNS)
112-
config := makeConfig(configuredName)
113-
114-
result, err := secrets.HandleRegistryAuthSecret(ctx, fakeClient, workspace, config, "", scheme, log)
115-
Expect(err).NotTo(HaveOccurred())
116-
Expect(result).NotTo(BeNil())
117-
Expect(result.Name).To(Equal(configuredName))
118-
})
119-
120-
It("returns the canonical backup auth secret as fallback when configured name is absent (the bug fix case)", func() {
121-
By("creating only the canonical DevWorkspaceBackupAuthSecretName secret in the workspace namespace")
104+
It("returns the canonical secret when it exists in the workspace namespace", func() {
105+
By("creating the canonical DevWorkspaceBackupAuthSecretName secret in the workspace namespace")
122106
canonicalSecret := makeSecret(constants.DevWorkspaceBackupAuthSecretName, workspaceNS)
123107

124108
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(canonicalSecret).Build()
125109
workspace := makeWorkspace(workspaceNS)
126-
// Configured name is something like "quay-backup-auth" — different from the canonical name.
127110
config := makeConfig("quay-backup-auth")
128111

129112
result, err := secrets.HandleRegistryAuthSecret(ctx, fakeClient, workspace, config, "", scheme, log)
130113
Expect(err).NotTo(HaveOccurred())
131-
Expect(result).NotTo(BeNil(), "should fall back to the canonical secret copied by CopySecret")
114+
Expect(result).NotTo(BeNil())
132115
Expect(result.Name).To(Equal(constants.DevWorkspaceBackupAuthSecretName))
133116
})
134117

135-
It("returns nil, nil when neither the configured name nor the canonical name exists in the workspace namespace", func() {
136-
By("using a fake client with no secrets at all")
118+
It("returns nil when the canonical secret does not exist in the workspace namespace", func() {
119+
By("using a fake client with no secrets")
137120
fakeClient := fake.NewClientBuilder().WithScheme(scheme).Build()
138121
workspace := makeWorkspace(workspaceNS)
139122
config := makeConfig("quay-backup-auth")
@@ -143,25 +126,8 @@ var _ = Describe("HandleRegistryAuthSecret (restore path: operatorConfigNamespac
143126
Expect(result).To(BeNil())
144127
})
145128

146-
It("returns the secret on the first lookup when the configured name IS the canonical name (no duplicate query)", func() {
147-
By("creating the canonical secret in the workspace namespace and configuring the same name")
148-
canonicalSecret := makeSecret(constants.DevWorkspaceBackupAuthSecretName, workspaceNS)
149-
150-
fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(canonicalSecret).Build()
151-
workspace := makeWorkspace(workspaceNS)
152-
// Configured name equals the canonical constant — must be found on the first Get.
153-
config := makeConfig(constants.DevWorkspaceBackupAuthSecretName)
154-
155-
result, err := secrets.HandleRegistryAuthSecret(ctx, fakeClient, workspace, config, "", scheme, log)
156-
Expect(err).NotTo(HaveOccurred())
157-
Expect(result).NotTo(BeNil())
158-
Expect(result.Name).To(Equal(constants.DevWorkspaceBackupAuthSecretName))
159-
})
160-
161-
It("propagates a real (non-NotFound) error from the fallback lookup", func() {
162-
By("wrapping a fake client so that the fallback lookup returns a server error")
163-
// The configured name differs from the canonical name, so the code will attempt the fallback
164-
// lookup. We simulate that lookup returning a non-NotFound error.
129+
It("propagates a non-NotFound error from the workspace namespace lookup", func() {
130+
By("wrapping a fake client so that the canonical name lookup returns a server error")
165131
errClient := &errorOnNameClient{
166132
Client: fake.NewClientBuilder().WithScheme(scheme).Build(),
167133
failName: constants.DevWorkspaceBackupAuthSecretName,
@@ -196,6 +162,3 @@ func (e *errorOnNameClient) Get(ctx context.Context, key client.ObjectKey, obj c
196162

197163
// Ensure errorOnNameClient satisfies client.Client at compile time.
198164
var _ client.Client = &errorOnNameClient{}
199-
200-
// secretGR is the GroupResource for secrets, used when constructing test errors.
201-
var secretGR = schema.GroupResource{Group: "", Resource: "secrets"} //nolint:unused

0 commit comments

Comments
 (0)