feat: support disabling hooks (#2691)

Signed-off-by: fabiobaiao <53570695+fabiobaiao@users.noreply.github.com>
This commit is contained in:
fabiobaiao
2026-07-16 11:54:13 +08:00
committed by GitHub
parent cfe4f87544
commit e26c6fd13f
3 changed files with 43 additions and 0 deletions
+1
View File
@@ -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"
+15
View File
@@ -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,
+27
View File
@@ -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