Skip to content

Commit

Permalink
controllers: add deployment context to migrate cmd (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
datdao authored Nov 9, 2023
1 parent 26867f5 commit 2aa7416
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 4 deletions.
32 changes: 32 additions & 0 deletions api/v1alpha1/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2023 The Atlas Operator Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v1alpha1

import "context"

type versionCtxKey struct{}

// WithVersionContext returns a new context with the given verison.
func WithVersionContext(ctx context.Context, version string) context.Context {
return context.WithValue(ctx, versionCtxKey{}, version)
}

// VersionFromContext returns the version from the given context.
func VersionFromContext(ctx context.Context) string {
if v := ctx.Value(versionCtxKey{}); v != nil {
return v.(string)
}
return ""
}
8 changes: 7 additions & 1 deletion controllers/atlasmigration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,13 @@ func (r *AtlasMigrationReconciler) reconcile(ctx context.Context, dir, envName s
}, nil
}
// Execute Atlas CLI migrate command
report, err := c.MigrateApply(ctx, &atlas.MigrateApplyParams{Env: envName})
report, err := c.MigrateApply(ctx, &atlas.MigrateApplyParams{
Env: envName,
Context: &atlas.DeployRunContext{
TriggerType: atlas.TriggerTypeKubernetes,
TriggerVersion: dbv1alpha1.VersionFromContext(ctx),
},
})
if err != nil {
return nil, transient(err)
}
Expand Down
73 changes: 73 additions & 0 deletions controllers/atlasmigration_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ package controllers
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -758,6 +764,59 @@ env {
}`, fileContent.String())
}

func TestMigrationWithDeploymentContext(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
type (
RunContext struct {
TriggerType string `json:"triggerType,omitempty"`
TriggerVersion string `json:"triggerVersion,omitempty"`
}
graphQLQuery struct {
Query string `json:"query"`
Variables json.RawMessage `json:"variables"`
MigrateApplyReport struct {
Input struct {
Context *RunContext `json:"context,omitempty"`
} `json:"input"`
}
}
)
var m graphQLQuery
require.NoError(t, json.NewDecoder(r.Body).Decode(&m))
switch {
case strings.Contains(m.Query, "query"):
memdir := &migrate.MemDir{}
memdir.WriteFile("30230412003626.sql", []byte(`CREATE TABLE foo (id INT PRIMARY KEY)`))
writeDir(t, memdir, w)
case strings.Contains(m.Query, "reportMigration"):
err := json.Unmarshal(m.Variables, &m.MigrateApplyReport)
require.NoError(t, err)
require.Equal(t, "my-version", m.MigrateApplyReport.Input.Context.TriggerVersion)
require.Equal(t, "KUBERNETES", m.MigrateApplyReport.Input.Context.TriggerType)
}
}))
defer srv.Close()
tt := migrationCliTest(t)
tt.initDefaultTokenSecret()
am := tt.getAtlasMigration()
am.Spec.Cloud.URL = srv.URL
am.Spec.Dir.Remote.Name = "my-remote-dir"
am.Spec.Cloud.Project = "my-project"
am.Spec.Cloud.TokenFrom = v1alpha1.TokenFrom{
SecretKeyRef: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: "my-secret",
},
Key: "token",
},
}
tt.k8s.put(am)
ctx := dbv1alpha1.WithVersionContext(context.Background(), "my-version")
result, err := tt.r.Reconcile(ctx, migrationReq())
require.NoError(tt, err)
require.EqualValues(tt, reconcile.Result{}, result)
}

func migrationObjmeta() metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: "atlas-migration",
Expand Down Expand Up @@ -912,3 +971,17 @@ func (t *migrationTest) initDefaultTokenSecret() {
func (t *migrationTest) events() []string {
return events(t.r.recorder)
}

func writeDir(t *testing.T, dir migrate.Dir, w io.Writer) {
// Checksum before archiving.
hf, err := dir.Checksum()
require.NoError(t, err)
ht, err := hf.MarshalText()
require.NoError(t, err)
require.NoError(t, dir.WriteFile(migrate.HashFileName, ht))
// Archive and send.
arc, err := migrate.ArchiveDir(dir)
require.NoError(t, err)
_, err = fmt.Fprintf(w, `{"data":{"dirState":{"content":%q}}}`, base64.StdEncoding.EncodeToString(arc))
require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ toolchain go1.21.2

require (
ariga.io/atlas v0.14.3-0.20231010104048-0c071bfc9161
ariga.io/atlas-go-sdk v0.1.1-0.20231029150106-1927c56b057b
ariga.io/atlas-go-sdk v0.1.1-0.20231102105853-9ab4fb4ddd54
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
golang.org/x/mod v0.13.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ariga.io/atlas v0.14.3-0.20231010104048-0c071bfc9161 h1:xZS2wAf1AzRNA/8iD2LTAXtIZuIDYDXsZRlYBAyBu0A=
ariga.io/atlas v0.14.3-0.20231010104048-0c071bfc9161/go.mod h1:isZrlzJ5cpoCoKFoY9knZug7Lq4pP1cm8g3XciLZ0Pw=
ariga.io/atlas-go-sdk v0.1.1-0.20231029150106-1927c56b057b h1:pFSDzqE7YU7S8+fRnkiycFXCe39hpLALr2fgYhxjzAQ=
ariga.io/atlas-go-sdk v0.1.1-0.20231029150106-1927c56b057b/go.mod h1:owkEEXw6jqne5KPVDfKsYB7cwMiMk3jtOiAAeKxS/yU=
ariga.io/atlas-go-sdk v0.1.1-0.20231102105853-9ab4fb4ddd54 h1:/LzEDWzKDJH2e8vht7+kl2VO5hmkSC5SCyKAZtkLhiI=
ariga.io/atlas-go-sdk v0.1.1-0.20231102105853-9ab4fb4ddd54/go.mod h1:owkEEXw6jqne5KPVDfKsYB7cwMiMk3jtOiAAeKxS/yU=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main

import (
"bytes"
"context"
"flag"
"os"
"strconv"
Expand Down Expand Up @@ -89,6 +90,9 @@ func main() {
LeaderElection: enableLeaderElection,
LeaderElectionID: "5220c287.atlasgo.io",
LeaderElectionReleaseOnCancel: true,
BaseContext: func() context.Context {
return dbv1alpha1.WithVersionContext(context.Background(), version)
},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down

0 comments on commit 2aa7416

Please sign in to comment.