diff --git a/api/v1alpha1/atlasschema_types.go b/api/v1alpha1/atlasschema_types.go index d8f94c1..806f69e 100644 --- a/api/v1alpha1/atlasschema_types.go +++ b/api/v1alpha1/atlasschema_types.go @@ -199,10 +199,14 @@ func (sc *AtlasSchema) IsHashModified(hash string) bool { // SetReady sets the Ready condition to true func (sc *AtlasSchema) SetReady(status AtlasSchemaStatus, report any) { var msg string - if j, err := json.Marshal(report); err != nil { - msg = fmt.Sprintf("Error marshalling apply response: %v", err) + if report != nil { + if j, err := json.Marshal(report); err != nil { + msg = fmt.Sprintf("Error marshalling apply response: %v", err) + } else { + msg = fmt.Sprintf("The schema has been applied successfully. Apply response: %s", j) + } } else { - msg = fmt.Sprintf("The schema has been applied successfully. Apply response: %s", j) + msg = "The schema has been applied successfully." } sc.Status = status meta.SetStatusCondition(&sc.Status.Conditions, metav1.Condition{ diff --git a/internal/controller/atlasschema_controller.go b/internal/controller/atlasschema_controller.go index 341cba2..0d0a693 100644 --- a/internal/controller/atlasschema_controller.go +++ b/internal/controller/atlasschema_controller.go @@ -272,7 +272,15 @@ func (r *AtlasSchemaReconciler) Reconcile(ctx context.Context, req ctrl.Request) To: []string{desiredURL}, Pending: true, }) - if err != nil { + switch { + case err != nil && strings.Contains(err.Error(), "no changes to be made"): + res.SetReady(dbv1alpha1.AtlasSchemaStatus{ + LastApplied: time.Now().Unix(), + ObservedHash: hash, + }, nil) + r.recorder.Event(res, corev1.EventTypeNormal, "Applied", "Applied schema") + return ctrl.Result{}, nil + case err != nil: reason, msg := "SchemaPlan", err.Error() res.SetNotReady(reason, msg) r.recorder.Event(res, corev1.EventTypeWarning, reason, msg) @@ -281,14 +289,15 @@ func (r *AtlasSchemaReconciler) Reconcile(ctx context.Context, req ctrl.Request) } r.recordErrEvent(res, err) return result(err) + default: + 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" + res.SetNotReady(reason, msg) + r.recorder.Event(res, corev1.EventTypeNormal, reason, msg) + return ctrl.Result{RequeueAfter: time.Second * 5}, nil } - 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" - res.SetNotReady(reason, msg) - r.recorder.Event(res, corev1.EventTypeNormal, reason, msg) - return ctrl.Result{RequeueAfter: time.Second * 5}, nil } // List the schema plans to check if there are any plans. switch plans, err := cli.SchemaPlanList(ctx, &atlasexec.SchemaPlanListParams{ diff --git a/test/e2e/testscript/schema-review-always.txtar b/test/e2e/testscript/schema-review-always.txtar index b58f1ac..db7b5e8 100644 --- a/test/e2e/testscript/schema-review-always.txtar +++ b/test/e2e/testscript/schema-review-always.txtar @@ -29,6 +29,19 @@ kubectl-wait-ready AtlasSchema/postgres # Inspect the schema to ensure it's correct atlas schema inspect -u ${DB_URL} cmp stdout schema-v1.hcl +kubectl get -o jsonpath --template='{.status.observed_hash}' AtlasSchema/postgres +stdout 2a59599f3d4f2f07d4fabb87bf872030020fd7410e10a1042ace7a0865953cac + +# Apply again without any change in the schema, it should be a no-op +kubectl patch -f schema.yaml --type merge --patch-file patch-comment.yaml +# Ensure the controller is aware of the change +exec sleep 10 +kubectl-wait-ready AtlasSchema/postgres +# Inspect the schema to ensure it's correct +atlas schema inspect -u ${DB_URL} +cmp stdout schema-v1.hcl +kubectl get -o jsonpath --template='{.status.observed_hash}' AtlasSchema/postgres +stdout 6a60e051a482fb40392f768e0a080bc101bd9d87efa21ff40472b1dbd7d4e7a8 -- empty.hcl -- schema "public" { comment = "standard public schema" @@ -51,6 +64,16 @@ table "t1" { schema "public" { comment = "standard public schema" } +-- patch-comment.yaml -- +spec: + schema: + sql: | + -- added some comment to cause the hash change + create table t1 ( + id int not null, + c1 int, + primary key (id) + ); -- schema.yaml -- apiVersion: db.atlasgo.io/v1alpha1 kind: AtlasSchema