Skip to content

Commit

Permalink
config refactoring: defer close config file
Browse files Browse the repository at this point in the history
Signed-off-by: Karen Almog <[email protected]>
  • Loading branch information
Karen Almog committed Dec 27, 2021
1 parent a0a65f6 commit 2772bc0
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion cmd/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/reset/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
78 changes: 39 additions & 39 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ package config

import (
"context"
"errors"
"fmt"
"io"
"os"
"strings"
"time"
Expand All @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 2772bc0

Please sign in to comment.