Skip to content

Commit

Permalink
Improve multiuser support for kind cfg file
Browse files Browse the repository at this point in the history
  • Loading branch information
fjammes committed Aug 7, 2024
1 parent c595933 commit ac21993
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
8 changes: 6 additions & 2 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,19 @@ func createCluster() {

slog.Debug("ktbx configuration", "data", c)

internal.GenerateKindConfigFile(c)
kindConfigFile, err := internal.GenerateKindConfigFile(c)
if err != nil {
slog.Error("unable to generate kind configuration file", "error", err)
os.Exit(1)
}

optName := ""
if clusterName != "" {
optName = " --name " + clusterName
}

cmd_tpl := "%v create cluster --config %v%v"
cmd := fmt.Sprintf(cmd_tpl, internal.Kind, internal.KindConfigFile, optName)
cmd := fmt.Sprintf(cmd_tpl, internal.Kind, kindConfigFile, optName)

_, _, err = ExecCmd(cmd, false)
if err != nil {
Expand Down
22 changes: 17 additions & 5 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
)

const Kind string = "kind"
const KindConfigFile string = "/tmp/kind-config.yaml"
const KindConfigFilename string = "kind-config.yaml"

type KtbxConfig struct {
AuditPolicy string `mapstructure:"auditPolicy" default:""`
Expand Down Expand Up @@ -87,16 +87,28 @@ func GetConfig() KtbxConfig {
return *c
}

func GenerateKindConfigFile(c KtbxConfig) {
slog.Info("Generate kind configuration file", "file", KindConfigFile)
func GenerateKindConfigFile(c KtbxConfig) (string, error) {
slog.Info("Generate kind configuration file", "file", KindConfigFilename)

f, err := os.Create(KindConfigFile)
cobra.CheckErr(err)
tmpdir, err := os.MkdirTemp("/tmp", "kind-config")
if err != nil {
slog.Error("unable to create temporary directory", "error", err)
return "", err
}

kindConfigFile := path.Join(tmpdir, KindConfigFilename)

f, err := os.Create(kindConfigFile)
if err != nil {
slog.Error("unable to create kind configuration file", "error", err)
return "", err
}
defer f.Close()

kindconfig := applyTemplate(c)
slog.Debug("kind configuration", "data", kindconfig)
f.WriteString(kindconfig)
return kindConfigFile, nil
}

func applyTemplate(sc KtbxConfig) string {
Expand Down
5 changes: 3 additions & 2 deletions internal/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ func TestGenerateKindConfigFile(t *testing.T) {
}

// Call the GenerateKindConfigFile function
GenerateKindConfigFile(config)
kindConfigFile, err := GenerateKindConfigFile(config)
require.NoError(err)

// Read the contents of the generated file
fileContents, err := os.ReadFile(KindConfigFile)
fileContents, err := os.ReadFile(kindConfigFile)
require.NoError(err)

// Assert the expected file contents
Expand Down

0 comments on commit ac21993

Please sign in to comment.