diff --git a/README.md b/README.md index 6939be2c..1b2b5e9a 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ It supports various backends including: - [Doppler](https://doppler.com/) - CredHub(Coming soon) - Pulumi State -- Kubernetes secrets +- Kubernetes - Use `vals eval -f refs.yaml` to replace all the `ref`s in the file to actual values and secrets. - Use `vals exec -f env.yaml -- ` to populate envvars and execute the command. @@ -219,7 +219,7 @@ Please see the [relevant unit test cases](https://github.com/helmfile/vals/blob/ - [1Password Connect](#1password-connect) - [Doppler](#doppler) - [Pulumi State](#pulumi-state) -- [Kubernetes secrets](#kubernetes-secrets) +- [Kubernetes](#kubernetes) Please see [pkg/providers](https://github.com/helmfile/vals/tree/master/pkg/providers) for the implementations of all the providers. The package names corresponds to the URI schemes. @@ -724,9 +724,9 @@ Examples: - `ref+pulumistateapi://aws-native_s3_Bucket/my-bucket/outputs/tags.%23(key==SomeKey).value?project=my-project&stack=my-stack` - `ref+pulumistateapi://kubernetes_storage.k8s.io__v1_StorageClass/gp2-encrypted/inputs/metadata.name?project=my-project&stack=my-stack` -### Kubernetes secrets +### Kubernetes -Fetch value from a Kubernetes secret: +Fetch value from a Kubernetes: - `ref+k8s://API_VERSION/KIND/NAMESPACE/NAME/KEY[?kubeConfigPath=&kubeContext=]` diff --git a/pkg/providers/k8s/k8s.go b/pkg/providers/k8s/k8s.go index e923f093..06f0104b 100644 --- a/pkg/providers/k8s/k8s.go +++ b/pkg/providers/k8s/k8s.go @@ -21,24 +21,32 @@ type provider struct { KubeContext string } -func New(l *log.Logger, cfg api.StaticConfig) (*provider, error) { +func New(l *log.Logger, cfg api.StaticConfig) (*provider) { p := &provider{ log: l, } + var err error - kubeConfig, err := getKubeConfig(cfg) + p.KubeConfigPath, err = getKubeConfigPath(cfg) if err != nil { - p.log.Debugf("Unable to get a valid Kubeconfig path: %s\n", err) - return nil, err + p.log.Debugf("vals-k8s: Unable to get a valid kubeConfig path: %s", err) + return nil } - p.KubeConfigPath = kubeConfig - p.KubeContext = getKubeContext(cfg) + p.KubeContext, err = getKubeContext(cfg) + if err != nil { + p.log.Debugf("vals-k8s: Unable to get a valid kubeContext: %s", err) + return nil + } + + if p.KubeContext == "" { + p.log.Debugf("vals-k8s: kubeContext was not provided. Using current context.") + } - return p, nil + return p } -func getKubeConfig(cfg api.StaticConfig) (string, error) { +func getKubeConfigPath(cfg api.StaticConfig) (string, error) { // Use kubeConfigPath from URI parameters if specified if cfg.String("kubeConfigPath") != "" { if _, err := os.Stat(cfg.String("kubeConfigPath")); err != nil { @@ -118,11 +126,11 @@ func (p *provider) GetStringMap(path string) (map[string]interface{}, error) { } // Return an empty Kube context if none is provided -func getKubeContext(cfg api.StaticConfig) string { +func getKubeContext(cfg api.StaticConfig) (string, error) { if cfg.String("kubeContext") != "" { - return cfg.String("kubeContext") + return cfg.String("kubeContext"), nil } - return "" + return "", nil } // Build the client-go config using a specific context @@ -136,10 +144,6 @@ func buildConfigWithContextFromFlags(context string, kubeconfigPath string) (*re // Fetch the object from the Kubernetes cluster func getObject(namespace string, name string, kubeConfigPath string, kubeContext string, ctx context.Context) (map[string][]byte, error) { - if kubeContext == "" { - fmt.Printf("vals-k8s: kubeContext was not provided. Using current context.\n") - } - config, err := buildConfigWithContextFromFlags(kubeContext, kubeConfigPath) if err != nil { diff --git a/pkg/stringmapprovider/stringmapprovider.go b/pkg/stringmapprovider/stringmapprovider.go index 28e2a39d..d0923a61 100644 --- a/pkg/stringmapprovider/stringmapprovider.go +++ b/pkg/stringmapprovider/stringmapprovider.go @@ -45,7 +45,7 @@ func New(l *log.Logger, provider api.StaticConfig) (api.LazyLoadedStringMapProvi case "gkms": return gkms.New(l, provider), nil case "k8s": - return k8s.New(l, provider) + return k8s.New(l, provider), nil } return nil, fmt.Errorf("failed initializing string-map provider from config: %v", provider) diff --git a/pkg/stringprovider/stringprovider.go b/pkg/stringprovider/stringprovider.go index cb8e0af7..8a0a35b9 100644 --- a/pkg/stringprovider/stringprovider.go +++ b/pkg/stringprovider/stringprovider.go @@ -66,7 +66,7 @@ func New(l *log.Logger, provider api.StaticConfig) (api.LazyLoadedStringProvider case "gkms": return gkms.New(l, provider), nil case "k8s": - return k8s.New(l, provider) + return k8s.New(l, provider), nil } return nil, fmt.Errorf("failed initializing string provider from config: %v", provider) diff --git a/vals.go b/vals.go index 8d48df20..efb07195 100644 --- a/vals.go +++ b/vals.go @@ -251,7 +251,8 @@ func (r *Runtime) prepare() (*expansion.ExpandRegexMatch, error) { p := gkms.New(r.logger, conf) return p, nil case ProviderK8s: - return k8s.New(r.logger, conf) + p := k8s.New(r.logger, conf) + return p, nil } return nil, fmt.Errorf("no provider registered for scheme %q", scheme) }