Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' into EVEREST-428-update-rc-pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
oksana-grishchenko authored Sep 21, 2023
2 parents 4586420 + 535420c commit a332d06
Show file tree
Hide file tree
Showing 35 changed files with 665 additions and 500 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ linters-settings:
- $all
- "!$test"
deny:
- pkg: github.com/pkg/errors
desc: use "errors" instead
- pkg: github.com/gogo/protobuf/proto
desc: use "github.com/golang/protobuf/proto" instead

Expand Down
8 changes: 7 additions & 1 deletion api-tests/tests/backup-storages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ test('update backup storage failures', async ({ request }) => {
},
errorText: '\'url\' is an invalid URL',
},
{
payload: {
bucket: '-asldf;asdfk;sadf',
},
errorText: 'request body has an error: doesn\'t match schema #/components/schemas/UpdateBackupStorageParams: property \"bucket\" is unsupported',
},
];

for (const testCase of testCases) {
Expand All @@ -187,7 +193,7 @@ test('update: backup storage not found', async ({ request }) => {

const response = await request.patch(`/v1/backup-storages/${name}`, {
data: {
type: 's3',
bucketName: 's3',
},
});

Expand Down
2 changes: 1 addition & 1 deletion api-tests/tests/monitoring-instances.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ test('patch monitoring instance type fails on missing key', async ({ request })

const getJson = await updated.json()

expect(getJson.message).toMatch('pmm key is required')
expect(getJson.message).toMatch('Pmm key is required')
})

test('create monitoring instance failures', async ({ request }) => {
Expand Down
29 changes: 15 additions & 14 deletions api/backup_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ package api

import (
"context"
"errors"
"fmt"
"net/http"

"github.com/AlekSi/pointer"
"github.com/google/uuid"
"github.com/jinzhu/gorm"
"github.com/labstack/echo/v4"
"github.com/lib/pq"
"github.com/pkg/errors"

"github.com/percona/percona-everest-backend/model"
"github.com/percona/percona-everest-backend/pkg/kubernetes"
Expand Down Expand Up @@ -75,7 +76,7 @@ func (e *EverestServer) CreateBackupStorage(ctx echo.Context) error { //nolint:f
})
}
if existingStorage != nil {
err = errors.Errorf("Storage %s already exists", params.Name)
err = fmt.Errorf("storage %s already exists", params.Name)
e.l.Error(err)
return ctx.JSON(http.StatusConflict, Error{Message: pointer.ToString(err.Error())})
}
Expand Down Expand Up @@ -152,7 +153,7 @@ func (e *EverestServer) DeleteBackupStorage(ctx echo.Context, backupStorageName

ks, err := e.storage.ListKubernetesClusters(c)
if err != nil {
e.l.Error(errors.Wrap(err, "Could not list Kubernetes clusters"))
e.l.Error(errors.Join(err, errors.New("could not list Kubernetes clusters")))
return ctx.JSON(http.StatusInternalServerError, Error{Message: pointer.ToString("Could not list Kubernetes clusters")})
}
if len(ks) == 0 {
Expand All @@ -161,30 +162,30 @@ func (e *EverestServer) DeleteBackupStorage(ctx echo.Context, backupStorageName
// FIXME: Revisit it once multi k8s support will be enabled
_, kubeClient, _, err := e.initKubeClient(ctx.Request().Context(), ks[0].ID)
if err != nil {
e.l.Error(errors.Wrap(err, "could not init kube client for config"))
e.l.Error(errors.Join(err, errors.New("could not init kube client for config")))
return nil
}

err = kubeClient.DeleteConfig(ctx.Request().Context(), bs, func(ctx context.Context, name string) (bool, error) {
return kubernetes.IsBackupStorageConfigInUse(ctx, name, kubeClient)
})
if err != nil && !errors.Is(err, kubernetes.ErrConfigInUse) {
e.l.Error(errors.Wrap(err, "could not delete config"))
e.l.Error(errors.Join(err, errors.New("could not delete config")))
return nil
}

err = e.storage.Transaction(func(tx *gorm.DB) error {
err := e.storage.DeleteBackupStorage(c, backupStorageName, tx)
if err != nil {
e.l.Error(err)
return errors.New("Could not delete backup storage")
return errors.New("could not delete backup storage")
}
if _, err := e.secretsStorage.DeleteSecret(c, bs.AccessKeyID); err != nil {
return errors.Wrap(err, "could not delete access key from secrets storage")
return errors.Join(err, errors.New("could not delete access key from secrets storage"))
}

if _, err := e.secretsStorage.DeleteSecret(c, bs.SecretKeyID); err != nil {
return errors.Wrap(err, "could not delete secret key from secrets storage")
return errors.Join(err, errors.New("could not delete secret key from secrets storage"))
}

return nil
Expand Down Expand Up @@ -293,12 +294,12 @@ func (e *EverestServer) performBackupStorageUpdate(
// FIXME: Revisit it once multi k8s support will be enabled
_, kubeClient, _, err := e.initKubeClient(ctx.Request().Context(), ks[0].ID)
if err != nil {
e.l.Error(errors.Wrap(err, "could not init kube client to update config"))
e.l.Error(errors.Join(err, errors.New("could not init kube client to update config")))
return ctx.JSON(http.StatusInternalServerError, Error{Message: pointer.ToString("Could not init kubernetes client to update config")})
}

if err := kubeClient.UpdateConfig(ctx.Request().Context(), bs, e.secretsStorage.GetSecret); err != nil {
e.l.Error(errors.Wrap(err, "could not update config"))
e.l.Error(errors.Join(err, errors.New("could not update config")))
return ctx.JSON(http.StatusInternalServerError, Error{Message: pointer.ToString("Could not update config on the kubernetes cluster")})
}

Expand Down Expand Up @@ -329,7 +330,7 @@ func (e *EverestServer) createSecrets(
err := e.secretsStorage.CreateSecret(ctx, newID, *accessKey)
if err != nil {
e.l.Error(err)
return newAccessKeyID, newSecretKeyID, errors.New("Could not store access key in secrets storage")
return newAccessKeyID, newSecretKeyID, errors.New("could not store access key in secrets storage")
}
}

Expand All @@ -341,7 +342,7 @@ func (e *EverestServer) createSecrets(
err := e.secretsStorage.CreateSecret(ctx, newID, *secretKey)
if err != nil {
e.l.Error(err)
return newAccessKeyID, newSecretKeyID, errors.New("Could not store secret key in secrets storage")
return newAccessKeyID, newSecretKeyID, errors.New("could not store secret key in secrets storage")
}
}

Expand Down Expand Up @@ -436,12 +437,12 @@ func (e *EverestServer) updateBackupStorage(
var pgErr *pq.Error
if errors.As(err, &pgErr) {
if pgErr.Code.Name() == pgErrUniqueViolation {
return http.StatusBadRequest, errors.New("Backup storage with the same name already exists. " + pgErr.Detail)
return http.StatusBadRequest, errors.New("backup storage with the same name already exists. " + pgErr.Detail)
}
}

e.l.Error(err)
return http.StatusInternalServerError, errors.New("Could not update backup storage")
return http.StatusInternalServerError, errors.New("could not update backup storage")
}

return 0, nil
Expand Down
39 changes: 20 additions & 19 deletions api/database_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ package api

import (
"context"
"errors"
"fmt"
"net/http"

"github.com/AlekSi/pointer"
"github.com/labstack/echo/v4"
everestv1alpha1 "github.com/percona/everest-operator/api/v1alpha1"
"github.com/pkg/errors"

"github.com/percona/percona-everest-backend/pkg/kubernetes"
)
Expand Down Expand Up @@ -145,7 +146,7 @@ func (e *EverestServer) UpdateDatabaseCluster(ctx echo.Context, kubernetesID str

oldDB, err := kubeClient.GetDatabaseCluster(ctx.Request().Context(), name)
if err != nil {
return errors.Wrap(err, "Could not get old Database Cluster")
return errors.Join(err, errors.New("could not get old Database Cluster"))
}
if dbc.Spec.Engine.Version != nil {
// XXX: Right now we do not support upgrading of versions
Expand Down Expand Up @@ -239,13 +240,13 @@ func (e *EverestServer) createK8SBackupStorages(ctx context.Context, kubeClient
for name := range names {
bs, err := e.storage.GetBackupStorage(ctx, nil, name)
if err != nil {
return errors.Wrap(err, "Could not get backup storage")
return errors.Join(err, errors.New("could not get backup storage"))
}

err = kubeClient.EnsureConfigExists(ctx, bs, e.secretsStorage.GetSecret)
if err != nil {
e.rollbackCreatedBackupStorages(ctx, kubeClient, processed)
return errors.Wrapf(err, "Could not create CRs for %s", name)
return errors.Join(err, fmt.Errorf("could not create CRs for %s", name))
}
processed = append(processed, name)
}
Expand All @@ -256,15 +257,15 @@ func (e *EverestServer) rollbackCreatedBackupStorages(ctx context.Context, kubeC
for _, name := range toDelete {
bs, err := e.storage.GetBackupStorage(ctx, nil, name)
if err != nil {
e.l.Error(errors.Wrap(err, "could not get backup storage"))
e.l.Error(errors.Join(err, errors.New("could not get backup storage")))
continue
}

err = kubeClient.DeleteConfig(ctx, bs, func(ctx context.Context, name string) (bool, error) {
return kubernetes.IsBackupStorageConfigInUse(ctx, name, kubeClient)
})
if err != nil && !errors.Is(err, kubernetes.ErrConfigInUse) {
e.l.Error(errors.Wrap(err, "could not delete backup storage config"))
e.l.Error(errors.Join(err, errors.New("could not delete backup storage config")))
continue
}
}
Expand All @@ -276,15 +277,15 @@ func (e *EverestServer) deleteK8SMonitoringConfig(
defer e.waitGroup.Done()
i, err := e.storage.GetMonitoringInstance(name)
if err != nil {
e.l.Error(errors.Wrap(err, "could get monitoring instance"))
e.l.Error(errors.Join(err, errors.New("could get monitoring instance")))
return
}

err = kubeClient.DeleteConfig(ctx, i, func(ctx context.Context, name string) (bool, error) {
return kubernetes.IsMonitoringConfigInUse(ctx, name, kubeClient)
})
if err != nil && !errors.Is(err, kubernetes.ErrConfigInUse) {
e.l.Error(errors.Wrap(err, "could not delete monitoring config in Kubernetes"))
e.l.Error(errors.Join(err, errors.New("could not delete monitoring config in Kubernetes")))
return
}
}
Expand All @@ -296,15 +297,15 @@ func (e *EverestServer) deleteK8SBackupStorages(
for name := range names {
bs, err := e.storage.GetBackupStorage(ctx, nil, name)
if err != nil {
e.l.Error(errors.Wrap(err, "could not get backup storage"))
e.l.Error(errors.Join(err, errors.New("could not get backup storage")))
continue
}

err = kubeClient.DeleteConfig(ctx, bs, func(ctx context.Context, name string) (bool, error) {
return kubernetes.IsBackupStorageConfigInUse(ctx, name, kubeClient)
})
if err != nil && !errors.Is(err, kubernetes.ErrConfigInUse) {
e.l.Error(errors.Wrap(err, "could not delete backup storage config in Kubernetes"))
e.l.Error(errors.Join(err, errors.New("could not delete backup storage config in Kubernetes")))
continue
}
}
Expand All @@ -315,14 +316,14 @@ func (e *EverestServer) deleteK8SBackupStorage(
) error {
bs, err := e.storage.GetBackupStorage(ctx, nil, name)
if err != nil {
return errors.Wrap(err, "could not find backup storage")
return errors.Join(err, errors.New("could not find backup storage"))
}

err = kubeClient.DeleteConfig(ctx, bs, func(ctx context.Context, name string) (bool, error) {
return kubernetes.IsBackupStorageConfigInUse(ctx, name, kubeClient)
})
if err != nil && !errors.Is(err, kubernetes.ErrConfigInUse) {
return errors.Wrap(err, "could not delete config in Kubernetes")
return errors.Join(err, errors.New("could not delete config in Kubernetes"))
}

return nil
Expand All @@ -340,13 +341,13 @@ func (e *EverestServer) createBackupStoragesOnUpdate(
for name := range toCreate {
bs, err := e.storage.GetBackupStorage(ctx, nil, name)
if err != nil {
return errors.Wrap(err, "Could not get backup storage")
return errors.Join(err, errors.New("could not get backup storage"))
}

err = kubeClient.EnsureConfigExists(ctx, bs, e.secretsStorage.GetSecret)
if err != nil {
e.rollbackCreatedBackupStorages(ctx, kubeClient, processed)
return errors.Wrapf(err, "Could not create CRs for %s", name)
return errors.Join(err, fmt.Errorf("could not create CRs for %s", name))
}
processed = append(processed, name)
}
Expand All @@ -366,7 +367,7 @@ func (e *EverestServer) deleteBackupStoragesOnUpdate(
for name := range toDelete {
err := e.deleteK8SBackupStorage(ctx, kubeClient, name)
if err != nil && !errors.Is(err, kubernetes.ErrConfigInUse) {
e.l.Error(errors.Wrapf(err, "Could not delete CRs for %s", name))
e.l.Error(errors.Join(err, fmt.Errorf("could not delete CRs for %s", name)))
}
}
}
Expand All @@ -385,12 +386,12 @@ func (e *EverestServer) createMonitoringInstanceOnUpdate(
if newName != "" && newName != oldName {
i, err := e.storage.GetMonitoringInstance(newName)
if err != nil {
return errors.Wrap(err, "Could not get monitoring instance")
return errors.Join(err, errors.New("could not get monitoring instance"))
}

err = kubeClient.EnsureConfigExists(ctx, i, e.secretsStorage.GetSecret)
if err != nil {
return errors.Wrap(err, "Could not create monitoring config in Kubernetes")
return errors.Join(err, errors.New("could not create monitoring config in Kubernetes"))
}
}

Expand All @@ -412,15 +413,15 @@ func (e *EverestServer) deleteMonitoringInstanceOnUpdate(
if oldName != "" && newName != oldName {
i, err := e.storage.GetMonitoringInstance(oldName)
if err != nil {
e.l.Error(errors.Wrap(err, "Could not get monitoring instance"))
e.l.Error(errors.Join(err, errors.New("could not get monitoring instance")))
return
}

err = kubeClient.DeleteConfig(ctx, i, func(ctx context.Context, name string) (bool, error) {
return kubernetes.IsMonitoringConfigInUse(ctx, name, kubeClient)
})
if err != nil && !errors.Is(err, kubernetes.ErrConfigInUse) {
e.l.Error(errors.Wrap(err, "Could not delete monitoring config from Kubernetes"))
e.l.Error(errors.Join(err, errors.New("could not delete monitoring config from Kubernetes")))
return
}
}
Expand Down
Loading

0 comments on commit a332d06

Please sign in to comment.