Skip to content

Commit

Permalink
internal/controller: replace string literals with constants for reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
datdao committed Feb 18, 2025
1 parent 59af26e commit 7e656c7
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 20 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha1/atlasmigration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (m *AtlasMigration) SetReady(status AtlasMigrationStatus) {
meta.SetStatusCondition(&m.Status.Conditions, metav1.Condition{
Type: readyCond,
Status: metav1.ConditionTrue,
Reason: "Applied",
Reason: ReasonApplied,
})
}

Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/atlasschema_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (sc *AtlasSchema) SetReady(status AtlasSchemaStatus, report any) {
meta.SetStatusCondition(&sc.Status.Conditions, metav1.Condition{
Type: readyCond,
Status: metav1.ConditionTrue,
Reason: "Applied",
Reason: ReasonApplied,
Message: msg,
})
}
Expand Down
18 changes: 18 additions & 0 deletions api/v1alpha1/reason.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package v1alpha1

const (
// ReasonReconciling represents for the reconciliation is in progress.
ReasonReconciling = "Reconciling"
// ReasonGettingDevDB represents the reason for getting the dev database.
ReasonGettingDevDB = "GettingDevDB"
// ReasonWhoAmI represents the reason for getting the current user via Atlas CLI
ReasonWhoAmI = "WhoAmI"
// ReasonApplyingMigration represents the reason for applied a schema/migration resource successfully.
ReasonApplied = "Applied"
// ReasonApprovalPending represents the reason for the approval is pending.
ReasonApprovalPending = "ApprovalPending"
// ReasonCreatingAtlasClient represents the reason for creating an Atlas client.
ReasonCreatingAtlasClient = "CreatingAtlasClient"
// ReasonCreatingWorkingDir represents the reason for creating a working directory.
ReasonCreatingWorkingDir = "CreatingWorkingDir"
)
16 changes: 8 additions & 8 deletions internal/controller/atlasmigration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (r *AtlasMigrationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}()
// When the resource is first created, create the "Ready" condition.
if len(res.Status.Conditions) == 0 {
res.SetNotReady("Reconciling", "Reconciling")
res.SetNotReady(dbv1alpha1.ReasonReconciling, "Reconciling")
return ctrl.Result{Requeue: true}, nil
}
data, err := r.extractData(ctx, res)
Expand All @@ -134,7 +134,7 @@ func (r *AtlasMigrationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
// any heavy jobs if the hash is different from the last applied.
// This is to ensure that other tools know we are still applying the changes.
if res.IsReady() && res.IsHashModified(data.ObservedHash) {
res.SetNotReady("Reconciling", "Current migration data has changed")
res.SetNotReady(dbv1alpha1.ReasonReconciling, "Current migration data has changed")
return ctrl.Result{Requeue: true}, nil
}
// ====================================================
Expand All @@ -149,7 +149,7 @@ func (r *AtlasMigrationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
// spin up a dev-db and get the connection string.
data.DevURL, err = r.devDB.devURL(ctx, res, *data.URL)
if err != nil {
return r.resultErr(res, err, "GettingDevDB")
return r.resultErr(res, err, dbv1alpha1.ReasonGettingDevDB)
}
}
// Reconcile given resource
Expand Down Expand Up @@ -260,18 +260,18 @@ func (r *AtlasMigrationReconciler) reconcile(ctx context.Context, data *migratio
defer wd.Close()
c, err := r.atlasClient(wd.Path(), data.Cloud)
if err != nil {
return r.resultErr(res, err, "CreatingAtlasClient")
return r.resultErr(res, err, dbv1alpha1.ReasonCreatingAtlasClient)
}
var whoami *atlasexec.WhoAmI
switch whoami, err = c.WhoAmI(ctx); {
case errors.Is(err, atlasexec.ErrRequireLogin):
log.Info("the resource is not connected to Atlas Cloud")
if data.Config != nil {
err = errors.New("login is required to use custom atlas.hcl config")
return r.resultErr(res, err, "WhoAmI")
return r.resultErr(res, err, dbv1alpha1.ReasonWhoAmI)
}
case err != nil:
return r.resultErr(res, err, "WhoAmI")
return r.resultErr(res, err, dbv1alpha1.ReasonWhoAmI)
default:
log.Info("the resource is connected to Atlas Cloud", "org", whoami.Org)
}
Expand Down Expand Up @@ -331,10 +331,10 @@ func (r *AtlasMigrationReconciler) reconcile(ctx context.Context, data *migratio
case StatePending:
res.Status.ApprovalURL = run.URL
err = transient(&ProtectedFlowError{
reason: "ApprovalPending",
reason: dbv1alpha1.ReasonApprovalPending,
msg: fmt.Sprintf("plan approval pending, review here: %s", run.URL),
})
return r.resultErr(res, err, "ApprovalPending")
return r.resultErr(res, err, dbv1alpha1.ReasonApprovalPending)
case StateAborted:
res.Status.ApprovalURL = run.URL
// Migration is aborted, no need to reapply
Expand Down
18 changes: 9 additions & 9 deletions internal/controller/atlasschema_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (r *AtlasSchemaReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}()
// When the resource is first created, create the "Ready" condition.
if len(res.Status.Conditions) == 0 {
res.SetNotReady("Reconciling", "Reconciling")
res.SetNotReady(dbv1alpha1.ReasonReconciling, "Reconciling")
return ctrl.Result{Requeue: true}, nil
}
data, err := r.extractData(ctx, res)
Expand All @@ -144,14 +144,14 @@ func (r *AtlasSchemaReconciler) Reconcile(ctx context.Context, req ctrl.Request)
// spin up a dev-db and get the connection string.
data.DevURL, err = r.devDB.devURL(ctx, res, *data.URL)
if err != nil {
return r.resultErr(res, err, "GettingDevDB")
return r.resultErr(res, err, dbv1alpha1.ReasonGettingDevDB)
}
}
// Create a working directory for the Atlas CLI
// The working directory contains the atlas.hcl config.
wd, err := atlasexec.NewWorkingDir(opts...)
if err != nil {
return r.resultErr(res, err, "CreatingWorkingDir")
return r.resultErr(res, err, dbv1alpha1.ReasonCreatingWorkingDir)
}
defer wd.Close()
// This function will be used to edit and re-render the atlas.hcl file in the working directory.
Expand All @@ -166,7 +166,7 @@ func (r *AtlasSchemaReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}
cli, err := r.atlasClient(wd.Path(), data.Cloud)
if err != nil {
return r.resultErr(res, err, "CreatingAtlasClient")
return r.resultErr(res, err, dbv1alpha1.ReasonCreatingAtlasClient)
}
// Calculate the hash of the current schema.
hash, err := cli.SchemaInspect(ctx, &atlasexec.SchemaInspectParams{
Expand All @@ -182,7 +182,7 @@ func (r *AtlasSchemaReconciler) Reconcile(ctx context.Context, req ctrl.Request)
// any heavy jobs if the hash is different from the last applied.
// This is to ensure that other tools know we are still applying the changes.
if res.IsReady() && res.IsHashModified(hash) {
res.SetNotReady("Reconciling", "current schema does not match last applied")
res.SetNotReady(dbv1alpha1.ReasonReconciling, "current schema does not match last applied")
return ctrl.Result{Requeue: true}, nil
}
// ====================================================
Expand All @@ -194,10 +194,10 @@ func (r *AtlasSchemaReconciler) Reconcile(ctx context.Context, req ctrl.Request)
case errors.Is(err, atlasexec.ErrRequireLogin):
log.Info("the resource is not connected to Atlas Cloud")
if data.Config != nil {
return r.resultErr(res, err, "WhoAmI")
return r.resultErr(res, err, dbv1alpha1.ReasonWhoAmI)
}
case err != nil:
res.SetNotReady("WhoAmI", err.Error())
res.SetNotReady(dbv1alpha1.ReasonWhoAmI, err.Error())
default:
log.Info("the resource is connected to Atlas Cloud", "org", whoami.Org)
}
Expand Down Expand Up @@ -282,7 +282,7 @@ func (r *AtlasSchemaReconciler) Reconcile(ctx context.Context, req ctrl.Request)
log.Info("created a new schema plan", "plan", plan.File.URL, "desiredURL", desiredURL)
res.Status.PlanURL = plan.File.URL
res.Status.PlanLink = plan.File.Link
reason, msg := "ApprovalPending", "Schema plan is waiting for approval"
reason, msg := dbv1alpha1.ReasonApprovalPending, "Schema plan is waiting for approval"
r.recorder.Event(res, corev1.EventTypeNormal, reason, msg)
return ctrl.Result{RequeueAfter: time.Second * 5}, nil
}
Expand Down Expand Up @@ -314,7 +314,7 @@ func (r *AtlasSchemaReconciler) Reconcile(ctx context.Context, req ctrl.Request)
log.Info("found a pending schema plan, waiting for approval", "plan", plans[0].URL)
res.Status.PlanURL = plans[0].URL
res.Status.PlanLink = plans[0].Link
reason, msg := "ApprovalPending", "Schema plan is waiting for approval"
reason, msg := dbv1alpha1.ReasonApprovalPending, "Schema plan is waiting for approval"
res.SetNotReady(reason, msg)
r.recorder.Event(res, corev1.EventTypeNormal, reason, msg)
return ctrl.Result{RequeueAfter: time.Second * 5}, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/atlasschema_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestReconcile_Reconcile(t *testing.T) {
},
})
// Third reconcile, return error for missing schema
assert(ctrl.Result{}, false, "CreatingWorkingDir", "the desired state is not set")
assert(ctrl.Result{}, false, dbv1alpha1.ReasonCreatingWorkingDir, "the desired state is not set")
// Add schema,
h.patch(t, &dbv1alpha1.AtlasSchema{
ObjectMeta: meta,
Expand Down

0 comments on commit 7e656c7

Please sign in to comment.