Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EVEREST-1225 don't edit storages in use #487

Merged
merged 12 commits into from
Jul 11, 2024
24 changes: 23 additions & 1 deletion api/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ var (
errDuplicatedStoragePG = errors.New("postgres clusters can't use the same storage for the different schedules")
errStorageDeletionPG = errors.New("the existing postgres schedules can't be deleted")
errStorageChangePG = errors.New("the existing postgres schedules can't change their storage")
errEditBackupStorageInUse = errors.New("can't edit bucket, region or url of the backup storage in use")

//nolint:gochecknoglobals
operatorEngine = map[everestv1alpha1.EngineType]string{
Expand Down Expand Up @@ -346,6 +347,14 @@ func (e *EverestServer) validateUpdateBackupStorageRequest(
return nil, err
}

used, err := e.kubeClient.IsBackupStorageUsed(ctx.Request().Context(), e.kubeClient.Namespace(), backupStorageName, nil)
if err != nil {
return nil, err
}
if used && basicStorageParamsAreChanged(bs, params) {
return nil, errEditBackupStorageInUse
}

url := &bs.Spec.EndpointURL
if params.Url != nil {
if ok := validateURL(*params.Url); !ok {
Expand Down Expand Up @@ -407,14 +416,27 @@ func (e *EverestServer) validateUpdateBackupStorageRequest(
region = *params.Region
}

err := validateBackupStorageAccess(ctx, string(bs.Spec.Type), url, bucketName, region, accessKey, secretKey, pointer.Get(params.VerifyTLS), pointer.Get(params.ForcePathStyle), l)
err = validateBackupStorageAccess(ctx, string(bs.Spec.Type), url, bucketName, region, accessKey, secretKey, pointer.Get(params.VerifyTLS), pointer.Get(params.ForcePathStyle), l)
if err != nil {
return nil, err
}

return &params, nil
}

func basicStorageParamsAreChanged(bs *everestv1alpha1.BackupStorage, params UpdateBackupStorageParams) bool {
if params.Url != nil && bs.Spec.EndpointURL != pointer.GetString(params.Url) {
return true
}
if params.BucketName != nil && bs.Spec.Bucket != pointer.GetString(params.BucketName) {
return true
}
if params.Region != nil && bs.Spec.Region != pointer.GetString(params.Region) {
return true
}
return false
}

func validateCreateBackupStorageRequest(ctx echo.Context, namespaces []string, l *zap.SugaredLogger) (*CreateBackupStorageParams, error) {
var params CreateBackupStorageParams
if err := ctx.Bind(&params); err != nil {
Expand Down
12 changes: 8 additions & 4 deletions pkg/kubernetes/backup_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ func (k *Kubernetes) IsBackupStorageUsed(ctx context.Context, namespace, backupS
}

for _, namespace := range namespaces {
list, err := k.client.ListDatabaseClusters(ctx, namespace, options)
dblist, err := k.client.ListDatabaseClusters(ctx, namespace, options)
if err != nil {
return false, err
}
if len(list.Items) > 0 {
if len(dblist.Items) > 0 {
return true, nil
}
bList, err := k.client.ListDatabaseClusterBackups(ctx, namespace, options)
Expand All @@ -98,8 +98,12 @@ func (k *Kubernetes) IsBackupStorageUsed(ctx context.Context, namespace, backupS
if err != nil {
return false, err
}
if len(rList.Items) > 0 {
return true, nil
for _, restore := range rList.Items {
for _, db := range dblist.Items {
if restore.Spec.DBClusterName == db.Name && !restore.IsComplete(db.Spec.Engine.Type) {
return true, nil
}
}
oksana-grishchenko marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Loading