Skip to content
This repository has been archived by the owner on Oct 22, 2021. It is now read-only.

Allow to read config also from environment #14

Merged
merged 5 commits into from Apr 24, 2020
Merged

Allow to read config also from environment #14

merged 5 commits into from Apr 24, 2020

Conversation

mudler
Copy link
Member

@mudler mudler commented Apr 21, 2020

Fixes #13

With this change, now we don't fail anymore if a config file is not provided:

$ ./binaries/eirini-loggregator-bridge                     
Namespace:  
Loggregator-endpoint:  
Loggregator-ca-path:  
Loggregator-cert-path:  
Loggregator-key-path:  
namespace is missing from configuration

With a config file, we keep reading it correctly:

$ ./binaries/eirini-loggregator-bridge --config config.yaml
Namespace:  default
Loggregator-endpoint:  ca_path
Loggregator-ca-path:  ca_path
Loggregator-cert-path:  ca_path
Loggregator-key-path:  ca_path
{"level":"info","ts":1587461901.3673894,"caller":"kubeconfig/getter.go:79","msg":" does not exist, using default kube config"}

while we allow now to setup configuration via environment variables and also override the config file:

$ NAMESPACE=foo ./binaries/eirini-loggregator-bridge --config config.yaml 
Namespace:  foo
Loggregator-endpoint:  ca_path
Loggregator-ca-path:  ca_path
Loggregator-cert-path:  ca_path
Loggregator-key-path:  ca_path

Note: EIRINI_LOGGREGATOR_BRIDGE_LOGLEVEL=DEBUG must be set to show the config values that are used

Copy link
Contributor

@mook-as mook-as left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this! However, it doesn't seem to work for me:

$ env NAMESPACE=ns LOGGREGATOR-ENDPOINT=foo LOGGREGATOR-CA-PATH=bar LOGGREGATOR-CERT-PATH=baz LOGGREGATOR-KEY-PATH=quux ./binaries/eirini-loggregator-bridge
Namespace:  ns
Loggregator-endpoint:  foo
Loggregator-ca-path:  bar
Loggregator-cert-path:  baz
Loggregator-key-path:  quux
namespace is missing from configuration

While the debugging bits seem to get the settings, the viper.Unmarshal didn't seem to. Note that I didn't have a --config anywhere.

@mudler
Copy link
Member Author

mudler commented Apr 22, 2020

Thanks for the review! you are right, I think we are hitting spf13/viper#188 , I'll have a look!

@mudler
Copy link
Member Author

mudler commented Apr 22, 2020

I think I've got it:

$ NAMESPACE=too LOGGREGATOR_KEY_PATH=testkey LOGGREGATOR_CERT_PATH=cert_path LOGGREGATOR_ENDPOINT=loggregator_endpoint_test LOGGREGATOR_CA_PATH=test ./binaries/eirini-loggregator-bridge
Namespace:  too
Loggregator-endpoint:  loggregator_endpoint_test
Loggregator-ca-path:  test
Loggregator-cert-path:  cert_path
Loggregator-key-path:  testkey
{"level":"info","ts":1587549438.6308644,"caller":"kubeconfig/getter.go:79","msg":" does not exist, using default kube config"}
{"level":"info","ts":1587549438.6309066,"caller":"kubeconfig/checker.go:36","msg":"Checking kube config"}
invalid kube config: Get "http://localhost:8080/version?timeout=32s": dial tcp [::1]:8080: connect: connection refused

I've also changed the debug lines to print the data from the config, so it's more easy to spot issues

@mudler mudler requested a review from mook-as April 22, 2020 11:17
mudler added a commit to cloudfoundry-incubator/eirinix-helm-release that referenced this pull request Apr 22, 2020
WIP: doing this aside with the InitContainer. The InitContainer can be dropped once
cloudfoundry-incubator/eirini-loggregator-bridge#14 is merged.
cmd/root.go Outdated
@@ -63,18 +66,45 @@ func init() {
rootCmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", "", "kubeconfig file path. This is optional, in cluster config will be used if not set")
}

// See: https://github.com/spf13/viper/issues/188#issuecomment-399884438
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is copied from a GitHub comment; what license is it under? We may need to rewrite it instead. (Also, I find the variable names to be opaque.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but otoh it won't get much different if we end up using reflection. I'll try a different approach then

cmd/root.go Outdated
t := ift.Field(i)
tv, ok := t.Tag.Lookup("mapstructure")
if !ok {
continue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I read the mapstructure docs correctly, this is doing the wrong thing: if the tag is missing, it should by default use the field name. Also, the tag may contain suffixes like ,squash and ,remain, so we should strip anything after a comma.

Alternatively, we can actually import mapstructure, and convert an instance of Config to JSON/YAML and have viper load that before loading the real config, according to a different comment in that issue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I read the mapstructure docs correctly, this is doing the wrong thing: if the tag is missing, it should by default use the field name. Also, the tag may contain suffixes like ,squash and ,remain, so we should strip anything after a comma.

Don't see the use case here, and the code would grow too much of complexity for a very tiny improvement that will make it even more fragile. I prefer to look after another way of doing it, avoding reflection at all ( I was unattracted by this solution as well, too ! )

Copy link
Member Author

@mudler mudler Apr 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been playing around with spf13/viper#188 (comment) here b1c8bd7 without luck - it doesn't seem to work anymore, and moreover forces to ship "defaults" which we tried to avoid here, prefering validation of proper input ( to avoid surprises during configuration )

WIP: it's not working yet
cmd/root.go Outdated
}

emptyConfigReader := bytes.NewReader(emptyConfigBytes)
viper.MergeConfig(emptyConfigReader)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm reading that viper bug correctly, the MergeConfig call needs to be done even if cfgFile == "".

Copy link
Member Author

@mudler mudler Apr 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried already few times as well (also by dropping any check to cfgFile) , I just ended up defining the binds manually: 32059c9

Copy link
Member Author

@mudler mudler Apr 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See spf13/viper#761 to check how Unmarshal in Viper works under the hood

@mudler mudler requested a review from mook-as April 23, 2020 14:21
@mudler
Copy link
Member Author

mudler commented Apr 23, 2020

If looks good, feel free to squash when merging, no need for the whole history ( I kept it here for reference )

Copy link
Contributor

@mook-as mook-as left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Sad that viper doesn't seem to work as well as we'd like, and we need the workarounds… ah well.

I'm happy with squashing as well, but I'll let you choose if you want to take the suggestions first.

Comment on lines +82 to +86
viper.SetDefault("NAMESPACE", "")
viper.SetDefault("LOGGREGATOR_KEY_PATH", "")
viper.SetDefault("LOGGREGATOR_ENDPOINT", "")
viper.SetDefault("LOGGREGATOR_CA_PATH", "")
viper.SetDefault("LOGGREGATOR_CERT_PATH", "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we actually need these (since they're the zero values already).

Suggested change
viper.SetDefault("NAMESPACE", "")
viper.SetDefault("LOGGREGATOR_KEY_PATH", "")
viper.SetDefault("LOGGREGATOR_ENDPOINT", "")
viper.SetDefault("LOGGREGATOR_CA_PATH", "")
viper.SetDefault("LOGGREGATOR_CERT_PATH", "")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need that, otherwise you cannot set them up properly without providing a config 🤷‍♂️

cmd/root.go Outdated Show resolved Hide resolved
Co-Authored-By: Mark Yen <3977982+mook-as@users.noreply.github.com>
@mudler mudler merged commit 3b15598 into master Apr 24, 2020
@mudler mudler deleted the read-env branch April 24, 2020 06:32
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow config to be passed in individually
2 participants