Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions controllers/backupcronjob/backupcronjob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package controllers

import (
"context"
"fmt"
"reflect"
"time"

Expand Down Expand Up @@ -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",
},
Expand Down
43 changes: 43 additions & 0 deletions controllers/backupcronjob/backupcronjob_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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 := "* * * * *"
Expand Down Expand Up @@ -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,
Expand Down
Loading