Skip to content

Commit

Permalink
config refactoring: address review comments
Browse files Browse the repository at this point in the history
This commit:
  - Removes the default value in cobra config flag
  - Adds a logic to set the default file under the config package

Signed-off-by: Karen Almog <[email protected]>
  • Loading branch information
Karen Almog committed Jan 20, 2022
1 parent d0d440c commit 9929989
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cmd/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewRestoreCmd() *cobra.Command {

cwd, err := os.Getwd()
if err != nil {
return nil
logrus.Fatal("failed to get local path")
}

restoredConfigPathDescription := fmt.Sprintf("Specify desired name and full path for the restored k0s.yaml file (default: %s/k0s_<archive timestamp>.yaml", cwd)
Expand Down
2 changes: 0 additions & 2 deletions internal/testutil/runtime_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ func (c *ConfigGetter) FakeConfigFromFile() (*v1beta1.ClusterConfig, error) {
loadingRules := config.ClientConfigLoadingRules{
RuntimeConfigPath: RuntimeFakePath,
Nodeconfig: c.NodeConfig,
CfgFileOverride: c.cfgFilePath,
K0sVars: c.k0sVars,
}
return loadingRules.Load()
Expand All @@ -87,7 +86,6 @@ func (c *ConfigGetter) FakeAPIConfig() (*v1beta1.ClusterConfig, error) {
loadingRules := config.ClientConfigLoadingRules{
RuntimeConfigPath: RuntimeFakePath,
Nodeconfig: c.NodeConfig,
CfgFileOverride: c.cfgFilePath,
APIClient: client.K0sV1beta1(),
K0sVars: c.k0sVars,
}
Expand Down
1 change: 0 additions & 1 deletion inttest/common/footloosesuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,6 @@ func (s *FootlooseSuite) GetKubeClientConfig(node string, k0sKubeconfigArgs ...s
kubeConfigCmd := fmt.Sprintf("%s kubeconfig admin %s 2>/dev/null", s.K0sFullPath, strings.Join(k0sKubeconfigArgs, " "))
kubeConf, err := ssh.ExecWithOutput(kubeConfigCmd)
if err != nil {
fmt.Println(string(kubeConf))
return nil, err
}
cfg, err := clientcmd.Load([]byte(kubeConf))
Expand Down
8 changes: 5 additions & 3 deletions pkg/config/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package config

import (
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -189,7 +190,8 @@ func GetControllerFlags() *pflag.FlagSet {
// it in multiple places
func FileInputFlag() *pflag.FlagSet {
flagset := &pflag.FlagSet{}
flagset.StringVarP(&CfgFile, "config", "c", constant.K0sConfigPathDefault, "config file, use '-' to read the config from stdin")
descString := fmt.Sprintf("config file, use '-' to read the config from stdin (default \"%s\")", constant.K0sConfigPathDefault)
flagset.StringVarP(&CfgFile, "config", "c", "", descString)

return flagset
}
Expand All @@ -202,8 +204,8 @@ func GetCmdOpts() CLIOptions {
K0sVars.DefaultStorageType = "kine"
}

// when a custom config file is used, verify that it can be opened
if CfgFile != constant.K0sConfigPathDefault {
// When CfgFile is set, verify the file can be opened
if CfgFile != "" {
_, err := os.Open(CfgFile)
if err != nil {
logrus.Fatalf("failed to load config file (%s): %v", CfgFile, err)
Expand Down
7 changes: 0 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ type ClientConfigLoadingRules struct {
// this parameter is mainly used for testing purposes, to override the default location on local dev system
RuntimeConfigPath string

// CfgFileOverride is an optional field for overriding the CfgFile parameter from cobra. Used mainly for testing purposes.
CfgFileOverride string

// K0sVars is needed for fetching the right config from the API
K0sVars constant.CfgVars
}
Expand Down Expand Up @@ -111,10 +108,6 @@ func (rules *ClientConfigLoadingRules) IsDefaultConfig() bool {
}

func (rules *ClientConfigLoadingRules) Load() (*v1beta1.ClusterConfig, error) {
if rules.CfgFileOverride != "" {
CfgFile = rules.CfgFileOverride
}

if rules.Nodeconfig {
return rules.fetchNodeConfig()
}
Expand Down
11 changes: 9 additions & 2 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,15 @@ spec:

// Test using config from a yaml file
func TestConfigFromDefaults(t *testing.T) {
CfgFile = constant.K0sConfigPathDefault // this path doesn't exist, so default values should be generated
defer os.Remove(configPathRuntimeTest)

CfgFile = ""
loadingRules := ClientConfigLoadingRules{RuntimeConfigPath: configPathRuntimeTest}
err := loadingRules.InitRuntimeConfig(constant.GetConfig(""))
if err != nil {
t.Fatalf("failed to initialize k0s config: %s", err.Error())
}

cfg, err := loadingRules.Load()
if err != nil {
t.Fatalf("failed to load config: %s", err.Error())
Expand Down Expand Up @@ -215,12 +220,12 @@ func TestNodeConfigWithAPIConfig(t *testing.T) {
}

func TestSingleNodeConfig(t *testing.T) {
CfgFile = constant.K0sConfigPathDefault // this path doesn't exist, so default values should be generated
defer os.Remove(configPathRuntimeTest)

loadingRules := ClientConfigLoadingRules{RuntimeConfigPath: configPathRuntimeTest, Nodeconfig: true}
k0sVars := constant.GetConfig("")
k0sVars.DefaultStorageType = "kine"
CfgFile = ""

err := loadingRules.InitRuntimeConfig(k0sVars)
if err != nil {
Expand All @@ -231,9 +236,11 @@ func TestSingleNodeConfig(t *testing.T) {
if err != nil {
t.Fatalf("failed to load config: %s", err.Error())
}

if cfg == nil {
t.Fatal("received an empty config! failing")
}

testCases := []struct {
name string
got string
Expand Down
43 changes: 33 additions & 10 deletions pkg/config/file_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,34 +61,57 @@ func (rules *ClientConfigLoadingRules) readRuntimeConfig() (clusterConfig *v1bet
func (rules *ClientConfigLoadingRules) ParseRuntimeConfig() (*v1beta1.ClusterConfig, error) {
var cfg *v1beta1.ClusterConfig

var storage *v1beta1.StorageSpec
if rules.K0sVars.DefaultStorageType == "kine" {
storage = &v1beta1.StorageSpec{
Type: v1beta1.KineStorageType,
Kine: v1beta1.DefaultKineConfig(rules.K0sVars.DataDir),
}
}
if rules.RuntimeConfigPath == "" {
rules.RuntimeConfigPath = runtimeConfigPathDefault
}

// don't create the runtime config file, if it already exists
// If runtime config already exists, use it as the source of truth
if file.Exists(rules.RuntimeConfigPath) {
logrus.Debugf("runtime config found: using %s", rules.RuntimeConfigPath)
CfgFile = rules.RuntimeConfigPath
}

var storage *v1beta1.StorageSpec
if rules.K0sVars.DefaultStorageType == "kine" {
storage = &v1beta1.StorageSpec{
Type: v1beta1.KineStorageType,
Kine: v1beta1.DefaultKineConfig(rules.K0sVars.DataDir),
// read config from runtime config
f, err := os.Open(rules.RuntimeConfigPath)
if err != nil {
return nil, err
}
defer f.Close()

cfg, err = v1beta1.ConfigFromReader(f, storage)
if err != nil {
return nil, err
}
return cfg, nil
}

switch CfgFile {
// stdin input
case "-":
return v1beta1.ConfigFromReader(os.Stdin, storage)
default:
f, err := os.Open(CfgFile)
case "":
// if no config is set, look for config in the default location
// if it does not exist there either, generate default config
f, err := os.Open(constant.K0sConfigPathDefault)
if err != nil {
if os.IsNotExist(err) {
return rules.generateDefaults(storage), nil
}
}
defer f.Close()

cfg, err = v1beta1.ConfigFromReader(f, storage)
if err != nil {
return nil, err
}
default:
f, err := os.Open(CfgFile)
if err != nil {
return nil, err
}
defer f.Close()
Expand Down

0 comments on commit 9929989

Please sign in to comment.