Skip to content

Commit 07abd99

Browse files
authored
RemoteRootSyncSet: clean up old ref status (#3744)
Also simplify the logic a little here.
1 parent 0632148 commit 07abd99

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

porch/controllers/remoterootsyncsets/pkg/controllers/remoterootsyncset/remoterootsync_controller.go

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -126,33 +126,52 @@ func (r *RemoteRootSyncSetReconciler) Reconcile(ctx context.Context, req ctrl.Re
126126

127127
var result ctrl.Result
128128

129-
var patchErrs []error
129+
var applyErrors []error
130130
for _, clusterRef := range subject.Spec.ClusterRefs {
131131
results, err := r.applyToClusterRef(ctx, &subject, clusterRef)
132132
if err != nil {
133-
patchErrs = append(patchErrs, err)
134-
}
135-
if updateTargetStatus(&subject, clusterRef, results, err) {
136-
if err := r.client.Status().Update(ctx, &subject); err != nil {
137-
patchErrs = append(patchErrs, err)
138-
}
133+
klog.Errorf("error applying to ref %v: %v", clusterRef, err)
134+
applyErrors = append(applyErrors, err)
139135
}
140136

137+
updateTargetStatus(&subject, clusterRef, results, err)
138+
139+
// TODO: Do we ever want to do a partial flush of results? Should we exit the loop and re-reconcile?
140+
141141
if results != nil && !(results.AllApplied() && results.AllHealthy()) {
142142
result.Requeue = true
143143
}
144144
}
145145

146-
if len(patchErrs) != 0 {
147-
for _, patchErr := range patchErrs {
148-
klog.Errorf("%v", patchErr)
146+
specTargets := make(map[api.ClusterRef]bool)
147+
for _, ref := range subject.Spec.ClusterRefs {
148+
specTargets[*ref] = true
149+
}
150+
151+
// Remove any old target statuses
152+
var keepTargets []api.TargetStatus
153+
for i := range subject.Status.Targets {
154+
target := &subject.Status.Targets[i]
155+
if specTargets[target.Ref] {
156+
keepTargets = append(keepTargets, *target)
149157
}
150-
return ctrl.Result{}, patchErrs[0]
158+
}
159+
subject.Status.Targets = keepTargets
160+
161+
updateAggregateStatus(&subject)
162+
163+
// TODO: Do this in a lazy way?
164+
if err := r.client.Status().Update(ctx, &subject); err != nil {
165+
return result, fmt.Errorf("error updating status: %w", err)
166+
}
167+
168+
if len(applyErrors) != 0 {
169+
return result, applyErrors[0]
151170
}
152171
return result, nil
153172
}
154173

155-
func updateTargetStatus(subject *api.RemoteRootSyncSet, ref *api.ClusterRef, applyResults *applyset.ApplyResults, err error) bool {
174+
func updateTargetStatus(subject *api.RemoteRootSyncSet, ref *api.ClusterRef, applyResults *applyset.ApplyResults, err error) {
156175
var found *api.TargetStatus
157176
for i := range subject.Status.Targets {
158177
target := &subject.Status.Targets[i]
@@ -186,9 +205,6 @@ func updateTargetStatus(subject *api.RemoteRootSyncSet, ref *api.ClusterRef, app
186205
meta.SetStatusCondition(&found.Conditions, metav1.Condition{Type: "Ready", Status: metav1.ConditionTrue, Reason: "Ready"})
187206
}
188207
}
189-
// TODO: SetStatusCondition should return an indiciation if anything has changes
190-
191-
return updateAggregateStatus(subject)
192208
}
193209

194210
func updateAggregateStatus(subject *api.RemoteRootSyncSet) bool {

0 commit comments

Comments
 (0)