Skip to content

Commit

Permalink
change keepDevDB flag to disposeDevDB
Browse files Browse the repository at this point in the history
  • Loading branch information
datdao committed Sep 12, 2023
1 parent 1de1750 commit 3dadceb
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 39 deletions.
4 changes: 2 additions & 2 deletions charts/atlas-operator/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ spec:
securityContext:
{{- toYaml .Values.containerSecurityContext | nindent 12 }}
env:
{{- if .Values.keepDevDB }}
- name: KEEP_DEVDB
{{- if .Values.disposeDevDB }}
- name: DISPOSE_DEVDB
value: "true"
{{- end }}
{{- with .Values.imagePullSecrets }}
Expand Down
4 changes: 2 additions & 2 deletions charts/atlas-operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ tolerations: []

affinity: {}

# By default, the operator will remove devdb pods after migration is complete.
# By default, the operator will recreate devdb pods after migration
# Set this to true to keep the devdb pods around.
keepDevDB: false
disposeDevDB: false
6 changes: 3 additions & 3 deletions controllers/atlasschema_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type (
configMapWatcher *watch.ResourceWatcher
secretWatcher *watch.ResourceWatcher
recorder record.EventRecorder
keepDevDB bool
disposeDevDB bool
}
// managedData contains information about the managed database and its desired state.
managedData struct {
Expand All @@ -74,15 +74,15 @@ type (
}
)

func NewAtlasSchemaReconciler(mgr Manager, execPath string, keepDevDB bool) *AtlasSchemaReconciler {
func NewAtlasSchemaReconciler(mgr Manager, execPath string, disposeDevDB bool) *AtlasSchemaReconciler {
return &AtlasSchemaReconciler{
Client: mgr.GetClient(),
scheme: mgr.GetScheme(),
execPath: execPath,
configMapWatcher: watch.New(),
secretWatcher: watch.New(),
recorder: mgr.GetEventRecorderFor("atlasschema-controller"),
keepDevDB: keepDevDB,
disposeDevDB: disposeDevDB,
}
}

Expand Down
10 changes: 3 additions & 7 deletions controllers/atlasschema_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ func TestReconcile_Reconcile(t *testing.T) {
ObjectMeta: meta,
Spec: dbv1alpha1.AtlasSchemaSpec{},
}
h, reconcile := newRunner(newAtlasSchemaReconcilerWrapper(true), func(cb *fake.ClientBuilder) {
h, reconcile := newRunner(func(mgr Manager, name string) reconcile.Reconciler {
return NewAtlasSchemaReconciler(mgr, name, false)
}, func(cb *fake.ClientBuilder) {
cb.WithObjects(obj)
})
assert := func(except ctrl.Result, ready bool, reason, msg string) {
Expand Down Expand Up @@ -702,9 +704,3 @@ func events(r record.EventRecorder) []string {
}
}
}

func newAtlasSchemaReconcilerWrapper(keepDevDB bool) func(Manager, string) reconcile.Reconciler {
return func(mgr Manager, name string) reconcile.Reconciler {
return NewAtlasSchemaReconciler(mgr, name, keepDevDB)
}
}
30 changes: 15 additions & 15 deletions controllers/devdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,21 @@ const (

// cleanUp clean up any resources created by the controller
func (r *AtlasSchemaReconciler) cleanUp(ctx context.Context, sc *dbv1alpha1.AtlasSchema) {
// If keepDevDB is true, delete pods to clean up
// Otherwise, scale down the deployment to 0
if r.keepDevDB {
// If disposeDevDB is true, scale down the deployment to 0
// Otherwise, delete pods to clean up
if r.disposeDevDB {
deploy := &appsv1.Deployment{}
key := nameDevDB(sc.ObjectMeta)
err := r.Get(ctx, key, deploy)
if err != nil {
r.recorder.Eventf(sc, corev1.EventTypeWarning, "CleanUpDevDB", "Error getting devDB deployment: %v", err)
return
}
deploy.Spec.Replicas = new(int32)
if err := r.Update(ctx, deploy); err != nil {
r.recorder.Eventf(sc, corev1.EventTypeWarning, "CleanUpDevDB", "Error scaling down devDB deployment: %v", err)
}
} else {
pods := &corev1.PodList{}
err := r.List(ctx, pods, client.MatchingLabels(map[string]string{
labelInstance: nameDevDB(sc.ObjectMeta).Name,
Expand All @@ -62,18 +74,6 @@ func (r *AtlasSchemaReconciler) cleanUp(ctx context.Context, sc *dbv1alpha1.Atla
r.recorder.Eventf(sc, corev1.EventTypeWarning, "CleanUpDevDB", "Error deleting devDB pod %s: %v", p.Name, err)
}
}
} else {
deploy := &appsv1.Deployment{}
key := nameDevDB(sc.ObjectMeta)
err := r.Get(ctx, key, deploy)
if err != nil {
r.recorder.Eventf(sc, corev1.EventTypeWarning, "CleanUpDevDB", "Error getting devDB deployment: %v", err)
return
}
deploy.Spec.Replicas = new(int32)
if err := r.Update(ctx, deploy); err != nil {
r.recorder.Eventf(sc, corev1.EventTypeWarning, "CleanUpDevDB", "Error scaling down devDB deployment: %v", err)
}
}

}
Expand Down
20 changes: 10 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ const (
envNoUpdate = "SKIP_VERCHECK"
vercheckURL = "https://vercheck.ariga.io"
execPath = "/atlas"
// keepDevDB when enabled it keeps the devdb running after migration
keepDevDB = "KEEP_DEVDB"
// disposeDevDB when enabled it deletes the devDB pods after the schema is created
disposeDevDB = "DISPOSE_DEVDB"
)

func init() {
Expand Down Expand Up @@ -94,8 +94,8 @@ func main() {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
keepDevDB := getKeepDevDBEnv()
if err = controllers.NewAtlasSchemaReconciler(mgr, execPath, keepDevDB).
disposeDevDB := getDisposeDevDBEnv()
if err = controllers.NewAtlasSchemaReconciler(mgr, execPath, disposeDevDB).
SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "AtlasSchema")
os.Exit(1)
Expand Down Expand Up @@ -159,16 +159,16 @@ func checkForUpdate() {
}
}

// getKeepDevDBEnv returns the value of the env var KEEP_DEVDB.
func getKeepDevDBEnv() bool {
env := os.Getenv(keepDevDB)
// getDisposeDevDBEnv returns the value of the env var DISPOSE_DEVDB.
func getDisposeDevDBEnv() bool {
env := os.Getenv(disposeDevDB)
if env == "" {
return false
}
keepDevDB, err := strconv.ParseBool(env)
disposeDevDB, err := strconv.ParseBool(env)
if err != nil {
setupLog.Error(err, "invalid value for env var KEEP_DEVDB, expected true or false")
setupLog.Error(err, "invalid value for env var DISPOSE_DEVDB, expected true or false")
os.Exit(1)
}
return keepDevDB
return disposeDevDB
}

0 comments on commit 3dadceb

Please sign in to comment.