Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
Signed-off-by: Mayank Shah <[email protected]>
  • Loading branch information
mayankshah1607 committed Dec 11, 2024
1 parent 2af7cb1 commit 939db3d
Show file tree
Hide file tree
Showing 43 changed files with 149 additions and 548 deletions.
4 changes: 2 additions & 2 deletions internal/server/backup_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (e *EverestServer) ListBackupStorages(c echo.Context, namespace string) err
}

// CreateBackupStorage creates a new backup storage object.
func (e *EverestServer) CreateBackupStorage(c echo.Context, namespace string) error { //nolint:funlen
func (e *EverestServer) CreateBackupStorage(c echo.Context, namespace string) error {
user, err := rbac.GetUser(c)
if err != nil {
return c.JSON(http.StatusInternalServerError, api.Error{
Expand Down Expand Up @@ -113,7 +113,7 @@ func (e *EverestServer) GetBackupStorage(c echo.Context, namespace, name string)
}

// UpdateBackupStorage updates of the specified backup storage.
func (e *EverestServer) UpdateBackupStorage(c echo.Context, namespace, name string) error { //nolint:funlen,cyclop
func (e *EverestServer) UpdateBackupStorage(c echo.Context, namespace, name string) error {
user, err := rbac.GetUser(c)
if err != nil {
return c.JSON(http.StatusInternalServerError, api.Error{
Expand Down
24 changes: 13 additions & 11 deletions internal/server/database_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package api ...
// Package server contains the API server implementation.
package server

import (
Expand Down Expand Up @@ -50,17 +50,16 @@ func (e *EverestServer) CreateDatabaseCluster(c echo.Context, namespace string)
result, err := e.handler.CreateDatabaseCluster(c.Request().Context(), user, dbc)
if err != nil {
return err
} else {
// Collect metrics immediately after a DB cluster has been created.
go func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

if err := e.collectMetrics(ctx, *e.config); err != nil {
e.l.Errorf("Could not send metrics: %s", err)
}
}()
}
// Collect metrics immediately after a DB cluster has been created.
go func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

if err := e.collectMetrics(ctx, *e.config); err != nil {
e.l.Errorf("Could not send metrics: %s", err)
}
}()
return c.JSON(http.StatusCreated, result)
}

Expand Down Expand Up @@ -128,6 +127,8 @@ func (e *EverestServer) GetDatabaseClusterComponents(c echo.Context, namespace,
}

// UpdateDatabaseCluster replaces the specified database cluster on the specified kubernetes cluster.
//
//nolint:dupl
func (e *EverestServer) UpdateDatabaseCluster(ctx echo.Context, namespace, name string) error {
dbc := &everestv1alpha1.DatabaseCluster{}
if err := e.getBodyFromContext(ctx, dbc); err != nil {
Expand All @@ -137,6 +138,7 @@ func (e *EverestServer) UpdateDatabaseCluster(ctx echo.Context, namespace, name
})
}
dbc.SetNamespace(namespace)
dbc.SetName(name)

user, err := rbac.GetUser(ctx)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/server/database_cluster_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package api ...
// Package server contains the API server implementation.
package server

import (
Expand Down
3 changes: 2 additions & 1 deletion internal/server/database_cluster_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package api ...
// Package server contains the API server implementation.
package server

import (
Expand Down Expand Up @@ -116,6 +116,7 @@ func (e *EverestServer) UpdateDatabaseClusterRestore(ctx echo.Context, namespace
})
}
restore.SetNamespace(namespace)
restore.SetName(name)

result, err := e.handler.UpdateDatabaseClusterRestore(ctx.Request().Context(), user, restore)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion internal/server/database_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package api ...
// Package server contains the API server implementation.
package server

import (
Expand Down Expand Up @@ -60,6 +60,8 @@ func (e *EverestServer) GetDatabaseEngine(ctx echo.Context, namespace, name stri
}

// UpdateDatabaseEngine Update the specified database engine on the specified namespace.
//
//nolint:dupl
func (e *EverestServer) UpdateDatabaseEngine(ctx echo.Context, namespace, name string) error {
dbe := &everestv1alpha1.DatabaseEngine{}
if err := e.getBodyFromContext(ctx, dbe); err != nil {
Expand All @@ -69,6 +71,7 @@ func (e *EverestServer) UpdateDatabaseEngine(ctx echo.Context, namespace, name s
})
}
dbe.SetNamespace(namespace)
dbe.SetName(name)

user, err := rbac.GetUser(ctx)
if err != nil {
Expand Down
24 changes: 6 additions & 18 deletions internal/server/everest.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package api contains the API server implementation.
// Package server contains the API server implementation.
package server

import (
Expand All @@ -34,11 +34,9 @@ import (
"go.uber.org/zap"
"golang.org/x/time/rate"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"

. "github.com/percona/everest/api"
"github.com/percona/everest/api"
"github.com/percona/everest/cmd/config"
"github.com/percona/everest/internal/server/handlers"
k8shandler "github.com/percona/everest/internal/server/handlers/k8s"
Expand Down Expand Up @@ -102,10 +100,8 @@ func NewEverestServer(ctx context.Context, c *config.EverestConfig, l *zap.Sugar
}

// initHTTPServer configures http server for the current EverestServer instance.
//
//nolint:funlen
func (e *EverestServer) initHTTPServer(ctx context.Context) error {
swagger, err := GetSwagger()
swagger, err := api.GetSwagger()
if err != nil {
return err
}
Expand Down Expand Up @@ -159,11 +155,12 @@ func (e *EverestServer) initHTTPServer(ctx context.Context) error {
apiGroup.Use(jwtMW)

apiGroup.Use(e.checkOperatorUpgradeState)
RegisterHandlers(apiGroup, e)
api.RegisterHandlers(apiGroup, e)

return nil
}

//nolint:ireturn
func newHandlerChain(
ctx context.Context,
log *zap.SugaredLogger,
Expand Down Expand Up @@ -249,7 +246,7 @@ func (e *EverestServer) jwtMiddleWare(ctx context.Context) (echo.MiddlewareFunc,
}

func newSkipperFunc() (echomiddleware.Skipper, error) {
swagger, err := GetSwagger()
swagger, err := api.GetSwagger()
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -363,12 +360,3 @@ func enforcerErrorHandler(next echo.HTTPErrorHandler) echo.HTTPErrorHandler {
next(err, c)
}
}

func attachK8sTypeMeta(obj client.Object) {
gvk, err := apiutil.GVKForObject(obj, scheme.Scheme)
if err != nil {
// we expect a valid GVK for the object, but since we cannot get it, we should halt the execution.
panic(errors.Join(err, errors.New("could not get GVK for object")))
}
obj.GetObjectKind().SetGroupVersionKind(gvk)
}
65 changes: 0 additions & 65 deletions internal/server/everest_test.go

This file was deleted.

3 changes: 3 additions & 0 deletions internal/server/handlers/handler.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package handlers contains the interface and types for the Everest API handlers.
package handlers

//go:generate ../../../bin/mockery --name=Handler --case=snake --inpackage
Expand All @@ -13,6 +14,8 @@ import (
// Each implementation of a handler is responsible for handling a specific set of operations (e.g, request validation, RBAC, KubeAPI, etc.).
// Handlers may be chained together using the SetNext() method to form a chain of responsibility.
// Each Handler implementation is individually responsible for calling the next handler in the chain.
//
//nolint:interfacebloat
type Handler interface {
// SetNext sets the next handler to call in the chain.
SetNext(h Handler)
Expand Down
8 changes: 4 additions & 4 deletions internal/server/handlers/k8s/backup_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
"github.com/percona/everest/api"
)

func (h *k8sHandler) ListBackupStorages(ctx context.Context, user, namespace string) (*everestv1alpha1.BackupStorageList, error) {
func (h *k8sHandler) ListBackupStorages(ctx context.Context, _, namespace string) (*everestv1alpha1.BackupStorageList, error) {
return h.kubeClient.ListBackupStorages(ctx, namespace)
}

func (h *k8sHandler) GetBackupStorage(ctx context.Context, user, namespace, name string) (*everestv1alpha1.BackupStorage, error) {
func (h *k8sHandler) GetBackupStorage(ctx context.Context, _, namespace, name string) (*everestv1alpha1.BackupStorage, error) {
return h.kubeClient.GetBackupStorage(ctx, namespace, name)
}

Expand Down Expand Up @@ -81,7 +81,7 @@ func (h *k8sHandler) CreateBackupStorage(ctx context.Context, user, namespace st
return updated, nil
}

func (h *k8sHandler) UpdateBackupStorage(ctx context.Context, user, namespace, name string, req *api.UpdateBackupStorageParams) (*everestv1alpha1.BackupStorage, error) {
func (h *k8sHandler) UpdateBackupStorage(ctx context.Context, _, namespace, name string, req *api.UpdateBackupStorageParams) (*everestv1alpha1.BackupStorage, error) {
if req.AccessKey != nil || req.SecretKey != nil {
_, err := h.kubeClient.UpdateSecret(ctx, &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -122,7 +122,7 @@ func (h *k8sHandler) UpdateBackupStorage(ctx context.Context, user, namespace, n
return h.kubeClient.UpdateBackupStorage(ctx, bs)
}

func (h *k8sHandler) DeleteBackupStorage(ctx context.Context, user, namespace, name string) error {
func (h *k8sHandler) DeleteBackupStorage(ctx context.Context, _, namespace, name string) error {
used, err := h.kubeClient.IsBackupStorageUsed(ctx, namespace, name)
if err != nil {
return fmt.Errorf("failed to check if backup storage is used: %w", err)
Expand Down
19 changes: 10 additions & 9 deletions internal/server/handlers/k8s/database_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ const (
pgDefaultUploadInterval = 60
)

func (h *k8sHandler) CreateDatabaseCluster(ctx context.Context, user string, db *everestv1alpha1.DatabaseCluster) (*everestv1alpha1.DatabaseCluster, error) {
func (h *k8sHandler) CreateDatabaseCluster(ctx context.Context, _ string, db *everestv1alpha1.DatabaseCluster) (*everestv1alpha1.DatabaseCluster, error) {
return h.kubeClient.CreateDatabaseCluster(ctx, db)
}

func (h *k8sHandler) ListDatabaseClusters(ctx context.Context, user, namespace string) (*everestv1alpha1.DatabaseClusterList, error) {
func (h *k8sHandler) ListDatabaseClusters(ctx context.Context, _, namespace string) (*everestv1alpha1.DatabaseClusterList, error) {
return h.kubeClient.ListDatabaseClusters(ctx, namespace)
}

func (h *k8sHandler) DeleteDatabaseCluster(ctx context.Context, user, namespace, name string, req *api.DeleteDatabaseClusterParams) error {
func (h *k8sHandler) DeleteDatabaseCluster(ctx context.Context, _, namespace, name string, req *api.DeleteDatabaseClusterParams) error {
cleanupStorage := pointer.Get(req.CleanupBackupStorage)

backups, err := h.kubeClient.ListDatabaseClusterBackups(ctx, namespace, metav1.ListOptions{})
Expand Down Expand Up @@ -76,15 +76,15 @@ func (h *k8sHandler) DeleteDatabaseCluster(ctx context.Context, user, namespace,
return h.kubeClient.DeleteDatabaseCluster(ctx, namespace, name)
}

func (h *k8sHandler) UpdateDatabaseCluster(ctx context.Context, user string, db *everestv1alpha1.DatabaseCluster) (*everestv1alpha1.DatabaseCluster, error) {
func (h *k8sHandler) UpdateDatabaseCluster(ctx context.Context, _ string, db *everestv1alpha1.DatabaseCluster) (*everestv1alpha1.DatabaseCluster, error) {
return h.kubeClient.UpdateDatabaseCluster(ctx, db)
}

func (h *k8sHandler) GetDatabaseCluster(ctx context.Context, user, namespace, name string) (*everestv1alpha1.DatabaseCluster, error) {
func (h *k8sHandler) GetDatabaseCluster(ctx context.Context, _, namespace, name string) (*everestv1alpha1.DatabaseCluster, error) {
return h.kubeClient.GetDatabaseCluster(ctx, namespace, name)
}

func (h *k8sHandler) GetDatabaseClusterCredentials(ctx context.Context, user, namespace, name string) (*api.DatabaseClusterCredential, error) {
func (h *k8sHandler) GetDatabaseClusterCredentials(ctx context.Context, _, namespace, name string) (*api.DatabaseClusterCredential, error) {
databaseCluster, err := h.kubeClient.GetDatabaseCluster(ctx, namespace, name)
if err != nil {
return nil, fmt.Errorf("failed to get database cluster %s/%s: %w", namespace, name, err)
Expand Down Expand Up @@ -112,7 +112,8 @@ func (h *k8sHandler) GetDatabaseClusterCredentials(ctx context.Context, user, na
return response, nil
}

func (h *k8sHandler) GetDatabaseClusterComponents(ctx context.Context, user, namespace, name string) ([]api.DatabaseClusterComponent, error) {
//nolint:funlen
func (h *k8sHandler) GetDatabaseClusterComponents(ctx context.Context, _, namespace, name string) ([]api.DatabaseClusterComponent, error) {
pods, err := h.kubeClient.GetPods(ctx, namespace, &metav1.LabelSelector{
MatchLabels: map[string]string{"app.kubernetes.io/instance": name},
})
Expand Down Expand Up @@ -180,7 +181,7 @@ func (h *k8sHandler) GetDatabaseClusterComponents(ctx context.Context, user, nam
return res, nil
}

func (h *k8sHandler) GetDatabaseClusterPitr(ctx context.Context, user, namespace, name string) (*api.DatabaseClusterPitr, error) {
func (h *k8sHandler) GetDatabaseClusterPitr(ctx context.Context, _, namespace, name string) (*api.DatabaseClusterPitr, error) {
databaseCluster, err := h.kubeClient.GetDatabaseCluster(ctx, namespace, name)
if err != nil {
return nil, fmt.Errorf("failed to get database cluster %s/%s: %w", namespace, name, err)
Expand Down Expand Up @@ -233,7 +234,7 @@ func (h *k8sHandler) GetDatabaseClusterPitr(ctx context.Context, user, namespace
}

//nolint:gochecknoglobals
var everestAPIConstantBackoff = backoff.WithMaxRetries(backoff.NewConstantBackOff(time.Second), 10)
var everestAPIConstantBackoff = backoff.WithMaxRetries(backoff.NewConstantBackOff(time.Second), 10) //nolint:mnd

func (h *k8sHandler) ensureBackupStorageProtection(ctx context.Context, backup *everestv1alpha1.DatabaseClusterBackup) error {
// We wrap this logic in a retry loop to reduce the chances of resource conflicts.
Expand Down
Loading

0 comments on commit 939db3d

Please sign in to comment.