Skip to content

Commit c91851a

Browse files
authored
add: Allow config and pre-processed configuration to leverage environment variables ($K3D_CONFIG env var) (#1446)
1 parent 63c7a2a commit c91851a

File tree

2 files changed

+54
-44
lines changed

2 files changed

+54
-44
lines changed

cmd/cluster/clusterCreate.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ import (
4747
"github.com/k3d-io/k3d/v5/version"
4848
)
4949

50-
var configFile string
51-
5250
const clusterCreateDescription = `
5351
Create a new k3s cluster with containerized nodes (k3s in docker).
5452
Every cluster will consist of one or more containers:
@@ -72,13 +70,15 @@ var (
7270
func initConfig() error {
7371
// Viper for pre-processed config options
7472
ppViper.SetEnvPrefix("K3D")
73+
ppViper.AutomaticEnv()
74+
ppViper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
7575

7676
if l.Log().GetLevel() >= logrus.DebugLevel {
7777
c, _ := yaml.Marshal(ppViper.AllSettings())
7878
l.Log().Debugf("Additional CLI Configuration:\n%s", c)
7979
}
8080

81-
return cliconfig.InitViperWithConfigFile(cfgViper, configFile)
81+
return cliconfig.InitViperWithConfigFile(cfgViper, ppViper.GetString("config"))
8282
}
8383

8484
// NewCmdClusterCreate returns a new cobra command
@@ -123,7 +123,7 @@ func NewCmdClusterCreate() *cobra.Command {
123123
l.Log().Fatalf("error processing/sanitizing simple config: %v", err)
124124
}
125125

126-
clusterConfig, err := config.TransformSimpleToClusterConfig(cmd.Context(), runtimes.SelectedRuntime, simpleCfg, configFile)
126+
clusterConfig, err := config.TransformSimpleToClusterConfig(cmd.Context(), runtimes.SelectedRuntime, simpleCfg, ppViper.GetString("config"))
127127
if err != nil {
128128
l.Log().Fatalln(err)
129129
}
@@ -208,7 +208,8 @@ func NewCmdClusterCreate() *cobra.Command {
208208
* Config File *
209209
***************/
210210

211-
cmd.Flags().StringVarP(&configFile, "config", "c", "", "Path of a config file to use")
211+
cmd.Flags().StringP("config", "c", "", "Path of a config file to use")
212+
_ = ppViper.BindPFlag("config", cmd.Flags().Lookup("config"))
212213
if err := cmd.MarkFlagFilename("config", "yaml", "yml"); err != nil {
213214
l.Log().Fatalln("Failed to mark flag 'config' as filename flag")
214215
}

cmd/cluster/clusterDelete.go

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ THE SOFTWARE.
2222
package cluster
2323

2424
import (
25+
"context"
2526
"errors"
2627
"fmt"
2728
"os"
2829
"path"
30+
"strings"
2931

3032
"github.com/k3d-io/k3d/v5/cmd/util"
3133
cliconfig "github.com/k3d-io/k3d/v5/cmd/util/config"
@@ -35,13 +37,31 @@ import (
3537
"github.com/k3d-io/k3d/v5/pkg/runtimes"
3638
k3d "github.com/k3d-io/k3d/v5/pkg/types"
3739
k3dutil "github.com/k3d-io/k3d/v5/pkg/util"
40+
"github.com/sirupsen/logrus"
41+
"sigs.k8s.io/yaml"
3842

3943
"github.com/spf13/cobra"
4044
"github.com/spf13/viper"
4145
)
4246

43-
var clusterDeleteConfigFile string
44-
var clusterDeleteCfgViper = viper.New()
47+
var (
48+
clusterDeleteCfgViper = viper.New()
49+
clusterDeletePpViper = viper.New()
50+
)
51+
52+
func initClusterDeleteConfig() error {
53+
// Viper for pre-processed config options
54+
clusterDeletePpViper.SetEnvPrefix("K3D")
55+
clusterDeletePpViper.AutomaticEnv()
56+
clusterDeletePpViper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
57+
58+
if l.Log().GetLevel() >= logrus.DebugLevel {
59+
c, _ := yaml.Marshal(clusterDeletePpViper.AllSettings())
60+
l.Log().Debugf("Additional CLI Configuration:\n%s", c)
61+
}
62+
63+
return cliconfig.InitViperWithConfigFile(clusterDeleteCfgViper, clusterDeletePpViper.GetString("config"))
64+
}
4565

4666
// NewCmdClusterDelete returns a new cobra command
4767
func NewCmdClusterDelete() *cobra.Command {
@@ -54,7 +74,7 @@ func NewCmdClusterDelete() *cobra.Command {
5474
Args: cobra.MinimumNArgs(0), // 0 or n arguments; 0 = default cluster name
5575
ValidArgsFunction: util.ValidArgsAvailableClusters,
5676
PreRunE: func(cmd *cobra.Command, args []string) error {
57-
return cliconfig.InitViperWithConfigFile(clusterDeleteCfgViper, clusterDeleteConfigFile)
77+
return initClusterDeleteConfig()
5878
},
5979
Run: func(cmd *cobra.Command, args []string) {
6080
clusters := parseDeleteClusterCmd(cmd, args)
@@ -99,7 +119,8 @@ func NewCmdClusterDelete() *cobra.Command {
99119
* Config File *
100120
***************/
101121

102-
cmd.Flags().StringVarP(&clusterDeleteConfigFile, "config", "c", "", "Path of a config file to use")
122+
cmd.Flags().StringP("config", "c", "", "Path of a config file to use")
123+
_ = clusterDeletePpViper.BindPFlag("config", cmd.Flags().Lookup("config"))
103124
if err := cmd.MarkFlagFilename("config", "yaml", "yml"); err != nil {
104125
l.Log().Fatalln("Failed to mark flag 'config' as filename flag")
105126
}
@@ -110,66 +131,54 @@ func NewCmdClusterDelete() *cobra.Command {
110131

111132
// parseDeleteClusterCmd parses the command input into variables required to delete clusters
112133
func parseDeleteClusterCmd(cmd *cobra.Command, args []string) []*k3d.Cluster {
113-
var clusters []*k3d.Cluster
114-
115134
// --all
116135
all, err := cmd.Flags().GetBool("all")
117136
if err != nil {
118137
l.Log().Fatalln(err)
119138
}
120139

121-
// --config
122-
if clusterDeleteConfigFile != "" {
123-
// not allowed with --all or more args
124-
if len(args) > 0 || all {
125-
l.Log().Fatalln("failed to delete cluster: cannot use `--config` flag with additional arguments or `--all`")
126-
}
127-
128-
cfg, err := config.SimpleConfigFromViper(clusterDeleteCfgViper)
129-
if err != nil {
130-
l.Log().Fatalln(err)
131-
}
132-
133-
if cfg.Name == "" {
134-
l.Log().Fatalln("failed to delete cluster via config file: no name in config file")
135-
}
136-
137-
c, err := client.ClusterGet(cmd.Context(), runtimes.SelectedRuntime, &k3d.Cluster{Name: cfg.Name})
138-
if errors.Is(err, client.ClusterGetNoNodesFoundError) {
139-
l.Log().Infof("No nodes found for cluster '%s', nothing to delete.", cfg.Name)
140-
return nil
141-
}
142-
143-
clusters = append(clusters, c)
144-
return clusters
145-
}
146-
147140
// --all was set
148141
if all {
149142
l.Log().Infoln("Deleting all clusters...")
150-
clusters, err = client.ClusterList(cmd.Context(), runtimes.SelectedRuntime)
143+
clusters, err := client.ClusterList(cmd.Context(), runtimes.SelectedRuntime)
151144
if err != nil {
152145
l.Log().Fatalln(err)
153146
}
154147
return clusters
155148
}
156149

157-
// args only
158-
clusternames := []string{k3d.DefaultClusterName}
150+
// args
159151
if len(args) != 0 {
160-
clusternames = args
152+
return getClusters(cmd.Context(), args...)
153+
}
154+
155+
// --config
156+
if clusterDeletePpViper.GetString("config") != "" {
157+
cfg, err := config.SimpleConfigFromViper(clusterDeleteCfgViper)
158+
if err != nil {
159+
l.Log().Fatalln(err)
160+
}
161+
if cfg.Name != "" {
162+
return getClusters(cmd.Context(), cfg.Name)
163+
}
161164
}
162165

166+
// default
167+
return getClusters(cmd.Context(), k3d.DefaultClusterName)
168+
}
169+
170+
func getClusters(ctx context.Context, clusternames ...string) []*k3d.Cluster {
171+
var clusters []*k3d.Cluster
163172
for _, name := range clusternames {
164-
c, err := client.ClusterGet(cmd.Context(), runtimes.SelectedRuntime, &k3d.Cluster{Name: name})
173+
c, err := client.ClusterGet(ctx, runtimes.SelectedRuntime, &k3d.Cluster{Name: name})
165174
if err != nil {
166-
if err == client.ClusterGetNoNodesFoundError {
175+
if errors.Is(err, client.ClusterGetNoNodesFoundError) {
176+
l.Log().Infof("No nodes found for cluster '%s', nothing to delete.", name)
167177
continue
168178
}
169179
l.Log().Fatalln(err)
170180
}
171181
clusters = append(clusters, c)
172182
}
173-
174183
return clusters
175184
}

0 commit comments

Comments
 (0)