From 795b4c62dedbc62a401409e2a465a662acfb9c22 Mon Sep 17 00:00:00 2001 From: Mateusz Gozdek Date: Tue, 16 Jun 2020 11:23:05 +0200 Subject: [PATCH] pkg/components/util: make UninstallComponent take kubeconfig content So we don't necesserily need a file on file system to perfrom this action, which is needed to resolve #608. Signed-off-by: Mateusz Gozdek --- cli/cmd/component-delete.go | 10 ++++++++-- pkg/components/util/install.go | 18 ++++-------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/cli/cmd/component-delete.go b/cli/cmd/component-delete.go index f2a1d59e8..b1587193e 100644 --- a/cli/cmd/component-delete.go +++ b/cli/cmd/component-delete.go @@ -16,6 +16,7 @@ package cmd import ( "fmt" + "io/ioutil" "strings" log "github.com/sirupsen/logrus" @@ -91,12 +92,17 @@ func runDelete(cmd *cobra.Command, args []string) { contextLogger.Fatalf("Error in finding kubeconfig file: %s", err) } - if err := deleteComponents(kubeconfig, componentsObjects...); err != nil { + kubeconfigContent, err := ioutil.ReadFile(kubeconfig) // #nosec G304 + if err != nil { + contextLogger.Fatalf("Failed to read kubeconfig file: %q: %v", kubeconfig, err) + } + + if err := deleteComponents(kubeconfigContent, componentsObjects...); err != nil { contextLogger.Fatal(err) } } -func deleteComponents(kubeconfig string, componentObjects ...components.Component) error { +func deleteComponents(kubeconfig []byte, componentObjects ...components.Component) error { for _, compObj := range componentObjects { fmt.Printf("Deleting component '%s'...\n", compObj.Metadata().Name) diff --git a/pkg/components/util/install.go b/pkg/components/util/install.go index d9c5f53ef..2acc0db76 100644 --- a/pkg/components/util/install.go +++ b/pkg/components/util/install.go @@ -137,7 +137,7 @@ func ReleaseExists(actionConfig action.Configuration, name string) (bool, error) } // UninstallComponent uninstalls a component and optionally removes it's namespace. -func UninstallComponent(c components.Component, kubeconfig string, deleteNSBool bool) error { +func UninstallComponent(c components.Component, kubeconfig []byte, deleteNSBool bool) error { name := c.Metadata().Name if name == "" { // This should never fail in real user usage, if this does that means the component was not @@ -152,12 +152,7 @@ func UninstallComponent(c components.Component, kubeconfig string, deleteNSBool panic(fmt.Errorf("component %s namespace is empty", name)) } - 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) + cfg, err := HelmActionConfig(ns, kubeconfig) if err != nil { return fmt.Errorf("failed preparing helm client: %w", err) } @@ -189,13 +184,8 @@ func UninstallComponent(c components.Component, kubeconfig string, deleteNSBool return nil } -func deleteNS(ns string, kubeconfig string) error { - kubeconfigContent, err := ioutil.ReadFile(kubeconfig) // #nosec G304 - if err != nil { - return fmt.Errorf("failed to read kubeconfig file: %v", err) - } - - cs, err := k8sutil.NewClientset(kubeconfigContent) +func deleteNS(ns string, kubeconfig []byte) error { + cs, err := k8sutil.NewClientset(kubeconfig) if err != nil { return err }