Skip to content

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

feat(tenancy): generate pipelines based on deployment config (#260)

p0ny06416612e9f2
diff --git a/api/issue.go b/api/issue.go
index b9d0d93402..d55c540fd9 100644
--- a/api/issue.go
+++ b/api/issue.go
@@ -137,9 +137,9 @@ type CreateDatabaseContext struct {
// UpdateSchemaDetail is the detail of updating database schema.
type UpdateSchemaDetail struct {
- // DatabaseId is the ID of a database.
+ // DatabaseID is the ID of a database.
DatabaseID int `json:"databaseId"`
- // DatabaseName is the name of databases, mutually exclusive to DatabaseId.
+ // DatabaseName is the name of databases, mutually exclusive to DatabaseID.
// This should be set when a project is in tenant mode, and ProjectID is derived from IssueCreate.
DatabaseName string `json:"databaseName"`
// Statement is the statement to update database schema.
diff --git a/server/deploy.go b/server/deploy.go
new file mode 100644
index 0000000000..4f3ca5c1e8
--- /dev/null
+++ b/server/deploy.go
@@ -0,0 +1,98 @@
+package server
+
+import (
+ "encoding/json"
+
+ "github.com/bytebase/bytebase/api"
+)
+
+// isMatchExpression checks whether a databases matches the query.
+// labels is a mapping from database label key to value.
+func isMatchExpression(labels map[string]string, expression *api.LabelSelectorRequirement) bool {
+ switch expression.Operator {
+ case api.InOperatorType:
+ value, ok := labels[expression.Key]
+ if !ok {
+ return false
+ }
+ for _, exprValue := range expression.Values {
+ if exprValue == value {
+ return true
+ }
+ }
+ return false
+ case api.ExistsOperatorType:
+ _, ok := labels[expression.Key]
+ return ok
+ default:
+ return false
+ }
+}
+
+func isMatchExpressions(labels map[string]string, expressionList []*api.LabelSelectorRequirement) bool {
+ // Empty expression list matches no databases.
+ if len(expressionList) == 0 {
+ return false
+ }
+ // Expressions are ANDed.
+ for _, expression := range expressionList {
+ if !isMatchExpression(labels, expression) {
+ return false
+ }
+ }
+ return true
+}
+
+// generatePipelineCreateFromDeploymentSchedule generates a pipeline, based on deployment schedule. It creates each stage and corresponding task list filled with database id. The caller is responsible to fill in other fields.
+func generatePipelineCreateFromDeploymentSchedule(schedule *api.DeploymentSchedule, databaseList []*api.Database) *api.PipelineCreate {
+ create := &api.PipelineCreate{}
+
+ // idToLabels maps databaseID -> label.Key -> label.Value
+ idToLabels := make(map[int]map[string]string)
+ for _, database := range databaseList {
+ if _, ok := idToLabels[database.ID]; !ok {
+ idToLabels[database.ID] = make(map[string]string)
+ }
+ var labelList []*api.DatabaseLabel
+ json.Unmarshal([]byte(database.Labels), &labelList)
+ for _, label := range labelList {
+ idToLabels[database.ID][label.Key] = label.Value
+ }
+ }
+
+ // idsSeen records database id which is already in a stage.
+ idsSeen := make(map[int]bool)
+
+ // For each stage, we loop over all databases to see if it is a match.
+ for _, deployment := range schedule.Deployments {
+
+ // For each stage, we will get a list of matched databases.
+ var matchedDatabaseList []int
+
+ // Loop over databaseList instead of idToLabels to get determinant results.
+ for _, database := range databaseList {
+ databaseID := database.ID
+ labels := idToLabels[databaseID]
+
+ // Skip if the database is already in a stage.
+ if idsSeen[databaseID] {
+ continue
+ }
+ if isMatchExpressions(labels, deployment.Spec.Selector.MatchExpressions) {
+ matchedDatabaseList = append(matchedDatabaseList, databaseID)
+ }
+ }
+
+ stageCreate := api.StageCreate{}
+
+ for _, id := range matchedDatabaseList {
+ databaseID := id
+ stageCreate.TaskList = append(stageCreate.TaskList, api.TaskCreate{DatabaseID: &databaseID})
+ idsSeen[id] = true
+ }
+
+ create.StageList = append(create.StageList, stageCreate)
+ }
+
+ return create
+}
diff --git a/server/deploy_test.go b/server/deploy_test.go
new file mode 100644
index 0000000000..b7ada0ea94
--- /dev/null
+++ b/server/deploy_test.go
@@ -0,0 +1,226 @@
+package server
+
+import (
+ "testing"
+
+ "github.com/bytebase/bytebase/api"
+ "github.com/kr/pretty"
+)
+
+func TestGeneratePipelineCreateFromDeploymentSchedule(t *testing.T) {
+
+ db := []int{0, 1, 2, 3, 4, 5, 6, 7}
+
+ tests := []struct {
+ name string
+ schedule *api.DeploymentSchedule
+ databaseList []*api.Database
+ want *api.PipelineCreate
+ // Notice relevant position is preserved from databaseList to want.
+ // e.g. in simpleDeployments the result is [db[0], db[1]] instead of [db[1], db[0]] in the first stage.
+ }{
+ {
+ "Tenant databases matching the query in a stage should exclude all databases from previous stages.",
+ &api.DeploymentSchedule{
+ Deployments: []*api.Deployment{
+ {
+ Spec: &api.DeploymentSpec{
+ Selector: &api.LabelSelector{
+ MatchExpressions: []*api.LabelSelectorRequirement{
+ {
+ Key: "bb.location",
+ Operator: "In",
+ Values: []string{"us-central1", "europe-west1"},
+ },
+ },
+ },
+ },
+ },
+ {
+ Spec: &api.DeploymentSpec{
+ Selector: &api.LabelSelector{
+ MatchExpressions: []*api.LabelSelectorRequirement{
+ {
+ Key: "bb.location",
+ Operator: "Exists",
+ Values: nil,
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ []*api.Database{
+ {
+ ID: db[0],
+ Labels: "[{\"key\":\"bb.location\",\"value\":\"us-central1\"},{\"key\":\"bb.tenant\",\"value\":\"bytebase\"},{\"key\":\"bb.environment\",\"value\":\"Dev\"}]",
+ },
+ {
+ ID: db[1],
+ Labels: "[{\"key\":\"bb.location\",\"value\":\"earth\"},{\"key\":\"bb.tenant\",\"value\":\"bytebase\"},{\"key\":\"bb.environment\",\"value\":\"Dev\"}]",
+ },
+ },
+ &api.PipelineCreate{
+ StageList: []api.StageCreate{
+ {
+ TaskList: []api.TaskCreate{
+ {
+ DatabaseID: &db[0],
+ },
+ },
+ },
+ {
+ TaskList: []api.TaskCreate{
+ {
+ DatabaseID: &db[1],
+ },
+ },
+ },
+ },
+ },
+ },
+ {
+ "simpleDeployments",
+ &api.DeploymentSchedule{
+ Deployments: []*api.Deployment{
+ {
+ Spec: &api.DeploymentSpec{
+ Selector: &api.LabelSelector{
+ MatchExpressions: []*api.LabelSelectorRequirement{
+ {
+ Key: "bb.location",
+ Operator: "In",
+ Values: []string{"us-central1", "europe-west1"},
+ },
+ },
+ },
+ },
+ },
+ {
+ Spec: &api.DeploymentSpec{
+ Selector: &api.LabelSelector{
+ MatchExpressions: []*api.LabelSelectorRequirement{
+ {
+ Key: "bb.location",
+ Operator: "In",
+ Values: []string{"earth"},
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ []*api.Database{
+ {
+ ID: db[0],
+ Labels: "[{\"key\":\"bb.location\",\"value\":\"us-central1\"},{\"key\":\"bb.tenant\",\"value\":\"bytebase\"},{\"key\":\"bb.environment\",\"value\":\"Dev\"}]",
+ },
+ {
+ ID: db[1],
+ Labels: "[{\"key\":\"bb.location\",\"value\":\"europe-west1\"},{\"key\":\"bb.tenant\",\"value\":\"bytebase\"},{\"key\":\"bb.environment\",\"value\":\"Dev\"}]",
+ },
+ {
+ ID: db[2],
+ Labels: "[{\"key\":\"bb.location\",\"value\":\"earth\"},{\"key\":\"bb.tenant\",\"value\":\"bytebase\"},{\"key\":\"bb.environment\",\"value\":\"Dev\"}]",
+ },
+ },
+ &api.PipelineCreate{
+ StageList: []api.StageCreate{
+ {
+ TaskList: []api.TaskCreate{
+ {
+ DatabaseID: &db[0],
+ },
+ {
+ DatabaseID: &db[1],
+ },
+ },
+ },
+ {
+ TaskList: []api.TaskCreate{
+ {
+ DatabaseID: &db[2],
+ },
+ },
+ },
+ },
+ },
+ },
+ {
+ "twoDifferentKeys",
+ &api.DeploymentSchedule{
+ Deployments: []*api.Deployment{
+
+ {
+ Spec: &api.DeploymentSpec{
+ Selector: &api.LabelSelector{
+ MatchExpressions: []*api.LabelSelectorRequirement{
+ {
+ Key: "bb.tenant",
+ Operator: "In",
+ Values: []string{"bytebase"},
+ },
+ },
+ },
+ },
+ },
+ {
+ Spec: &api.DeploymentSpec{
+ Selector: &api.LabelSelector{
+ MatchExpressions: []*api.LabelSelectorRequirement{
+ {
+ Key: "bb.location",
+ Operator: "In",
+ Values: []string{"us-central1", "europe-west1"},
+ },
+ },
+ },
+ },
+ },
+ },
+ },
+ []*api.Database{
+ {
+ ID: db[0],
+ Labels: "[{\"key\":\"bb.location\",\"value\":\"us-central1\"},{\"key\":\"bb.tenant\",\"value\":\"bytebase\"},{\"key\":\"bb.environment\",\"value\":\"Dev\"}]",
+ },
+ {
+ ID: db[1],
+ Labels: "[{\"key\":\"bb.location\",\"value\":\"europe-west1\"},{\"key\":\"bb.tenant\",\"value\":\"bytebase\"},{\"key\":\"bb.environment\",\"value\":\"Dev\"}]",
+ },
+ {
+ ID: db[2],
+ Labels: "[{\"key\":\"bb.location\",\"value\":\"earth\"},{\"key\":\"bb.environment\",\"value\":\"Dev\"}]",
+ },
+ },
+ &api.PipelineCreate{
+ StageList: []api.StageCreate{
+ {
+ TaskList: []api.TaskCreate{
+ {
+ DatabaseID: &db[0],
+ },
+ {
+ DatabaseID: &db[1],
+ },
+ },
+ },
+ {
+ TaskList: []api.TaskCreate{},
+ },
+ },
+ },
+ },
+ }
+
+ for _, test := range tests {
+ create := generatePipelineCreateFromDeploymentSchedule(test.schedule, test.databaseList)
+
+ diff := pretty.Diff(create, test.want)
+ if len(diff) > 0 {
+ t.Errorf("%q: GetDeploymentSchedule() got create %+v, want %+v, diff %+v.", test.name, create, test.want, diff)
+ }
+ }
+}