diff --git a/pkg/envvar/const.go b/pkg/envvar/const.go index 7a2e4542..2a79766c 100644 --- a/pkg/envvar/const.go +++ b/pkg/envvar/const.go @@ -2,6 +2,7 @@ package envvar const ( DisableInsecureFeatures = "HELMFILE_DISABLE_INSECURE_FEATURES" + DisableHooks = "HELMFILE_DISABLE_HOOKS" // use helm status to check if a release exists before installing it UseHelmStatusToCheckReleaseExistence = "HELMFILE_USE_HELM_STATUS_TO_CHECK_RELEASE_EXISTENCE" diff --git a/pkg/event/bus.go b/pkg/event/bus.go index 09dfaba6..76e11eba 100644 --- a/pkg/event/bus.go +++ b/pkg/event/bus.go @@ -3,11 +3,14 @@ package event import ( goContext "context" "fmt" + "os" + "strconv" "strings" "go.uber.org/zap" "github.com/helmfile/helmfile/pkg/environment" + "github.com/helmfile/helmfile/pkg/envvar" "github.com/helmfile/helmfile/pkg/filesystem" "github.com/helmfile/helmfile/pkg/helmexec" "github.com/helmfile/helmfile/pkg/tmpl" @@ -42,7 +45,19 @@ type Bus struct { Logger *zap.SugaredLogger } +var ( + disableHooks bool +) + +func init() { + disableHooks, _ = strconv.ParseBool(os.Getenv(envvar.DisableHooks)) +} + func (bus *Bus) Trigger(evt string, evtErr error, context map[string]any) (bool, error) { + if disableHooks && len(bus.Hooks) > 0 { + return false, fmt.Errorf("%s is active, hooks are disabled", envvar.DisableHooks) + } + if bus.Runner == nil { bus.Runner = helmexec.ShellRunner{ Dir: bus.BasePath, diff --git a/pkg/event/bus_test.go b/pkg/event/bus_test.go index 26c4b0f3..2334f175 100644 --- a/pkg/event/bus_test.go +++ b/pkg/event/bus_test.go @@ -7,6 +7,7 @@ import ( "strings" "testing" + "github.com/stretchr/testify/require" "go.uber.org/zap" "go.uber.org/zap/zaptest/observer" @@ -43,6 +44,32 @@ func (r *runner) Execute(cmd string, args []string, env map[string]string, enabl return []byte(""), nil } +func TestTrigger_DisabledHooks(t *testing.T) { + currentVal := disableHooks + + { + disableHooks = true + bus := &Bus{Hooks: []Hook{{}}} + _, err := bus.Trigger("foo", nil, nil) + require.EqualError(t, err, "HELMFILE_DISABLE_HOOKS is active, hooks are disabled") + } + + disableHooks = currentVal +} + +func TestTrigger_DisabledHooksWithEmptyHooks(t *testing.T) { + currentVal := disableHooks + + { + disableHooks = true + bus := &Bus{} + _, err := bus.Trigger("foo", nil, nil) + require.NoError(t, err) + } + + disableHooks = currentVal +} + func TestTrigger(t *testing.T) { cases := []struct { name string