Skip to content

Commit c92e62c

Browse files
Mate Oryorymate
authored andcommitted
cp: call tf init during cp up only, with creds
1 parent 4b621a5 commit c92e62c

File tree

2 files changed

+91
-82
lines changed

2 files changed

+91
-82
lines changed

internal/cli/command/controlplane/init.go

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package controlplane
1616

1717
import (
1818
"fmt"
19-
"io/ioutil"
2019
"os"
2120
"os/exec"
2221
"path/filepath"
@@ -29,7 +28,6 @@ import (
2928
"github.com/banzaicloud/banzai-cli/internal/cli/input"
3029
"github.com/banzaicloud/banzai-cli/internal/cli/utils"
3130
"github.com/google/uuid"
32-
json "github.com/json-iterator/go"
3331
log "github.com/sirupsen/logrus"
3432
"github.com/spf13/cobra"
3533
"gopkg.in/yaml.v2"
@@ -407,12 +405,6 @@ func runInit(options initOptions, banzaiCli cli.Cli) error {
407405

408406
out["installer"] = installer
409407

410-
err = initStateBackend(options.cpContext, out)
411-
412-
if err != nil {
413-
return err
414-
}
415-
416408
return options.writeValues(out)
417409
}
418410

@@ -446,45 +438,6 @@ func initImageValues(options initOptions, out map[string]interface{}) (image str
446438
return image, tag
447439
}
448440

449-
func initStateBackend(options *cpContext, values map[string]interface{}) error {
450-
var stateData []byte
451-
452-
if stateValues, ok := values["state"]; ok {
453-
var err error
454-
stateData, err = json.MarshalIndent(stateValues, "", " ")
455-
if err != nil {
456-
return errors.WrapIf(err, "failed to marshal state backend configuration")
457-
}
458-
} else {
459-
stateData = []byte(fmt.Sprintf(localStateBackend, tfstateFilename))
460-
}
461-
462-
err := ioutil.WriteFile(options.workspace+"/state.tf.json", stateData, 0600)
463-
if err != nil {
464-
return errors.WrapIf(err, "failed to create state backend configuration")
465-
}
466-
467-
err = os.MkdirAll(options.workspace+"/.terraform", 0700)
468-
if err != nil {
469-
return errors.WrapIf(err, "failed to create state backend directory")
470-
}
471-
472-
stateFile, err := os.Create(options.workspace + "/.terraform/terraform.tfstate")
473-
if err != nil {
474-
return errors.WrapIf(err, "failed to create state config file")
475-
}
476-
_ = stateFile.Close()
477-
478-
if err := runTerraform("init", options, nil); err != nil {
479-
return errors.WrapIf(err, "failed to init state backend")
480-
}
481-
482-
// remove old state.tf if any
483-
_ = os.Remove(options.workspace + "/state.tf")
484-
485-
return nil
486-
}
487-
488441
func getAmazonCredentialsRegion(defaultAwsRegion string) (string, string, error) {
489442
id, region, _, err := input.GetAmazonCredentialsRegion("")
490443
if err != nil {

internal/cli/command/controlplane/up.go

Lines changed: 91 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ package controlplane
1616

1717
import (
1818
"bytes"
19+
"encoding/json"
1920
"fmt"
2021
"io/ioutil"
2122
"net"
23+
"os"
2224
"os/exec"
2325
"path/filepath"
2426
"strings"
@@ -130,10 +132,6 @@ func runUp(options *createOptions, banzaiCli cli.Cli) error {
130132
return errors.New("workspace is already initialized but a different --provider is specified")
131133
}
132134

133-
if err := initStateBackend(options.cpContext, values); err != nil {
134-
return err
135-
}
136-
137135
source := "/export"
138136
var defaultValues map[string]interface{}
139137
exportHandlers := []ExportedFilesHandler{
@@ -146,7 +144,34 @@ func runUp(options *createOptions, banzaiCli cli.Cli) error {
146144
return err
147145
}
148146

149-
var env map[string]string
147+
env := make(map[string]string)
148+
149+
switch values["provider"] {
150+
case providerCustom:
151+
if pc, ok := values["providerConfig"]; ok {
152+
pc := cast.ToStringMap(pc)
153+
if _, ok := pc["accessKey"]; ok { // if using aws key
154+
_, creds, err := input.GetAmazonCredentials()
155+
if err != nil {
156+
return errors.WrapIf(err, "failed to get AWS credentials")
157+
}
158+
env = creds
159+
}
160+
}
161+
case providerEc2:
162+
_, creds, err := input.GetAmazonCredentials()
163+
if err != nil {
164+
return errors.WrapIf(err, "failed to get AWS credentials")
165+
}
166+
env = creds
167+
}
168+
169+
if options.terraformInit {
170+
if err := initStateBackend(options.cpContext, values, env); err != nil {
171+
return errors.WrapIf(err, "failed to initialize state backend")
172+
}
173+
}
174+
150175
switch values["provider"] {
151176
case providerPke:
152177
err := ensurePKECluster(banzaiCli, options.cpContext)
@@ -161,37 +186,19 @@ func runUp(options *createOptions, banzaiCli cli.Cli) error {
161186
}
162187

163188
case providerEc2:
164-
_, creds, err := input.GetAmazonCredentials()
165-
if err != nil {
166-
return errors.WrapIf(err, "failed to get AWS credentials")
167-
}
168-
169189
useGeneratedKey := true
170190
if pc, ok := values["providerConfig"]; ok {
171191
if pc, ok := pc.(map[string]interface{}); ok {
172192
useGeneratedKey = pc["key_name"] != nil && pc["key_name"] != ""
173193
}
174194
}
175195

176-
if err := ensureEC2Cluster(options.cpContext, creds, useGeneratedKey); err != nil {
196+
if err := ensureEC2Cluster(options.cpContext, env, useGeneratedKey); err != nil {
177197
return errors.WrapIf(err, "failed to create EC2 cluster")
178198
}
179-
env = creds
180199

181200
case providerCustom:
182-
creds := map[string]string{}
183-
if pc, ok := values["providerConfig"]; ok {
184-
pc := cast.ToStringMap(pc)
185-
if _, ok := pc["accessKey"]; ok {
186-
_, awsCreds, err := input.GetAmazonCredentials()
187-
if err != nil {
188-
return errors.WrapIf(err, "failed to get AWS credentials")
189-
}
190-
creds = awsCreds
191-
}
192-
}
193-
194-
if err := ensureCustomCluster(options.cpContext, creds); err != nil {
201+
if err := ensureCustomCluster(options.cpContext, env); err != nil {
195202
return errors.WrapIf(err, "failed to create Custom infrastructure")
196203
}
197204

@@ -202,16 +209,6 @@ func runUp(options *createOptions, banzaiCli cli.Cli) error {
202209
}
203210

204211
log.Info("Deploying Banzai Cloud Pipeline to Kubernetes cluster...")
205-
if values["provider"] == providerCustom {
206-
_, creds, err := input.GetAmazonCredentials()
207-
if err != nil {
208-
return errors.WrapIf(err, "failed to get AWS credentials")
209-
}
210-
env = map[string]string{}
211-
for k, v := range creds {
212-
env[k] = v
213-
}
214-
}
215212

216213
if err := runTerraform("apply", options.cpContext, env); err != nil {
217214
return errors.WrapIf(err, "failed to deploy pipeline components")
@@ -332,6 +329,65 @@ func imageFileExists(options *cpContext, source string) (bool, error) {
332329
}
333330
}
334331

332+
func stringifyMap(m interface{}) interface{} {
333+
switch v := m.(type) {
334+
case map[string]interface{}:
335+
out := make(map[string]interface{})
336+
for k, v := range v {
337+
out[k] = stringifyMap(v)
338+
}
339+
return out
340+
case map[interface{}]interface{}:
341+
out := make(map[string]interface{})
342+
for k, v := range v {
343+
out[fmt.Sprint(k)] = stringifyMap(v)
344+
}
345+
return out
346+
default:
347+
return v
348+
}
349+
}
350+
351+
func initStateBackend(options *cpContext, values map[string]interface{}, env map[string]string) error {
352+
var stateData []byte
353+
354+
if stateValues, ok := values["state"]; ok {
355+
values := stringifyMap(stateValues)
356+
var err error
357+
stateData, err = json.MarshalIndent(values, "", " ")
358+
if err != nil {
359+
return errors.WrapIf(err, "failed to marshal state backend configuration")
360+
}
361+
} else {
362+
stateData = []byte(fmt.Sprintf(localStateBackend, tfstateFilename))
363+
}
364+
365+
err := ioutil.WriteFile(options.workspace+"/state.tf.json", stateData, 0600)
366+
if err != nil {
367+
return errors.WrapIf(err, "failed to create state backend configuration")
368+
}
369+
370+
err = os.MkdirAll(options.workspace+"/.terraform", 0700)
371+
if err != nil {
372+
return errors.WrapIf(err, "failed to create state backend directory")
373+
}
374+
375+
stateFile, err := os.Create(options.workspace + "/.terraform/terraform.tfstate")
376+
if err != nil {
377+
return errors.WrapIf(err, "failed to create state config file")
378+
}
379+
_ = stateFile.Close()
380+
381+
if err := runTerraform("init", options, env); err != nil {
382+
return errors.WrapIf(err, "failed to init state backend")
383+
}
384+
385+
// remove old state.tf if any
386+
_ = os.Remove(options.workspace + "/state.tf")
387+
388+
return nil
389+
}
390+
335391
func defaultValuesExporter(source string, defaultValues *map[string]interface{}) ExportedFilesHandler {
336392
return ExportedFilesHandler(func(files map[string][]byte) error {
337393
if valuesFileContent, ok := files[source]; ok {

0 commit comments

Comments
 (0)