Skip to content

Commit

Permalink
internal/controller: prioritize custom config when merging (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
datdao authored Jan 15, 2025
1 parent 9d7fe13 commit 424d10f
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ test: manifests generate fmt vet envtest ## Run tests.
.PHONY: test-e2e
SKAFFOLD_PROFILE ?= kustomize
test-e2e: manifests generate fmt vet ## Run the e2e tests. Expected an isolated environment using Kind.
SKAFFOLD_PROFILE=${SKAFFOLD_PROFILE} go test -timeout 20m -v ./test/e2e/ -run="^TestOperator/${TEST_RUN}" -parallel 4
SKAFFOLD_PROFILE=${SKAFFOLD_PROFILE} go test -timeout 20m -v ./test/e2e/ -run="^TestOperator/${TEST_RUN}" -parallel 3

.PHONY: kind-image
kind-image:
Expand Down
3 changes: 1 addition & 2 deletions internal/controller/atlasmigration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,7 @@ func (d *migrationData) render(w io.Writer) error {
}
// Merge the config block if it is set
if d.Config != nil {
mergeBlocks(d.Config.Body(), f.Body())
f = d.Config
mergeBlocks(f.Body(), d.Config.Body())
}
env := searchBlock(f.Body(), hclwrite.NewBlock("env", []string{d.EnvName}))
if env == nil {
Expand Down
21 changes: 11 additions & 10 deletions internal/controller/atlasmigration_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1063,12 +1063,12 @@ func TestCustomAtlasHCL(t *testing.T) {
var fileContent bytes.Buffer
require.NoError(t, migrate.render(&fileContent))
require.EqualValues(t, `env "kubernetes" {
url = "sqlite://file2/?mode=memory"
dev = "sqlite://dev/?mode=memory"
migration {
dir = "file://migrations"
baseline = "20230412003626"
}
dev = "sqlite://dev/?mode=memory"
url = "sqlite://file2/?mode=memory"
}
`, fileContent.String())
}
Expand Down Expand Up @@ -1131,20 +1131,21 @@ env "kubernetes" {
}
var fileContent bytes.Buffer
require.NoError(t, migrate.render(&fileContent))
require.EqualValues(t, `atlas {
require.EqualValues(t, `env "kubernetes" {
migration {
dir = "atlas://my-remote-dir?tag=my-remote-tag"
}
dev = "sqlite://dev/?mode=memory"
url = "sqlite://file2/?mode=memory"
}
atlas {
cloud {
token = "my-token"
url = "https://atlasgo.io/"
project = "my-project"
}
}
env "kubernetes" {
url = "sqlite://file2/?mode=memory"
dev = "sqlite://dev/?mode=memory"
migration {
dir = "atlas://my-remote-dir?tag=my-remote-tag"
}
}`, fileContent.String())
`, fileContent.String())
}

func TestCustomAtlasHCL_BaselineTemplate(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions internal/controller/atlasschema_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,8 +789,7 @@ func (d *managedData) render(w io.Writer) error {
}
// Merge config into the atlas.hcl file.
if d.Config != nil {
mergeBlocks(d.Config.Body(), f.Body())
f = d.Config
mergeBlocks(f.Body(), d.Config.Body())
}
if d.EnvName == "" {
return errors.New("env name is not set")
Expand Down
47 changes: 9 additions & 38 deletions internal/controller/atlasschema_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,17 @@ env "kubernetes" {
}
}
}
`),
`),
}
err := data.render(&buf)
require.NoError(t, err)
expected := `
env "kubernetes" {
expected := `env "kubernetes" {
schema {
src = "file://schema.sql"
}
url = "mysql://root:password@localhost:3306/test"
dev = "mysql://root:password@localhost:3306/dev"
schemas = ["foo", "bar"]
diff {
concurrent_index {
create = true
Expand All @@ -573,42 +578,8 @@ env "kubernetes" {
error = true
}
}
dev = "mysql://root:password@localhost:3306/dev"
schemas = ["foo", "bar"]
url = "mysql://root:password@localhost:3306/test"
schema {
src = "file://schema.sql"
}
}
`
require.EqualValues(t, expected, buf.String())
}

func TestCustomAtlasHCL_UnnamedBlock(t *testing.T) {
var buf bytes.Buffer
data := &managedData{
EnvName: defaultEnvName,
URL: must(url.Parse("mysql://root:password@localhost:3306/test")),
Desired: must(url.Parse("file://schema.sql")),
Schemas: []string{"foo", "bar"},
Config: mustParseHCL(`
env {
name = atlas.env
dev = "mysql://root:password@localhost:3306/dev"
}`),
}
err := data.render(&buf)
require.NoError(t, err)
expected := `
env {
name = atlas.env
dev = "mysql://root:password@localhost:3306/dev"
schemas = ["foo", "bar"]
url = "mysql://root:password@localhost:3306/test"
schema {
src = "file://schema.sql"
}
}`
`
require.EqualValues(t, expected, buf.String())
}

Expand Down
7 changes: 0 additions & 7 deletions test/e2e/testscript/migration-config-variables.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,13 @@ kubectl create secret generic migration-config --from-file=config.hcl
kubectl patch -f migration.yaml --type merge --patch-file patch-migration-config-from-configmap.yaml
kubectl wait --for=jsonpath='{.status.conditions[*].reason}'=Applied --timeout=500s AtlasMigrations/sample

# Ensure the operator priority the spec url over the config
kubectl patch -f migration.yaml --type merge --patch-file patch-invalid-url.yaml
kubectl wait --for=jsonpath='{.status.conditions[*].message}'='"Error: postgres: scanning system variables: pq: Could not detect default username. Please provide one explicitly"' --timeout=500s AtlasMigrations/sample

-- patch-migration-config-from-configmap.yaml --
spec:
config:
configFrom:
secretKeyRef:
name: migration-config
key: config.hcl
-- patch-invalid-url.yaml --
spec:
url: "postgres://invalid"
-- config.hcl --
variable "db_url" {
type = string
Expand Down
7 changes: 0 additions & 7 deletions test/e2e/testscript/schema-config-variables.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ kubectl create secret generic schema-config --from-file=config.hcl
kubectl patch -f schema.yaml --type merge --patch-file patch-schema-config-from-configmap.yaml
kubectl wait --for=jsonpath='{.status.conditions[*].reason}'=Applied --timeout=500s AtlasSchemas/sample

# Ensure the operator priority the spec url over the config
kubectl patch -f schema.yaml --type merge --patch-file patch-invalid-url.yaml
kubectl wait --for=jsonpath='{.status.conditions[*].message}'='"Error: postgres: scanning system variables: pq: Could not detect default username. Please provide one explicitly"' --timeout=500s AtlasSchemas/sample

-- schema.yaml --
apiVersion: db.atlasgo.io/v1alpha1
kind: AtlasSchema
Expand Down Expand Up @@ -75,9 +71,6 @@ spec:
secretKeyRef:
name: schema-config
key: config.hcl
-- patch-invalid-url.yaml --
spec:
url: "postgres://invalid"
-- config.hcl --
variable "db_url" {
type = string
Expand Down

0 comments on commit 424d10f

Please sign in to comment.