@@ -21,6 +21,7 @@ import (
21
21
"io"
22
22
"os"
23
23
"strconv"
24
+ "time"
24
25
25
26
"github.com/pkg/errors"
26
27
"go.opentelemetry.io/otel/trace"
@@ -50,6 +51,7 @@ import (
50
51
"github.com/crunchydata/postgres-operator/internal/pgmonitor"
51
52
"github.com/crunchydata/postgres-operator/internal/pki"
52
53
"github.com/crunchydata/postgres-operator/internal/postgres"
54
+ "github.com/crunchydata/postgres-operator/internal/util"
53
55
"github.com/crunchydata/postgres-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
54
56
)
55
57
@@ -61,15 +63,17 @@ const (
61
63
// Reconciler holds resources for the PostgresCluster reconciler
62
64
type Reconciler struct {
63
65
Client client.Client
64
- Owner client.FieldOwner
65
- Recorder record.EventRecorder
66
- Tracer trace.Tracer
67
66
IsOpenShift bool
68
-
69
- PodExec func (
67
+ Owner client.FieldOwner
68
+ PGOVersion string
69
+ PodExec func (
70
70
namespace , pod , container string ,
71
71
stdin io.Reader , stdout , stderr io.Writer , command ... string ,
72
72
) error
73
+ Recorder record.EventRecorder
74
+ Registration util.Registration
75
+ RegistrationURL string
76
+ Tracer trace.Tracer
73
77
}
74
78
75
79
// +kubebuilder:rbac:groups="",resources="events",verbs={create,patch}
@@ -209,13 +213,17 @@ func (r *Reconciler) Reconcile(
209
213
return result , err
210
214
}
211
215
212
- if config .RegistrationRequired () {
216
+ if config .RegistrationRequired () && ! r . registrationValid () {
213
217
if ! registrationRequiredStatusFound (cluster ) {
214
- addRegistrationRequiredStatus (cluster )
218
+ addRegistrationRequiredStatus (cluster , r . PGOVersion )
215
219
return patchClusterStatus ()
216
220
}
217
221
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 ) {
219
227
emitEncumbranceWarning (cluster , r )
220
228
// Encumbrance is just an early return from the reconciliation loop.
221
229
return patchClusterStatus ()
@@ -224,6 +232,17 @@ func (r *Reconciler) Reconcile(
224
232
}
225
233
}
226
234
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
+
227
246
// if the cluster is paused, set a condition and return
228
247
if cluster .Spec .Paused != nil && * cluster .Spec .Paused {
229
248
meta .SetStatusCondition (& cluster .Status .Conditions , metav1.Condition {
@@ -390,6 +409,20 @@ func (r *Reconciler) Reconcile(
390
409
return patchClusterStatus ()
391
410
}
392
411
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
+
393
426
// deleteControlled safely deletes object when it is controlled by cluster.
394
427
func (r * Reconciler ) deleteControlled (
395
428
ctx context.Context , cluster * v1beta1.PostgresCluster , object client.Object ,
0 commit comments