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 take kubeconfig content
Browse files Browse the repository at this point in the history
Instead of kubeconfig file path. This simplifies the tests and moves us
closer towards resolving #608.

Signed-off-by: Mateusz Gozdek <mateusz@kinvolk.io>
  • Loading branch information
invidian committed Jun 16, 2020
1 parent 4249278 commit 77a3429
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 60 deletions.
13 changes: 9 additions & 4 deletions cli/cmd/cluster-apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,20 @@ func runClusterApply(cmd *cobra.Command, args []string) {
ctxLogger.Fatalf("Verify cluster: %v", err)
}

kubeconfigContent, err := ioutil.ReadFile(kubeconfigPath) // #nosec G304
if err != nil {
ctxLogger.Fatalf("Failed to read kubeconfig file: %q: %v", kubeconfigPath, err)
}

// Do controlplane upgrades only if cluster already exists and it is not a managed platform.
if exists && !p.Meta().Managed {
fmt.Printf("\nEnsuring that cluster controlplane is up to date.\n")

cu := controlplaneUpdater{
kubeconfigPath: kubeconfigPath,
assetDir: assetDir,
ctxLogger: *ctxLogger,
ex: *ex,
kubeconfig: kubeconfigContent,
assetDir: assetDir,
ctxLogger: *ctxLogger,
ex: *ex,
}

releases := []string{"pod-checkpointer", "kube-apiserver", "kubernetes", "calico"}
Expand Down
10 changes: 5 additions & 5 deletions cli/cmd/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ func clusterExists(ctxLogger *logrus.Entry, ex *terraform.Executor) bool {
}

type controlplaneUpdater struct {
kubeconfigPath string
assetDir string
ctxLogger logrus.Entry
ex terraform.Executor
kubeconfig []byte
assetDir string
ctxLogger logrus.Entry
ex terraform.Executor
}

func (c controlplaneUpdater) getControlplaneChart(name string) (*chart.Chart, error) {
Expand Down Expand Up @@ -187,7 +187,7 @@ func (c controlplaneUpdater) upgradeComponent(component string) {
"component": component,
})

actionConfig, err := util.HelmActionConfig("kube-system", c.kubeconfigPath)
actionConfig, err := util.HelmActionConfig("kube-system", c.kubeconfig)
if err != nil {
ctxLogger.Fatalf("Failed initializing helm: %v", err)
}
Expand Down
23 changes: 14 additions & 9 deletions pkg/components/util/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ func InstallAsRelease(name string, c components.Component, kubeconfig string) er
return fmt.Errorf("component %s namespace is empty", name)
}

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

actionConfig, err := HelmActionConfig(ns, kubeconfigContent)
if err != nil {
return fmt.Errorf("failed preparing helm client: %w", err)
}
Expand Down Expand Up @@ -100,15 +105,10 @@ func InstallAsRelease(name string, c components.Component, kubeconfig string) er
}

// HelmActionConfig creates initialized Helm action configuration.
func HelmActionConfig(ns string, kubeconfig string) (*action.Configuration, error) {
func HelmActionConfig(ns string, kubeconfig []byte) (*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)
getter, err := k8sutil.NewGetter(kubeconfig)
if err != nil {
return nil, fmt.Errorf("failed to create Kubernetes client getter: %v", err)
}
Expand Down Expand Up @@ -152,7 +152,12 @@ func UninstallComponent(c components.Component, kubeconfig string, deleteNSBool
panic(fmt.Errorf("component %s namespace is empty", name))
}

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

cfg, err := HelmActionConfig(ns, kubeconfigContent)
if err != nil {
return fmt.Errorf("failed preparing helm client: %w", err)
}
Expand Down
44 changes: 2 additions & 42 deletions pkg/components/util/install_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package util_test

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

"github.com/kinvolk/lokomotive/pkg/components/util"
Expand Down Expand Up @@ -30,51 +28,13 @@ contexts:
)

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 {
if _, err := util.HelmActionConfig("foo", []byte(validKubeconfig)); 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 {
if _, err := util.HelmActionConfig("foo", []byte("foo")); err == nil {
t.Fatalf("creating helm action config from invalid kubeconfig file should fail")
}
}

0 comments on commit 77a3429

Please sign in to comment.