Skip to content

Commit 02727e7

Browse files
committed
Refactors kstatus to handle unstructured
1 parent 742fc00 commit 02727e7

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

pkg/patterns/addon/pkg/status/aggregate.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ func (a *aggregator) Reconciled(ctx context.Context, src declarative.Declarative
123123
}
124124

125125
log.WithValues("name", unstruct.GetName()).WithValues("status", status).Info("updating status")
126-
fmt.Println("here")
127126
err = a.client.Status().Update(ctx, src)
128127
if err != nil {
129128
log.Error(err, "updating status")
@@ -136,7 +135,7 @@ func (a *aggregator) Reconciled(ctx context.Context, src declarative.Declarative
136135
}
137136

138137
func (a *aggregator) deployment(ctx context.Context, src declarative.DeclarativeObject, name string) (bool, error) {
139-
key := client.ObjectKey{Namespace: src.GetNamespace(), Name: name}
138+
key := client.ObjectKey{Name: name}
140139
dep := &appsv1.Deployment{}
141140

142141
if err := a.client.Get(ctx, key, dep); err != nil {

pkg/patterns/declarative/reconciler.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -291,43 +291,45 @@ func (r *Reconciler) reconcileExists(ctx context.Context, name types.NamespacedN
291291

292292
addonObject, ok := instance.(addonsv1alpha1.CommonObject)
293293
unstruct, unstructOk := instance.(*unstructured.Unstructured)
294-
aggregatedStatus := string(aggregateStatus(statusMap))
294+
aggregatedPhase := string(aggregateStatus(statusMap))
295+
changed := false
295296
if ok {
296297
addonStatus := addonObject.GetCommonStatus()
297-
if addonStatus.Phase != aggregatedStatus {
298-
addonStatus.Phase = aggregatedStatus
298+
if addonStatus.Phase != aggregatedPhase {
299+
addonStatus.Phase = aggregatedPhase
299300
addonObject.SetCommonStatus(addonStatus)
300-
log.WithValues("name", addonObject.GetName()).WithValues("status", addonStatus).Info("updating status")
301-
err = r.client.Status().Update(ctx, addonObject)
302-
if err != nil {
303-
log.Error(err, "error updating status")
304-
return reconcile.Result{}, err
305-
}
301+
changed = true
306302
}
307303
} else if unstructOk {
308304
statusPhase, _, err := unstructured.NestedString(unstruct.Object, "status", "phase")
309305
if err != nil {
306+
log.Error(err, "error retrieving status")
310307
return reconcile.Result{}, err
311308
}
312309

313-
if statusPhase != aggregatedStatus {
314-
err := unstructured.SetNestedField(unstruct.Object, statusPhase, "status", "phase")
315-
if err != nil {
316-
return reconcile.Result{}, err
317-
}
318-
log.WithValues("name", addonObject.GetName()).WithValues("phase", statusPhase).Info("updating status")
319-
err = r.client.Status().Update(ctx, unstruct)
310+
if statusPhase != aggregatedPhase {
311+
err := unstructured.SetNestedField(unstruct.Object, aggregatedPhase, "status", "phase")
320312
if err != nil {
321-
log.Error(err, "error updating status")
313+
log.Error(err, "error retrieving status")
322314
return reconcile.Result{}, err
323315
}
316+
changed = true
324317
}
325318
} else {
326319
return reconcile.Result{}, fmt.Errorf("instance %T was not an addonsv1alpha1.CommonObject or unstructured." +
327320
"Unstructured",
328321
instance)
329322
}
330323

324+
if changed == true {
325+
log.WithValues("name", instance.GetName()).WithValues("phase", aggregatedPhase).Info("updating status")
326+
err = r.client.Status().Update(ctx, instance)
327+
if err != nil {
328+
log.Error(err, "error updating status")
329+
return reconcile.Result{}, err
330+
}
331+
}
332+
331333
if r.options.sink != nil {
332334
if err := r.options.sink.Notify(ctx, instance, objects); err != nil {
333335
log.Error(err, "notifying sink")
@@ -620,12 +622,13 @@ func aggregateStatus(m map[status.Status]bool) status.Status {
620622
failed := m[status.FailedStatus]
621623

622624
if inProgress || terminating {
623-
return status.TerminatingStatus
625+
return status.InProgressStatus
624626
}
625627

626628
if failed {
627629
return status.FailedStatus
628630
}
631+
629632
return status.CurrentStatus
630633
}
631634

0 commit comments

Comments
 (0)