Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(tenancy): require bb.environment on deployment (#320)

spinningbot6e735223c218
diff --git a/api/deploy.go b/api/deploy.go
index d31e856298..840280a3d1 100644
--- a/api/deploy.go
+++ b/api/deploy.go
@@ -116,6 +116,7 @@ func ValidateAndGetDeploymentSchedule(payload string) (*DeploymentSchedule, erro
}
for _, d := range schedule.Deployments {
+ hasEnv := false
for _, e := range d.Spec.Selector.MatchExpressions {
switch e.Operator {
case InOperatorType:
@@ -129,6 +130,15 @@ func ValidateAndGetDeploymentSchedule(payload string) (*DeploymentSchedule, erro
default:
return nil, common.Errorf(common.Invalid, fmt.Errorf("expression key %q has invalid operator %q", e.Key, e.Operator))
}
+ if e.Key == EnvironmentKeyName {
+ hasEnv = true
+ if e.Operator != InOperatorType || len(e.Values) != 1 {
+ return nil, common.Errorf(common.Invalid, fmt.Errorf("label %q should must use operator %q with exactly one value", EnvironmentKeyName, InOperatorType))
+ }
+ }
+ }
+ if !hasEnv {
+ return nil, common.Errorf(common.Invalid, fmt.Errorf("deployment should contain %q label", EnvironmentKeyName))
}
}
return schedule, nil
diff --git a/api/deploy_test.go b/api/deploy_test.go
index dbae93be5e..dbccebb1d5 100644
--- a/api/deploy_test.go
+++ b/api/deploy_test.go
@@ -15,7 +15,7 @@ func TestGetDeploymentSchedule(t *testing.T) {
}{
{
"complexDeployments",
- `{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"In","values":["us-central1","europe-west1"]}]}}},{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"Exists"}]}}}]}`,
+ `{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"bb.environment","operator":"In","values":["prod"]},{"key":"location","operator":"In","values":["us-central1","europe-west1"]}]}}},{"spec":{"selector":{"matchExpressions":[{"key":"bb.environment","operator":"In","values":["prod"]},{"key":"location","operator":"Exists"}]}}}]}`,
&DeploymentSchedule{
Deployments: []*Deployment{
{
@@ -23,6 +23,10 @@ func TestGetDeploymentSchedule(t *testing.T) {
Selector: &LabelSelector{
MatchExpressions: []*LabelSelectorRequirement{
{
+ Key: "bb.environment",
+ Operator: "In",
+ Values: []string{"prod"},
+ }, {
Key: "location",
Operator: "In",
Values: []string{"us-central1", "europe-west1"},
@@ -36,6 +40,10 @@ func TestGetDeploymentSchedule(t *testing.T) {
Selector: &LabelSelector{
MatchExpressions: []*LabelSelectorRequirement{
{
+ Key: "bb.environment",
+ Operator: "In",
+ Values: []string{"prod"},
+ }, {
Key: "location",
Operator: "Exists",
Values: nil,
@@ -47,34 +55,44 @@ func TestGetDeploymentSchedule(t *testing.T) {
},
},
false,
- },
- {
+ }, {
"invalidPayload",
- `{"unmatchdeployments":[{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"In","values":["us-central1","europe-west1"]}]}}},{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"Exists"}]}}}]}`,
+ `{"unmatchdeployments":[{"spec":{"selector":{"matchExpressions":[{"key":"bb.environment","operator":"In","values":["prod"]},{"key":"location","operator":"In","values":["us-central1","europe-west1"]}]}}},{"spec":{"selector":{"matchExpressions":[{"key":"bb.environment","operator":"In","values":["prod"]},{"key":"location","operator":"Exists"}]}}}]}`,
&DeploymentSchedule{},
false,
- },
- {
+ }, {
"json",
`{`,
nil,
true,
- },
- {
+ }, {
"inOperatorWithNoValue",
- `{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"In"}]}}}]}`,
+ `{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"bb.environment","operator":"In","values":["prod"]},{"key":"location","operator":"In"}]}}}]}`,
nil,
true,
- },
- {
+ }, {
"existsOperatorWithValues",
- `{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"Exists","values":["us-central1","europe-west1"]}]}}}]}`,
+ `{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"bb.environment","operator":"In","values":["prod"]},{"key":"location","operator":"Exists","values":["us-central1","europe-west1"]}]}}}]}`,
nil,
true,
- },
- {
+ }, {
"invalidOperator",
- `{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"invalid"}]}}}]}`,
+ `{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"bb.environment","operator":"In","values":["prod"]},{"key":"location","operator":"invalid"}]}}}]}`,
+ nil,
+ true,
+ }, {
+ "missingEnvironment",
+ `{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"In","values":["us-central1","europe-west1"]}]}}}]}`,
+ nil,
+ true,
+ }, {
+ "environmentExistsOperator",
+ `{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"bb.environment","operator":"Exists"},{"key":"location","operator":"In","values":["us-central1","europe-west1"]}]}}}]}`,
+ nil,
+ true,
+ }, {
+ "environmentMultiValues",
+ `{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"bb.environment","operator":"In","values":["prod", "dev"]},{"key":"location","operator":"In","values":["us-central1","europe-west1"]}]}}}]}`,
nil,
true,
},
diff --git a/server/deploy.go b/server/deploy.go
index bcf47e30e6..eb4871f4f7 100644
--- a/server/deploy.go
+++ b/server/deploy.go
@@ -44,6 +44,7 @@ func isMatchExpressions(labels map[string]string, expressionList []*api.LabelSel
}
// getDatabaseMatrixFromDeploymentSchedule gets a pipeline based on deployment schedule.
+// The returned matrix doesn't include deployment with no matched database.
func getDatabaseMatrixFromDeploymentSchedule(schedule *api.DeploymentSchedule, name string, databaseList []*api.Database) ([][]*api.Database, error) {
var pipeline [][]*api.Database
@@ -94,7 +95,9 @@ func getDatabaseMatrixFromDeploymentSchedule(schedule *api.DeploymentSchedule, n
stage = append(stage, databaseMap[id])
}
- pipeline = append(pipeline, stage)
+ if len(stage) > 0 {
+ pipeline = append(pipeline, stage)
+ }
}
return pipeline, nil
diff --git a/server/deploy_test.go b/server/deploy_test.go
index 5123433f98..de859a52e7 100644
--- a/server/deploy_test.go
+++ b/server/deploy_test.go
@@ -166,7 +166,6 @@ func TestGetDatabaseMatrixFromDeploymentSchedule(t *testing.T) {
},
[][]*api.Database{
{dbs[0], dbs[2]},
- {},
},
},
{
diff --git a/server/issue.go b/server/issue.go
index c1d87c930e..a729938f9d 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -760,17 +760,33 @@ func (s *Server) createPipelineFromIssue(ctx context.Context, issueCreate *api.I
}
// Convert to pipelineCreate
for i, stage := range p {
- stageCreate := api.StageCreate{
- Name: fmt.Sprintf("Deployment %v", i),
- }
+ // Since environment is required for stage, we use an internal bb system environment for tenant deployments.
+ environmentSet := make(map[string]bool)
+ var environmentID int
+ var taskCreateList []api.TaskCreate
for _, database := range stage {
+ environmentSet[database.Instance.Environment.Name] = true
+ environmentID = database.Instance.EnvironmentID
taskCreate, err := getSchemaUpdateTask(database, m.MigrationType, m.VCSPushEvent, d)
if err != nil {
return nil, err
}
- stageCreate.TaskList = append(stageCreate.TaskList, *taskCreate)
+ taskCreateList = append(taskCreateList, *taskCreate)
+ }
+ if len(environmentSet) != 1 {
+ var environments []string
+ for k := range environmentSet {
+ environments = append(environments, k)
+ }
+ err := fmt.Errorf("All databases in a stage should have the same environment; got %s", strings.Join(environments, ","))
+ return nil, echo.NewHTTPError(http.StatusInternalServerError, err.Error()).SetInternal(err)
}
- pc.StageList = append(pc.StageList, stageCreate)
+
+ pc.StageList = append(pc.StageList, api.StageCreate{
+ Name: fmt.Sprintf("Deployment %v", i),
+ EnvironmentID: environmentID,
+ TaskList: taskCreateList,
+ })
}
pipelineCreate = pc
} else {
diff --git a/store/seed/test/10070__db.sql b/store/seed/test/10070__db.sql
index 5d2bd58b77..a29b28ba33 100644
--- a/store/seed/test/10070__db.sql
+++ b/store/seed/test/10070__db.sql
@@ -586,6 +586,120 @@ VALUES
''
);
+-- Databases for tenant mode project.
+INSERT INTO
+ db (
+ id,
+ creator_id,
+ updater_id,
+ instance_id,
+ project_id,
+ name,
+ character_set,
+ `collation`,
+ sync_status,
+ last_successful_sync_ts,
+ schema_version
+ )
+VALUES
+ (
+ 7021,
+ 101,
+ 101,
+ 6001,
+ 3005,
+ 'tenant_db1',
+ 'utf8mb4',
+ 'utf8mb4_general_ci',
+ 'OK',
+ 1624558090,
+ 'v1'
+ );
+INSERT INTO
+ db (
+ id,
+ creator_id,
+ updater_id,
+ instance_id,
+ project_id,
+ name,
+ character_set,
+ `collation`,
+ sync_status,
+ last_successful_sync_ts,
+ schema_version
+ )
+VALUES
+ (
+ 7022,
+ 101,
+ 101,
+ 6002,
+ 3005,
+ 'tenant_db1',
+ 'utf8mb4',
+ 'utf8mb4_general_ci',
+ 'OK',
+ 1624558090,
+ 'v1'
+ );
+INSERT INTO
+ db (
+ id,
+ creator_id,
+ updater_id,
+ instance_id,
+ project_id,
+ name,
+ character_set,
+ `collation`,
+ sync_status,
+ last_successful_sync_ts,
+ schema_version
+ )
+VALUES
+ (
+ 7023,
+ 101,
+ 101,
+ 6001,
+ 3006,
+ 'tenant_db2',
+ 'utf8mb4',
+ 'utf8mb4_general_ci',
+ 'OK',
+ 1624558090,
+ '2021v1'
+ );
+INSERT INTO
+ db (
+ id,
+ creator_id,
+ updater_id,
+ instance_id,
+ project_id,
+ name,
+ character_set,
+ `collation`,
+ sync_status,
+ last_successful_sync_ts,
+ schema_version
+ )
+VALUES
+ (
+ 7024,
+ 101,
+ 101,
+ 6002,
+ 3006,
+ 'tenant_db2',
+ 'utf8mb4',
+ 'utf8mb4_general_ci',
+ 'OK',
+ 1624558090,
+ '2021v1'
+ );
+
-- Database for prod environment archived mysql instance 6099
INSERT INTO
db (
diff --git a/store/seed/test/10200__label.sql b/store/seed/test/10200__label.sql
index 65d2f9f77b..1deebb8c0d 100644
--- a/store/seed/test/10200__label.sql
+++ b/store/seed/test/10200__label.sql
@@ -65,7 +65,7 @@ VALUES
"bytebase"
);
--- Database labels.
+-- Database labels for database in tenant mode disabled project.
INSERT INTO
db_label (
id,
@@ -103,3 +103,79 @@ VALUES
"bb.tenant",
"bytebase"
);
+
+-- Database labels for database in tenant mode project.
+INSERT INTO
+ db_label (
+ id,
+ creator_id,
+ updater_id,
+ database_id,
+ key,
+ value
+ )
+VALUES
+ (
+ 20023,
+ 1,
+ 1,
+ 7021,
+ "bb.location",
+ "earth"
+ );
+
+INSERT INTO
+ db_label (
+ id,
+ creator_id,
+ updater_id,
+ database_id,
+ key,
+ value
+ )
+VALUES
+ (
+ 20024,
+ 1,
+ 1,
+ 7022,
+ "bb.tenant",
+ "bytebase"
+ );
+INSERT INTO
+ db_label (
+ id,
+ creator_id,
+ updater_id,
+ database_id,
+ key,
+ value
+ )
+VALUES
+ (
+ 20025,
+ 1,
+ 1,
+ 7023,
+ "bb.location",
+ "earth"
+ );
+
+INSERT INTO
+ db_label (
+ id,
+ creator_id,
+ updater_id,
+ database_id,
+ key,
+ value
+ )
+VALUES
+ (
+ 20026,
+ 1,
+ 1,
+ 7024,
+ "bb.tenant",
+ "bytebase"
+ );
diff --git a/store/seed/test/10210__deployment_config.sql b/store/seed/test/10210__deployment_config.sql
index 8e65064e05..48232c6b31 100644
--- a/store/seed/test/10210__deployment_config.sql
+++ b/store/seed/test/10210__deployment_config.sql
@@ -1,4 +1,4 @@
--- Deployment config with regional deployments for project 3005.
+-- Deployment config with regional deployments for tenant mode project.
INSERT INTO
deployment_config (
id,
@@ -14,6 +14,25 @@ VALUES
101,
101,
3005,
- 'regional',
- '{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"IN","values":["us-central1","europe-west1"]}]}}},{"spec":{"selector":{"matchExpressions":[{"key":"location","operator":"EXISTS"}]}}}]}'
+ 'regional-1',
+ '{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"bb.environment","operator":"In","values":["Dev"]},{"key":"bb.location","operator":"In","values":["earth"]}]}}},{"spec":{"selector":{"matchExpressions":[{"key":"bb.environment","operator":"In","values":["Integration"]},{"key":"bb.tenant","operator":"Exists"}]}}}]}'
+ );
+
+INSERT INTO
+ deployment_config (
+ id,
+ creator_id,
+ updater_id,
+ project_id,
+ name,
+ config
+ )
+VALUES
+ (
+ 21002,
+ 101,
+ 101,
+ 3006,
+ 'regional-2',
+ '{"deployments":[{"spec":{"selector":{"matchExpressions":[{"key":"bb.environment","operator":"In","values":["Dev"]},{"key":"bb.location","operator":"In","values":["earth"]}]}}},{"spec":{"selector":{"matchExpressions":[{"key":"bb.environment","operator":"In","values":["Integration"]},{"key":"bb.tenant","operator":"Exists"}]}}}]}'
);