Skip to content

Commit 1ee4b77

Browse files
committed
fixup! Add deprecation handling for .Updated template field
Address PR review feedback - Remove regex pattern matching in favor of t.Execute() error catching - Rename DeprecatedTemplateFieldReason to RemovedTemplateFieldReason - Update all comments from 'deprecated' to 'removed' - Remove TODO comments and improve error handling comments - Update test names and variables to reflect 'removed' terminology Signed-off-by: cappyzawa <[email protected]>
1 parent 7a04844 commit 1ee4b77

File tree

5 files changed

+21
-30
lines changed

5 files changed

+21
-30
lines changed

api/v1beta2/condition_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ const (
3737
// InvalidPolicySelectorReason represents an invalid policy selector.
3838
InvalidPolicySelectorReason string = "InvalidPolicySelector"
3939

40-
// DeprecatedTemplateFieldReason represents usage of deprecated template field.
41-
DeprecatedTemplateFieldReason string = "DeprecatedTemplateField"
40+
// RemovedTemplateFieldReason represents usage of removed template field.
41+
RemovedTemplateFieldReason string = "RemovedTemplateField"
4242
)

internal/controller/imageupdateautomation_controller.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,11 +481,10 @@ func (r *ImageUpdateAutomationReconciler) reconcile(ctx context.Context, sp *pat
481481

482482
pushResult, err = sm.CommitAndPush(ctx, obj, policyResult, pushCfg...)
483483
if err != nil {
484-
// Check if error is due to deprecated template field usage.
484+
// Check if error is due to removed template field usage.
485485
// Set Stalled condition and return nil error to prevent requeue, allowing user to fix template.
486-
// TODO: Remove this block after .Updated template field is completely removed from the API.
487-
if errors.Is(err, source.ErrDeprecatedTemplateField) {
488-
conditions.MarkStalled(obj, imagev1.DeprecatedTemplateFieldReason, "%s", err)
486+
if errors.Is(err, source.ErrRemovedTemplateField) {
487+
conditions.MarkStalled(obj, imagev1.RemovedTemplateFieldReason, "%s", err)
489488
result, retErr = ctrl.Result{}, nil
490489
return
491490
}

internal/controller/imageupdateautomation_controller_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -841,9 +841,8 @@ Automation: %s/update-test
841841
}
842842
}
843843

844-
// TestImageUpdateAutomationReconciler_deprecatedTemplateField tests deprecated .Updated template field usage.
845-
// TODO: Remove this test function after .Updated template field is completely removed from the API.
846-
func TestImageUpdateAutomationReconciler_deprecatedTemplateField(t *testing.T) {
844+
// TestImageUpdateAutomationReconciler_removedTemplateField tests removed .Updated template field usage.
845+
func TestImageUpdateAutomationReconciler_removedTemplateField(t *testing.T) {
847846
g := NewWithT(t)
848847
ctx := context.TODO()
849848

@@ -860,7 +859,7 @@ func TestImageUpdateAutomationReconciler_deprecatedTemplateField(t *testing.T) {
860859
fixture := "testdata/appconfig"
861860
latest := "helloworld:v1.0.0"
862861

863-
deprecatedTemplate := `Commit summary
862+
removedTemplate := `Commit summary
864863
865864
Automation: {{ .AutomationObject }}
866865
@@ -906,7 +905,7 @@ Images:
906905
updateStrategy := &imagev1.UpdateStrategy{
907906
Strategy: imagev1.UpdateStrategySetters,
908907
}
909-
err := createImageUpdateAutomation(ctx, testEnv, "update-test", s.namespace, s.gitRepoName, s.gitRepoNamespace, s.branch, s.branch, "", deprecatedTemplate, "", updateStrategy)
908+
err := createImageUpdateAutomation(ctx, testEnv, "update-test", s.namespace, s.gitRepoName, s.gitRepoNamespace, s.branch, s.branch, "", removedTemplate, "", updateStrategy)
910909
g.Expect(err).ToNot(HaveOccurred())
911910
defer func() {
912911
g.Expect(deleteImageUpdateAutomation(ctx, testEnv, "update-test", s.namespace)).To(Succeed())
@@ -923,8 +922,8 @@ Images:
923922
stalledCondition := apimeta.FindStatusCondition(imageUpdate.Status.Conditions, meta.StalledCondition)
924923
return stalledCondition != nil &&
925924
stalledCondition.Status == metav1.ConditionTrue &&
926-
stalledCondition.Reason == imagev1.DeprecatedTemplateFieldReason &&
927-
strings.Contains(stalledCondition.Message, "template uses deprecated '.Updated' field")
925+
stalledCondition.Reason == imagev1.RemovedTemplateFieldReason &&
926+
strings.Contains(stalledCondition.Message, "template uses removed '.Updated' field")
928927
}, timeout).Should(BeTrue())
929928

930929
head, _ := localRepo.Head()

internal/source/source.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"fmt"
2323
"os"
2424
"path/filepath"
25-
"regexp"
2625
"strings"
2726
"text/template"
2827
"time"
@@ -49,19 +48,15 @@ import (
4948
// ErrInvalidSourceConfiguration is an error for invalid source configuration.
5049
var ErrInvalidSourceConfiguration = errors.New("invalid source configuration")
5150

52-
// ErrDeprecatedTemplateField is an error for deprecated template field usage.
53-
var ErrDeprecatedTemplateField = errors.New("template uses deprecated '.Updated' field. Please use '.Changed' instead. See: https://fluxcd.io/flux/components/image/imageupdateautomations/#message-template")
51+
// ErrRemovedTemplateField is an error for removed template field usage.
52+
var ErrRemovedTemplateField = errors.New("template uses removed '.Updated' field. Please use '.Changed' instead. See: https://fluxcd.io/flux/components/image/imageupdateautomations/#message-template")
5453

5554
const defaultMessageTemplate = `Update from image update automation`
5655

57-
// deprecatedUpdatedPattern matches deprecated .Updated field usage in templates
58-
var deprecatedUpdatedPattern = regexp.MustCompile(`\.Updated\.`)
59-
6056
// TemplateData is the type of the value given to the commit message
6157
// template.
6258
type TemplateData struct {
6359
AutomationObject types.NamespacedName
64-
Updated update.Result
6560
Changed update.ResultV2
6661
Values map[string]string
6762
}
@@ -286,7 +281,6 @@ func (sm SourceManager) CommitAndPush(ctx context.Context, obj *imagev1.ImageUpd
286281
// Perform a Git commit.
287282
templateValues := &TemplateData{
288283
AutomationObject: sm.automationObjKey,
289-
Updated: policyResult.ImageResult,
290284
Changed: policyResult,
291285
Values: obj.Spec.GitSpec.Commit.MessageTemplateValues,
292286
}
@@ -353,10 +347,6 @@ func templateMsg(messageTemplate string, templateValues *TemplateData) (string,
353347
messageTemplate = defaultMessageTemplate
354348
}
355349

356-
if deprecatedUpdatedPattern.MatchString(messageTemplate) {
357-
return "", ErrDeprecatedTemplateField
358-
}
359-
360350
// Includes only functions that are guaranteed to always evaluate to the same result for given input.
361351
// This removes the possibility of accidentally relying on where or when the template runs.
362352
// https://github.com/Masterminds/sprig/blob/3ac42c7bc5e4be6aa534e036fb19dde4a996da2e/functions.go#L70
@@ -367,6 +357,9 @@ func templateMsg(messageTemplate string, templateValues *TemplateData) (string,
367357

368358
b := &strings.Builder{}
369359
if err := t.Execute(b, *templateValues); err != nil {
360+
if strings.Contains(err.Error(), "can't evaluate field Updated in type source.TemplateData") {
361+
return "", ErrRemovedTemplateField
362+
}
370363
return "", fmt.Errorf("failed to run template from spec: %w", err)
371364
}
372365
return b.String(), nil

internal/source/source_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ import (
5555
)
5656

5757
const (
58-
originRemote = "origin"
59-
testCommitTemplateDeprecated = `Commit summary
58+
originRemote = "origin"
59+
testCommitTemplateRemoved = `Commit summary
6060
6161
Automation: {{ .AutomationObject }}
6262
@@ -469,13 +469,13 @@ func test_sourceManager_CommitAndPush(t *testing.T, proto string) {
469469
checkRefSpecBranch string
470470
}{
471471
{
472-
name: "push to cloned branch with deprecated template field",
472+
name: "push to cloned branch with removed template field",
473473
gitSpec: &imagev1.GitSpec{
474474
Push: &imagev1.PushSpec{
475475
Branch: "main",
476476
},
477477
Commit: imagev1.CommitSpec{
478-
MessageTemplate: testCommitTemplateDeprecated,
478+
MessageTemplate: testCommitTemplateRemoved,
479479
},
480480
},
481481
gitRepoReference: &sourcev1.GitRepositoryRef{
@@ -762,7 +762,7 @@ Testing: value
762762
g.Expect(err != nil).To(Equal(tt.wantErr))
763763
if tt.wantErr {
764764
g.Expect(pushResult).To(BeNil())
765-
g.Expect(err).To(MatchError(ErrDeprecatedTemplateField))
765+
g.Expect(err).To(MatchError(ErrRemovedTemplateField))
766766
return
767767
}
768768
if tt.noChange {

0 commit comments

Comments
 (0)