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

Commit

Permalink
EVEREST-496 Add multi-namespaces support to DBClusters (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
recharte authored Feb 7, 2024
1 parent 3603057 commit 7d93a85
Show file tree
Hide file tree
Showing 28 changed files with 1,792 additions and 1,661 deletions.
4 changes: 2 additions & 2 deletions api/backup_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (e *EverestServer) ListBackupStorages(ctx echo.Context) error {

// CreateBackupStorage creates a new backup storage object.
func (e *EverestServer) CreateBackupStorage(ctx echo.Context) error { //nolint:funlen
namespaces, err := e.kubeClient.GetWatchedNamespaces(ctx.Request().Context(), e.kubeClient.Namespace())
namespaces, err := e.kubeClient.GetDBNamespaces(ctx.Request().Context(), e.kubeClient.Namespace())
if err != nil {
e.l.Error(err)
return ctx.JSON(http.StatusInternalServerError, Error{
Expand Down Expand Up @@ -258,7 +258,7 @@ func (e *EverestServer) UpdateBackupStorage(ctx echo.Context, backupStorageName
})
}

namespaces, err := e.kubeClient.GetWatchedNamespaces(ctx.Request().Context(), e.kubeClient.Namespace())
namespaces, err := e.kubeClient.GetDBNamespaces(ctx.Request().Context(), e.kubeClient.Namespace())
if err != nil {
e.l.Error(err)
return ctx.JSON(http.StatusInternalServerError, Error{
Expand Down
38 changes: 19 additions & 19 deletions api/database_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
)

// CreateDatabaseCluster creates a new db cluster inside the given k8s cluster.
func (e *EverestServer) CreateDatabaseCluster(ctx echo.Context) error {
func (e *EverestServer) CreateDatabaseCluster(ctx echo.Context, namespace string) error {
dbc := &DatabaseCluster{}
if err := e.getBodyFromContext(ctx, dbc); err != nil {
e.l.Error(err)
Expand All @@ -42,30 +42,30 @@ func (e *EverestServer) CreateDatabaseCluster(ctx echo.Context) error {
})
}

if err := e.validateDatabaseClusterCR(ctx, dbc); err != nil {
if err := e.validateDatabaseClusterCR(ctx, namespace, dbc); err != nil {
return ctx.JSON(http.StatusBadRequest, Error{Message: pointer.ToString(err.Error())})
}

return e.proxyKubernetes(ctx, "", databaseClusterKind, "")
return e.proxyKubernetes(ctx, namespace, databaseClusterKind, "")
}

// ListDatabaseClusters lists the created database clusters on the specified kubernetes cluster.
func (e *EverestServer) ListDatabaseClusters(ctx echo.Context) error {
return e.proxyKubernetes(ctx, "", databaseClusterKind, "")
func (e *EverestServer) ListDatabaseClusters(ctx echo.Context, namespace string) error {
return e.proxyKubernetes(ctx, namespace, databaseClusterKind, "")
}

// DeleteDatabaseCluster deletes a database cluster on the specified kubernetes cluster.
func (e *EverestServer) DeleteDatabaseCluster(ctx echo.Context, name string) error {
return e.proxyKubernetes(ctx, "", databaseClusterKind, name)
func (e *EverestServer) DeleteDatabaseCluster(ctx echo.Context, namespace, name string) error {
return e.proxyKubernetes(ctx, namespace, databaseClusterKind, name)
}

// GetDatabaseCluster retrieves the specified database cluster on the specified kubernetes cluster.
func (e *EverestServer) GetDatabaseCluster(ctx echo.Context, name string) error {
return e.proxyKubernetes(ctx, "", databaseClusterKind, name)
func (e *EverestServer) GetDatabaseCluster(ctx echo.Context, namespace, name string) error {
return e.proxyKubernetes(ctx, namespace, databaseClusterKind, name)
}

// UpdateDatabaseCluster replaces the specified database cluster on the specified kubernetes cluster.
func (e *EverestServer) UpdateDatabaseCluster(ctx echo.Context, name string) error {
func (e *EverestServer) UpdateDatabaseCluster(ctx echo.Context, namespace, name string) error {
dbc := &DatabaseCluster{}
if err := e.getBodyFromContext(ctx, dbc); err != nil {
e.l.Error(err)
Expand All @@ -74,29 +74,29 @@ func (e *EverestServer) UpdateDatabaseCluster(ctx echo.Context, name string) err
})
}

if err := e.validateDatabaseClusterCR(ctx, dbc); err != nil {
if err := e.validateDatabaseClusterCR(ctx, namespace, dbc); err != nil {
return ctx.JSON(http.StatusBadRequest, Error{Message: pointer.ToString(err.Error())})
}

oldDB, err := e.kubeClient.GetDatabaseCluster(ctx.Request().Context(), name)
oldDB, err := e.kubeClient.GetDatabaseCluster(ctx.Request().Context(), namespace, name)
if err != nil {
return errors.Join(err, errors.New("could not get old Database Cluster"))
}
if err := validateDatabaseClusterOnUpdate(dbc, oldDB); err != nil {
return ctx.JSON(http.StatusBadRequest, Error{Message: pointer.ToString(err.Error())})
}

return e.proxyKubernetes(ctx, "", databaseClusterKind, name)
return e.proxyKubernetes(ctx, namespace, databaseClusterKind, name)
}

// GetDatabaseClusterCredentials returns credentials for the specified database cluster.
func (e *EverestServer) GetDatabaseClusterCredentials(ctx echo.Context, name string) error {
databaseCluster, err := e.kubeClient.GetDatabaseCluster(ctx.Request().Context(), name)
func (e *EverestServer) GetDatabaseClusterCredentials(ctx echo.Context, namespace, name string) error {
databaseCluster, err := e.kubeClient.GetDatabaseCluster(ctx.Request().Context(), namespace, name)
if err != nil {
e.l.Error(err)
return ctx.JSON(http.StatusInternalServerError, Error{Message: pointer.ToString(err.Error())})
}
secret, err := e.kubeClient.GetSecret(ctx.Request().Context(), "percona-everest", databaseCluster.Spec.Engine.UserSecretsName)
secret, err := e.kubeClient.GetSecret(ctx.Request().Context(), namespace, databaseCluster.Spec.Engine.UserSecretsName)
if err != nil {
e.l.Error(err)
return ctx.JSON(http.StatusInternalServerError, Error{Message: pointer.ToString(err.Error())})
Expand All @@ -120,8 +120,8 @@ func (e *EverestServer) GetDatabaseClusterCredentials(ctx echo.Context, name str
}

// GetDatabaseClusterPitr returns the point-in-time recovery related information for the specified database cluster.
func (e *EverestServer) GetDatabaseClusterPitr(ctx echo.Context, name string) error {
databaseCluster, err := e.kubeClient.GetDatabaseCluster(ctx.Request().Context(), name)
func (e *EverestServer) GetDatabaseClusterPitr(ctx echo.Context, namespace, name string) error {
databaseCluster, err := e.kubeClient.GetDatabaseCluster(ctx.Request().Context(), namespace, name)
if err != nil {
e.l.Error(err)
return ctx.JSON(http.StatusInternalServerError, Error{Message: pointer.ToString(err.Error())})
Expand All @@ -139,7 +139,7 @@ func (e *EverestServer) GetDatabaseClusterPitr(ctx echo.Context, name string) er
},
}),
}
backups, err := e.kubeClient.ListDatabaseClusterBackups(ctx.Request().Context(), options)
backups, err := e.kubeClient.ListDatabaseClusterBackups(ctx.Request().Context(), namespace, options)
if err != nil {
e.l.Error(err)
return ctx.JSON(http.StatusInternalServerError, Error{Message: pointer.ToString(err.Error())})
Expand Down
18 changes: 9 additions & 9 deletions api/database_cluster_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
)

// ListDatabaseClusterBackups returns list of the created database cluster backups on the specified kubernetes cluster.
func (e *EverestServer) ListDatabaseClusterBackups(ctx echo.Context, name string) error {
func (e *EverestServer) ListDatabaseClusterBackups(ctx echo.Context, namespace, name string) error {
req := ctx.Request()
if err := validateRFC1035(name, "name"); err != nil {
return ctx.JSON(http.StatusBadRequest, Error{Message: pointer.ToString(err.Error())})
Expand All @@ -46,11 +46,11 @@ func (e *EverestServer) ListDatabaseClusterBackups(ctx echo.Context, name string
path = strings.TrimSuffix(path, name)
path = strings.ReplaceAll(path, "database-clusters", "database-cluster-backups")
req.URL.Path = path
return e.proxyKubernetes(ctx, "", databaseClusterBackupKind, "")
return e.proxyKubernetes(ctx, namespace, databaseClusterBackupKind, "")
}

// CreateDatabaseClusterBackup creates a database cluster backup on the specified kubernetes cluster.
func (e *EverestServer) CreateDatabaseClusterBackup(ctx echo.Context) error {
func (e *EverestServer) CreateDatabaseClusterBackup(ctx echo.Context, namespace string) error {
dbb := &DatabaseClusterBackup{}
if err := e.getBodyFromContext(ctx, dbb); err != nil {
e.l.Error(err)
Expand All @@ -59,19 +59,19 @@ func (e *EverestServer) CreateDatabaseClusterBackup(ctx echo.Context) error {
})
}
// TODO: Improve returns status code in EVEREST-616
if err := validateDatabaseClusterBackup(ctx.Request().Context(), dbb, e.kubeClient); err != nil {
if err := validateDatabaseClusterBackup(ctx.Request().Context(), namespace, dbb, e.kubeClient); err != nil {
e.l.Error(err)
return ctx.JSON(http.StatusBadRequest, Error{Message: pointer.ToString(err.Error())})
}
return e.proxyKubernetes(ctx, "", databaseClusterBackupKind, "")
return e.proxyKubernetes(ctx, namespace, databaseClusterBackupKind, "")
}

// DeleteDatabaseClusterBackup deletes the specified cluster backup on the specified kubernetes cluster.
func (e *EverestServer) DeleteDatabaseClusterBackup(ctx echo.Context, name string) error {
return e.proxyKubernetes(ctx, "", databaseClusterBackupKind, name)
func (e *EverestServer) DeleteDatabaseClusterBackup(ctx echo.Context, namespace, name string) error {
return e.proxyKubernetes(ctx, namespace, databaseClusterBackupKind, name)
}

// GetDatabaseClusterBackup returns the specified cluster backup on the specified kubernetes cluster.
func (e *EverestServer) GetDatabaseClusterBackup(ctx echo.Context, name string) error {
return e.proxyKubernetes(ctx, "", databaseClusterBackupKind, name)
func (e *EverestServer) GetDatabaseClusterBackup(ctx echo.Context, namespace, name string) error {
return e.proxyKubernetes(ctx, namespace, databaseClusterBackupKind, name)
}
26 changes: 13 additions & 13 deletions api/database_cluster_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (
)

// ListDatabaseClusterRestores List of the created database cluster restores on the specified kubernetes cluster.
func (e *EverestServer) ListDatabaseClusterRestores(ctx echo.Context, name string) error {
func (e *EverestServer) ListDatabaseClusterRestores(ctx echo.Context, namespace, name string) error {
req := ctx.Request()
if err := validateRFC1035(name, "name"); err != nil {
return ctx.JSON(http.StatusBadRequest, Error{Message: pointer.ToString(err.Error())})
Expand All @@ -47,25 +47,25 @@ func (e *EverestServer) ListDatabaseClusterRestores(ctx echo.Context, name strin
path = strings.TrimSuffix(path, name)
path = strings.ReplaceAll(path, "database-clusters", "database-cluster-restores")
req.URL.Path = path
return e.proxyKubernetes(ctx, "", databaseClusterRestoreKind, "")
return e.proxyKubernetes(ctx, namespace, databaseClusterRestoreKind, "")
}

// CreateDatabaseClusterRestore Create a database cluster restore on the specified kubernetes cluster.
func (e *EverestServer) CreateDatabaseClusterRestore(ctx echo.Context) error {
func (e *EverestServer) CreateDatabaseClusterRestore(ctx echo.Context, namespace string) error {
restore := &DatabaseClusterRestore{}
if err := e.getBodyFromContext(ctx, restore); err != nil {
e.l.Error(err)
return ctx.JSON(http.StatusBadRequest, Error{
Message: pointer.ToString("Could not get DatabaseClusterRestore from the request body"),
})
}
if err := validateDatabaseClusterRestore(ctx.Request().Context(), restore, e.kubeClient); err != nil {
if err := validateDatabaseClusterRestore(ctx.Request().Context(), namespace, restore, e.kubeClient); err != nil {
e.l.Error(err)
return ctx.JSON(http.StatusBadRequest, Error{
Message: pointer.ToString(err.Error()),
})
}
dbCluster, err := e.kubeClient.GetDatabaseCluster(ctx.Request().Context(), restore.Spec.DbClusterName)
dbCluster, err := e.kubeClient.GetDatabaseCluster(ctx.Request().Context(), namespace, restore.Spec.DbClusterName)
if err != nil {
e.l.Error(err)
return ctx.JSON(http.StatusInternalServerError, Error{
Expand All @@ -79,33 +79,33 @@ func (e *EverestServer) CreateDatabaseClusterRestore(ctx echo.Context) error {
})
}

return e.proxyKubernetes(ctx, "", databaseClusterRestoreKind, "")
return e.proxyKubernetes(ctx, namespace, databaseClusterRestoreKind, "")
}

// DeleteDatabaseClusterRestore Delete the specified cluster restore on the specified kubernetes cluster.
func (e *EverestServer) DeleteDatabaseClusterRestore(ctx echo.Context, name string) error {
return e.proxyKubernetes(ctx, "", databaseClusterRestoreKind, name)
func (e *EverestServer) DeleteDatabaseClusterRestore(ctx echo.Context, namespace, name string) error {
return e.proxyKubernetes(ctx, namespace, databaseClusterRestoreKind, name)
}

// GetDatabaseClusterRestore Returns the specified cluster restore on the specified kubernetes cluster.
func (e *EverestServer) GetDatabaseClusterRestore(ctx echo.Context, name string) error {
return e.proxyKubernetes(ctx, "", databaseClusterRestoreKind, name)
func (e *EverestServer) GetDatabaseClusterRestore(ctx echo.Context, namespace, name string) error {
return e.proxyKubernetes(ctx, namespace, databaseClusterRestoreKind, name)
}

// UpdateDatabaseClusterRestore Replace the specified cluster restore on the specified kubernetes cluster.
func (e *EverestServer) UpdateDatabaseClusterRestore(ctx echo.Context, name string) error {
func (e *EverestServer) UpdateDatabaseClusterRestore(ctx echo.Context, namespace, name string) error {
restore := &DatabaseClusterRestore{}
if err := e.getBodyFromContext(ctx, restore); err != nil {
e.l.Error(err)
return ctx.JSON(http.StatusBadRequest, Error{
Message: pointer.ToString("Could not get DatabaseClusterRestore from the request body"),
})
}
if err := validateDatabaseClusterRestore(ctx.Request().Context(), restore, e.kubeClient); err != nil {
if err := validateDatabaseClusterRestore(ctx.Request().Context(), namespace, restore, e.kubeClient); err != nil {
e.l.Error(err)
return ctx.JSON(http.StatusBadRequest, Error{
Message: pointer.ToString(err.Error()),
})
}
return e.proxyKubernetes(ctx, "", databaseClusterRestoreKind, name)
return e.proxyKubernetes(ctx, namespace, databaseClusterRestoreKind, name)
}
4 changes: 2 additions & 2 deletions api/database_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ func (e *EverestServer) ListDatabaseEngines(ctx echo.Context, namespace string)
}

// GetDatabaseEngine Get the specified database engine on the specified namespace.
func (e *EverestServer) GetDatabaseEngine(ctx echo.Context, namespace string, name string) error {
func (e *EverestServer) GetDatabaseEngine(ctx echo.Context, namespace, name string) error {
return e.proxyKubernetes(ctx, namespace, databaseEngineKind, name)
}

// UpdateDatabaseEngine Update the specified database engine on the specified namespace.
func (e *EverestServer) UpdateDatabaseEngine(ctx echo.Context, namespace string, name string) error {
func (e *EverestServer) UpdateDatabaseEngine(ctx echo.Context, namespace, name string) error {
return e.proxyKubernetes(ctx, namespace, databaseEngineKind, name)
}
Loading

0 comments on commit 7d93a85

Please sign in to comment.