From 43ce6b57da5f7d61fa68e51cfbb0d4e914a2794c Mon Sep 17 00:00:00 2001 From: Baofa Fan Date: Fri, 4 Aug 2017 23:55:52 +0800 Subject: [PATCH] delete secret when helm reset (#2715) * delete secret when helm reset * add test * expected 3 actions --- cmd/helm/installer/install.go | 1 - cmd/helm/installer/uninstall.go | 12 +++++++++++- cmd/helm/installer/uninstall_test.go | 28 ++++++++++++++++++++++------ cmd/helm/reset_test.go | 12 ++++++------ 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/cmd/helm/installer/install.go b/cmd/helm/installer/install.go index 7e8707fc8..8884e6bab 100644 --- a/cmd/helm/installer/install.go +++ b/cmd/helm/installer/install.go @@ -251,7 +251,6 @@ func createSecret(client corev1.SecretsGetter, opts *Options) error { // generateSecret builds the secret object that hold Tiller secrets. func generateSecret(opts *Options) (*v1.Secret, error) { - const secretName = "tiller-secret" labels := generateLabels(map[string]string{"name": "tiller"}) secret := &v1.Secret{ diff --git a/cmd/helm/installer/uninstall.go b/cmd/helm/installer/uninstall.go index f501bc36d..3d0710969 100644 --- a/cmd/helm/installer/uninstall.go +++ b/cmd/helm/installer/uninstall.go @@ -28,6 +28,7 @@ import ( const ( deploymentName = "tiller-deploy" serviceName = "tiller-deploy" + secretName = "tiller-secret" ) // Uninstall uses Kubernetes client to uninstall Tiller. @@ -35,7 +36,10 @@ func Uninstall(client internalclientset.Interface, opts *Options) error { if err := deleteService(client.Core(), opts.Namespace); err != nil { return err } - return deleteDeployment(client, opts.Namespace) + if err := deleteDeployment(client, opts.Namespace); err != nil { + return err + } + return deleteSecret(client.Core(), opts.Namespace) } // deleteService deletes the Tiller Service resource @@ -53,6 +57,12 @@ func deleteDeployment(client internalclientset.Interface, namespace string) erro return ingoreNotFound(err) } +// deleteSecret deletes the Tiller Secret resource +func deleteSecret(client coreclient.SecretsGetter, namespace string) error { + err := client.Secrets(namespace).Delete(secretName, &metav1.DeleteOptions{}) + return ingoreNotFound(err) +} + func ingoreNotFound(err error) error { if apierrors.IsNotFound(err) { return nil diff --git a/cmd/helm/installer/uninstall_test.go b/cmd/helm/installer/uninstall_test.go index e7b325522..a6e9d1d33 100644 --- a/cmd/helm/installer/uninstall_test.go +++ b/cmd/helm/installer/uninstall_test.go @@ -34,8 +34,8 @@ func TestUninstall(t *testing.T) { t.Errorf("unexpected error: %#+v", err) } - if actions := fc.Actions(); len(actions) != 6 { - t.Errorf("unexpected actions: %v, expected 6 actions got %d", actions, len(actions)) + if actions := fc.Actions(); len(actions) != 7 { + t.Errorf("unexpected actions: %v, expected 7 actions got %d", actions, len(actions)) } } @@ -50,8 +50,8 @@ func TestUninstall_serviceNotFound(t *testing.T) { t.Errorf("unexpected error: %#+v", err) } - if actions := fc.Actions(); len(actions) != 6 { - t.Errorf("unexpected actions: %v, expected 6 actions got %d", actions, len(actions)) + if actions := fc.Actions(); len(actions) != 7 { + t.Errorf("unexpected actions: %v, expected 7 actions got %d", actions, len(actions)) } } @@ -66,7 +66,23 @@ func TestUninstall_deploymentNotFound(t *testing.T) { t.Errorf("unexpected error: %#+v", err) } - if actions := fc.Actions(); len(actions) != 6 { - t.Errorf("unexpected actions: %v, expected 6 actions got %d", actions, len(actions)) + if actions := fc.Actions(); len(actions) != 7 { + t.Errorf("unexpected actions: %v, expected 7 actions got %d", actions, len(actions)) + } +} + +func TestUninstall_secretNotFound(t *testing.T) { + fc := &fake.Clientset{} + fc.AddReactor("delete", "secrets", func(action testcore.Action) (bool, runtime.Object, error) { + return true, nil, apierrors.NewNotFound(api.Resource("secrets"), "1") + }) + + opts := &Options{Namespace: api.NamespaceDefault} + if err := Uninstall(fc, opts); err != nil { + t.Errorf("unexpected error: %#+v", err) + } + + if actions := fc.Actions(); len(actions) != 7 { + t.Errorf("unexpected actions: %v, expect 7 actions got %d", actions, len(actions)) } } diff --git a/cmd/helm/reset_test.go b/cmd/helm/reset_test.go index ca97d1d2b..d6febd132 100644 --- a/cmd/helm/reset_test.go +++ b/cmd/helm/reset_test.go @@ -52,8 +52,8 @@ func TestResetCmd(t *testing.T) { t.Errorf("unexpected error: %v", err) } actions := fc.Actions() - if len(actions) != 2 { - t.Errorf("Expected 2 actions, got %d", len(actions)) + if len(actions) != 3 { + t.Errorf("Expected 3 actions, got %d", len(actions)) } expected := "Tiller (the Helm server-side component) has been uninstalled from your Kubernetes Cluster." if !strings.Contains(buf.String(), expected) { @@ -86,8 +86,8 @@ func TestResetCmd_removeHelmHome(t *testing.T) { t.Errorf("unexpected error: %v", err) } actions := fc.Actions() - if len(actions) != 2 { - t.Errorf("Expected 2 actions, got %d", len(actions)) + if len(actions) != 3 { + t.Errorf("Expected 3 actions, got %d", len(actions)) } expected := "Tiller (the Helm server-side component) has been uninstalled from your Kubernetes Cluster." if !strings.Contains(buf.String(), expected) { @@ -157,8 +157,8 @@ func TestReset_forceFlag(t *testing.T) { t.Errorf("unexpected error: %v", err) } actions := fc.Actions() - if len(actions) != 2 { - t.Errorf("Expected 2 actions, got %d", len(actions)) + if len(actions) != 3 { + t.Errorf("Expected 3 actions, got %d", len(actions)) } expected := "Tiller (the Helm server-side component) has been uninstalled from your Kubernetes Cluster." if !strings.Contains(buf.String(), expected) {