Skip to content

Commit 2709599

Browse files
egeguneshors
andauthored
K8SPSMDB-976: Fix PBM config if there's no storage configured in CR (#1320)
* K8SPSMDB-976: Fix PBM config if there's no storage configured in CR * fix pbm storage if credentials don't exist --------- Co-authored-by: Viacheslav Sarzhan <[email protected]>
1 parent dcc31ae commit 2709599

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

pkg/controller/perconaservermongodb/backup.go

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
batchv1 "k8s.io/api/batch/v1"
1515
corev1 "k8s.io/api/core/v1"
1616
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
17+
k8serrors "k8s.io/apimachinery/pkg/api/errors"
1718
"k8s.io/apimachinery/pkg/labels"
1819
"k8s.io/apimachinery/pkg/types"
1920
"k8s.io/client-go/util/retry"
@@ -368,17 +369,39 @@ func (r *ReconcilePerconaServerMongoDB) updatePITR(ctx context.Context, cr *api.
368369
return errors.Wrap(err, "get pitr.enabled")
369370
}
370371

371-
// if PiTR is enabled we know there is only one storage
372-
var storage api.BackupStorageSpec
373-
for name, stg := range cr.Spec.Backup.Storages {
374-
storage = stg
375-
log.Info("Configuring PBM with storage", "storage", name)
376-
break
377-
}
372+
if len(cr.Spec.Backup.Storages) == 1 {
373+
// if PiTR is enabled user can configure only one storage
374+
var storage api.BackupStorageSpec
375+
for name, stg := range cr.Spec.Backup.Storages {
376+
storage = stg
377+
log.Info("Configuring PBM with storage", "storage", name)
378+
break
379+
}
378380

379-
err = pbm.SetConfig(ctx, r.client, cr, storage)
380-
if err != nil {
381-
return errors.Wrap(err, "set PBM config")
381+
var secretName string
382+
switch storage.Type {
383+
case api.BackupStorageS3:
384+
secretName = storage.S3.CredentialsSecret
385+
case api.BackupStorageAzure:
386+
secretName = storage.Azure.CredentialsSecret
387+
}
388+
389+
exists, err := secretExists(ctx, r.client, types.NamespacedName{Name: secretName, Namespace: cr.Namespace})
390+
if err != nil {
391+
return errors.Wrap(err, "check storage credentials secret")
392+
}
393+
394+
if !exists {
395+
log.Error(nil, "Storage credentials secret does not exist", "secret", secretName)
396+
return nil
397+
}
398+
399+
err = pbm.SetConfig(ctx, r.client, cr, storage)
400+
if err != nil {
401+
return errors.Wrap(err, "set PBM config")
402+
}
403+
404+
log.Info("Configured PBM storage")
382405
}
383406

384407
return nil
@@ -567,3 +590,16 @@ func (r *ReconcilePerconaServerMongoDB) resyncPBMIfNeeded(ctx context.Context, c
567590

568591
return nil
569592
}
593+
594+
func secretExists(ctx context.Context, cl client.Client, nn types.NamespacedName) (bool, error) {
595+
var secret corev1.Secret
596+
err := cl.Get(ctx, nn, &secret)
597+
if err != nil {
598+
if k8serrors.IsNotFound(err) {
599+
return false, nil
600+
}
601+
return false, err
602+
}
603+
604+
return true, nil
605+
}

0 commit comments

Comments
 (0)