Skip to content

Commit cac0930

Browse files
committed
updated profiles to be able to run 'stacks' by reading more than one config file
Signed-off-by: Christian Hernandez <christian@chernand.io>
1 parent 57cd49f commit cac0930

File tree

3 files changed

+62
-16
lines changed

3 files changed

+62
-16
lines changed

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var KubeConfig string
2929
// rootCmd represents the base command when called without any subcommands
3030
var rootCmd = &cobra.Command{
3131
Use: "bekind",
32-
Version: "v0.4.1",
32+
Version: "v0.4.2",
3333
Short: "Installs an opinionated KIND cluster",
3434
Long: `This command installs a KIND cluster.
3535
The KIND cluster is then configured based on what configuration file is passed.`,

cmd/run.go

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ limitations under the License.
1616
package cmd
1717

1818
import (
19+
"fmt"
1920
"os"
21+
"path/filepath"
2022

2123
log "github.com/sirupsen/logrus"
2224
"github.com/spf13/cobra"
@@ -35,24 +37,56 @@ var runCmd = &cobra.Command{
3537
Long: profileLongHelp(),
3638
ValidArgsFunction: profileValidArgs,
3739
Run: func(cmd *cobra.Command, args []string) {
38-
// Set Config file based on the profile
39-
viper.SetConfigFile(ProfileDir + "/" + args[0] + "/config.yaml")
40+
// Look for all yaml files in the profile directory
41+
configFiles, err := filepath.Glob(filepath.Join(ProfileDir+"/"+args[0], "*.yaml"))
42+
if err != nil {
43+
log.Fatal(err)
44+
}
4045

41-
// Read the config file, Only displaying an error if there was a problem reading the file.
42-
if err := viper.ReadInConfig(); err != nil {
43-
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
44-
log.Fatal(err)
45-
}
46+
// If no config files are found, exit with an error
47+
if len(configFiles) == 0 {
48+
log.Fatalf("No config files found in profile directory: %s", ProfileDir+"/"+args[0])
4649
}
4750

48-
// If the view flag is set, show the config
49-
if view, _ := cmd.Flags().GetBool("view"); view {
50-
showconfigCmd.Run(cmd, []string{})
51-
os.Exit(0)
51+
// Get view flag
52+
view, err := cmd.Flags().GetBool("view")
53+
if err != nil {
54+
log.Fatal(err)
5255
}
5356

54-
// Run the profile
55-
startCmd.Run(cmd, []string{})
57+
// Iterate over all config files and run the profile for each one
58+
for _, configFile := range configFiles {
59+
// Set Config file based on the profile
60+
viper.SetConfigFile(configFile)
61+
62+
// Read the config file, Only displaying an error if there was a problem reading the file.
63+
if err := viper.ReadInConfig(); err != nil {
64+
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
65+
log.Fatal(err)
66+
}
67+
}
68+
69+
// If the view flag is set, show the config
70+
if view {
71+
// Crude, but it works
72+
// TODO: Create a "BeKindStack" YAML struct and use that to display the config
73+
fmt.Println("---")
74+
showconfigCmd.Run(cmd, []string{})
75+
} else {
76+
// I assume you want to "Run the profile"
77+
if !view {
78+
log.Info("Running profile: ", args[0], " with config file: ", filepath.Base(configFile))
79+
startCmd.Run(cmd, []string{})
80+
}
81+
82+
}
83+
84+
// Clear viper config for next iteration
85+
viper.Reset()
86+
87+
// Reset global variables from start.go to prevent state leakage between iterations
88+
ResetGlobalVars()
89+
}
5690
},
5791
}
5892

@@ -93,7 +127,9 @@ func profileLongHelp() string {
93127
return `You can use "run" to run the specified profile. Profiles needs to be
94128
stored in the ~/.bekind/profiles/{{name}} directory.
95129
96-
The profile directory should contain a config.yaml file. For example:
130+
The profile directory should contain a config file in YAML format. For example:
131+
132+
~/.bekind/profiles/{{name}}/config.yaml
97133
98-
~/.bekind/profiles/{{name}}/config.yaml`
134+
NOTE: You can have multiple YAML configurations in the same profile directory.`
99135
}

cmd/start.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ var Domain string = "127.0.0.1.nip.io"
8181
// Set the default Kind Image version
8282
var KindImageVersion string
8383

84+
// ResetGlobalVars resets all global variables to their default state
85+
// This is needed when running multiple profiles in sequence
86+
func ResetGlobalVars() {
87+
log.Debug("Resetting global variables for next profile iteration")
88+
HC = nil // Clear the helm charts slice
89+
pullImages = true
90+
Domain = "127.0.0.1.nip.io"
91+
KindImageVersion = ""
92+
}
93+
8494
// startCmd represents the start command
8595
var startCmd = &cobra.Command{
8696
Use: "start",

0 commit comments

Comments
 (0)