diff --git a/Makefile b/Makefile index 401c8fb..498a50b 100644 --- a/Makefile +++ b/Makefile @@ -7,3 +7,6 @@ build: install: build mv bin/vals ~/bin/ + +lint: + golangci-lint run -v --out-format=github-actions diff --git a/README.md b/README.md index 6939be2..1b2b5e9 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 86e869d..0441f5c 100644 --- a/pkg/providers/k8s/k8s.go +++ b/pkg/providers/k8s/k8s.go @@ -21,24 +21,28 @@ 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) - return p, nil + if p.KubeContext == "" { + p.log.Debugf("vals-k8s: kubeContext was not provided. Using current context.") + } + + 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 { @@ -130,10 +134,6 @@ func buildConfigWithContextFromFlags(context string, kubeconfigPath string) (*re // Fetch the object from the Kubernetes cluster func getObject(kind string, namespace string, name string, kubeConfigPath string, kubeContext string, ctx context.Context) (map[string]string, 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/providers/k8s/k8s_test.go b/pkg/providers/k8s/k8s_test.go index 43d4fbc..b769146 100644 --- a/pkg/providers/k8s/k8s_test.go +++ b/pkg/providers/k8s/k8s_test.go @@ -136,7 +136,7 @@ func Test_getObject(t *testing.T) { } } -func Test_getKubeConfig(t *testing.T) { +func Test_getKubeConfigPath(t *testing.T) { homeDir, _ := os.UserHomeDir() testcases := []struct { config config.MapConfig @@ -198,7 +198,7 @@ func Test_getKubeConfig(t *testing.T) { if tc.kubeConfigEnvVar != "" { os.Setenv("KUBECONFIG", tc.kubeConfigEnvVar) } - got, err := getKubeConfig(tc.config) + got, err := getKubeConfigPath(tc.config) if err != nil { if err.Error() != tc.wantErr { t.Fatalf("unexpected error: want %q, got %q", tc.wantErr, err.Error()) @@ -337,7 +337,7 @@ func Test_GetString(t *testing.T) { conf := map[string]interface{}{} conf["kubeConfigPath"] = fmt.Sprintf("%s/.kube/config", homeDir) conf["kubeContext"] = "minikube" - p, _ := New(logger, config.MapConfig{M: conf}) + p := New(logger, config.MapConfig{M: conf}) got, err := p.GetString(tc.path) if err != nil { diff --git a/pkg/stringmapprovider/stringmapprovider.go b/pkg/stringmapprovider/stringmapprovider.go index 28e2a39..d0923a6 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 cb8e0af..8a0a35b 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 8d48df2..c5b4f2d 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) }