Skip to content

Commit

Permalink
Implement timeouts in helm reconcile loop
Browse files Browse the repository at this point in the history
Up to now there was a context passed but it was never cancelled.

Signed-off-by: Juan Luis de Sousa-Valadas Castaño <[email protected]>
  • Loading branch information
juanluisvaladas committed Oct 26, 2023
1 parent e49166c commit 62f0a7e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
8 changes: 5 additions & 3 deletions pkg/component/controller/extensions_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func (cr *ChartReconciler) Reconcile(ctx context.Context, req reconcile.Request)
return reconcile.Result{}, nil
}
func (cr *ChartReconciler) uninstall(ctx context.Context, chart v1beta1.Chart) error {
if err := cr.helm.UninstallRelease(chart.Status.ReleaseName, chart.Status.Namespace); err != nil {
if err := cr.helm.UninstallRelease(ctx, chart.Status.ReleaseName, chart.Status.Namespace); err != nil {
return fmt.Errorf("can't uninstall release `%s/%s`: %w", chart.Status.Namespace, chart.Status.ReleaseName, err)
}
return nil
Expand Down Expand Up @@ -252,7 +252,8 @@ func (cr *ChartReconciler) updateOrInstallChart(ctx context.Context, chart v1bet
if chart.Status.ReleaseName == "" {
// new chartRelease
cr.L.Tracef("Start update or install %s", chart.Spec.ChartName)
chartRelease, err = cr.helm.InstallChart(chart.Spec.ChartName,
chartRelease, err = cr.helm.InstallChart(ctx,
chart.Spec.ChartName,
chart.Spec.Version,
chart.Spec.ReleaseName,
chart.Spec.Namespace,
Expand All @@ -265,7 +266,8 @@ func (cr *ChartReconciler) updateOrInstallChart(ctx context.Context, chart v1bet
} else {
if cr.chartNeedsUpgrade(chart) {
// update
chartRelease, err = cr.helm.UpgradeChart(chart.Spec.ChartName,
chartRelease, err = cr.helm.UpgradeChart(ctx,
chart.Spec.ChartName,
chart.Spec.Version,
chart.Status.ReleaseName,
chart.Status.Namespace,
Expand Down
16 changes: 11 additions & 5 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package helm

import (
"context"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -209,7 +210,7 @@ func (hc *Commands) isInstallable(chart *chart.Chart) bool {

// InstallChart installs a helm chart
// InstallChart, UpgradeChart and UninstallRelease(releaseName are *NOT* thread-safe
func (hc *Commands) InstallChart(chartName string, version string, releaseName string, namespace string, values map[string]interface{}, timeout time.Duration) (*release.Release, error) {
func (hc *Commands) InstallChart(ctx context.Context, chartName string, version string, releaseName string, namespace string, values map[string]interface{}, timeout time.Duration) (*release.Release, error) {
cfg, err := hc.getActionCfg(namespace)
if err != nil {
return nil, fmt.Errorf("can't create action configuration: %v", err)
Expand Down Expand Up @@ -249,7 +250,7 @@ func (hc *Commands) InstallChart(chartName string, version string, releaseName s
if err != nil {
return nil, fmt.Errorf("can't reload loadedChart `%s`: %v", chartDir, err)
}
chartRelease, err := install.Run(loadedChart, values)
chartRelease, err := install.RunWithContext(ctx, loadedChart, values)
if err != nil {
return nil, fmt.Errorf("can't install loadedChart `%s`: %v", loadedChart.Name(), err)
}
Expand All @@ -258,7 +259,7 @@ func (hc *Commands) InstallChart(chartName string, version string, releaseName s

// UpgradeChart upgrades a helm chart.
// InstallChart, UpgradeChart and UninstallRelease(releaseName are *NOT* thread-safe
func (hc *Commands) UpgradeChart(chartName string, version string, releaseName string, namespace string, values map[string]interface{}, timeout time.Duration) (*release.Release, error) {
func (hc *Commands) UpgradeChart(ctx context.Context, chartName string, version string, releaseName string, namespace string, values map[string]interface{}, timeout time.Duration) (*release.Release, error) {
cfg, err := hc.getActionCfg(namespace)
if err != nil {
return nil, fmt.Errorf("can't create action configuration: %v", err)
Expand Down Expand Up @@ -292,7 +293,7 @@ func (hc *Commands) UpgradeChart(chartName string, version string, releaseName s
return nil, fmt.Errorf("can't reload loadedChart `%s`: %v", chartDir, err)
}

chartRelease, err := upgrade.Run(releaseName, loadedChart, values)
chartRelease, err := upgrade.RunWithContext(ctx, releaseName, loadedChart, values)
if err != nil {
return nil, fmt.Errorf("can't upgrade loadedChart `%s`: %v", loadedChart.Metadata.Name, err)
}
Expand All @@ -315,12 +316,17 @@ func (hc *Commands) ListReleases(namespace string) ([]*release.Release, error) {

// UninstallRelease uninstalls a release.
// InstallChart, UpgradeChart and UninstallRelease(releaseName are *NOT* thread-safe
func (hc *Commands) UninstallRelease(releaseName string, namespace string) error {
func (hc *Commands) UninstallRelease(ctx context.Context, releaseName string, namespace string) error {
cfg, err := hc.getActionCfg(namespace)
if err != nil {
return fmt.Errorf("can't create helmAction configuration: %v", err)
}
helmAction := action.NewUninstall(cfg)
deadline, ok := ctx.Deadline()
if ok {
helmAction.Timeout = time.Until(deadline)
}

if _, err := helmAction.Run(releaseName); err != nil {
return fmt.Errorf("can't uninstall release `%s`: %v", releaseName, err)
}
Expand Down

0 comments on commit 62f0a7e

Please sign in to comment.