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

Commit

Permalink
EVEREST-434 move to errors package (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
gen1us2k authored Sep 21, 2023
1 parent f84f530 commit 69fb781
Show file tree
Hide file tree
Showing 30 changed files with 218 additions and 192 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
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
18 changes: 9 additions & 9 deletions api/everest.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package api
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/fs"
"net/http"
Expand All @@ -29,7 +30,6 @@ import (
"github.com/deepmap/oapi-codegen/pkg/middleware"
"github.com/labstack/echo/v4"
echomiddleware "github.com/labstack/echo/v4/middleware"
"github.com/pkg/errors"
"go.uber.org/zap"

"github.com/percona/percona-everest-backend/cmd/config"
Expand Down Expand Up @@ -89,7 +89,7 @@ func (e *EverestServer) initKubeClient(ctx context.Context, kubernetesID string)
k, err := e.storage.GetKubernetesCluster(ctx, kubernetesID)
if err != nil {
e.l.Error(err)
return nil, nil, http.StatusBadRequest, errors.New("Could not find Kubernetes cluster")
return nil, nil, http.StatusBadRequest, errors.New("could not find Kubernetes cluster")
}

kubeClient, err := kubernetes.NewFromSecretsStorage(
Expand All @@ -98,7 +98,7 @@ func (e *EverestServer) initKubeClient(ctx context.Context, kubernetesID string)
)
if err != nil {
e.l.Error(err)
return k, nil, http.StatusInternalServerError, errors.New("Could not create Kubernetes client from kubeconfig")
return k, nil, http.StatusInternalServerError, errors.New("could not create Kubernetes client from kubeconfig")
}

return k, kubeClient, 0, nil
Expand All @@ -112,7 +112,7 @@ func (e *EverestServer) initHTTPServer() error {
}
fsys, err := fs.Sub(public.Static, "dist")
if err != nil {
return errors.Wrap(err, "error reading filesystem")
return errors.Join(err, errors.New("error reading filesystem"))
}
staticFilesHandler := http.FileServer(http.FS(fsys))
indexFS := echo.MustSubFS(public.Index, "dist")
Expand All @@ -133,7 +133,7 @@ func (e *EverestServer) initHTTPServer() error {

basePath, err := swagger.Servers.BasePath()
if err != nil {
return errors.Wrap(err, "could not get base path")
return errors.Join(err, errors.New("could not get base path"))
}

// Use our validation middleware to check all requests against the OpenAPI schema.
Expand All @@ -155,7 +155,7 @@ func (e *EverestServer) Start() error {
func (e *EverestServer) Shutdown(ctx context.Context) error {
e.l.Info("Shutting down http server")
if err := e.echo.Shutdown(ctx); err != nil {
e.l.Error(errors.Wrap(err, "could not shut down http server"))
e.l.Error(errors.Join(err, errors.New("could not shut down http server")))
} else {
e.l.Info("http server shut down")
}
Expand All @@ -168,7 +168,7 @@ func (e *EverestServer) Shutdown(ctx context.Context) error {
defer e.waitGroup.Done()
e.l.Info("Shutting down database storage")
if err := e.storage.Close(); err != nil {
e.l.Error(errors.Wrap(err, "could not shut down database storage"))
e.l.Error(errors.Join(err, errors.New("could not shut down database storage")))
} else {
e.l.Info("Database storage shut down")
}
Expand All @@ -179,7 +179,7 @@ func (e *EverestServer) Shutdown(ctx context.Context) error {
defer e.waitGroup.Done()
e.l.Info("Shutting down secrets storage")
if err := e.secretsStorage.Close(); err != nil {
e.l.Error(errors.Wrap(err, "could not shut down secret storage"))
e.l.Error(errors.Join(err, errors.New("could not shut down secret storage")))
} else {
e.l.Info("Secret storage shut down")
}
Expand Down Expand Up @@ -208,7 +208,7 @@ func (e *EverestServer) getBodyFromContext(ctx echo.Context, into any) error {

decoder := json.NewDecoder(reader)
if err := decoder.Decode(into); err != nil {
return errors.Wrap(err, "could not decode body")
return errors.Join(err, errors.New("could not decode body"))
}
return nil
}
Loading

0 comments on commit 69fb781

Please sign in to comment.