Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(providers): k8s, log message getting into stdout #188

Merged
merged 4 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ build:

install: build
mv bin/vals ~/bin/

lint:
golangci-lint run -v --out-format=github-actions
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -- <COMMAND>` to populate envvars and execute the command.
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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=<path_to_kubeconfig>&kubeContext=<kubernetes context name>]`

Expand Down
22 changes: 11 additions & 11 deletions pkg/providers/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/providers/k8s/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/stringmapprovider/stringmapprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/stringprovider/stringprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion vals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down