@@ -18,6 +18,7 @@ package controllers
18
18
19
19
import (
20
20
"context"
21
+ "errors"
21
22
"fmt"
22
23
"time"
23
24
@@ -107,7 +108,7 @@ func (r *GroupSyncReconciler) Reconcile(context context.Context, req ctrl.Reques
107
108
return r .wrapMetricsErrorWithMetrics (prometheusLabels , context , instance , err )
108
109
}
109
110
110
- syncStartTime := ISO8601 ( time .Now ())
111
+ syncStartTime := time .Now (). Format ( time . RFC3339 )
111
112
// Perform Sync
112
113
groups , err := groupSyncer .Sync ()
113
114
@@ -162,7 +163,7 @@ func (r *GroupSyncReconciler) Reconcile(context context.Context, req ctrl.Reques
162
163
ocpGroup .Labels [constants .SyncProvider ] = providerLabel
163
164
164
165
// Add Gloabl Annotations/Labels
165
- ocpGroup .Annotations [constants .SyncTimestamp ] = ISO8601 ( time .Now ())
166
+ ocpGroup .Annotations [constants .SyncTimestamp ] = time .Now (). UTC (). Format ( time . RFC3339 )
166
167
167
168
ocpGroup .Users = group .Users
168
169
@@ -205,7 +206,7 @@ func (r *GroupSyncReconciler) Reconcile(context context.Context, req ctrl.Reques
205
206
if err == nil && instance .Spec .Schedule != "" {
206
207
sched , _ := cron .ParseStandard (instance .Spec .Schedule )
207
208
208
- currentTime := time .Now ()
209
+ currentTime := time .Now (). UTC ()
209
210
nextScheduledTime := sched .Next (currentTime )
210
211
nextScheduledSynchronization .With (prometheus.Labels {METRICS_CR_NAMESPACE_LABEL : instance .GetNamespace (), METRICS_CR_NAME_LABEL : instance .GetName ()}).Set (float64 (nextScheduledTime .UTC ().Unix ()))
211
212
successResult .RequeueAfter = nextScheduledTime .Sub (currentTime )
@@ -231,6 +232,14 @@ func (r *GroupSyncReconciler) wrapMetricsErrorWithMetrics(prometheusLabels prome
231
232
232
233
func (r * GroupSyncReconciler ) pruneGroups (context context.Context , instance * redhatcopv1alpha1.GroupSync , providerLabel string , syncStartTime string , logger logr.Logger ) (int , error ) {
233
234
prunedGroups := 0
235
+
236
+ syncStartDatetime , syncStartParseError := time .Parse (time .RFC3339 , syncStartTime )
237
+
238
+ // Should not occur
239
+ if syncStartParseError != nil {
240
+ return prunedGroups , syncStartParseError
241
+ }
242
+
234
243
ocpGroups := & userv1.GroupList {}
235
244
opts := []client.ListOption {
236
245
client .InNamespace ("" ),
@@ -242,29 +251,32 @@ func (r *GroupSyncReconciler) pruneGroups(context context.Context, instance *red
242
251
}
243
252
244
253
for _ , group := range ocpGroups .Items {
245
- if group .Annotations [constants .SyncTimestamp ] < syncStartTime {
246
- logger .Info ("pruneGroups" , "Delete Group" , group .Name )
247
- err = r .GetClient ().Delete (context , & group )
248
- prunedGroups ++
249
- if err != nil {
250
- return prunedGroups , err
254
+
255
+ if groupSyncTime , ok := group .Annotations [constants .SyncTimestamp ]; ok {
256
+
257
+ groupSyncDatetime , groupSyncTimeParseErr := time .Parse (time .RFC3339 , groupSyncTime )
258
+
259
+ if groupSyncTimeParseErr == nil {
260
+ if groupSyncDatetime .Before (syncStartDatetime ) {
261
+ logger .Info ("pruneGroups" , "Delete Group" , group .Name )
262
+ err = r .GetClient ().Delete (context , & group )
263
+ prunedGroups ++
264
+ if err != nil {
265
+ return prunedGroups , err
266
+ }
267
+ }
268
+ } else {
269
+ if groupSyncTimeParseErr != nil {
270
+ logger .Error (groupSyncTimeParseErr , "Error parsing group start time annotation" , "Group Name" , group .Name , "Time" , syncStartTime )
271
+ }
251
272
}
273
+ } else {
274
+ logger .Error (errors .New ("unable to locate sync timestamp annotation" ), "Group Name" , group .Name )
252
275
}
253
276
}
254
277
return prunedGroups , nil
255
278
}
256
279
257
- func ISO8601 (t time.Time ) string {
258
- var tz string
259
- if zone , offset := t .Zone (); zone == "UTC" {
260
- tz = "Z"
261
- } else {
262
- tz = fmt .Sprintf ("%03d00" , offset / 3600 )
263
- }
264
- return fmt .Sprintf ("%04d-%02d-%02dT%02d:%02d:%02d%s" ,
265
- t .Year (), t .Month (), t .Day (), t .Hour (), t .Minute (), t .Second (), tz )
266
- }
267
-
268
280
func mergeMap (m1 , m2 map [string ]string ) map [string ]string {
269
281
270
282
if m1 != nil {
0 commit comments