Skip to content

Commit

Permalink
Fix namespace create without manifest (#1543)
Browse files Browse the repository at this point in the history
If a namespace was created without a manifest, KUDO would fail with a segfault, because it tries to add an annotation to a nil map. This has been fixed and tests have been added for namespace creation.

Signed-off-by: Jan Schlicht <jan@d2iq.com>
  • Loading branch information
Jan Schlicht committed May 29, 2020
1 parent 5e6ccf2 commit 03edc91
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/kudoctl/util/kudo/kudo.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,10 @@ func (c *Client) CreateNamespace(namespace, manifest string) error {
}
ns.TypeMeta.Kind = "Namespace"
ns.Name = namespace

if ns.Annotations == nil {
ns.Annotations = map[string]string{}
}
ns.Annotations["created-by"] = "kudo-cli"

_, err := c.kubeClientset.CoreV1().Namespaces().Create(ns)
Expand Down
55 changes: 55 additions & 0 deletions pkg/kudoctl/util/kudo/kudo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,3 +615,58 @@ func TestKudoClient_DeleteInstance(t *testing.T) {
}
}
}

func TestKudoClient_CreateNamespace(t *testing.T) {
tests := []struct {
name string
namespace string
manifest string
shouldFail bool
}{
{
name: "Invalid manifest",
namespace: "foo-test",
manifest: "invalid namespace resource",
shouldFail: true,
},
{
name: "Namespace without manifest",
namespace: "foo-test",
manifest: "",
shouldFail: false,
},
{
name: "Namespace name overwrites manifest",
namespace: "foo-test",
manifest: `apiVersion: v1
kind: Namespace
metadata:
name: bar-test
`,
shouldFail: false,
},
}

for _, test := range tests {
k2o := newTestSimpleK2o()

err := k2o.CreateNamespace(test.namespace, test.manifest)
if err == nil {
if test.shouldFail {
t.Errorf("expected test %s to fail", test.name)
} else {
namespace, err := k2o.kubeClientset.
CoreV1().
Namespaces().
Get(test.namespace, metav1.GetOptions{})
assert.NilError(t, err)

assert.Equal(t, namespace.Annotations["created-by"], "kudo-cli")
}
} else {
if !test.shouldFail {
t.Errorf("expected test %s to succeed but got error: %v", test.name, err)
}
}
}
}

0 comments on commit 03edc91

Please sign in to comment.