Skip to content

Commit 4e0f7e2

Browse files
committed
fix: restore init container uses configured registry auth secret name
When backing up, CopySecret always named the copied secret with the hardcoded constant DevWorkspaceBackupAuthSecretName ("devworkspace- backup-registry-auth") regardless of the admin-configured name (e.g. "quay-backup-auth"). On restore, GetNamespaceRegistryAuthSecret was called with an empty operatorConfigNamespace, so it only searched the workspace namespace, did not find the secret there (it was either not copied at all or copied under the wrong name), and returned nil — causing the workspace-restore init container to start without registry auth and immediately crash with CrashLoopBackOff. Changes: - CopySecret: add secretName parameter; use it for the copy's ObjectMeta Name instead of the hardcoded constant. - HandleRegistryAuthSecret: pass secretName (the configured auth secret name) to CopySecret. - GetNamespaceRegistryAuthSecret: add operatorConfigNamespace parameter and thread it through to HandleRegistryAuthSecret. - pkg/library/restore: resolve the operator namespace via infrastructure.GetNamespace() and pass it to GetNamespaceRegistryAuthSecret so the lookup can fall back to the operator namespace when the secret is absent from the workspace NS. Migration note: any existing copies named "devworkspace-backup-registry- auth" become orphaned under their DevWorkspace ownerReference; Kubernetes garbage-collects them when the workspace is deleted. No active cleanup code is needed. Assisted-by: Claude Sonnet 4.6 (1M context) Signed-off-by: Oleksii Kurinnyi <okurinny@redhat.com>
1 parent fcee0cd commit 4e0f7e2

File tree

3 files changed

+272
-13
lines changed

3 files changed

+272
-13
lines changed

pkg/library/restore/restore.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,18 @@ import (
2222
"strings"
2323

2424
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
25-
"github.com/devfile/devworkspace-operator/pkg/common"
26-
devfileConstants "github.com/devfile/devworkspace-operator/pkg/library/constants"
27-
dwResources "github.com/devfile/devworkspace-operator/pkg/library/resources"
28-
"github.com/devfile/devworkspace-operator/pkg/secrets"
2925
"github.com/go-logr/logr"
3026
corev1 "k8s.io/api/core/v1"
3127
"k8s.io/apimachinery/pkg/runtime"
3228
"sigs.k8s.io/controller-runtime/pkg/client"
3329

3430
"github.com/devfile/devworkspace-operator/internal/images"
31+
"github.com/devfile/devworkspace-operator/pkg/common"
3532
"github.com/devfile/devworkspace-operator/pkg/constants"
33+
"github.com/devfile/devworkspace-operator/pkg/infrastructure"
34+
devfileConstants "github.com/devfile/devworkspace-operator/pkg/library/constants"
35+
dwResources "github.com/devfile/devworkspace-operator/pkg/library/resources"
36+
"github.com/devfile/devworkspace-operator/pkg/secrets"
3637
)
3738

3839
const (
@@ -114,7 +115,11 @@ func GetWorkspaceRestoreInitContainer(
114115
MountPath: constants.DefaultProjectsSourcesRoot,
115116
},
116117
}
117-
registryAuthSecret, err := secrets.GetNamespaceRegistryAuthSecret(ctx, k8sClient, workspace.DevWorkspace, workspace.Config, scheme, log)
118+
operatorNamespace, err := infrastructure.GetNamespace()
119+
if err != nil {
120+
return nil, nil, fmt.Errorf("failed to get operator namespace for workspace restore: %w", err)
121+
}
122+
registryAuthSecret, err := secrets.GetNamespaceRegistryAuthSecret(ctx, k8sClient, workspace.DevWorkspace, workspace.Config, operatorNamespace, scheme, log)
118123
if err != nil {
119124
return nil, nil, fmt.Errorf("handling registry auth secret for workspace restore: %w", err)
120125
}

pkg/secrets/backup.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ import (
3232
"github.com/devfile/devworkspace-operator/pkg/provision/sync"
3333
)
3434

35-
// GetRegistryAuthSecret retrieves the registry authentication secret for accessing backup images
36-
// based on the operator configuration.
35+
// GetNamespaceRegistryAuthSecret retrieves the registry authentication secret for accessing backup images
36+
// based on the operator configuration. operatorConfigNamespace is the namespace where the operator is
37+
// running; if non-empty, the secret is also looked up there and copied to the workspace namespace when found.
3738
func GetNamespaceRegistryAuthSecret(ctx context.Context, c client.Client, workspace *dw.DevWorkspace,
38-
dwOperatorConfig *controllerv1alpha1.OperatorConfiguration, scheme *runtime.Scheme, log logr.Logger,
39+
dwOperatorConfig *controllerv1alpha1.OperatorConfiguration, operatorConfigNamespace string, scheme *runtime.Scheme, log logr.Logger,
3940
) (*corev1.Secret, error) {
40-
return HandleRegistryAuthSecret(ctx, c, workspace, dwOperatorConfig, "", scheme, log)
41+
return HandleRegistryAuthSecret(ctx, c, workspace, dwOperatorConfig, operatorConfigNamespace, scheme, log)
4142
}
4243

4344
func HandleRegistryAuthSecret(ctx context.Context, c client.Client, workspace *dw.DevWorkspace,
@@ -81,15 +82,16 @@ func HandleRegistryAuthSecret(ctx context.Context, c client.Client, workspace *d
8182
return nil, err
8283
}
8384
log.Info("Successfully retrieved registry auth secret for backup job", "secretName", secretName)
84-
return CopySecret(ctx, c, workspace, registryAuthSecret, scheme, log)
85+
return CopySecret(ctx, c, workspace, registryAuthSecret, secretName, scheme, log)
8586
}
8687

87-
// CopySecret copies the given secret from the operator namespace to the workspace namespace.
88-
func CopySecret(ctx context.Context, c client.Client, workspace *dw.DevWorkspace, sourceSecret *corev1.Secret, scheme *runtime.Scheme, log logr.Logger) (namespaceSecret *corev1.Secret, err error) {
88+
// CopySecret copies the given secret from the operator namespace to the workspace namespace,
89+
// naming the copy secretName (the configured auth secret name from the operator config).
90+
func CopySecret(ctx context.Context, c client.Client, workspace *dw.DevWorkspace, sourceSecret *corev1.Secret, secretName string, scheme *runtime.Scheme, log logr.Logger) (namespaceSecret *corev1.Secret, err error) {
8991
// Construct the desired secret state
9092
desiredSecret := &corev1.Secret{
9193
ObjectMeta: metav1.ObjectMeta{
92-
Name: constants.DevWorkspaceBackupAuthSecretName,
94+
Name: secretName,
9395
Namespace: workspace.Namespace,
9496
Labels: map[string]string{
9597
constants.DevWorkspaceWatchSecretLabel: "true",

pkg/secrets/backup_test.go

Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
//
2+
// Copyright (c) 2019-2026 Red Hat, Inc.
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
16+
// Generated by Claude Sonnet 4.6 (1M context)
17+
18+
package secrets_test
19+
20+
import (
21+
"context"
22+
"testing"
23+
24+
. "github.com/onsi/ginkgo/v2"
25+
. "github.com/onsi/gomega"
26+
corev1 "k8s.io/api/core/v1"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"k8s.io/apimachinery/pkg/runtime"
29+
"k8s.io/apimachinery/pkg/types"
30+
"sigs.k8s.io/controller-runtime/pkg/client"
31+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
32+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
33+
34+
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
35+
controllerv1alpha1 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
36+
"github.com/devfile/devworkspace-operator/pkg/secrets"
37+
)
38+
39+
func TestSecrets(t *testing.T) {
40+
RegisterFailHandler(Fail)
41+
RunSpecs(t, "Secrets Suite")
42+
}
43+
44+
var _ = Describe("Backup secrets", func() {
45+
var (
46+
ctx context.Context
47+
scheme *runtime.Scheme
48+
)
49+
50+
const (
51+
operatorNamespace = "devworkspace-controller"
52+
workspaceNamespace = "user-namespace"
53+
configuredAuthSecretName = "quay-backup-auth"
54+
)
55+
56+
BeforeEach(func() {
57+
ctx = context.Background()
58+
59+
scheme = runtime.NewScheme()
60+
Expect(dw.AddToScheme(scheme)).To(Succeed())
61+
Expect(corev1.AddToScheme(scheme)).To(Succeed())
62+
Expect(controllerv1alpha1.AddToScheme(scheme)).To(Succeed())
63+
})
64+
65+
// buildFakeClient creates a fake client with the given objects pre-populated.
66+
buildFakeClient := func(objs ...client.Object) client.Client {
67+
return fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build()
68+
}
69+
70+
// buildWorkspace creates a minimal DevWorkspace for use in tests.
71+
buildWorkspace := func(name, namespace string) *dw.DevWorkspace {
72+
return &dw.DevWorkspace{
73+
ObjectMeta: metav1.ObjectMeta{
74+
Name: name,
75+
Namespace: namespace,
76+
UID: "test-uid",
77+
},
78+
}
79+
}
80+
81+
// buildConfig creates an OperatorConfiguration with the given registry auth secret name.
82+
buildConfig := func(authSecretName string) *controllerv1alpha1.OperatorConfiguration {
83+
return &controllerv1alpha1.OperatorConfiguration{
84+
Workspace: &controllerv1alpha1.WorkspaceConfig{
85+
BackupCronJob: &controllerv1alpha1.BackupCronJobConfig{
86+
Registry: &controllerv1alpha1.RegistryConfig{
87+
Path: "my-registry:5000",
88+
AuthSecret: authSecretName,
89+
},
90+
},
91+
},
92+
}
93+
}
94+
95+
// buildSecret creates a secret with the given name, namespace and data.
96+
buildSecret := func(name, namespace string, data map[string][]byte) *corev1.Secret {
97+
return &corev1.Secret{
98+
ObjectMeta: metav1.ObjectMeta{
99+
Name: name,
100+
Namespace: namespace,
101+
},
102+
Data: data,
103+
Type: corev1.SecretTypeDockerConfigJson,
104+
}
105+
}
106+
107+
log := zap.New(zap.UseDevMode(true)).WithName("SecretsTest")
108+
109+
Context("CopySecret", func() {
110+
It("creates the copy using the configured secret name, not devworkspace-backup-registry-auth", func() {
111+
workspace := buildWorkspace("my-workspace", workspaceNamespace)
112+
sourceSecret := buildSecret(configuredAuthSecretName, operatorNamespace, map[string][]byte{
113+
".dockerconfigjson": []byte(`{"auths":{}}`),
114+
})
115+
fakeClient := buildFakeClient(workspace)
116+
117+
By("calling CopySecret with the configured secret name")
118+
result, err := secrets.CopySecret(ctx, fakeClient, workspace, sourceSecret, configuredAuthSecretName, scheme, log)
119+
Expect(err).NotTo(HaveOccurred())
120+
// The first sync creates the object and returns desiredSecret (NotInSyncError path)
121+
Expect(result).NotTo(BeNil())
122+
Expect(result.Name).To(Equal(configuredAuthSecretName))
123+
Expect(result.Namespace).To(Equal(workspaceNamespace))
124+
125+
By("verifying the secret was created in the workspace namespace with the correct name")
126+
clusterSecret := &corev1.Secret{}
127+
Expect(fakeClient.Get(ctx, types.NamespacedName{
128+
Name: configuredAuthSecretName,
129+
Namespace: workspaceNamespace,
130+
}, clusterSecret)).To(Succeed())
131+
Expect(clusterSecret.Name).To(Equal(configuredAuthSecretName))
132+
Expect(clusterSecret.Data).To(Equal(sourceSecret.Data))
133+
134+
By("verifying the hardcoded default name was NOT used")
135+
legacySecret := &corev1.Secret{}
136+
err = fakeClient.Get(ctx, types.NamespacedName{
137+
Name: "devworkspace-backup-registry-auth",
138+
Namespace: workspaceNamespace,
139+
}, legacySecret)
140+
Expect(client.IgnoreNotFound(err)).To(Succeed())
141+
Expect(err).To(HaveOccurred(), "legacy hardcoded name must not be used")
142+
})
143+
})
144+
145+
Context("HandleRegistryAuthSecret", func() {
146+
It("returns secret directly when it exists in the workspace namespace with the configured name", func() {
147+
workspace := buildWorkspace("my-workspace", workspaceNamespace)
148+
config := buildConfig(configuredAuthSecretName)
149+
// Secret already exists in workspace namespace
150+
localSecret := buildSecret(configuredAuthSecretName, workspaceNamespace, map[string][]byte{
151+
".dockerconfigjson": []byte(`{"auths":{"local":{}}}`),
152+
})
153+
fakeClient := buildFakeClient(workspace, localSecret)
154+
155+
By("calling HandleRegistryAuthSecret with operator namespace")
156+
result, err := secrets.HandleRegistryAuthSecret(ctx, fakeClient, workspace, config, operatorNamespace, scheme, log)
157+
Expect(err).NotTo(HaveOccurred())
158+
Expect(result).NotTo(BeNil())
159+
Expect(result.Name).To(Equal(configuredAuthSecretName))
160+
Expect(result.Namespace).To(Equal(workspaceNamespace))
161+
})
162+
163+
It("copies secret from operator namespace to workspace namespace with the configured name", func() {
164+
workspace := buildWorkspace("my-workspace", workspaceNamespace)
165+
config := buildConfig(configuredAuthSecretName)
166+
// Secret only exists in the operator namespace
167+
operatorSecret := buildSecret(configuredAuthSecretName, operatorNamespace, map[string][]byte{
168+
".dockerconfigjson": []byte(`{"auths":{"operator":{}}}`),
169+
})
170+
fakeClient := buildFakeClient(workspace, operatorSecret)
171+
172+
By("calling HandleRegistryAuthSecret — secret missing from workspace NS, present in operator NS")
173+
result, err := secrets.HandleRegistryAuthSecret(ctx, fakeClient, workspace, config, operatorNamespace, scheme, log)
174+
Expect(err).NotTo(HaveOccurred())
175+
Expect(result).NotTo(BeNil())
176+
// The result should use the configured name
177+
Expect(result.Name).To(Equal(configuredAuthSecretName))
178+
Expect(result.Namespace).To(Equal(workspaceNamespace))
179+
180+
By("verifying the copy exists in workspace namespace with the configured name")
181+
clusterSecret := &corev1.Secret{}
182+
Expect(fakeClient.Get(ctx, types.NamespacedName{
183+
Name: configuredAuthSecretName,
184+
Namespace: workspaceNamespace,
185+
}, clusterSecret)).To(Succeed())
186+
187+
By("verifying the legacy hardcoded name was NOT created")
188+
legacySecret := &corev1.Secret{}
189+
err = fakeClient.Get(ctx, types.NamespacedName{
190+
Name: "devworkspace-backup-registry-auth",
191+
Namespace: workspaceNamespace,
192+
}, legacySecret)
193+
Expect(client.IgnoreNotFound(err)).To(Succeed())
194+
Expect(err).To(HaveOccurred(), "legacy hardcoded name must not be created")
195+
})
196+
197+
It("returns nil, nil when operator namespace is given but secret is in neither namespace", func() {
198+
workspace := buildWorkspace("my-workspace", workspaceNamespace)
199+
config := buildConfig(configuredAuthSecretName)
200+
fakeClient := buildFakeClient(workspace) // No secrets pre-created
201+
202+
result, err := secrets.HandleRegistryAuthSecret(ctx, fakeClient, workspace, config, operatorNamespace, scheme, log)
203+
// Secret not found in operator namespace returns an error (not found)
204+
// The function logs the error and returns it
205+
Expect(err).To(HaveOccurred())
206+
Expect(result).To(BeNil())
207+
})
208+
})
209+
210+
Context("GetNamespaceRegistryAuthSecret", func() {
211+
It("end-to-end: with operatorConfigNamespace, copies secret from operator NS and returns it with configured name", func() {
212+
workspace := buildWorkspace("my-workspace", workspaceNamespace)
213+
config := buildConfig(configuredAuthSecretName)
214+
// Secret only in operator namespace
215+
operatorSecret := buildSecret(configuredAuthSecretName, operatorNamespace, map[string][]byte{
216+
".dockerconfigjson": []byte(`{"auths":{"e2e":{}}}`),
217+
})
218+
fakeClient := buildFakeClient(workspace, operatorSecret)
219+
220+
By("calling GetNamespaceRegistryAuthSecret with a non-empty operatorConfigNamespace")
221+
result, err := secrets.GetNamespaceRegistryAuthSecret(ctx, fakeClient, workspace, config, operatorNamespace, scheme, log)
222+
Expect(err).NotTo(HaveOccurred())
223+
Expect(result).NotTo(BeNil())
224+
Expect(result.Name).To(Equal(configuredAuthSecretName))
225+
Expect(result.Namespace).To(Equal(workspaceNamespace))
226+
227+
By("verifying the copy exists with the configured name in the workspace namespace")
228+
clusterSecret := &corev1.Secret{}
229+
Expect(fakeClient.Get(ctx, types.NamespacedName{
230+
Name: configuredAuthSecretName,
231+
Namespace: workspaceNamespace,
232+
}, clusterSecret)).To(Succeed())
233+
})
234+
235+
It("legacy no-op case: with operatorConfigNamespace empty and secret in workspace NS, returns it directly", func() {
236+
workspace := buildWorkspace("my-workspace", workspaceNamespace)
237+
config := buildConfig(configuredAuthSecretName)
238+
// Secret already exists in workspace namespace
239+
localSecret := buildSecret(configuredAuthSecretName, workspaceNamespace, map[string][]byte{
240+
".dockerconfigjson": []byte(`{"auths":{"local":{}}}`),
241+
})
242+
fakeClient := buildFakeClient(workspace, localSecret)
243+
244+
By("calling GetNamespaceRegistryAuthSecret with empty operatorConfigNamespace (legacy behavior)")
245+
result, err := secrets.GetNamespaceRegistryAuthSecret(ctx, fakeClient, workspace, config, "", scheme, log)
246+
Expect(err).NotTo(HaveOccurred())
247+
Expect(result).NotTo(BeNil())
248+
Expect(result.Name).To(Equal(configuredAuthSecretName))
249+
Expect(result.Namespace).To(Equal(workspaceNamespace))
250+
})
251+
})
252+
})

0 commit comments

Comments
 (0)