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
21 changes: 16 additions & 5 deletions controllers/cleanupcronjob/cleanupcronjob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,25 @@ func (r *CleanupCronJobReconciler) Reconcile(ctx context.Context, req ctrl.Reque
return ctrl.Result{}, client.IgnoreNotFound(err)
}

cleanupConfig := dwOperatorConfig.Config.Workspace.CleanupCronJob
log = log.WithValues("CleanupCronJob", cleanupConfig)

if cleanupConfig == nil {
log.Info("DevWorkspaceOperatorConfig does not have cleanup configuration, stopping cron schedler and skipping reconciliation")
if dwOperatorConfig.Config == nil {
log.Info("DevWorkspaceOperatorConfig does not have config, stopping cron scheduler and skipping reconciliation")
r.stopCron(log)
return ctrl.Result{}, nil
}
if dwOperatorConfig.Config.Workspace == nil {
log.Info("DevWorkspaceOperatorConfig does not have workspace config, stopping cron scheduler and skipping reconciliation")
r.stopCron(log)
return ctrl.Result{}, nil
}
if dwOperatorConfig.Config.Workspace.CleanupCronJob == nil {
log.Info("DevWorkspaceOperatorConfig does not have cleanup configuration, stopping cron scheduler and skipping reconciliation")
r.stopCron(log)
return ctrl.Result{}, nil
}

cleanupConfig := dwOperatorConfig.Config.Workspace.CleanupCronJob
log = log.WithValues("CleanupCronJob", cleanupConfig)

if cleanupConfig.Enable == nil || !*cleanupConfig.Enable {
log.Info("DevWorkspace pruning is disabled, stopping cron scheduler and skipping reconciliation")
r.stopCron(log)
Expand Down
32 changes: 30 additions & 2 deletions controllers/cleanupcronjob/cleanupcronjob_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,34 @@ var _ = Describe("CleanupCronJobReconciler", func() {
Expect(reconciler.cron.Entries()).To(BeEmpty())
})

It("Should not start cron if dwOperatorConfig.Config is nil", func() {
dwoc := &controllerv1alpha1.DevWorkspaceOperatorConfig{
ObjectMeta: metav1.ObjectMeta{Name: nameNamespace.Name, Namespace: nameNamespace.Namespace},
Config: nil,
}
Expect(fakeClient.Create(ctx, dwoc)).To(Succeed())

result, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: nameNamespace})
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(ctrl.Result{}))
Expect(reconciler.cron.Entries()).To(BeEmpty())
})

It("Should not start cron if dwOperatorConfig.Config.Workspace is nil", func() {
dwoc := &controllerv1alpha1.DevWorkspaceOperatorConfig{
ObjectMeta: metav1.ObjectMeta{Name: nameNamespace.Name, Namespace: nameNamespace.Namespace},
Config: &controllerv1alpha1.OperatorConfiguration{
Workspace: nil,
},
}
Expect(fakeClient.Create(ctx, dwoc)).To(Succeed())

result, err := reconciler.Reconcile(ctx, ctrl.Request{NamespacedName: nameNamespace})
Expect(err).ToNot(HaveOccurred())
Expect(result).To(Equal(ctrl.Result{}))
Expect(reconciler.cron.Entries()).To(BeEmpty())
})

It("Should not start cron if received event from different namespace", func() {
dwoc := &controllerv1alpha1.DevWorkspaceOperatorConfig{
ObjectMeta: metav1.ObjectMeta{Name: nameNamespace.Name, Namespace: "other-namespace"},
Expand Down Expand Up @@ -185,7 +213,7 @@ var _ = Describe("CleanupCronJobReconciler", func() {
Expect(reconciler.cron.Entries()).To(BeEmpty())
})

It("Should do not start cron if pruning is disabled", func() {
It("Should not start cron if pruning is disabled", func() {
enabled := false
dwoc := &controllerv1alpha1.DevWorkspaceOperatorConfig{
ObjectMeta: metav1.ObjectMeta{Name: nameNamespace.Name, Namespace: nameNamespace.Namespace},
Expand All @@ -205,7 +233,7 @@ var _ = Describe("CleanupCronJobReconciler", func() {
Expect(reconciler.cron.Entries()).To(BeEmpty())
})

It("Should do not start cron if schedule is missing", func() {
It("Should not start cron if schedule is missing", func() {
enabled := true
dwoc := &controllerv1alpha1.DevWorkspaceOperatorConfig{
ObjectMeta: metav1.ObjectMeta{Name: nameNamespace.Name, Namespace: nameNamespace.Namespace},
Expand Down
Loading