Skip to content

Commit e9e7e98

Browse files
Merge pull request #4064 from ricardomaraschini/retry-to-save-helm-chart-object
bug: retry saving helm chart object status
2 parents cbaff36 + 5333b91 commit e9e7e98

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

pkg/component/controller/extensions_controller.go

+38-18
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
"k8s.io/apimachinery/pkg/runtime/schema"
4545
"k8s.io/apimachinery/pkg/types"
4646
"k8s.io/client-go/tools/clientcmd"
47+
apiretry "k8s.io/client-go/util/retry"
4748
"sigs.k8s.io/controller-runtime/pkg/builder"
4849
"sigs.k8s.io/controller-runtime/pkg/client"
4950
ctrlconfig "sigs.k8s.io/controller-runtime/pkg/config"
@@ -289,8 +290,13 @@ func (cr *ChartReconciler) updateOrInstallChart(ctx context.Context, chart v1bet
289290
timeout = defaultTimeout
290291
}
291292
defer func() {
292-
if err != nil {
293-
cr.updateStatus(ctx, chart, chartRelease, err)
293+
if err == nil {
294+
return
295+
}
296+
if err := apiretry.RetryOnConflict(apiretry.DefaultRetry, func() error {
297+
return cr.updateStatus(ctx, chart, chartRelease, err)
298+
}); err != nil {
299+
cr.L.WithError(err).Error("Failed to update status for chart release, give up", chart.Name)
294300
}
295301
}()
296302
if chart.Status.ReleaseName == "" {
@@ -323,7 +329,11 @@ func (cr *ChartReconciler) updateOrInstallChart(ctx context.Context, chart v1bet
323329
}
324330
}
325331
}
326-
cr.updateStatus(ctx, chart, chartRelease, nil)
332+
if err := apiretry.RetryOnConflict(apiretry.DefaultRetry, func() error {
333+
return cr.updateStatus(ctx, chart, chartRelease, nil)
334+
}); err != nil {
335+
cr.L.WithError(err).Error("Failed to update status for chart release, give up", chart.Name)
336+
}
327337
return nil
328338
}
329339

@@ -334,26 +344,36 @@ func (cr *ChartReconciler) chartNeedsUpgrade(chart v1beta1.Chart) bool {
334344
chart.Status.ValuesHash == chart.Spec.HashValues())
335345
}
336346

337-
func (cr *ChartReconciler) updateStatus(ctx context.Context, chart v1beta1.Chart, chartRelease *release.Release, err error) {
338-
339-
chart.Spec.YamlValues()
347+
// updateStatus updates the status of the chart with the given release information. This function
348+
// starts by fetching an updated version of the chart from the api as the install may take a while
349+
// to complete and the chart may have been updated in the meantime. If returns the error returned
350+
// by the Update operation. Moreover, if the chart has indeed changed in the meantime we already
351+
// have an event for it so we will see it again soon.
352+
func (cr *ChartReconciler) updateStatus(ctx context.Context, chart v1beta1.Chart, chartRelease *release.Release, err error) error {
353+
nsn := types.NamespacedName{Namespace: chart.Namespace, Name: chart.Name}
354+
var updchart v1beta1.Chart
355+
if err := cr.Get(ctx, nsn, &updchart); err != nil {
356+
return fmt.Errorf("can't get updated version of chart %s: %w", chart.Name, err)
357+
}
358+
chart.Spec.YamlValues() // XXX what is this function for ?
340359
if chartRelease != nil {
341-
chart.Status.ReleaseName = chartRelease.Name
342-
chart.Status.Version = chartRelease.Chart.Metadata.Version
343-
chart.Status.AppVersion = chartRelease.Chart.AppVersion()
344-
chart.Status.Revision = int64(chartRelease.Version)
345-
chart.Status.Namespace = chartRelease.Namespace
346-
}
347-
chart.Status.Updated = time.Now().String()
360+
updchart.Status.ReleaseName = chartRelease.Name
361+
updchart.Status.Version = chartRelease.Chart.Metadata.Version
362+
updchart.Status.AppVersion = chartRelease.Chart.AppVersion()
363+
updchart.Status.Revision = int64(chartRelease.Version)
364+
updchart.Status.Namespace = chartRelease.Namespace
365+
}
366+
updchart.Status.Updated = time.Now().String()
367+
updchart.Status.Error = ""
348368
if err != nil {
349-
chart.Status.Error = err.Error()
350-
} else {
351-
chart.Status.Error = ""
369+
updchart.Status.Error = err.Error()
352370
}
353-
chart.Status.ValuesHash = chart.Spec.HashValues()
354-
if updErr := cr.Client.Status().Update(ctx, &chart); updErr != nil {
371+
updchart.Status.ValuesHash = chart.Spec.HashValues()
372+
if updErr := cr.Client.Status().Update(ctx, &updchart); updErr != nil {
355373
cr.L.WithError(updErr).Error("Failed to update status for chart release", chart.Name)
374+
return updErr
356375
}
376+
return nil
357377
}
358378

359379
func (ec *ExtensionsController) addRepo(repo k0sAPI.Repository) error {

0 commit comments

Comments
 (0)