@@ -21,6 +21,7 @@ import (
2121 "io"
2222 "os"
2323 "strconv"
24+ "time"
2425
2526 "github.com/pkg/errors"
2627 "go.opentelemetry.io/otel/trace"
@@ -50,6 +51,7 @@ import (
5051 "github.com/crunchydata/postgres-operator/internal/pgmonitor"
5152 "github.com/crunchydata/postgres-operator/internal/pki"
5253 "github.com/crunchydata/postgres-operator/internal/postgres"
54+ "github.com/crunchydata/postgres-operator/internal/util"
5355 "github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
5456)
5557
@@ -61,15 +63,17 @@ const (
6163// Reconciler holds resources for the PostgresCluster reconciler
6264type Reconciler struct {
6365 Client client.Client
64- Owner client.FieldOwner
65- Recorder record.EventRecorder
66- Tracer trace.Tracer
6766 IsOpenShift bool
68-
69- PodExec func (
67+ Owner client.FieldOwner
68+ PGOVersion string
69+ PodExec func (
7070 namespace , pod , container string ,
7171 stdin io.Reader , stdout , stderr io.Writer , command ... string ,
7272 ) error
73+ Recorder record.EventRecorder
74+ Registration util.Registration
75+ RegistrationURL string
76+ Tracer trace.Tracer
7377}
7478
7579// +kubebuilder:rbac:groups="",resources="events",verbs={create,patch}
@@ -209,13 +213,17 @@ func (r *Reconciler) Reconcile(
209213 return result , err
210214 }
211215
212- if config .RegistrationRequired () {
216+ if config .RegistrationRequired () && ! r . registrationValid () {
213217 if ! registrationRequiredStatusFound (cluster ) {
214- addRegistrationRequiredStatus (cluster )
218+ addRegistrationRequiredStatus (cluster , r . PGOVersion )
215219 return patchClusterStatus ()
216220 }
217221
218- if shouldEncumberReconciliation (cluster ) {
222+ if r .tokenAuthenticationFailed () {
223+ r .Recorder .Event (cluster , corev1 .EventTypeWarning , "Token Authentication Failed" , "See " + r .RegistrationURL + " for details." )
224+ }
225+
226+ if shouldEncumberReconciliation (r .Registration .Authenticated , cluster , r .PGOVersion ) {
219227 emitEncumbranceWarning (cluster , r )
220228 // Encumbrance is just an early return from the reconciliation loop.
221229 return patchClusterStatus ()
@@ -224,6 +232,17 @@ func (r *Reconciler) Reconcile(
224232 }
225233 }
226234
235+ if config .RegistrationRequired () && r .registrationValid () {
236+ if tokenRequiredConditionFound (cluster ) {
237+ meta .RemoveStatusCondition (& cluster .Status .Conditions , v1beta1 .TokenRequired )
238+ }
239+
240+ if registrationRequiredStatusFound (cluster ) {
241+ cluster .Status .RegistrationRequired = nil
242+ r .Recorder .Event (cluster , corev1 .EventTypeNormal , "Token Verified" , "Thank you for registering your installation of Crunchy Postgres for Kubernetes." )
243+ }
244+ }
245+
227246 // if the cluster is paused, set a condition and return
228247 if cluster .Spec .Paused != nil && * cluster .Spec .Paused {
229248 meta .SetStatusCondition (& cluster .Status .Conditions , metav1.Condition {
@@ -390,6 +409,20 @@ func (r *Reconciler) Reconcile(
390409 return patchClusterStatus ()
391410}
392411
412+ func (r * Reconciler ) tokenAuthenticationFailed () bool {
413+ return r .Registration .TokenFileFound && r .Registration .Authenticated
414+ }
415+
416+ func (r * Reconciler ) registrationValid () bool {
417+ expiry := r .Registration .Exp
418+ authenticated := r .Registration .Authenticated
419+ // Use epoch time in seconds, consistent with RFC 7519.
420+ now := time .Now ().Unix ()
421+ expired := expiry < now
422+
423+ return authenticated && ! expired
424+ }
425+
393426// deleteControlled safely deletes object when it is controlled by cluster.
394427func (r * Reconciler ) deleteControlled (
395428 ctx context.Context , cluster * v1beta1.PostgresCluster , object client.Object ,
0 commit comments