From 5fb9be737170e68d9be50d98bd32a127bc62f73d Mon Sep 17 00:00:00 2001 From: "Giau. Tran Minh" <12751435+giautm@users.noreply.github.com> Date: Thu, 3 Oct 2024 19:08:28 +0700 Subject: [PATCH] atlas/schema: check the whoami result (#197) --- go.mod | 2 +- go.sum | 4 +- internal/controller/atlasschema_controller.go | 54 +++++++++++-------- internal/controller/lint.go | 13 ++--- 4 files changed, 41 insertions(+), 32 deletions(-) diff --git a/go.mod b/go.mod index cbca855..fa5e03a 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ toolchain go1.22.0 require ( ariga.io/atlas v0.21.2-0.20240424054305-cf871e4a7c1e - ariga.io/atlas-go-sdk v0.6.4-0.20241002105845-ab6583b472e8 + ariga.io/atlas-go-sdk v0.6.4-0.20241003090743-da72e048c153 github.com/rogpeppe/go-internal v1.13.1 github.com/stretchr/testify v1.9.0 golang.org/x/mod v0.18.0 diff --git a/go.sum b/go.sum index 0d1d270..79cb1fc 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ ariga.io/atlas v0.21.2-0.20240424054305-cf871e4a7c1e h1:3iOt1DD4c6IGRjxcm+C5IeI+pHo7HnqMoN14xjVjxsE= ariga.io/atlas v0.21.2-0.20240424054305-cf871e4a7c1e/go.mod h1:VPlcXdd4w2KqKnH54yEZcry79UAhpaWaxEsmn5JRNoE= -ariga.io/atlas-go-sdk v0.6.4-0.20241002105845-ab6583b472e8 h1:C5ZatdFRE+p5enmFpwlViDR6kNWqdcmtLLzyvhf320I= -ariga.io/atlas-go-sdk v0.6.4-0.20241002105845-ab6583b472e8/go.mod h1:9Q+/04PVyJHUse1lEE9Kp6E18xj/6mIzaUTcWYSjSnQ= +ariga.io/atlas-go-sdk v0.6.4-0.20241003090743-da72e048c153 h1:DLrVzxbqRbzvtJP8q2FJe83y9SRKXQmEUVYt2NWhQas= +ariga.io/atlas-go-sdk v0.6.4-0.20241003090743-da72e048c153/go.mod h1:9Q+/04PVyJHUse1lEE9Kp6E18xj/6mIzaUTcWYSjSnQ= github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60= github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7lmo= diff --git a/internal/controller/atlasschema_controller.go b/internal/controller/atlasschema_controller.go index 43edae1..f9f63de 100644 --- a/internal/controller/atlasschema_controller.go +++ b/internal/controller/atlasschema_controller.go @@ -153,23 +153,39 @@ func (r *AtlasSchemaReconciler) Reconcile(ctx context.Context, req ctrl.Request) return result(err) } } + opts := []atlasexec.Option{atlasexec.WithAtlasHCL(data.render)} + if u := data.Desired; u != nil && u.Scheme == dbv1alpha1.SchemaTypeFile { + // Write the schema file to the working directory. + opts = append(opts, func(ce *atlasexec.WorkingDir) error { + _, err := ce.WriteFile(filepath.Join(u.Host, u.Path), data.schema) + return err + }) + } // Create a working directory for the Atlas CLI // The working directory contains the atlas.hcl config. - wd, err := atlasexec.NewWorkingDir(atlasexec.WithAtlasHCL(data.render)) + wd, err := atlasexec.NewWorkingDir(opts...) if err != nil { res.SetNotReady("CreatingWorkingDir", err.Error()) r.recordErrEvent(res, err) return result(err) } defer wd.Close() - // Write the schema file to the working directory. - if u := data.Desired; u != nil && u.Scheme == dbv1alpha1.SchemaTypeFile { - _, err = wd.WriteFile(filepath.Join(u.Host, u.Path), data.schema) - if err != nil { - res.SetNotReady("CreatingSchemaFile", err.Error()) - r.recordErrEvent(res, err) - return result(err) - } + cli, err := r.atlasClient(wd.Path(), data.Cloud) + if err != nil { + res.SetNotReady("CreatingAtlasClient", err.Error()) + r.recordErrEvent(res, err) + return result(err) + } + var whoami *atlasexec.WhoAmI + switch whoami, err = cli.WhoAmI(ctx); { + case errors.Is(err, atlasexec.ErrRequireLogin): + log.Info("the resource is not connected to Atlas Cloud") + case err != nil: + res.SetNotReady("WhoAmI", err.Error()) + r.recordErrEvent(res, err) + return result(err) + default: + log.Info("the resource is connected to Atlas Cloud", "org", whoami.Org) } switch { case res.Status.LastApplied == 0: @@ -208,7 +224,12 @@ func (r *AtlasSchemaReconciler) Reconcile(ctx context.Context, req ctrl.Request) return result(err) } } - report, err := r.apply(ctx, wd, data) + report, err := cli.SchemaApply(ctx, &atlasexec.SchemaApplyParams{ + Env: data.EnvName, + To: data.Desired.String(), + TxMode: string(data.TxMode), + AutoApprove: true, + }) if err != nil { res.SetNotReady("ApplyingSchema", err.Error()) r.recorder.Event(res, corev1.EventTypeWarning, "ApplyingSchema", err.Error()) @@ -272,19 +293,6 @@ func (r *AtlasSchemaReconciler) watchRefs(res *dbv1alpha1.AtlasSchema) { } } -func (r *AtlasSchemaReconciler) apply(ctx context.Context, wd *atlasexec.WorkingDir, data *managedData) (*atlasexec.SchemaApply, error) { - cli, err := r.atlasClient(wd.Path(), data.Cloud) - if err != nil { - return nil, err - } - return cli.SchemaApply(ctx, &atlasexec.SchemaApplyParams{ - Env: data.EnvName, - To: data.Desired.String(), - TxMode: string(data.TxMode), - AutoApprove: true, - }) -} - // extractData extracts the info about the managed database and its desired state. func (r *AtlasSchemaReconciler) extractData(ctx context.Context, res *dbv1alpha1.AtlasSchema) (_ *managedData, err error) { var ( diff --git a/internal/controller/lint.go b/internal/controller/lint.go index 88320da..10e3e85 100644 --- a/internal/controller/lint.go +++ b/internal/controller/lint.go @@ -81,13 +81,11 @@ func (r *AtlasSchemaReconciler) lint(ctx context.Context, wd *atlasexec.WorkingD if err != nil { return err } - if diags := destructive(lint); len(diags) > 0 { - return &destructiveErr{diags: diags} - } - return nil + return destructive(lint) } -func destructive(rep *atlasexec.SummaryReport) (checks []sqlcheck.Diagnostic) { +func destructive(rep *atlasexec.SummaryReport) error { + var checks []sqlcheck.Diagnostic for _, f := range rep.Files { for _, r := range f.Reports { if f.Error == "" { @@ -100,7 +98,10 @@ func destructive(rep *atlasexec.SummaryReport) (checks []sqlcheck.Diagnostic) { } } } - return + if len(checks) > 0 { + return &destructiveErr{diags: checks} + } + return nil } type destructiveErr struct {