Skip to content

Commit 9215707

Browse files
Merge pull request #263 from sabre1041/prune-group-parse-time
Correct logic for parsing and comparing sync time
2 parents 5534fa1 + e7b85d6 commit 9215707

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

controllers/groupsync_controller.go

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package controllers
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223
"time"
2324

@@ -107,7 +108,7 @@ func (r *GroupSyncReconciler) Reconcile(context context.Context, req ctrl.Reques
107108
return r.wrapMetricsErrorWithMetrics(prometheusLabels, context, instance, err)
108109
}
109110

110-
syncStartTime := ISO8601(time.Now())
111+
syncStartTime := time.Now().Format(time.RFC3339)
111112
// Perform Sync
112113
groups, err := groupSyncer.Sync()
113114

@@ -162,7 +163,7 @@ func (r *GroupSyncReconciler) Reconcile(context context.Context, req ctrl.Reques
162163
ocpGroup.Labels[constants.SyncProvider] = providerLabel
163164

164165
// Add Gloabl Annotations/Labels
165-
ocpGroup.Annotations[constants.SyncTimestamp] = ISO8601(time.Now())
166+
ocpGroup.Annotations[constants.SyncTimestamp] = time.Now().UTC().Format(time.RFC3339)
166167

167168
ocpGroup.Users = group.Users
168169

@@ -205,7 +206,7 @@ func (r *GroupSyncReconciler) Reconcile(context context.Context, req ctrl.Reques
205206
if err == nil && instance.Spec.Schedule != "" {
206207
sched, _ := cron.ParseStandard(instance.Spec.Schedule)
207208

208-
currentTime := time.Now()
209+
currentTime := time.Now().UTC()
209210
nextScheduledTime := sched.Next(currentTime)
210211
nextScheduledSynchronization.With(prometheus.Labels{METRICS_CR_NAMESPACE_LABEL: instance.GetNamespace(), METRICS_CR_NAME_LABEL: instance.GetName()}).Set(float64(nextScheduledTime.UTC().Unix()))
211212
successResult.RequeueAfter = nextScheduledTime.Sub(currentTime)
@@ -231,6 +232,14 @@ func (r *GroupSyncReconciler) wrapMetricsErrorWithMetrics(prometheusLabels prome
231232

232233
func (r *GroupSyncReconciler) pruneGroups(context context.Context, instance *redhatcopv1alpha1.GroupSync, providerLabel string, syncStartTime string, logger logr.Logger) (int, error) {
233234
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+
234243
ocpGroups := &userv1.GroupList{}
235244
opts := []client.ListOption{
236245
client.InNamespace(""),
@@ -242,29 +251,32 @@ func (r *GroupSyncReconciler) pruneGroups(context context.Context, instance *red
242251
}
243252

244253
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+
}
251272
}
273+
} else {
274+
logger.Error(errors.New("unable to locate sync timestamp annotation"), "Group Name", group.Name)
252275
}
253276
}
254277
return prunedGroups, nil
255278
}
256279

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-
268280
func mergeMap(m1, m2 map[string]string) map[string]string {
269281

270282
if m1 != nil {

pkg/provider/ldap/helpers/groupsyncer.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -172,19 +172,7 @@ func (s *LDAPGroupSyncer) makeOpenShiftGroup(ldapGroupUID string, usernames []st
172172

173173
// overwrite Group Users data
174174
group.Users = usernames
175-
group.Annotations[LDAPSyncTimeAnnotation] = ISO8601(time.Now())
175+
group.Annotations[LDAPSyncTimeAnnotation] = time.Now().UTC().Format(time.RFC3339)
176176

177177
return group, nil
178178
}
179-
180-
// ISO8601 returns an ISO 6801 formatted string from a time.
181-
func ISO8601(t time.Time) string {
182-
var tz string
183-
if zone, offset := t.Zone(); zone == "UTC" {
184-
tz = "Z"
185-
} else {
186-
tz = fmt.Sprintf("%03d00", offset/3600)
187-
}
188-
return fmt.Sprintf("%04d-%02d-%02dT%02d:%02d:%02d%s",
189-
t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), tz)
190-
}

0 commit comments

Comments
 (0)