From b81e735baef25ce75bcbde7fd8b721fffe43eaa5 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Wed, 16 Jul 2025 12:29:34 +0300 Subject: [PATCH] Make `.spec.update.strategy` optional Signed-off-by: Stefan Prodan --- api/v1beta2/imageupdateautomation_types.go | 4 +- ...lkit.fluxcd.io_imageupdateautomations.yaml | 2 - docs/api/v1beta2/image-automation.md | 1 + .../imageupdateautomation_controller_test.go | 66 ++++++++++++++++++- 4 files changed, 68 insertions(+), 5 deletions(-) diff --git a/api/v1beta2/imageupdateautomation_types.go b/api/v1beta2/imageupdateautomation_types.go index 09d243d8..9a966484 100644 --- a/api/v1beta2/imageupdateautomation_types.go +++ b/api/v1beta2/imageupdateautomation_types.go @@ -83,9 +83,9 @@ const ( // inlined here. type UpdateStrategy struct { // Strategy names the strategy to be used. - // +required + // +optional // +kubebuilder:default=Setters - Strategy UpdateStrategyName `json:"strategy"` + Strategy UpdateStrategyName `json:"strategy,omitempty"` // Path to the directory containing the manifests to be updated. // Defaults to 'None', which translates to the root path diff --git a/config/crd/bases/image.toolkit.fluxcd.io_imageupdateautomations.yaml b/config/crd/bases/image.toolkit.fluxcd.io_imageupdateautomations.yaml index a7fca845..734b6c06 100644 --- a/config/crd/bases/image.toolkit.fluxcd.io_imageupdateautomations.yaml +++ b/config/crd/bases/image.toolkit.fluxcd.io_imageupdateautomations.yaml @@ -608,8 +608,6 @@ spec: enum: - Setters type: string - required: - - strategy type: object required: - interval diff --git a/docs/api/v1beta2/image-automation.md b/docs/api/v1beta2/image-automation.md index ba8501ec..556c9e48 100644 --- a/docs/api/v1beta2/image-automation.md +++ b/docs/api/v1beta2/image-automation.md @@ -874,6 +874,7 @@ UpdateStrategyName +(Optional)

Strategy names the strategy to be used.

diff --git a/internal/controller/imageupdateautomation_controller_test.go b/internal/controller/imageupdateautomation_controller_test.go index f48607eb..217d816a 100644 --- a/internal/controller/imageupdateautomation_controller_test.go +++ b/internal/controller/imageupdateautomation_controller_test.go @@ -1409,7 +1409,7 @@ func TestImageUpdateAutomationReconciler_e2e(t *testing.T) { } } -func TestImageUpdateAutomationReconciler_defaulting(t *testing.T) { +func TestImageUpdateAutomationReconciler_DefaultUpdate(t *testing.T) { g := NewWithT(t) branch := rand.String(8) @@ -1468,6 +1468,70 @@ func TestImageUpdateAutomationReconciler_defaulting(t *testing.T) { To(Equal(&imagev1.UpdateStrategy{Strategy: imagev1.UpdateStrategySetters})) } +func TestImageUpdateAutomationReconciler_DefaultStrategy(t *testing.T) { + g := NewWithT(t) + + branch := rand.String(8) + ctx, cancel := context.WithTimeout(context.Background(), timeout) + defer cancel() + + // Create a test namespace. + namespace, err := testEnv.CreateNamespace(ctx, "image-auto-test") + g.Expect(err).ToNot(HaveOccurred()) + defer func() { g.Expect(testEnv.Delete(ctx, namespace)).To(Succeed()) }() + + // Create an instance of ImageUpdateAutomation. + key := types.NamespacedName{ + Name: "update-" + rand.String(5), + Namespace: namespace.Name, + } + auto := &imagev1.ImageUpdateAutomation{ + ObjectMeta: metav1.ObjectMeta{ + Name: key.Name, + Namespace: key.Namespace, + }, + Spec: imagev1.ImageUpdateAutomationSpec{ + SourceRef: imagev1.CrossNamespaceSourceReference{ + Kind: "GitRepository", + Name: "garbage", + }, + Interval: metav1.Duration{Duration: 2 * time.Hour}, + GitSpec: &imagev1.GitSpec{ + Checkout: &imagev1.GitCheckoutSpec{ + Reference: sourcev1.GitRepositoryRef{ + Branch: branch, + }, + }, + Commit: imagev1.CommitSpec{ + Author: imagev1.CommitUser{ + Email: testAuthorEmail, + }, + MessageTemplate: "nothing", + }, + }, + Update: &imagev1.UpdateStrategy{ + Path: "./test-path", + }, + }, + } + g.Expect(testEnv.Create(ctx, auto)).To(Succeed()) + defer func() { + g.Expect(testEnv.Delete(ctx, auto)).To(Succeed()) + }() + + // Should default .spec.update to {strategy: Setters}. + var fetchedAuto imagev1.ImageUpdateAutomation + g.Eventually(func() bool { + err := testEnv.Get(ctx, key, &fetchedAuto) + return err == nil + }, timeout, time.Second).Should(BeTrue()) + g.Expect(fetchedAuto.Spec.Update). + To(Equal(&imagev1.UpdateStrategy{ + Strategy: imagev1.UpdateStrategySetters, + Path: "./test-path", + })) +} + func TestImageUpdateAutomationReconciler_notify(t *testing.T) { g := NewWithT(t) testPushResult, err := source.NewPushResult("branch", "rev", "test commit message")