Remove logic that limits execution of hooks to only once

Signed-off-by: Anton Bretting <sajfer@gmail.com>
This commit is contained in:
Anton Bretting 2022-06-11 13:51:56 +02:00 committed by Yusuke Kuoka
parent 32477e6cf2
commit c6e8afd3d0
4 changed files with 16 additions and 41 deletions

View File

@ -6,8 +6,9 @@ Running preapply hook for foo:
hook[preapply] logs | foo hook[preapply] logs | foo
hook[preapply] logs | hook[preapply] logs |
hook[echo]: already executed by "preapply", skipping "presync"
hook[presync] logs | foo
hook[presync] logs |
UPDATED RELEASES: UPDATED RELEASES:
NAME CHART VERSION NAME CHART VERSION

View File

@ -11,7 +11,6 @@ import (
"github.com/helmfile/helmfile/pkg/helmexec" "github.com/helmfile/helmfile/pkg/helmexec"
"github.com/helmfile/helmfile/pkg/tmpl" "github.com/helmfile/helmfile/pkg/tmpl"
"go.uber.org/zap" "go.uber.org/zap"
"golang.org/x/exp/slices"
) )
type Hook struct { type Hook struct {
@ -21,7 +20,6 @@ type Hook struct {
Kubectl map[string]string `yaml:"kubectlApply,omitempty"` Kubectl map[string]string `yaml:"kubectlApply,omitempty"`
Args []string `yaml:"args"` Args []string `yaml:"args"`
ShowLogs bool `yaml:"showlogs"` ShowLogs bool `yaml:"showlogs"`
Executed bool `yaml:"-"`
} }
type event struct { type event struct {
@ -74,10 +72,6 @@ func (bus *Bus) Trigger(evt string, evtErr error, context map[string]interface{}
} }
} }
if slices.Contains(hook.Events, "preapply") && evt == "presync" && hook.Executed {
bus.Logger.Infof("hook[%s]: already executed by \"preapply\", skipping \"%s\"\n", name, evt)
continue
}
if hook.Kubectl != nil { if hook.Kubectl != nil {
if hook.Command != "" { if hook.Command != "" {
bus.Logger.Warnf("warn: ignoring command '%s' given within a kubectlApply hook", hook.Command) bus.Logger.Warnf("warn: ignoring command '%s' given within a kubectlApply hook", hook.Command)

View File

@ -41,21 +41,21 @@ func TestTrigger(t *testing.T) {
}{ }{
{ {
"okhook1", "okhook1",
&Hook{"okhook1", []string{"foo"}, "ok", nil, []string{}, true, false}, &Hook{"okhook1", []string{"foo"}, "ok", nil, []string{}, true},
"foo", "foo",
true, true,
"", "",
}, },
{ {
"okhooké", "okhooké",
&Hook{"okhook2", []string{"foo"}, "ok", nil, []string{}, false, false}, &Hook{"okhook2", []string{"foo"}, "ok", nil, []string{}, false},
"foo", "foo",
true, true,
"", "",
}, },
{ {
"missinghook1", "missinghook1",
&Hook{"okhook1", []string{"foo"}, "ok", nil, []string{}, false, false}, &Hook{"okhook1", []string{"foo"}, "ok", nil, []string{}, false},
"bar", "bar",
false, false,
"", "",
@ -69,70 +69,70 @@ func TestTrigger(t *testing.T) {
}, },
{ {
"nghook1", "nghook1",
&Hook{"nghook1", []string{"foo"}, "ng", nil, []string{}, false, false}, &Hook{"nghook1", []string{"foo"}, "ng", nil, []string{}, false},
"foo", "foo",
false, false,
"hook[nghook1]: command `ng` failed: cmd failed due to invalid cmd: ng", "hook[nghook1]: command `ng` failed: cmd failed due to invalid cmd: ng",
}, },
{ {
"nghook2", "nghook2",
&Hook{"nghook2", []string{"foo"}, "ok", nil, []string{"ng"}, false, false}, &Hook{"nghook2", []string{"foo"}, "ok", nil, []string{"ng"}, false},
"foo", "foo",
false, false,
"hook[nghook2]: command `ok` failed: cmd failed due to invalid arg: ng", "hook[nghook2]: command `ok` failed: cmd failed due to invalid arg: ng",
}, },
{ {
"okkubeapply1", "okkubeapply1",
&Hook{"okkubeapply1", []string{"foo"}, "", map[string]string{"kustomize": "kustodir"}, []string{}, false, false}, &Hook{"okkubeapply1", []string{"foo"}, "", map[string]string{"kustomize": "kustodir"}, []string{}, false},
"foo", "foo",
true, true,
"", "",
}, },
{ {
"okkubeapply2", "okkubeapply2",
&Hook{"okkubeapply2", []string{"foo"}, "", map[string]string{"filename": "resource.yaml"}, []string{}, false, false}, &Hook{"okkubeapply2", []string{"foo"}, "", map[string]string{"filename": "resource.yaml"}, []string{}, false},
"foo", "foo",
true, true,
"", "",
}, },
{ {
"kokubeapply", "kokubeapply",
&Hook{"kokubeapply", []string{"foo"}, "", map[string]string{"kustomize": "kustodir", "filename": "resource.yaml"}, []string{}, true, false}, &Hook{"kokubeapply", []string{"foo"}, "", map[string]string{"kustomize": "kustodir", "filename": "resource.yaml"}, []string{}, true},
"foo", "foo",
false, false,
"hook[kokubeapply]: kustomize & filename cannot be used together", "hook[kokubeapply]: kustomize & filename cannot be used together",
}, },
{ {
"kokubeapply2", "kokubeapply2",
&Hook{"kokubeapply2", []string{"foo"}, "", map[string]string{}, []string{}, true, false}, &Hook{"kokubeapply2", []string{"foo"}, "", map[string]string{}, []string{}, true},
"foo", "foo",
false, false,
"hook[kokubeapply2]: either kustomize or filename must be given", "hook[kokubeapply2]: either kustomize or filename must be given",
}, },
{ {
"kokubeapply3", "kokubeapply3",
&Hook{"", []string{"foo"}, "", map[string]string{}, []string{}, true, false}, &Hook{"", []string{"foo"}, "", map[string]string{}, []string{}, true},
"foo", "foo",
false, false,
"hook[kubectlApply]: either kustomize or filename must be given", "hook[kubectlApply]: either kustomize or filename must be given",
}, },
{ {
"warnkubeapply1", "warnkubeapply1",
&Hook{"warnkubeapply1", []string{"foo"}, "ok", map[string]string{"filename": "resource.yaml"}, []string{}, true, false}, &Hook{"warnkubeapply1", []string{"foo"}, "ok", map[string]string{"filename": "resource.yaml"}, []string{}, true},
"foo", "foo",
true, true,
"", "",
}, },
{ {
"warnkubeapply2", "warnkubeapply2",
&Hook{"warnkubeapply2", []string{"foo"}, "", map[string]string{"filename": "resource.yaml"}, []string{"ng"}, true, false}, &Hook{"warnkubeapply2", []string{"foo"}, "", map[string]string{"filename": "resource.yaml"}, []string{"ng"}, true},
"foo", "foo",
true, true,
"", "",
}, },
{ {
"warnkubeapply3", "warnkubeapply3",
&Hook{"warnkubeapply3", []string{"foo"}, "ok", map[string]string{"filename": "resource.yaml"}, []string{"ng"}, true, false}, &Hook{"warnkubeapply3", []string{"foo"}, "ok", map[string]string{"filename": "resource.yaml"}, []string{"ng"}, true},
"foo", "foo",
true, true,
"", "",

View File

@ -19,7 +19,6 @@ import (
"github.com/imdario/mergo" "github.com/imdario/mergo"
"github.com/variantdev/chartify" "github.com/variantdev/chartify"
"golang.org/x/exp/slices"
"github.com/helmfile/helmfile/pkg/environment" "github.com/helmfile/helmfile/pkg/environment"
"github.com/helmfile/helmfile/pkg/event" "github.com/helmfile/helmfile/pkg/event"
@ -2266,15 +2265,6 @@ func (st *HelmState) TriggerPreapplyEvent(r *ReleaseSpec, helmfileCommand string
func (st *HelmState) triggerReleaseEvent(evt string, evtErr error, r *ReleaseSpec, helmfileCmd string) (bool, error) { func (st *HelmState) triggerReleaseEvent(evt string, evtErr error, r *ReleaseSpec, helmfileCmd string) (bool, error) {
var stateRelease ReleaseSpec
for id, release := range st.Releases {
if release.Name == r.Name {
stateRelease = st.Releases[id]
break
}
}
bus := &event.Bus{ bus := &event.Bus{
Hooks: r.Hooks, Hooks: r.Hooks,
StateFilePath: st.FilePath, StateFilePath: st.FilePath,
@ -2292,17 +2282,7 @@ func (st *HelmState) triggerReleaseEvent(evt string, evtErr error, r *ReleaseSpe
"HelmfileCommand": helmfileCmd, "HelmfileCommand": helmfileCmd,
} }
executed, err := bus.Trigger(evt, evtErr, data) return bus.Trigger(evt, evtErr, data)
if executed {
for id, hook := range stateRelease.Hooks {
if slices.Contains(hook.Events, evt) {
stateRelease.Hooks[id].Executed = true
}
}
}
return executed, err
} }
// ResolveDeps returns a copy of this helmfile state with the concrete chart version numbers filled in for remote chart dependencies // ResolveDeps returns a copy of this helmfile state with the concrete chart version numbers filled in for remote chart dependencies