From 2772bc0e74f0115ea279c605ce9db74f114fcf15 Mon Sep 17 00:00:00 2001 From: Karen Almog Date: Mon, 27 Dec 2021 23:29:25 +0000 Subject: [PATCH] config refactoring: defer close config file Signed-off-by: Karen Almog --- cmd/backup/backup.go | 2 +- cmd/install/install.go | 2 +- cmd/reset/reset.go | 2 +- cmd/restore/restore.go | 2 +- pkg/config/config.go | 78 +++++++++++++++++++++--------------------- 5 files changed, 43 insertions(+), 43 deletions(-) diff --git a/cmd/backup/backup.go b/cmd/backup/backup.go index dedeed2170dc..b68b38d8a9fb 100644 --- a/cmd/backup/backup.go +++ b/cmd/backup/backup.go @@ -90,7 +90,7 @@ func (c *CmdOpts) backup() error { func preRunValidateConfig(cmd *cobra.Command, args []string) error { c := CmdOpts(config.GetCmdOpts()) - _, err := config.ValidateYaml(c.CfgFile, c.K0sVars) + _, err := config.GetConfigFromYAML(c.CfgFile, c.K0sVars) if err != nil { return err } diff --git a/cmd/install/install.go b/cmd/install/install.go index 584771a3861e..7ad8821a5586 100644 --- a/cmd/install/install.go +++ b/cmd/install/install.go @@ -97,7 +97,7 @@ func (c *CmdOpts) convertFileParamsToAbsolute() (err error) { func preRunValidateConfig(_ *cobra.Command, _ []string) error { c := CmdOpts(config.GetCmdOpts()) - _, err := config.ValidateYaml(c.CfgFile, c.K0sVars) + _, err := config.GetConfigFromYAML(c.CfgFile, c.K0sVars) if err != nil { return err } diff --git a/cmd/reset/reset.go b/cmd/reset/reset.go index a51d6b51ceeb..4096d4658ea3 100644 --- a/cmd/reset/reset.go +++ b/cmd/reset/reset.go @@ -75,7 +75,7 @@ func (c *CmdOpts) reset() error { func preRunValidateConfig(_ *cobra.Command, _ []string) error { c := CmdOpts(config.GetCmdOpts()) - _, err := config.ValidateYaml(c.CfgFile, c.K0sVars) + _, err := config.GetConfigFromYAML(c.CfgFile, c.K0sVars) if err != nil { return err } diff --git a/cmd/restore/restore.go b/cmd/restore/restore.go index 5d7cd6e90871..7f8be26da584 100644 --- a/cmd/restore/restore.go +++ b/cmd/restore/restore.go @@ -96,7 +96,7 @@ func (c *CmdOpts) restore(path string) error { // TODO Need to move to some common place, now it's defined in restore and backup commands func preRunValidateConfig(_ *cobra.Command, _ []string) error { c := CmdOpts(config.GetCmdOpts()) - _, err := config.ValidateYaml(c.CfgFile, c.K0sVars) + _, err := config.GetConfigFromYAML(c.CfgFile, c.K0sVars) if err != nil { return err } diff --git a/pkg/config/config.go b/pkg/config/config.go index 2147146cbbc0..6e1779aef42e 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -17,9 +17,7 @@ package config import ( "context" - "errors" "fmt" - "io" "os" "strings" "time" @@ -29,7 +27,6 @@ import ( v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/tools/clientcmd" - "github.com/k0sproject/k0s/internal/pkg/file" cfgClient "github.com/k0sproject/k0s/pkg/apis/k0s.k0sproject.io/clientset" "github.com/k0sproject/k0s/pkg/apis/k0s.k0sproject.io/v1beta1" "github.com/k0sproject/k0s/pkg/constant" @@ -39,13 +36,8 @@ import ( var ( resourceType = v1.TypeMeta{APIVersion: "k0s.k0sproject.io/v1beta1", Kind: "clusterconfigs"} getOpts = v1.GetOptions{TypeMeta: resourceType} - errNoConfig = errors.New("no configuration found") ) -func IsErrNoConfig(err error) bool { - return err == errNoConfig -} - func getConfigFromAPI(kubeConfig string) (*v1beta1.ClusterConfig, error) { timeout := time.After(120 * time.Second) ticker := time.NewTicker(3 * time.Second) @@ -74,7 +66,7 @@ func GetFullConfig(cfgPath string, k0sVars constant.CfgVars) (clusterConfig *v1b // no config file exists, using defaults logrus.Warn("no config file given, using defaults") } - cfg, err := ValidateYaml(cfgPath, k0sVars) + cfg, err := GetConfigFromYAML(cfgPath, k0sVars) if err != nil { return nil, err } @@ -117,40 +109,66 @@ func GetYamlFromFile(cfgPath string, k0sVars constant.CfgVars) (clusterConfig *v // no config file exists, using defaults logrus.Warn("no config file given, using defaults") } - cfg, err := ValidateYaml(cfgPath, k0sVars) + cfg, err := GetConfigFromYAML(cfgPath, k0sVars) if err != nil { return nil, err } return cfg, nil } -func ValidateYaml(cfgPath string, k0sVars constant.CfgVars) (clusterConfig *v1beta1.ClusterConfig, err error) { +// GetConfigFromYAML will attempt to read a config yaml, validate it and return a clusterConfig object +func GetConfigFromYAML(cfgPath string, k0sVars constant.CfgVars) (clusterConfig *v1beta1.ClusterConfig, err error) { var storage *v1beta1.StorageSpec var cfg *v1beta1.ClusterConfig CfgFile = cfgPath + // first, let's set the default storage type if k0sVars.DefaultStorageType == "kine" { storage = &v1beta1.StorageSpec{ Type: v1beta1.KineStorageType, Kine: v1beta1.DefaultKineConfig(k0sVars.DataDir), } } - if file.Exists(constant.K0sConfigPathDefault) { - logrus.Debugf("found config file in %s", constant.K0sConfigPathDefault) - } - cfgReader, err := getConfigReader() - if err != nil { - if IsErrNoConfig(err) { - cfg = v1beta1.DefaultClusterConfig(storage) - } else { + + switch CfgFile { + // read config file flag + default: + f, err := os.Open(CfgFile) + if err != nil { return nil, err } - } else { - cfg, err = v1beta1.ConfigFromReader(cfgReader, storage) + defer f.Close() + + cfg, err = v1beta1.ConfigFromReader(f, storage) if err != nil { return nil, err } + + // stdin input + case "-": + cfg, err = v1beta1.ConfigFromReader(os.Stdin, storage) + + // config file not provided: try to read config from default location. + // if not exists, generate default config + case constant.K0sConfigPathDefault: + f, err := os.Open(constant.K0sConfigPathDefault) + if err != nil { + if os.IsNotExist(err) { + logrus.Debugf("could not find config in %s, using defaults", constant.K0sConfigPathDefault) + cfg = v1beta1.DefaultClusterConfig(storage) + } else { + return nil, err + } + } + if err == nil { + logrus.Debugf("found config file in %s", constant.K0sConfigPathDefault) + cfg, err = v1beta1.ConfigFromReader(f, storage) + if err != nil { + return nil, err + } + defer f.Close() + } } if cfg.Spec.Storage.Type == v1beta1.KineStorageType && cfg.Spec.Storage.Kine == nil { @@ -188,21 +206,3 @@ func GetNodeConfig(cfgPath string, k0sVars constant.CfgVars) (*v1beta1.ClusterCo } return nodeConfig, nil } - -func getConfigReader() (io.Reader, error) { - switch CfgFile { - case "-": - return os.Stdin, nil - case "", constant.K0sConfigPathDefault: - f, err := os.Open(constant.K0sConfigPathDefault) - if err == nil { - return f, nil - } - if os.IsNotExist(err) { - return nil, errNoConfig - } - return nil, err - default: - return os.Open(CfgFile) - } -}