@@ -126,33 +126,52 @@ func (r *RemoteRootSyncSetReconciler) Reconcile(ctx context.Context, req ctrl.Re
126
126
127
127
var result ctrl.Result
128
128
129
- var patchErrs []error
129
+ var applyErrors []error
130
130
for _ , clusterRef := range subject .Spec .ClusterRefs {
131
131
results , err := r .applyToClusterRef (ctx , & subject , clusterRef )
132
132
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 )
139
135
}
140
136
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
+
141
141
if results != nil && ! (results .AllApplied () && results .AllHealthy ()) {
142
142
result .Requeue = true
143
143
}
144
144
}
145
145
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 )
149
157
}
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 ]
151
170
}
152
171
return result , nil
153
172
}
154
173
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 ) {
156
175
var found * api.TargetStatus
157
176
for i := range subject .Status .Targets {
158
177
target := & subject .Status .Targets [i ]
@@ -186,9 +205,6 @@ func updateTargetStatus(subject *api.RemoteRootSyncSet, ref *api.ClusterRef, app
186
205
meta .SetStatusCondition (& found .Conditions , metav1.Condition {Type : "Ready" , Status : metav1 .ConditionTrue , Reason : "Ready" })
187
206
}
188
207
}
189
- // TODO: SetStatusCondition should return an indiciation if anything has changes
190
-
191
- return updateAggregateStatus (subject )
192
208
}
193
209
194
210
func updateAggregateStatus (subject * api.RemoteRootSyncSet ) bool {
0 commit comments