-
Notifications
You must be signed in to change notification settings - Fork 249
refactor(cli): Use Viper to manage Keptn config #5694
refactor(cli): Use Viper to manage Keptn config #5694
Conversation
29cd494
to
15966bf
Compare
d891e1a
to
4c17d6d
Compare
Codecov Report
@@ Coverage Diff @@
## master #5694 +/- ##
==========================================
- Coverage 54.33% 53.23% -1.11%
==========================================
Files 399 326 -73
Lines 23436 22351 -1085
Branches 1204 1054 -150
==========================================
- Hits 12735 11899 -836
+ Misses 9788 9553 -235
+ Partials 913 899 -14
|
ac615c9
to
1fe2872
Compare
@@ -62,6 +62,8 @@ func init() { | |||
rootCmd.PersistentFlags().BoolVarP(&mocking, "mock", "", false, "Disables communication to a Keptn endpoint") | |||
rootCmd.PersistentFlags().StringVarP(&namespace, "namespace", "n", "keptn", | |||
"Specify the namespace where Keptn should be installed, used and uninstalled in") | |||
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config-file", "", "", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -32,7 +32,7 @@ Supported keys: | |||
cmd.SilenceUsage = false | |||
return errors.New("required arguments KEY and VALUE") | |||
} | |||
configMng = config.NewCLIConfigManager() | |||
configMng = config.NewCLIConfigManager("") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function signature NewCLIConfigManager
is changed to support custom config file path. I have done a global replace NewCLIConfigManager()
-> NewCLIConfigManager("")
@@ -20,6 +20,7 @@ require ( | |||
github.com/mitchellh/mapstructure v1.4.2 | |||
github.com/spf13/cast v1.4.1 // indirect | |||
github.com/spf13/cobra v1.2.1 | |||
github.com/spf13/viper v1.9.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Latest version of Viper at the time of this PR
https://pkg.go.dev/github.com/spf13/viper?tab=versions
@@ -34,6 +34,7 @@ cloud.google.com/go/bigquery v1.8.0/go.mod h1:J5hqkt3O0uAFnINi6JXValWIb1v0goeZM7 | |||
cloud.google.com/go/datastore v1.0.0/go.mod h1:LXYbyblFSglQ5pkeyhO+Qmw7ukd3C+pD7TKLgZqpHYE= | |||
cloud.google.com/go/datastore v1.1.0/go.mod h1:umbIZjpQpHh4hmRpGhH4tLFup+FVzqBi1b3c64qFpCk= | |||
cloud.google.com/go/firestore v1.1.0/go.mod h1:ulACoGHTpvq5r8rxGJ4ddJZBZqakUQqClKRT5SZwBmk= | |||
cloud.google.com/go/firestore v1.6.0/go.mod h1:afJwI0vaXwAG54kI7A//lP/lSPDkQORQuMkv56TxEPU= |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the changes in this file are because of two reasons:
- I added viper as a dependency (CI was failing) with
# In the keptn root directory
$ go get cli/pkg/config
- I updated the dependency from viper 1.8.1 -> 1.9.0 in the
go.mod
because 1.9.0 is the latest stable version at the time of this comment. 1.8.1 was added automatically on running thego get
command above. After updating thego.mod
file I ran the same command again
# In the keptn root directory
$ go get cli/pkg/config
- Did
git pull upstream master --rebase
and resolved merge conflicts. I accepted both the changes (from upstreammaster
and my branch)
} | ||
|
||
var mgr *CLIConfigManager |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maintain an internal CLIConfigManager
struct to avoid re-initializing new viper
instances.
if err := json.Unmarshal(data, &cliConfig); err != nil { | ||
|
||
if err := c.viper.Unmarshal(&cliConfig, func(dConfig *mapstructure.DecoderConfig) { | ||
dConfig.DecodeHook = mapstructure.StringToTimeHookFunc(time.RFC3339) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
StringToTimeHookFunc
decode hook was added because viper.Unmarshal
does not know how to unmarshal json time string into *time.Time
(it throws an error if we don't add the decode hook)
} | ||
|
||
// GetCLIConfig gets the already loaded configuration | ||
func (c *CLIConfigManager) GetCLIConfig() (CLIConfig, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Many places in the code use LoadCLIConfig
which reads in the config file every time LoadCLIConfig
function is called. GetCLIConfig
is lighter version of the function. It only returns the config already loaded in the Viper.
data, err := json.Marshal(config) | ||
if err != nil { | ||
return fmt.Errorf("error when marshalling config file: %w", err) | ||
} | ||
if err := ioutil.WriteFile(c.CLIConfigPath, []byte(data), 0644); err != nil { | ||
|
||
if err := c.viper.MergeConfig(bytes.NewReader(data)); err != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
config
passed as parameter overwrites config stored in the Viper
return nil | ||
} | ||
|
||
// GetKeptnDefaultConfigPath returns default Keptn Config file path | ||
func GetKeptnDefaultConfigPath() (string, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Helper function to get default keptn config file path
@@ -60,6 +64,12 @@ func TestStoreCLIConfig(t *testing.T) { | |||
} | |||
|
|||
data, err := fileutils.ReadFileAsStr(mng.CLIConfigPath) | |||
|
|||
// This is to remove the indentation viper adds | |||
data = strings.ReplaceAll(data, " ", "") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Viper stores the config like this
{
"automatic_version_check":true,
"current-context":"",
"kube_context_check":true,
"last_version_check":"2020-02-20T00:00:00Z"
}
But we want to flatten it out (remove indentations) so that we can compare it with the testConfig
d6e4051
to
8e9701a
Compare
KubeContextCheck bool `json:"kube_context_check"` | ||
LastVersionCheck *time.Time `json:"last_version_check"` | ||
CurrentContext string `json:"current-context"` | ||
AutomaticVersionCheck bool `json:"automatic_version_check" mapstructure:"automatic_version_check"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} | ||
|
||
// CLIConfigManager manages the path of the CLI config | ||
type CLIConfigManager struct { | ||
CLIConfigPath string | ||
viper *viper.Viper |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have kept the field private for now. We can make this public in the future if required.
cli/pkg/config/cli_config.go
Outdated
dir, err := keptnutils.GetKeptnDirectory() | ||
if err != nil { | ||
log.Fatal(err) | ||
fmt.Printf("no value for --config-file was specified\n defaulting to: %s\n", cfgFile) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use logging.PrintLog()
with VerboseLevel
instead of fmt.Printf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will change this. Thanks for the review 🙏
428afea
to
ef509e0
Compare
- custom config file path - this is an overwrite of yesterday's PR - use decode hook to unmarshal time - move viper to CLIConfigMgr - try out writing config using viper - add string param to `NewCLIConfigManager` - use json marshal with viper to write the config -- add a new func to get already loaded cli config -- this would prevent reading in the cli config from the file every time - add viper to go.mod - clean up -- remove shorthand for `config-file` -- shorthand `c` and `f` are already used in subcommands (go test fails) - rename `KeptnConfigPath` -> `GetKeptnDefaultConfigPath` - fix spacing - add viper to go.sum - move cli config path check to NewCLIConfigManager - use specified config path in LoadCLIConfig - print msg about using default config path if none is specified -- print `data` and `testConfig` to analyse the difference -- CI is failing because of ^ - remove indentation added by viper in the tests - add `\n` to log msg - use the same config mgr everywhere -- make viper private (can make it public later if needed) - use viper 1.9.0 -- latest version at the time of this commit - revert cli/main.go (changes not needed) - replace fmt.Printf with verbose level logging.PrintLog - update go.sum Signed-off-by: Suraj Banakar (vadasambar) <surajrbanakar@gmail.com>
ef509e0
to
5c5a7be
Compare
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
Fixes #5490
This PR uses Viper internally to manage the Keptn Config.
Why
How to test
keptn <any-command> --cli-config ~/.foo/my-keptn-config
to use a config stored at path other than~/.keptn/config
Things to note
go.sum
. Adding viper 1.9.0 lead to many changes ingo.sum
(additive. I have not removed any dependency)