Skip to content
This repository has been archived by the owner on Jun 29, 2022. It is now read-only.

Commit

Permalink
pkg/components/util: make HelmActionConfig use kubeconfig file content
Browse files Browse the repository at this point in the history
As part of #608, this commit changes the implementation of
HelmActionConfig function to read the content of given kubeconfig file
and then use the content to construct Helm's actionConfig. This will
make easier changing the function signature to accept content of
kubeconfig file in the future changes.

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
  • Loading branch information
invidian committed Jun 16, 2020
1 parent dd00f37 commit c424964
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 3 deletions.
16 changes: 13 additions & 3 deletions pkg/components/util/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ package util

import (
"fmt"
"io/ioutil"

"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/kube"
"helm.sh/helm/v3/pkg/storage/driver"

"github.com/kinvolk/lokomotive/pkg/components"
"github.com/kinvolk/lokomotive/pkg/k8sutil"
)

// InstallComponent installs given component using given kubeconfig.
Expand Down Expand Up @@ -99,11 +100,20 @@ func InstallAsRelease(name string, c components.Component, kubeconfig string) er
func HelmActionConfig(ns string, kubeconfig string) (*action.Configuration, error) {
actionConfig := &action.Configuration{}

kubeconfigContent, err := ioutil.ReadFile(kubeconfig) // #nosec G304
if err != nil {
return nil, fmt.Errorf("failed to read kubeconfig file %q: %v", kubeconfig, err)
}

getter, err := k8sutil.NewGetter(kubeconfigContent)
if err != nil {
return nil, fmt.Errorf("failed to create Kubernetes client getter: %v", err)
}

// TODO: Add some logging implementation? We currently just pass an empty function for logging.
kubeConfig := kube.GetConfig(kubeconfig, "", ns)
logF := func(format string, v ...interface{}) {}

if err := actionConfig.Init(kubeConfig, ns, "secret", logF); err != nil {
if err := actionConfig.Init(getter, ns, "secret", logF); err != nil {
return nil, fmt.Errorf("failed initializing helm: %w", err)
}

Expand Down
80 changes: 80 additions & 0 deletions pkg/components/util/install_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package util_test

import (
"io/ioutil"
"os"
"testing"

"github.com/kinvolk/lokomotive/pkg/components/util"
)

const (
validKubeconfig = `
apiVersion: v1
kind: Config
clusters:
- name: admin
cluster:
server: https://nonexistent:6443
users:
- name: admin
user:
token: "foo.bar"
current-context: admin
contexts:
- name: admin
context:
cluster: admin
user: admin
`
)

func TestHelmActionConfigFromValidKubeconfigFile(t *testing.T) {
tmpFile, err := ioutil.TempFile("", "lokoctl-tests-")
if err != nil {
t.Fatalf("creating tmp file should succeed, got: %v", err)
}

defer func() {
if err := os.Remove(tmpFile.Name()); err != nil {
t.Logf("failed to remove tmp file %q: %v", tmpFile.Name(), err)
}
}()

if _, err := tmpFile.Write([]byte(validKubeconfig)); err != nil {
t.Fatalf("writing to tmp file %q should succeed, got: %v", tmpFile.Name(), err)
}

if err := tmpFile.Close(); err != nil {
t.Fatalf("closing tmp file %q should succeed, got: %v", tmpFile.Name(), err)
}

if _, err := util.HelmActionConfig("foo", tmpFile.Name()); err != nil {
t.Fatalf("creating helm action config from valid kubeconfig file should succeed, got: %v", err)
}
}

func TestHelmActionConfigFromInvalidKubeconfigFile(t *testing.T) {
tmpFile, err := ioutil.TempFile("", "lokoctl-tests-")
if err != nil {
t.Fatalf("creating tmp file should succeed, got: %v", err)
}

defer func() {
if err := os.Remove(tmpFile.Name()); err != nil {
t.Logf("failed to remove tmp file %q: %v", tmpFile.Name(), err)
}
}()

if _, err := tmpFile.Write([]byte("foo")); err != nil {
t.Fatalf("writing to tmp file %q should succeed, got: %v", tmpFile.Name(), err)
}

if err := tmpFile.Close(); err != nil {
t.Fatalf("closing tmp file %q should succeed, got: %v", tmpFile.Name(), err)
}

if _, err := util.HelmActionConfig("foo", tmpFile.Name()); err == nil {
t.Fatalf("creating helm action config from invalid kubeconfig file should fail")
}
}

0 comments on commit c424964

Please sign in to comment.