Skip to content

Commit

Permalink
Merge pull request helm#79 from Sajfer/preapply
Browse files Browse the repository at this point in the history
Add preapply hook
  • Loading branch information
yxxhero committed Sep 19, 2022
2 parents 9019cec + bb13ef6 commit 24810a4
Show file tree
Hide file tree
Showing 18 changed files with 712 additions and 11 deletions.
6 changes: 5 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,7 @@ Once `events` are triggered, associated `hooks` are executed, by running the `co
Currently supported `events` are:

- `prepare`
- `preapply`
- `presync`
- `preuninstall`
- `postuninstall`
Expand All @@ -1192,9 +1193,12 @@ Currently supported `events` are:
Hooks associated to `prepare` events are triggered after each release in your helmfile is loaded from YAML, before execution.
`prepare` hooks are triggered on the release as long as it is not excluded by the helmfile selector(e.g. `helmfile -l key=value`).

Hooks associated to `presync` events are triggered before each release is applied to the remote cluster.
Hooks associated to `presync` events are triggered before each release is installed or upgraded on the remote cluster.
This is the ideal event to execute any commands that may mutate the cluster state as it will not be run for read-only operations like `lint`, `diff` or `template`.

`preapply` hooks are triggered before a release is uninstalled, installed, or upgraded as part of `helmfile apply`.
This is the ideal event to hook into when you are going to use `helmfile apply` for every kind of change, and you want the hook to be called only when any kind of change is being made.

`preuninstall` hooks are triggered immediately before a release is uninstalled as part of `helmfile apply`, `helmfile sync`, `helmfile delete`, and `helmfile destroy`.

`postuninstall` hooks are triggered immediately after successful uninstall of a release while running `helmfile apply`, `helmfile sync`, `helmfile delete`, `helmfile destroy`.
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ require (
github.com/variantdev/vals v0.18.0
go.uber.org/multierr v1.6.0
go.uber.org/zap v1.23.0
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
gopkg.in/yaml.v2 v2.4.0
gotest.tools v2.2.0+incompatible
helm.sh/helm/v3 v3.8.1
k8s.io/apimachinery v0.24.4
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9
)

require (
Expand Down Expand Up @@ -194,7 +196,6 @@ require (
k8s.io/client-go v0.23.4 // indirect
k8s.io/klog/v2 v2.60.1 // indirect
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 // indirect
oras.land/oras-go v1.1.0 // indirect
sigs.k8s.io/kustomize/api v0.10.1 // indirect
sigs.k8s.io/kustomize/kyaml v0.13.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
32 changes: 28 additions & 4 deletions pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ func (a *App) visitStates(fileOrDir string, defOpts LoadOpts, converge func(*sta
go func() {
sig := <-sigs

errs := []error{fmt.Errorf("Received [%s] to shutdown ", sig)}
errs := []error{fmt.Errorf("received [%s] to shutdown ", sig)}
_ = context{app: a, st: st, retainValues: defOpts.RetainValuesFiles}.clean(errs)
// See http://tldp.org/LDP/abs/html/exitcodes.html
switch sig {
Expand Down Expand Up @@ -1342,7 +1342,7 @@ Do you really want to apply?
a.Logger.Debug(*infoMsg)
}

applyErrs := []error{}
var applyErrs []error

affectedReleases := state.AffectedReleases{}

Expand All @@ -1355,15 +1355,27 @@ Do you really want to apply?
// We deleted releases by traversing the DAG in reverse order
if len(releasesToBeDeleted) > 0 {
_, deletionErrs := withDAG(st, helm, a.Logger, state.PlanOptions{Reverse: true, SelectedReleases: toDelete, SkipNeeds: true}, a.WrapWithoutSelector(func(subst *state.HelmState, helm helmexec.Interface) []error {
var rs []state.ReleaseSpec
var (
rs []state.ReleaseSpec
preapplyErrors []error
)

for _, r := range subst.Releases {
release := r
if r2, ok := releasesToBeDeleted[state.ReleaseToID(&release)]; ok {
if _, err := st.TriggerPreapplyEvent(&r2, "apply"); err != nil {
preapplyErrors = append(applyErrs, err)
continue
}

rs = append(rs, r2)
}
}

if len(preapplyErrors) > 0 {
return preapplyErrors
}

subst.Releases = rs

return subst.DeleteReleasesForSync(&affectedReleases, helm, c.Concurrency())
Expand All @@ -1377,15 +1389,27 @@ Do you really want to apply?
// We upgrade releases by traversing the DAG
if len(releasesToBeUpdated) > 0 {
_, updateErrs := withDAG(st, helm, a.Logger, state.PlanOptions{SelectedReleases: toUpdate, Reverse: false, SkipNeeds: true, IncludeTransitiveNeeds: c.IncludeTransitiveNeeds()}, a.WrapWithoutSelector(func(subst *state.HelmState, helm helmexec.Interface) []error {
var rs []state.ReleaseSpec
var (
rs []state.ReleaseSpec
preapplyErrors []error
)

for _, r := range subst.Releases {
release := r
if r2, ok := releasesToBeUpdated[state.ReleaseToID(&release)]; ok {
if _, err := st.TriggerPreapplyEvent(&r2, "apply"); err != nil {
preapplyErrors = append(applyErrs, err)
continue
}

rs = append(rs, r2)
}
}

if len(preapplyErrors) > 0 {
return preapplyErrors
}

subst.Releases = rs

syncOpts := state.SyncOpts{
Expand Down
Loading

0 comments on commit 24810a4

Please sign in to comment.