Skip to content

Commit 907fe17

Browse files
authored
fix: ensure that we create secret and services only when not found (cloudnative-pg#1145)
Closes cloudnative-pg#1028 Signed-off-by: Armando Ruocco <armando.ruocco@enterprisedb.com> Signed-off-by: Armando Ruocco <armando.ruocco@enterprisedb.com>
1 parent 6a2f9ec commit 907fe17

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

controllers/cluster_create.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/cloudnative-pg/cloudnative-pg/pkg/certs"
4040
"github.com/cloudnative-pg/cloudnative-pg/pkg/management/log"
4141
"github.com/cloudnative-pg/cloudnative-pg/pkg/postgres"
42+
"github.com/cloudnative-pg/cloudnative-pg/pkg/resources"
4243
"github.com/cloudnative-pg/cloudnative-pg/pkg/specs"
4344
"github.com/cloudnative-pg/cloudnative-pg/pkg/utils"
4445
"github.com/cloudnative-pg/cloudnative-pg/pkg/versions"
@@ -179,7 +180,7 @@ func (r *ClusterReconciler) reconcileSuperuserSecret(ctx context.Context, cluste
179180
postgresPassword)
180181
SetClusterOwnerAnnotationsAndLabels(&postgresSecret.ObjectMeta, cluster)
181182

182-
if err := r.Create(ctx, postgresSecret); err != nil {
183+
if err := resources.CreateIfNotFound(ctx, r.Client, postgresSecret); err != nil {
183184
if !apierrs.IsAlreadyExists(err) {
184185
return err
185186
}
@@ -223,7 +224,7 @@ func (r *ClusterReconciler) reconcileAppUserSecret(ctx context.Context, cluster
223224
appPassword)
224225

225226
SetClusterOwnerAnnotationsAndLabels(&appSecret.ObjectMeta, cluster)
226-
if err := r.Create(ctx, appSecret); err != nil {
227+
if err := resources.CreateIfNotFound(ctx, r.Client, appSecret); err != nil {
227228
if !apierrs.IsAlreadyExists(err) {
228229
return err
229230
}
@@ -273,7 +274,7 @@ func (r *ClusterReconciler) createPostgresServices(ctx context.Context, cluster
273274
anyService := specs.CreateClusterAnyService(*cluster)
274275
SetClusterOwnerAnnotationsAndLabels(&anyService.ObjectMeta, cluster)
275276

276-
if err := r.Create(ctx, anyService); err != nil {
277+
if err := resources.CreateIfNotFound(ctx, r.Client, anyService); err != nil {
277278
if !apierrs.IsAlreadyExists(err) {
278279
return err
279280
}
@@ -282,7 +283,7 @@ func (r *ClusterReconciler) createPostgresServices(ctx context.Context, cluster
282283
readService := specs.CreateClusterReadService(*cluster)
283284
SetClusterOwnerAnnotationsAndLabels(&readService.ObjectMeta, cluster)
284285

285-
if err := r.Create(ctx, readService); err != nil {
286+
if err := resources.CreateIfNotFound(ctx, r.Client, readService); err != nil {
286287
if !apierrs.IsAlreadyExists(err) {
287288
return err
288289
}
@@ -291,7 +292,7 @@ func (r *ClusterReconciler) createPostgresServices(ctx context.Context, cluster
291292
readOnlyService := specs.CreateClusterReadOnlyService(*cluster)
292293
SetClusterOwnerAnnotationsAndLabels(&readOnlyService.ObjectMeta, cluster)
293294

294-
if err := r.Create(ctx, readOnlyService); err != nil {
295+
if err := resources.CreateIfNotFound(ctx, r.Client, readOnlyService); err != nil {
295296
if !apierrs.IsAlreadyExists(err) {
296297
return err
297298
}
@@ -300,7 +301,7 @@ func (r *ClusterReconciler) createPostgresServices(ctx context.Context, cluster
300301
readWriteService := specs.CreateClusterReadWriteService(*cluster)
301302
SetClusterOwnerAnnotationsAndLabels(&readWriteService.ObjectMeta, cluster)
302303

303-
if err := r.Create(ctx, readWriteService); err != nil {
304+
if err := resources.CreateIfNotFound(ctx, r.Client, readWriteService); err != nil {
304305
if !apierrs.IsAlreadyExists(err) {
305306
return err
306307
}

0 commit comments

Comments
 (0)