@ -22,12 +22,13 @@ import (
"github.com/pkg/errors"
"github.com/pkg/errors"
"helm.sh/helm/v3/pkg/kube"
"helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/release"
helmtime "helm.sh/helm/v3/pkg/time"
helmtime "helm.sh/helm/v3/pkg/time"
)
)
// execHook executes all of the hooks for the given hook event.
// execHook executes all of the hooks for the given hook event.
func ( cfg * Configuration ) execHook ( rl * release . Release , hook release . HookEvent , timeout time . Duration ) error {
func ( cfg * Configuration ) execHook ( rl * release . Release , hook release . HookEvent , timeout time . Duration , syncDeleteHook bool ) error {
executingHooks := [ ] * release . Hook { }
executingHooks := [ ] * release . Hook { }
for _ , h := range rl . Hooks {
for _ , h := range rl . Hooks {
@ -51,7 +52,7 @@ func (cfg *Configuration) execHook(rl *release.Release, hook release.HookEvent,
h . DeletePolicies = [ ] release . HookDeletePolicy { release . HookBeforeHookCreation }
h . DeletePolicies = [ ] release . HookDeletePolicy { release . HookBeforeHookCreation }
}
}
if err := cfg . deleteHookByPolicy ( h , release . HookBeforeHookCreation ); err != nil {
if err := cfg . deleteHookByPolicy ( h , release . HookBeforeHookCreation , syncDeleteHook , timeout ); err != nil {
return err
return err
}
}
@ -88,7 +89,7 @@ func (cfg *Configuration) execHook(rl *release.Release, hook release.HookEvent,
h . LastRun . Phase = release . HookPhaseFailed
h . LastRun . Phase = release . HookPhaseFailed
// If a hook is failed, check the annotation of the hook to determine whether the hook should be deleted
// If a hook is failed, check the annotation of the hook to determine whether the hook should be deleted
// under failed condition. If so, then clear the corresponding resource object in the hook
// under failed condition. If so, then clear the corresponding resource object in the hook
if err := cfg . deleteHookByPolicy ( h , release . HookFailed ); err != nil {
if err := cfg . deleteHookByPolicy ( h , release . HookFailed , syncDeleteHook , timeout ); err != nil {
return err
return err
}
}
return err
return err
@ -99,7 +100,7 @@ func (cfg *Configuration) execHook(rl *release.Release, hook release.HookEvent,
// If all hooks are successful, check the annotation of each hook to determine whether the hook should be deleted
// If all hooks are successful, check the annotation of each hook to determine whether the hook should be deleted
// under succeeded condition. If so, then clear the corresponding resource object in each hook
// under succeeded condition. If so, then clear the corresponding resource object in each hook
for _ , h := range executingHooks {
for _ , h := range executingHooks {
if err := cfg . deleteHookByPolicy ( h , release . HookSucceeded ); err != nil {
if err := cfg . deleteHookByPolicy ( h , release . HookSucceeded , syncDeleteHook , timeout ); err != nil {
return err
return err
}
}
}
}
@ -120,7 +121,7 @@ func (x hookByWeight) Less(i, j int) bool {
}
}
// deleteHookByPolicy deletes a hook if the hook policy instructs it to
// deleteHookByPolicy deletes a hook if the hook policy instructs it to
func ( cfg * Configuration ) deleteHookByPolicy ( h * release . Hook , policy release . HookDeletePolicy ) error {
func ( cfg * Configuration ) deleteHookByPolicy ( h * release . Hook , policy release . HookDeletePolicy , syncDeleteHook bool , deleteTimeout time . Duration ) error {
// Never delete CustomResourceDefinitions; this could cause lots of
// Never delete CustomResourceDefinitions; this could cause lots of
// cascading garbage collection.
// cascading garbage collection.
if h . Kind == "CustomResourceDefinition" {
if h . Kind == "CustomResourceDefinition" {
@ -135,6 +136,13 @@ func (cfg *Configuration) deleteHookByPolicy(h *release.Hook, policy release.Hoo
if len ( errs ) > 0 {
if len ( errs ) > 0 {
return errors . New ( joinErrors ( errs ) )
return errors . New ( joinErrors ( errs ) )
}
}
if syncDeleteHook {
if kubeClient , ok := cfg . KubeClient . ( kube . InterfaceExt ) ; ok {
if err := kubeClient . WaitForDelete ( resources , deleteTimeout ) ; err != nil {
return err
}
}
}
}
}
return nil
return nil
}
}