99 "crypto/tls"
1010 "crypto/x509"
1111 "encoding/json"
12+ "errors"
1213 "fmt"
1314 "io"
1415 "net/http"
@@ -21,7 +22,7 @@ import (
2122
2223 corev1 "k8s.io/api/core/v1"
2324 "k8s.io/apimachinery/pkg/api/equality"
24- "k8s.io/apimachinery/pkg/api/errors"
25+ k8serrors "k8s.io/apimachinery/pkg/api/errors"
2526 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2728 "k8s.io/apimachinery/pkg/runtime"
@@ -124,7 +125,7 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
124125
125126 err := r .ManagedClient .Get (ctx , request .NamespacedName , instance )
126127 if err != nil {
127- if errors .IsNotFound (err ) {
128+ if k8serrors .IsNotFound (err ) {
128129 // The replicated policy on the managed cluster was deleted.
129130 // check if it was deleted by user by checking if it still exists on hub
130131 hubInstance := & policiesv1.Policy {}
@@ -133,7 +134,7 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
133134 ctx , types.NamespacedName {Namespace : r .ClusterNamespaceOnHub , Name : request .Name }, hubInstance ,
134135 )
135136 if err != nil {
136- if errors .IsNotFound (err ) {
137+ if k8serrors .IsNotFound (err ) {
137138 // confirmed deleted on hub, doing nothing
138139 reqLogger .Info ("Policy was deleted, no status to update" )
139140
@@ -172,11 +173,11 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
172173 err = r .HubClient .Get (ctx , types.NamespacedName {Namespace : r .ClusterNamespaceOnHub , Name : request .Name }, hubPlc )
173174 if err != nil {
174175 // hub policy not found, it has been deleted
175- if errors .IsNotFound (err ) {
176+ if k8serrors .IsNotFound (err ) {
176177 reqLogger .Info ("Hub policy not found, it has been deleted" )
177178 // try to delete local one
178179 err = r .ManagedClient .Delete (ctx , instance )
179- if err == nil || errors .IsNotFound (err ) {
180+ if err == nil || k8serrors .IsNotFound (err ) {
180181 // no err or err is not found means local policy has been deleted
181182 reqLogger .Info ("Managed policy was deleted" )
182183
@@ -355,32 +356,25 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
355356
356357 return ! history [i ].eventTime .Before (& history [j ].eventTime )
357358 }
358- // Timestamps are the same: attempt to use the event name.
359- // Conventionally (in client-go), the event name has a hexadecimal
360- // nanosecond timestamp as a suffix after a period.
361- iNameParts := strings .Split (history [i ].EventName , "." )
362- jNameParts := strings .Split (history [j ].EventName , "." )
363- errMsg := "Unable to interpret hexadecimal timestamp in event name, " +
364- "can't guarantee ordering of events in this status"
365-
366- iNanos , err := strconv .ParseInt (iNameParts [len (iNameParts )- 1 ], 16 , 64 )
359+
360+ iTime , err := parseTimestampFromEventName (history [i ].EventName )
367361 if err != nil {
368- reqLogger .Error (err , errMsg , "eventName" , history [ i ]. EventName )
362+ reqLogger .Error (err , "Can't guarantee ordering of events in this status" )
369363
370364 return false
371365 }
372366
373- jNanos , err := strconv . ParseInt ( jNameParts [ len ( jNameParts ) - 1 ], 16 , 64 )
367+ jTime , err := parseTimestampFromEventName ( history [ j ]. EventName )
374368 if err != nil {
375- reqLogger .Error (err , errMsg , "eventName" , history [ j ]. EventName )
369+ reqLogger .Error (err , "Can't guarantee ordering of events in this status" )
376370
377371 return false
378372 }
379373
380374 reqLogger .V (2 ).Info ("Event timestamp collision, order determined by hex timestamp in name" ,
381375 "event1Name" , history [i ].EventName , "event2Name" , history [j ].EventName )
382376
383- return iNanos > jNanos
377+ return iTime . After ( jTime . Time )
384378 }
385379
386380 return ! history [i ].LastTimestamp .Time .Before (history [j ].LastTimestamp .Time )
@@ -495,6 +489,19 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
495489 return reconcile.Result {}, nil
496490}
497491
492+ // parseTimestampFromEventName will parse the event name for a hexadecimal nanosecond timestamp as a suffix after a
493+ // period. This is a client-go convention that is repeated in the policy framework.
494+ func parseTimestampFromEventName (eventName string ) (metav1.Time , error ) {
495+ nameParts := strings .Split (eventName , "." )
496+
497+ nanos , err := strconv .ParseInt (nameParts [len (nameParts )- 1 ], 16 , 64 )
498+ if err != nil {
499+ return metav1.Time {}, errors .New ("Unable to find a valid hexadecimal timestamp in event name: " + eventName )
500+ }
501+
502+ return metav1 .Unix (0 , nanos ), nil
503+ }
504+
498505func parseComplianceFromMessage (message string ) policiesv1.ComplianceState {
499506 cleanMsg := strings .ToLower (
500507 strings .TrimSpace (
@@ -535,10 +542,18 @@ func ceRequestFromEvent(event *corev1.Event) (utils.ComplianceAPIEventRequest, e
535542
536543 compliance := parseComplianceFromMessage (event .Message )
537544
545+ var timestamp metav1.Time
546+
547+ if timestampFromEvent , err := parseTimestampFromEventName (event .Name ); err == nil {
548+ timestamp = timestampFromEvent
549+ } else {
550+ timestamp = event .LastTimestamp
551+ }
552+
538553 ce .Event = utils.ComplianceAPIEvent {
539554 Compliance : compliance ,
540555 Message : strings .TrimLeft (event .Message [len (compliance ):], " ;" ),
541- Timestamp : event . LastTimestamp .Format (time .RFC3339Nano ),
556+ Timestamp : timestamp .Format (time .RFC3339Nano ),
542557 ReportedBy : "governance-policy-framework" ,
543558 }
544559
@@ -571,7 +586,7 @@ func StartComplianceEventsSyncer(
571586 var clusterID string
572587
573588 idClusterClaim , err := managedClient .Resource (clusterClaimGVR ).Get (ctx , "id.k8s.io" , metav1.GetOptions {})
574- if err != nil && ! errors .IsNotFound (err ) {
589+ if err != nil && ! k8serrors .IsNotFound (err ) {
575590 return err
576591 }
577592
0 commit comments