From 6e607620673f321cd67584cbcbfb324dd5f521e2 Mon Sep 17 00:00:00 2001 From: Alexander Matyushentsev Date: Fri, 11 Oct 2019 10:47:21 -0700 Subject: [PATCH] Issue #2480 - Helm Hook is executed twice if annotated with both pre-install and pre-upgrade annotations (#2481) --- controller/sync_phase.go | 8 ++++++-- controller/sync_phase_test.go | 9 +++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/controller/sync_phase.go b/controller/sync_phase.go index ee639f5273..2bb32aa548 100644 --- a/controller/sync_phase.go +++ b/controller/sync_phase.go @@ -11,13 +11,17 @@ func syncPhases(obj *unstructured.Unstructured) []v1alpha1.SyncPhase { if hook.Skip(obj) { return nil } else if hook.IsHook(obj) { - var phases []v1alpha1.SyncPhase + phasesMap := make(map[v1alpha1.SyncPhase]bool) for _, hookType := range hook.Types(obj) { switch hookType { case v1alpha1.HookTypePreSync, v1alpha1.HookTypeSync, v1alpha1.HookTypePostSync, v1alpha1.HookTypeSyncFail: - phases = append(phases, v1alpha1.SyncPhase(hookType)) + phasesMap[v1alpha1.SyncPhase(hookType)] = true } } + var phases []v1alpha1.SyncPhase + for phase := range phasesMap { + phases = append(phases, phase) + } return phases } else { return []v1alpha1.SyncPhase{v1alpha1.SyncPhaseSync} diff --git a/controller/sync_phase_test.go b/controller/sync_phase_test.go index 2fe35c41fa..1b0b9b7b4a 100644 --- a/controller/sync_phase_test.go +++ b/controller/sync_phase_test.go @@ -43,6 +43,15 @@ func TestSyncPhaseTwoPhases(t *testing.T) { assert.ElementsMatch(t, []SyncPhase{SyncPhasePreSync, SyncPhasePostSync}, syncPhases(pod("PreSync,PostSync"))) } +func TestSyncDuplicatedPhases(t *testing.T) { + assert.ElementsMatch(t, []SyncPhase{SyncPhasePreSync}, syncPhases(pod("PreSync,PreSync"))) + assert.ElementsMatch(t, []SyncPhase{SyncPhasePreSync}, syncPhases(podWithHelmHook("pre-install,pre-upgrade"))) +} + func pod(hookType string) *unstructured.Unstructured { return test.Annotate(test.NewPod(), "argocd.argoproj.io/hook", hookType) } + +func podWithHelmHook(hookType string) *unstructured.Unstructured { + return test.Annotate(test.NewPod(), "helm.sh/hook", hookType) +}