Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ngtuna committed Nov 28, 2017
1 parent 1c58df8 commit 8c15500
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
14 changes: 7 additions & 7 deletions cmd/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
30 changes: 15 additions & 15 deletions cmd/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@ 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 (
GcTag = "bitnami/kubeapps"
KubeappsNS = "kubeapps"
KubelessNS = "kubeless"
SystemNS = "kube-system"
SecretID = "mongodb"
Kubeapps_NS = "kubeapps"
MongoDB_Secret = "mongodb"
)

var upCmd = &cobra.Command{
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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,
},
Expand Down
6 changes: 4 additions & 2 deletions cmd/up_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
}
Expand All @@ -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)
Expand Down

0 comments on commit 8c15500

Please sign in to comment.