Skip to content

Commit

Permalink
atlas/schema: check the whoami result (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm authored Oct 3, 2024
1 parent 2566baa commit 5fb9be7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 32 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
54 changes: 31 additions & 23 deletions internal/controller/atlasschema_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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 (
Expand Down
13 changes: 7 additions & 6 deletions internal/controller/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand All @@ -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 {
Expand Down

0 comments on commit 5fb9be7

Please sign in to comment.