63
63
)
64
64
65
65
const (
66
- externalSyncCreatedConditionType = "ExternalSyncCreated"
66
+ conditionReconciling = "Reconciling"
67
+ conditionStalled = "Stalled"
68
+
69
+ reasonCreateSync = "CreateSync"
70
+ reasonUpdateSync = "UpdateSync"
71
+ reasonError = "Error"
67
72
)
68
73
69
74
// RemoteRootSyncReconciler reconciles a RemoteRootSync object
@@ -121,10 +126,16 @@ func (r *RemoteRootSyncReconciler) Reconcile(ctx context.Context, req ctrl.Reque
121
126
// The object is being deleted
122
127
if controllerutil .ContainsFinalizer (& remoterootsync , myFinalizerName ) {
123
128
// our finalizer is present, so lets handle any external dependency
124
- if meta . IsStatusConditionTrue ( remoterootsync .Status .Conditions , externalSyncCreatedConditionType ) {
129
+ if remoterootsync .Status .SyncCreated {
125
130
// Delete the external sync resource
126
131
err := r .deleteExternalResources (ctx , & remoterootsync )
127
132
if err != nil && ! apierrors .IsNotFound (err ) {
133
+ statusError := r .updateStatus (ctx , & remoterootsync , "" , err )
134
+
135
+ if statusError != nil {
136
+ logger .Error (statusError , "Failed to update status" )
137
+ }
138
+
128
139
// if fail to delete the external dependency here, return with error
129
140
// so that it can be retried
130
141
return ctrl.Result {}, fmt .Errorf ("have problem to delete external resource: %w" , err )
@@ -145,42 +156,66 @@ func (r *RemoteRootSyncReconciler) Reconcile(ctx context.Context, req ctrl.Reque
145
156
return ctrl.Result {}, nil
146
157
}
147
158
148
- clusterRef := & remoterootsync .Spec .ClusterRef
149
- dynCl , err := r .getDynamicClientForCluster (ctx , clusterRef )
150
- if err != nil {
151
- return ctrl.Result {}, err
152
- }
159
+ syncStatus , syncError := r .syncExternalSync (ctx , & remoterootsync )
153
160
154
- if err := r .patchRootSync (ctx , dynCl , req .Name , & remoterootsync ); err != nil {
161
+ if err := r .updateStatus (ctx , & remoterootsync , syncStatus , syncError ); err != nil {
162
+ logger .Error (err , "Failed to update status" )
155
163
return ctrl.Result {}, err
156
164
}
157
165
158
- r .setupWatches (ctx , remoterootsync .Name , remoterootsync .Namespace , remoterootsync .Spec .ClusterRef )
166
+ return ctrl.Result {}, syncError
167
+ }
168
+
169
+ func (r * RemoteRootSyncReconciler ) syncExternalSync (ctx context.Context , rrs * gitopsv1alpha1.RemoteRootSync ) (string , error ) {
170
+ syncName := rrs .Name
171
+ clusterRef := & rrs .Spec .ClusterRef
159
172
160
- syncStatus , err := checkSyncStatus (ctx , dynCl , req . Name )
173
+ dynCl , err := r . getDynamicClientForCluster (ctx , clusterRef )
161
174
if err != nil {
162
- return ctrl. Result {} , err
175
+ return "" , fmt . Errorf ( "failed to create client: %w" , err )
163
176
}
164
177
165
- if err := r .updateStatus (ctx , & remoterootsync , syncStatus ); err != nil {
166
- logger .Error (err , "Failed to update status" )
167
- return ctrl.Result {}, err
178
+ if err := r .patchRootSync (ctx , dynCl , syncName , rrs ); err != nil {
179
+ return "" , fmt .Errorf ("failed to create/update sync: %w" , err )
168
180
}
169
181
170
- return ctrl.Result {}, nil
182
+ r .setupWatches (ctx , rrs .Name , rrs .Namespace , rrs .Spec .ClusterRef )
183
+
184
+ syncStatus , err := checkSyncStatus (ctx , dynCl , syncName )
185
+ if err != nil {
186
+ return "" , fmt .Errorf ("faild to check status: %w" , err )
187
+ }
188
+
189
+ return syncStatus , nil
171
190
}
172
191
173
- func (r * RemoteRootSyncReconciler ) updateStatus (ctx context.Context , rrs * gitopsv1alpha1.RemoteRootSync , syncStatus string ) error {
192
+ func (r * RemoteRootSyncReconciler ) updateStatus (ctx context.Context , rrs * gitopsv1alpha1.RemoteRootSync , syncStatus string , syncError error ) error {
174
193
logger := klog .FromContext (ctx )
175
194
176
- // Don't update if there are no changes.
177
-
178
195
rrsPrior := rrs .DeepCopy ()
196
+ conditions := & rrs .Status .Conditions
179
197
180
- rrs .Status .SyncStatus = syncStatus
181
- rrs .Status .ObservedGeneration = rrs .Generation
198
+ if syncError == nil {
199
+ rrs .Status .SyncStatus = syncStatus
200
+ rrs .Status .SyncCreated = true
201
+
202
+ meta .RemoveStatusCondition (conditions , conditionReconciling )
203
+ meta .RemoveStatusCondition (conditions , conditionStalled )
204
+ } else {
205
+ reconcileReason := reasonUpdateSync
206
+
207
+ rrs .Status .SyncStatus = "Unknown"
208
+
209
+ if ! rrs .Status .SyncCreated {
210
+ rrs .Status .SyncStatus = ""
211
+ reconcileReason = reasonCreateSync
212
+ }
182
213
183
- meta .SetStatusCondition (& rrs .Status .Conditions , metav1.Condition {Type : externalSyncCreatedConditionType , Status : metav1 .ConditionTrue , Reason : "SyncCreated" })
214
+ meta .SetStatusCondition (conditions , metav1.Condition {Type : conditionReconciling , Status : metav1 .ConditionTrue , Reason : reconcileReason })
215
+ meta .SetStatusCondition (conditions , metav1.Condition {Type : conditionStalled , Status : metav1 .ConditionTrue , Reason : reasonError , Message : syncError .Error ()})
216
+ }
217
+
218
+ rrs .Status .ObservedGeneration = rrs .Generation
184
219
185
220
if reflect .DeepEqual (rrs .Status , rrsPrior .Status ) {
186
221
return nil
0 commit comments