1515package cmd
1616
1717import (
18- "context"
1918 "fmt"
20- "io/ioutil"
2119 "strings"
2220
2321 log "github.com/sirupsen/logrus"
2422 "github.com/spf13/cobra"
25- "helm.sh/helm/v3/pkg/action"
26- "k8s.io/apimachinery/pkg/api/errors"
27- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2823
2924 "github.com/kinvolk/lokomotive/pkg/components"
3025 "github.com/kinvolk/lokomotive/pkg/components/util"
31- "github.com/kinvolk/lokomotive/pkg/k8sutil"
3226)
3327
3428var componentDeleteCmd = & cobra.Command {
@@ -102,11 +96,11 @@ func runDelete(cmd *cobra.Command, args []string) {
10296 }
10397}
10498
105- func deleteComponents (kubeconfig string , componentObjects ... components.Component ) error {
99+ func deleteComponents (kubeconfig [] byte , componentObjects ... components.Component ) error {
106100 for _ , compObj := range componentObjects {
107101 fmt .Printf ("Deleting component '%s'...\n " , compObj .Metadata ().Name )
108102
109- if err := deleteHelmRelease (compObj , kubeconfig , deleteNamespace ); err != nil {
103+ if err := util . UninstallComponent (compObj , kubeconfig , deleteNamespace ); err != nil {
110104 return err
111105 }
112106
@@ -118,75 +112,3 @@ func deleteComponents(kubeconfig string, componentObjects ...components.Componen
118112
119113 return nil
120114}
121-
122- // deleteComponent deletes a component.
123- func deleteHelmRelease (c components.Component , kubeconfig string , deleteNSBool bool ) error {
124- name := c .Metadata ().Name
125- if name == "" {
126- // This should never fail in real user usage, if this does that means the component was not
127- // created with all the needed information.
128- panic (fmt .Errorf ("component name is empty" ))
129- }
130-
131- ns := c .Metadata ().Namespace
132- if ns == "" {
133- // This should never fail in real user usage, if this does that means the component was not
134- // created with all the needed information.
135- panic (fmt .Errorf ("component %s namespace is empty" , name ))
136- }
137-
138- cfg , err := util .HelmActionConfig (ns , kubeconfig )
139- if err != nil {
140- return fmt .Errorf ("failed preparing helm client: %w" , err )
141- }
142-
143- history := action .NewHistory (cfg )
144- // Check if the component's release exists. If it does only then proceed to delete.
145- //
146- // Note: It is assumed that this call will return error only when the release does not exist.
147- // The error check is ignored to make `lokoctl component delete ..` idempotent.
148- // We rely on the fact that the 'component name' == 'release name'. Since component's name is
149- // hardcoded and unlikely to change release name won't change as well. And they will be
150- // consistent if installed by lokoctl. So it is highly unlikely that following call will return
151- // any other error than "release not found".
152- if _ , err := history .Run (name ); err == nil {
153- uninstall := action .NewUninstall (cfg )
154-
155- // Ignore the err when we have deleted the release already or it does not exist for some reason.
156- if _ , err := uninstall .Run (name ); err != nil {
157- return err
158- }
159- }
160-
161- if deleteNSBool {
162- if err := deleteNS (ns , kubeconfig ); err != nil {
163- return err
164- }
165- }
166-
167- return nil
168- }
169-
170- func deleteNS (ns string , kubeconfig string ) error {
171- kubeconfigContent , err := ioutil .ReadFile (kubeconfig ) // #nosec G304
172- if err != nil {
173- return fmt .Errorf ("failed to read kubeconfig file: %v" , err )
174- }
175-
176- cs , err := k8sutil .NewClientset (kubeconfigContent )
177- if err != nil {
178- return err
179- }
180-
181- // Delete the manually created namespace which was not created by helm.
182- if err = cs .CoreV1 ().Namespaces ().Delete (context .TODO (), ns , metav1.DeleteOptions {}); err != nil {
183- // Ignore error when the namespace does not exist.
184- if errors .IsNotFound (err ) {
185- return nil
186- }
187-
188- return err
189- }
190-
191- return nil
192- }
0 commit comments