@@ -30,6 +30,10 @@ func (r *OLSConfigReconciler) reconcilePostgresServer(ctx context.Context, olsco
3030 Name : "reconcile Postgres Secret" ,
3131 Task : r .reconcilePostgresSecret ,
3232 },
33+ {
34+ Name : "reconcile Postgres CA Secret" ,
35+ Task : r .reconcilePostgresCA ,
36+ },
3337 {
3438 Name : "reconcile Postgres Service" ,
3539 Task : r .reconcilePostgresService ,
@@ -70,14 +74,13 @@ func (r *OLSConfigReconciler) reconcilePostgresDeployment(ctx context.Context, c
7074 existingDeployment := & appsv1.Deployment {}
7175 err = r .Get (ctx , client.ObjectKey {Name : PostgresDeploymentName , Namespace : r .Options .Namespace }, existingDeployment )
7276 if err != nil && errors .IsNotFound (err ) {
73- updateDeploymentAnnotations (desiredDeployment , map [string ]string {
74- PostgresConfigHashKey : r .stateCache [PostgresConfigHashStateCacheKey ],
75- PostgresSecretHashKey : r .stateCache [PostgresSecretHashStateCacheKey ],
76- })
77- updateDeploymentTemplateAnnotations (desiredDeployment , map [string ]string {
77+ annotations := map [string ]string {
7878 PostgresConfigHashKey : r .stateCache [PostgresConfigHashStateCacheKey ],
7979 PostgresSecretHashKey : r .stateCache [PostgresSecretHashStateCacheKey ],
80- })
80+ PostgresCAHashKey : r .stateCache [PostgresCAHashStateCacheKey ],
81+ }
82+ updateDeploymentAnnotations (desiredDeployment , annotations )
83+ updateDeploymentTemplateAnnotations (desiredDeployment , annotations )
8184 r .logger .Info ("creating a new OLS postgres deployment" , "deployment" , desiredDeployment .Name )
8285 err = r .Create (ctx , desiredDeployment )
8386 if err != nil {
@@ -273,3 +276,61 @@ func (r *OLSConfigReconciler) reconcilePostgresNetworkPolicy(ctx context.Context
273276 r .logger .Info ("OLS postgres network policy reconciled" , "network policy" , networkPolicy .Name )
274277 return nil
275278}
279+
280+ func (r * OLSConfigReconciler ) reconcilePostgresCA (ctx context.Context , cr * olsv1alpha1.OLSConfig ) error {
281+ certBytes := []byte {}
282+
283+ // Get service CA certificate from ConfigMap
284+ tmpCM := & corev1.ConfigMap {}
285+ err := r .Client .Get (ctx , client.ObjectKey {Name : OLSCAConfigMap , Namespace : r .Options .Namespace }, tmpCM )
286+ if err != nil {
287+ if ! errors .IsNotFound (err ) {
288+ return fmt .Errorf ("failed to get %s ConfigMap: %w" , OLSCAConfigMap , err )
289+ }
290+ r .logger .Info ("CA ConfigMap not found, skipping CA bundle" , "configmap" , OLSCAConfigMap )
291+ } else {
292+ if caCert , exists := tmpCM .Data [PostgresServiceCACertKeyName ]; exists {
293+ certBytes = append (certBytes , []byte (PostgresServiceCACertKeyName )... )
294+ certBytes = append (certBytes , []byte (caCert )... )
295+ }
296+ }
297+
298+ // Get serving cert from Secret
299+ tmpSec := & corev1.Secret {}
300+ err = r .Client .Get (ctx , client.ObjectKey {Name : PostgresCertsSecretName , Namespace : r .Options .Namespace }, tmpSec )
301+ if err != nil {
302+ if ! errors .IsNotFound (err ) {
303+ return fmt .Errorf ("failed to get %s Secret: %w" , PostgresCertsSecretName , err )
304+ }
305+ r .logger .Info ("serving cert Secret not found, skipping server certificate" , "secret" , PostgresCertsSecretName )
306+ } else {
307+ if tlsCert , exists := tmpSec .Data [PostgresTLSCertKeyName ]; exists {
308+ certBytes = append (certBytes , []byte (PostgresTLSCertKeyName )... )
309+ certBytes = append (certBytes , tlsCert ... )
310+ }
311+ }
312+
313+ // Calculate hash based on available inputs
314+ combinedHash := ""
315+ if len (certBytes ) > 0 {
316+ var err error
317+ if combinedHash , err = hashBytes (certBytes ); err != nil {
318+ return fmt .Errorf ("failed to generate Postgres CA hash: %w" , err )
319+ }
320+ }
321+
322+ // Store existing hash before updating
323+ existingHash := r .stateCache [PostgresCAHashStateCacheKey ]
324+
325+ // Always update state cache to ensure it's set, even if value hasn't changed
326+ r .stateCache [PostgresCAHashStateCacheKey ] = combinedHash
327+
328+ // Check if hash changed (including changes to/from empty string)
329+ if combinedHash == existingHash {
330+ return nil
331+ }
332+
333+ r .logger .Info ("Postgres CA hash updated, deployment will be updated via updatePostgresDeployment" )
334+
335+ return nil
336+ }
0 commit comments