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 40e809f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 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
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
17 changes: 15 additions & 2 deletions pkg/config/file_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,25 @@ func (rules *ClientConfigLoadingRules) ParseRuntimeConfig() (*v1beta1.ClusterCon
// 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 40e809f

Please sign in to comment.