diff --git a/api/v1alpha1/atlasmigration_types.go b/api/v1alpha1/atlasmigration_types.go index 048b2b9..7f39eba 100644 --- a/api/v1alpha1/atlasmigration_types.go +++ b/api/v1alpha1/atlasmigration_types.go @@ -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, }) } diff --git a/api/v1alpha1/atlasschema_types.go b/api/v1alpha1/atlasschema_types.go index 65b5bcf..d3ecc9d 100644 --- a/api/v1alpha1/atlasschema_types.go +++ b/api/v1alpha1/atlasschema_types.go @@ -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, }) } diff --git a/api/v1alpha1/reason.go b/api/v1alpha1/reason.go new file mode 100644 index 0000000..9170842 --- /dev/null +++ b/api/v1alpha1/reason.go @@ -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" +) diff --git a/internal/controller/atlasmigration_controller.go b/internal/controller/atlasmigration_controller.go index 75d6ebd..a55478a 100644 --- a/internal/controller/atlasmigration_controller.go +++ b/internal/controller/atlasmigration_controller.go @@ -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) @@ -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 } // ==================================================== @@ -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 @@ -260,7 +260,7 @@ 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); { @@ -268,10 +268,10 @@ func (r *AtlasMigrationReconciler) reconcile(ctx context.Context, data *migratio 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) } @@ -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 diff --git a/internal/controller/atlasschema_controller.go b/internal/controller/atlasschema_controller.go index 0d5e7c9..87be7a0 100644 --- a/internal/controller/atlasschema_controller.go +++ b/internal/controller/atlasschema_controller.go @@ -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) @@ -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. @@ -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{ @@ -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 } // ==================================================== @@ -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) } @@ -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 } @@ -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 diff --git a/internal/controller/atlasschema_controller_test.go b/internal/controller/atlasschema_controller_test.go index 2e9c321..4918f1f 100644 --- a/internal/controller/atlasschema_controller_test.go +++ b/internal/controller/atlasschema_controller_test.go @@ -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,