@@ -44,6 +44,7 @@ import (
44
44
"k8s.io/apimachinery/pkg/runtime/schema"
45
45
"k8s.io/apimachinery/pkg/types"
46
46
"k8s.io/client-go/tools/clientcmd"
47
+ apiretry "k8s.io/client-go/util/retry"
47
48
"sigs.k8s.io/controller-runtime/pkg/builder"
48
49
"sigs.k8s.io/controller-runtime/pkg/client"
49
50
ctrlconfig "sigs.k8s.io/controller-runtime/pkg/config"
@@ -289,8 +290,13 @@ func (cr *ChartReconciler) updateOrInstallChart(ctx context.Context, chart v1bet
289
290
timeout = defaultTimeout
290
291
}
291
292
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 )
294
300
}
295
301
}()
296
302
if chart .Status .ReleaseName == "" {
@@ -323,7 +329,11 @@ func (cr *ChartReconciler) updateOrInstallChart(ctx context.Context, chart v1bet
323
329
}
324
330
}
325
331
}
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
+ }
327
337
return nil
328
338
}
329
339
@@ -334,26 +344,36 @@ func (cr *ChartReconciler) chartNeedsUpgrade(chart v1beta1.Chart) bool {
334
344
chart .Status .ValuesHash == chart .Spec .HashValues ())
335
345
}
336
346
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 ?
340
359
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 = ""
348
368
if err != nil {
349
- chart .Status .Error = err .Error ()
350
- } else {
351
- chart .Status .Error = ""
369
+ updchart .Status .Error = err .Error ()
352
370
}
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 {
355
373
cr .L .WithError (updErr ).Error ("Failed to update status for chart release" , chart .Name )
374
+ return updErr
356
375
}
376
+ return nil
357
377
}
358
378
359
379
func (ec * ExtensionsController ) addRepo (repo k0sAPI.Repository ) error {
0 commit comments