From 8c15500020e12da78e74e63fbaa5aa35ed5733ee Mon Sep 17 00:00:00 2001 From: Tuna Date: Thu, 23 Nov 2017 19:46:11 +0700 Subject: [PATCH] minor changes --- cmd/down.go | 14 +++++++------- cmd/up.go | 30 +++++++++++++++--------------- cmd/up_test.go | 6 ++++-- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/cmd/down.go b/cmd/down.go index 2b15a4618e3..b1e7862bb62 100644 --- a/cmd/down.go +++ b/cmd/down.go @@ -55,7 +55,7 @@ var downCmd = &cobra.Command{ } //delete mongodb secret - err = deleteMongoDBSecret(c, Kubeapps_NS) + err = deleteMongoDBSecret(c, MongoDB_Secret, Kubeapps_NS) if err != nil { return err } @@ -82,24 +82,24 @@ func init() { downCmd.Flags().Int64("grace-period", -1, "Number of seconds given to resources to terminate gracefully. A negative value is ignored.") } -func deleteMongoDBSecret(c kubecfg.DeleteCmd, ns string) error { +func deleteMongoDBSecret(c kubecfg.DeleteCmd, name, ns string) error { gvk := schema.GroupVersionKind{Version: "v1", Kind: "Secret"} rc, err := clientForGroupVersionKind(c.ClientPool, c.Discovery, gvk, ns) if err != nil { - return fmt.Errorf("can't delete secret object %s due to: %v", SecretID, err) + return fmt.Errorf("can't delete secret object %s due to: %v", name, err) } - _, err = rc.Get(SecretID) + _, err = rc.Get(name) if err == nil { - err = rc.Delete(SecretID, &metav1.DeleteOptions{}) + err = rc.Delete(name, &metav1.DeleteOptions{}) if err != nil { - return fmt.Errorf("can't delete secret object %s due to: %v", SecretID, err) + return fmt.Errorf("can't delete secret object %s due to: %v", name, err) } } else { if k8sErrors.IsNotFound(err) { // if it doesn't exist, just continue return nil } else { - return fmt.Errorf("can't delete secret object %s due to: %v", SecretID, err) + return fmt.Errorf("can't delete secret object %s due to: %v", name, err) } } return nil diff --git a/cmd/up.go b/cmd/up.go index 72b5b30f0a5..65868c1fa3f 100644 --- a/cmd/up.go +++ b/cmd/up.go @@ -37,7 +37,6 @@ import ( k8sErrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" - "k8s.io/client-go/discovery" ) const ( @@ -45,8 +44,8 @@ const ( KubeappsNS = "kubeapps" KubelessNS = "kubeless" SystemNS = "kube-system" - SecretID = "mongodb" Kubeapps_NS = "kubeapps" + MongoDB_Secret = "mongodb" ) var upCmd = &cobra.Command{ @@ -137,21 +136,21 @@ List of components that kubeapps up installs: // mongodb secret // FIXME (tuna): if the mongodb secret exists then do nothing, // otherwise, add it (with new generated rand pw) to the objs list then do full update. - if prevsecret, ok, err := mongoSecretExists(c); err != nil { + if prevsecret, exist, err := mongoSecretExists(c, MongoDB_Secret, Kubeapps_NS); err != nil { return err - } else if !ok { + } else if !exist { pwFields := []string{"mongodb-password", "mongodb-root-password"} pw := make(map[string]string) for _, p := range pwFields { s, err := generateRandomString(10) if err != nil { - return fmt.Errorf("secret %s can't be created due to the random generator get failed: %v", SecretID, err) + return fmt.Errorf("secret %s can't be created due to the random generator get failed: %v", MongoDB_Secret, err) } pw[p] = s } - secret := buildMongoDBSecret(pw) + secret := buildMongoDBSecret(pw, MongoDB_Secret, Kubeapps_NS) objs = append(objs, secret) - } else if ok { + } else if exist { // add prevsecret to the list so it won't be GC-ed objs = append(objs, prevsecret) } @@ -218,14 +217,15 @@ func printOutput(w io.Writer, c *kubernetes.Clientset) error { return nil } -func mongoSecretExists(c kubecfg.ApplyCmd) (*unstructured.Unstructured, bool, error) { + +func mongoSecretExists(c kubecfg.ApplyCmd, name, ns string) (*unstructured.Unstructured, bool, error) { gvk := schema.GroupVersionKind{Version: "v1", Kind: "Secret"} - rc, err := clientForGroupVersionKind(c.ClientPool, c.Discovery, gvk, Kubeapps_NS) + rc, err := clientForGroupVersionKind(c.ClientPool, c.Discovery, gvk, ns) if err != nil { return nil, false, err } pwFields := []string{"mongodb-password", "mongodb-root-password"} - prevSec, err := rc.Get(SecretID) + prevSec, err := rc.Get(name) if err != nil { if k8sErrors.IsNotFound(err) { return nil, false, nil @@ -237,24 +237,24 @@ func mongoSecretExists(c kubecfg.ApplyCmd) (*unstructured.Unstructured, bool, er prevPw := prevSec.Object["data"].(map[string]interface{}) for _, p := range pwFields { if prevPw[p] == nil { - return nil, true, fmt.Errorf("secret %s already exists but it doesn't contain the expected key %s", SecretID, p) + return nil, true, fmt.Errorf("secret %s already exists but it doesn't contain the expected key %s", name, p) } } } else { - return nil, true, fmt.Errorf("secret %s already exists but it doesn't contain any expected key", SecretID) + return nil, true, fmt.Errorf("secret %s already exists but it doesn't contain any expected key", name) } return prevSec, true, nil } } -func buildMongoDBSecret(pw map[string]string) *unstructured.Unstructured{ +func buildMongoDBSecret(pw map[string]string, name, ns string) *unstructured.Unstructured { return &unstructured.Unstructured{ Object: map[string]interface{}{ "kind": "Secret", "apiVersion": "v1", "metadata": map[string]interface{}{ - "name": SecretID, - "namespace": Kubeapps_NS, + "name": name, + "namespace": ns, }, "data": pw, }, diff --git a/cmd/up_test.go b/cmd/up_test.go index 71eb102fcba..52e68ba52ca 100644 --- a/cmd/up_test.go +++ b/cmd/up_test.go @@ -174,8 +174,10 @@ func TestBuildMongoDBSecret(t *testing.T) { "foo": "bar", "bar": "baz", } + name := "foo" + ns := "my-ns" - sr := buildMongoDBSecret(pw) + sr := buildMongoDBSecret(pw, name, ns) if sr.Object["kind"] != "Secret" { t.Errorf("expect kind = secret, got %v", sr.Object["kind"]) } @@ -184,7 +186,7 @@ func TestBuildMongoDBSecret(t *testing.T) { } meta := sr.Object["metadata"].(map[string]interface{}) - if meta["name"].(string) != SecretID || meta["namespace"].(string) != Kubeapps_NS { + if meta["name"].(string) != name || meta["namespace"].(string) != ns { t.Errorf("wrong metadata") } data := sr.Object["data"].(map[string]string)