From 853e1add5b1b4349090d7e30bc02a139b207627d Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Tue, 17 Nov 2026 10:31:54 +0530 Subject: [PATCH] fix: copy creator label onto backup Job pods (#1740) The pod mutating webhook rejects backup pods that have a workspace id and no controller.devfile.io/creator label, so backup Jobs stay at 0/1 and never push a backup image. Signed-off-by: Rohan Kumar Co-authored-by: Cursor --- .../backupcronjob/backupcronjob_controller.go | 27 +++++++----- .../backupcronjob_controller_test.go | 43 +++++++++++++++++++ 2 files changed, 60 insertions(+), 10 deletions(-) diff --git a/controllers/backupcronjob/backupcronjob_controller.go b/controllers/backupcronjob/backupcronjob_controller.go index 86b46750a..2da8f026a 100644 --- a/controllers/backupcronjob/backupcronjob_controller.go +++ b/controllers/backupcronjob/backupcronjob_controller.go @@ -17,6 +17,7 @@ package controllers import ( "context" + "fmt" "reflect" "time" @@ -386,26 +387,32 @@ func (r *BackupCronJobReconciler) createBackupJob( orasExtraArgs = backUpConfig.OrasConfig.ExtraArgs } + // The pod webhook rejects a pod that has a workspace id and no + // controller.devfile.io/creator key. An empty value is allowed: the + // DevWorkspace create webhook stores the user UID, which can be empty. + creator, ok := workspace.Labels[constants.DevWorkspaceCreatorLabel] + if !ok { + return fmt.Errorf("DevWorkspace %s/%s is missing the %s label; backup Job pod would be rejected by the mutating webhook", workspace.Namespace, workspace.Name, constants.DevWorkspaceCreatorLabel) + } + jobLabels := map[string]string{ + constants.DevWorkspaceIDLabel: dwID, + constants.DevWorkspaceNameLabel: workspace.Name, + constants.DevWorkspaceBackupJobLabel: "true", + constants.DevWorkspaceCreatorLabel: creator, + } + job := &batchv1.Job{ ObjectMeta: metav1.ObjectMeta{ GenerateName: constants.DevWorkspaceBackupJobNamePrefix, Namespace: workspace.Namespace, - Labels: map[string]string{ - constants.DevWorkspaceIDLabel: dwID, - constants.DevWorkspaceNameLabel: workspace.Name, - constants.DevWorkspaceBackupJobLabel: "true", - }, + Labels: jobLabels, }, Spec: batchv1.JobSpec{ TTLSecondsAfterFinished: ptr.To[int32](120), BackoffLimit: backUpConfig.BackoffLimit, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ - Labels: map[string]string{ - constants.DevWorkspaceIDLabel: dwID, - constants.DevWorkspaceNameLabel: workspace.Name, - constants.DevWorkspaceBackupJobLabel: "true", - }, + Labels: jobLabels, Annotations: map[string]string{ "io.kubernetes.cri-o.Devices": "/dev/fuse", }, diff --git a/controllers/backupcronjob/backupcronjob_controller_test.go b/controllers/backupcronjob/backupcronjob_controller_test.go index 42f3f9ec2..180dfefab 100644 --- a/controllers/backupcronjob/backupcronjob_controller_test.go +++ b/controllers/backupcronjob/backupcronjob_controller_test.go @@ -386,6 +386,9 @@ var _ = Describe("BackupCronJobReconciler", func() { "backup job pod template must carry the workspace ID label so deleteCommonPVC can see pods still using the PVC") Expect(job.Spec.Template.Labels).To(HaveKeyWithValue(constants.DevWorkspaceNameLabel, "dw-recent")) Expect(job.Spec.Template.Labels).To(HaveKeyWithValue(constants.DevWorkspaceBackupJobLabel, "true")) + Expect(job.Labels).To(HaveKeyWithValue(constants.DevWorkspaceCreatorLabel, "test-creator")) + Expect(job.Spec.Template.Labels).To(HaveKeyWithValue(constants.DevWorkspaceCreatorLabel, "test-creator"), + "backup job pod template must carry the creator label or the pod webhook rejects pod creation") Expect(job.Spec.Template.Spec.ServiceAccountName).To(Equal("devworkspace-job-runner-id-recent")) container := job.Spec.Template.Spec.Containers[0] expectedEnvs := []corev1.EnvVar{ @@ -405,6 +408,43 @@ var _ = Describe("BackupCronJobReconciler", func() { Expect(container.VolumeMounts).Should(ContainElements(expectedVolumeMounts), "container volume mounts should include mounts needed for backup") }) + It("creates a Job when the creator label is present but empty", func() { + enabled := true + schedule := "* * * * *" + dwoc := &controllerv1alpha1.DevWorkspaceOperatorConfig{ + ObjectMeta: metav1.ObjectMeta{Name: nameNamespace.Name, Namespace: nameNamespace.Namespace}, + Config: &controllerv1alpha1.OperatorConfiguration{ + Workspace: &controllerv1alpha1.WorkspaceConfig{ + BackupCronJob: &controllerv1alpha1.BackupCronJobConfig{ + Enable: &enabled, + Schedule: schedule, + Registry: &controllerv1alpha1.RegistryConfig{ + Path: "fake-registry", + }, + }, + }, + }, + } + config.SetGlobalConfigForTesting(dwoc.Config) + Expect(fakeClient.Create(ctx, dwoc)).To(Succeed()) + dw := createDevWorkspace("dw-empty-creator", "ns-empty-creator", false, metav1.NewTime(time.Now().Add(-10*time.Minute))) + dw.Labels[constants.DevWorkspaceCreatorLabel] = "" + dw.Status.Phase = dwv2.DevWorkspaceStatusStopped + dw.Status.DevWorkspaceId = "id-empty-creator" + Expect(fakeClient.Create(ctx, dw)).To(Succeed()) + + pvc := &corev1.PersistentVolumeClaim{ObjectMeta: metav1.ObjectMeta{Name: "claim-devworkspace", Namespace: dw.Namespace}} + Expect(fakeClient.Create(ctx, pvc)).To(Succeed()) + + Expect(reconciler.executeBackupSync(ctx, dwoc, log)).To(Succeed()) + + jobList := &batchv1.JobList{} + Expect(fakeClient.List(ctx, jobList, &client.ListOptions{Namespace: dw.Namespace})).To(Succeed()) + Expect(jobList.Items).To(HaveLen(1)) + Expect(jobList.Items[0].Labels).To(HaveKeyWithValue(constants.DevWorkspaceCreatorLabel, "")) + Expect(jobList.Items[0].Spec.Template.Labels).To(HaveKeyWithValue(constants.DevWorkspaceCreatorLabel, "")) + }) + It("creates a Job with configured backoffLimit", func() { enabled := true schedule := "* * * * *" @@ -1354,6 +1394,9 @@ func createDevWorkspace(name, namespace string, started bool, lastTransitionTime ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: namespace, + Labels: map[string]string{ + constants.DevWorkspaceCreatorLabel: "test-creator", + }, }, Spec: dwv2.DevWorkspaceSpec{ Started: started,