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

Commit

Permalink
EVEREST-827-adopt-to-namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
oksana-grishchenko committed Feb 8, 2024
1 parent cee9eba commit ca8cab2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
34 changes: 24 additions & 10 deletions api/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ var (

errDBCEmptyMetadata = errors.New("databaseCluster's Metadata should not be empty")
errDBCNameEmpty = errors.New("databaseCluster's metadata.name should not be empty")
errDBCNamespaceEmpty = errors.New("databaseCluster's metadata.namespace should not be empty")
errDBCNameWrongFormat = errors.New("databaseCluster's metadata.name should be a string")
errDBCNamespaceWrongFormat = errors.New("databaseCluster's metadata.namespace should be a string")
errNotEnoughMemory = fmt.Errorf("memory limits should be above %s", minMemQuantity.String())
errInt64NotSupported = errors.New("specifying resources using int64 data type is not supported. Please use string format for that")
errNotEnoughCPU = fmt.Errorf("CPU limits should be above %s", minCPUQuantity.String())
Expand Down Expand Up @@ -458,31 +460,42 @@ func validateUpdateMonitoringInstanceType(params UpdateMonitoringInstanceJSONReq
}

func validateCreateDatabaseClusterRequest(dbc DatabaseCluster) error {
strName, err := nameFromDatabaseCluster(dbc)
name, _, err := nameFromDatabaseCluster(dbc)
if err != nil {
return err
}

return validateRFC1035(strName, "metadata.name")
return validateRFC1035(name, "metadata.name")
}

func nameFromDatabaseCluster(dbc DatabaseCluster) (string, error) {
func nameFromDatabaseCluster(dbc DatabaseCluster) (string, string, error) {
if dbc.Metadata == nil {
return "", errDBCEmptyMetadata
return "", "", errDBCEmptyMetadata
}

md := *dbc.Metadata
name, ok := md["name"]
if !ok {
return "", errDBCNameEmpty
return "", "", errDBCNameEmpty
}

strName, ok := name.(string)
if !ok {
return "", errDBCNameWrongFormat
return "", "", errDBCNameWrongFormat
}

return strName, nil
md = *dbc.Metadata
ns, ok := md["namespace"]
if !ok {
return "", "", errDBCNamespaceEmpty
}

strNS, ok := ns.(string)
if !ok {
return "", "", errDBCNamespaceWrongFormat
}

return strName, strNS, nil
}

func (e *EverestServer) validateDatabaseClusterCR(ctx echo.Context, namespace string, databaseCluster *DatabaseCluster) error { //nolint:cyclop
Expand Down Expand Up @@ -978,7 +991,7 @@ type dataSourceStruct struct {
} `json:"pitr,omitempty"`
}

func validatePGReposForAPIDB(ctx context.Context, dbc *DatabaseCluster, getBackupsFunc func(ctx context.Context, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error)) error {
func validatePGReposForAPIDB(ctx context.Context, dbc *DatabaseCluster, getBackupsFunc func(context.Context, string, metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error)) error {
bs := make(map[string]bool)
if dbc.Spec != nil && dbc.Spec.Backup != nil && dbc.Spec.Backup.Schedules != nil {
for _, shed := range *dbc.Spec.Backup.Schedules {
Expand All @@ -991,11 +1004,12 @@ func validatePGReposForAPIDB(ctx context.Context, dbc *DatabaseCluster, getBacku
}
}

dbcName, err := nameFromDatabaseCluster(*dbc)
dbcName, dbcNamespace, err := nameFromDatabaseCluster(*dbc)
if err != nil {
return err
}
backups, err := getBackupsFunc(ctx, metav1.ListOptions{

backups, err := getBackupsFunc(ctx, dbcNamespace, metav1.ListOptions{
LabelSelector: fmt.Sprintf("clusterName=%s", dbcName),
})
if err != nil {
Expand Down
38 changes: 19 additions & 19 deletions api/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,13 +727,13 @@ func TestValidatePGReposForAPIDB(t *testing.T) {
cases := []struct {
name string
cluster []byte
getBackupsFunc func(ctx context.Context, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error)
getBackupsFunc func(ctx context.Context, ns string, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error)
err error
}{
{
name: "ok: no schedules no backups",
cluster: []byte(`{"metaData":{"name":"some"}}`),
getBackupsFunc: func(ctx context.Context, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error) {
cluster: []byte(`{"metaData":{"name":"some","namespace":"ns"}}`),
getBackupsFunc: func(ctx context.Context, ns string, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error) {
return &everestv1alpha1.DatabaseClusterBackupList{
Items: []everestv1alpha1.DatabaseClusterBackup{},
}, nil
Expand All @@ -742,8 +742,8 @@ func TestValidatePGReposForAPIDB(t *testing.T) {
},
{
name: "ok: 2 schedules 2 backups with the same storages",
cluster: []byte(`{"metaData":{"name":"some"},"spec":{"backup":{"schedules":[{"backupStorageName":"bs1"},{"backupStorageName":"bs2"}]}}}`),
getBackupsFunc: func(ctx context.Context, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error) {
cluster: []byte(`{"metaData":{"name":"some","namespace":"ns"},"spec":{"backup":{"schedules":[{"backupStorageName":"bs1"},{"backupStorageName":"bs2"}]}}}`),
getBackupsFunc: func(ctx context.Context, ns string, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error) {
return &everestv1alpha1.DatabaseClusterBackupList{
Items: []everestv1alpha1.DatabaseClusterBackup{
{Spec: everestv1alpha1.DatabaseClusterBackupSpec{BackupStorageName: "bs1"}},
Expand All @@ -755,8 +755,8 @@ func TestValidatePGReposForAPIDB(t *testing.T) {
},
{
name: "ok: 3 schedules",
cluster: []byte(`{"metaData":{"name":"some"},"spec":{"backup":{"schedules":[{"backupStorageName":"bs1"},{"backupStorageName":"bs2"},{"backupStorageName":"bs3"}]}}}`),
getBackupsFunc: func(ctx context.Context, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error) {
cluster: []byte(`{"metaData":{"name":"some","namespace":"ns"},"spec":{"backup":{"schedules":[{"backupStorageName":"bs1"},{"backupStorageName":"bs2"},{"backupStorageName":"bs3"}]}}}`),
getBackupsFunc: func(ctx context.Context, ns string, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error) {
return &everestv1alpha1.DatabaseClusterBackupList{
Items: []everestv1alpha1.DatabaseClusterBackup{},
}, nil
Expand All @@ -765,8 +765,8 @@ func TestValidatePGReposForAPIDB(t *testing.T) {
},
{
name: "ok: 3 backups with different storages",
cluster: []byte(`{"metaData":{"name":"some"}}`),
getBackupsFunc: func(ctx context.Context, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error) {
cluster: []byte(`{"metaData":{"name":"some","namespace":"ns"}}`),
getBackupsFunc: func(ctx context.Context, ns string, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error) {
return &everestv1alpha1.DatabaseClusterBackupList{
Items: []everestv1alpha1.DatabaseClusterBackup{
{Spec: everestv1alpha1.DatabaseClusterBackupSpec{BackupStorageName: "bs1"}},
Expand All @@ -779,8 +779,8 @@ func TestValidatePGReposForAPIDB(t *testing.T) {
},
{
name: "ok: 5 backups with repeating storages",
cluster: []byte(`{"metaData":{"name":"some"}}`),
getBackupsFunc: func(ctx context.Context, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error) {
cluster: []byte(`{"metaData":{"name":"some","namespace":"ns"}}`),
getBackupsFunc: func(ctx context.Context, ns string, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error) {
return &everestv1alpha1.DatabaseClusterBackupList{
Items: []everestv1alpha1.DatabaseClusterBackup{
{Spec: everestv1alpha1.DatabaseClusterBackupSpec{BackupStorageName: "bs1"}},
Expand All @@ -795,8 +795,8 @@ func TestValidatePGReposForAPIDB(t *testing.T) {
},
{
name: "error: 4 backups with different storages",
cluster: []byte(`{"metaData":{"name":"some"}}`),
getBackupsFunc: func(ctx context.Context, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error) {
cluster: []byte(`{"metaData":{"name":"some","namespace":"ns"}}`),
getBackupsFunc: func(ctx context.Context, ns string, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error) {
return &everestv1alpha1.DatabaseClusterBackupList{
Items: []everestv1alpha1.DatabaseClusterBackup{
{Spec: everestv1alpha1.DatabaseClusterBackupSpec{BackupStorageName: "bs1"}},
Expand All @@ -810,8 +810,8 @@ func TestValidatePGReposForAPIDB(t *testing.T) {
},
{
name: "ok: 4 backups with same storages",
cluster: []byte(`{"metaData":{"name":"some"}}`),
getBackupsFunc: func(ctx context.Context, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error) {
cluster: []byte(`{"metaData":{"name":"some","namespace":"ns"}}`),
getBackupsFunc: func(ctx context.Context, ns string, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error) {
return &everestv1alpha1.DatabaseClusterBackupList{
Items: []everestv1alpha1.DatabaseClusterBackup{
{Spec: everestv1alpha1.DatabaseClusterBackupSpec{BackupStorageName: "bs1"}},
Expand All @@ -825,8 +825,8 @@ func TestValidatePGReposForAPIDB(t *testing.T) {
},
{
name: "error: 4 schedules",
cluster: []byte(`{"metaData":{"name":"some"},"spec":{"backup":{"schedules":[{"backupStorageName":"bs1"},{"backupStorageName":"bs2"},{"backupStorageName":"bs3"},{"backupStorageName":"bs4"}]}}}`),
getBackupsFunc: func(ctx context.Context, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error) {
cluster: []byte(`{"metaData":{"name":"some","namespace":"ns"},"spec":{"backup":{"schedules":[{"backupStorageName":"bs1"},{"backupStorageName":"bs2"},{"backupStorageName":"bs3"},{"backupStorageName":"bs4"}]}}}`),
getBackupsFunc: func(ctx context.Context, ns string, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error) {
return &everestv1alpha1.DatabaseClusterBackupList{
Items: []everestv1alpha1.DatabaseClusterBackup{},
}, nil
Expand All @@ -835,8 +835,8 @@ func TestValidatePGReposForAPIDB(t *testing.T) {
},
{
name: "error: 2 schedules 2 backups with different storages",
cluster: []byte(`{"metaData":{"name":"some"},"spec":{"backup":{"schedules":[{"backupStorageName":"bs1"},{"backupStorageName":"bs2"}]}}}`),
getBackupsFunc: func(ctx context.Context, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error) {
cluster: []byte(`{"metaData":{"name":"some","namespace":"ns"},"spec":{"backup":{"schedules":[{"backupStorageName":"bs1"},{"backupStorageName":"bs2"}]}}}`),
getBackupsFunc: func(ctx context.Context, ns string, options metav1.ListOptions) (*everestv1alpha1.DatabaseClusterBackupList, error) {
return &everestv1alpha1.DatabaseClusterBackupList{
Items: []everestv1alpha1.DatabaseClusterBackup{
{Spec: everestv1alpha1.DatabaseClusterBackupSpec{BackupStorageName: "bs3"}},
Expand Down

0 comments on commit ca8cab2

Please sign in to comment.