github.com/canthefason/helm@v2.2.1-0.20170221172616-16b043b8d505+incompatible/cmd/helm/reset.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "errors" 21 "fmt" 22 "io" 23 "os" 24 25 "github.com/spf13/cobra" 26 "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" 27 28 "k8s.io/helm/cmd/helm/helmpath" 29 "k8s.io/helm/cmd/helm/installer" 30 "k8s.io/helm/pkg/helm" 31 "k8s.io/helm/pkg/kube" 32 "k8s.io/helm/pkg/proto/hapi/release" 33 ) 34 35 const resetDesc = ` 36 This command uninstalls Tiller (the helm server side component) from your 37 Kubernetes Cluster and optionally deletes local configuration in 38 $HELM_HOME (default ~/.helm/) 39 ` 40 41 type resetCmd struct { 42 force bool 43 removeHelmHome bool 44 namespace string 45 out io.Writer 46 home helmpath.Home 47 client helm.Interface 48 kubeClient internalclientset.Interface 49 kubeCmd *kube.Client 50 } 51 52 func newResetCmd(client helm.Interface, out io.Writer) *cobra.Command { 53 d := &resetCmd{ 54 out: out, 55 client: client, 56 } 57 58 cmd := &cobra.Command{ 59 Use: "reset", 60 Short: "uninstalls Tiller from a cluster", 61 Long: resetDesc, 62 PersistentPreRunE: setupConnection, 63 RunE: func(cmd *cobra.Command, args []string) error { 64 if len(args) != 0 { 65 return errors.New("This command does not accept arguments") 66 } 67 68 d.namespace = tillerNamespace 69 d.home = helmpath.Home(homePath()) 70 d.client = ensureHelmClient(d.client) 71 72 return d.run() 73 }, 74 } 75 76 f := cmd.Flags() 77 f.BoolVarP(&d.force, "force", "f", false, "forces Tiller uninstall even if there are releases installed") 78 f.BoolVar(&d.removeHelmHome, "remove-helm-home", false, "if set deletes $HELM_HOME") 79 80 return cmd 81 } 82 83 // runReset uninstalls tiller from Kubernetes Cluster and deletes local config 84 func (d *resetCmd) run() error { 85 if d.kubeClient == nil { 86 _, c, err := getKubeClient(kubeContext) 87 if err != nil { 88 return fmt.Errorf("could not get kubernetes client: %s", err) 89 } 90 d.kubeClient = c 91 } 92 if d.kubeCmd == nil { 93 d.kubeCmd = getKubeCmd(kubeContext) 94 } 95 96 res, err := d.client.ListReleases( 97 helm.ReleaseListStatuses([]release.Status_Code{release.Status_DEPLOYED}), 98 ) 99 if err != nil { 100 return prettyError(err) 101 } 102 103 if len(res.Releases) > 0 && !d.force { 104 return fmt.Errorf("There are still %d deployed releases (Tip: use --force).", len(res.Releases)) 105 } 106 107 if err := installer.Uninstall(d.kubeClient, d.kubeCmd, d.namespace, flagDebug); err != nil { 108 return fmt.Errorf("error unstalling Tiller: %s", err) 109 } 110 111 if d.removeHelmHome { 112 if err := deleteDirectories(d.home, d.out); err != nil { 113 return err 114 } 115 } 116 117 fmt.Fprintln(d.out, "Tiller (the helm server side component) has been uninstalled from your Kubernetes Cluster.") 118 return nil 119 } 120 121 // deleteDirectories deletes $HELM_HOME 122 func deleteDirectories(home helmpath.Home, out io.Writer) error { 123 if _, err := os.Stat(home.String()); err == nil { 124 fmt.Fprintf(out, "Deleting %s \n", home.String()) 125 if err := os.RemoveAll(home.String()); err != nil { 126 return fmt.Errorf("Could not remove %s: %s", home.String(), err) 127 } 128 } 129 130 return nil 131 }