Skip to content

Commit

Permalink
add namespace flag for kudo init (#903)
Browse files Browse the repository at this point in the history
  • Loading branch information
brennerm authored and kensipe committed Oct 16, 2019
1 parent 9799a7c commit a6e1755
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
4 changes: 3 additions & 1 deletion pkg/kudoctl/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type initCmd struct {
dryRun bool
output string
version string
ns string
wait bool
timeout int64
clientOnly bool
Expand All @@ -82,6 +83,7 @@ func newInitCmd(fs afero.Fs, out io.Writer) *cobra.Command {
return err
}
i.home = Settings.Home
i.ns = Settings.Namespace
clog.V(8).Printf("init cmd %v", i)
return i.run()
},
Expand Down Expand Up @@ -122,7 +124,7 @@ func (initCmd *initCmd) validate(flags *flag.FlagSet) error {

// run initializes local config and installs KUDO manager to Kubernetes cluster.
func (initCmd *initCmd) run() error {
opts := cmdInit.NewOptions(initCmd.version)
opts := cmdInit.NewOptions(initCmd.version, initCmd.ns)
// if image provided switch to it.
if initCmd.image != "" {
opts.Image = initCmd.image
Expand Down
7 changes: 5 additions & 2 deletions pkg/kudoctl/cmd/init/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,18 @@ type Options struct {
}

// NewOptions provides an option struct with defaults
func NewOptions(v string) Options {
func NewOptions(v string, ns string) Options {

if v == "" {
v = version.Get().GitVersion
}
if ns == "" {
ns = defaultns
}

return Options{
Version: v,
Namespace: defaultns,
Namespace: ns,
TerminationGracePeriodSeconds: defaultGracePeriod,
Image: fmt.Sprintf("kudobuilder/controller:v%v", v),
}
Expand Down
54 changes: 53 additions & 1 deletion pkg/kudoctl/cmd/init_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cmd
import (
"bytes"
"context"
"fmt"
"log"
"os"
"strings"
Expand All @@ -19,6 +20,7 @@ import (
"github.com/stretchr/testify/assert"
apiextensionsclient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -77,6 +79,56 @@ func TestIntegInitForCRDs(t *testing.T) {
assert.Nil(t, testClient.Create(context.TODO(), instance))
}

func TestIntegInitWithNameSpace(t *testing.T) {
namespace := "integration-test"
// Kubernetes client caches the types, se we need to re-initialize it.
testClient, err := testutils.NewRetryClient(testenv.Config, client.Options{
Scheme: testutils.Scheme(),
})
assert.Nil(t, err)
kclient := getKubeClient(t)

instance := testutils.NewResource("kudo.dev/v1alpha1", "Instance", "zk", "ns")
// Verify that we cannot create the instance, because the test environment is empty.
assert.IsType(t, &meta.NoKindMatchError{}, testClient.Create(context.TODO(), instance))

// Install all of the CRDs.
crds := cmdinit.CRDs()
defer deleteInitObjects(testClient)

var buf bytes.Buffer
cmd := &initCmd{
out: &buf,
fs: afero.NewMemMapFs(),
client: kclient,
ns: namespace,
}
err = cmd.run()
assert.Nil(t, err)

// WaitForCRDs to be created... the init cmd did NOT wait
assert.Nil(t, testutils.WaitForCRDs(testenv.DiscoveryClient, crds))

// Kubernetes client caches the types, so we need to re-initialize it.
testClient, err = testutils.NewRetryClient(testenv.Config, client.Options{
Scheme: testutils.Scheme(),
})
assert.Nil(t, err)
kclient = getKubeClient(t)

// make sure that the controller lives in the correct namespace
statefulsets, err := kclient.KubeClient.AppsV1().StatefulSets(namespace).List(metav1.ListOptions{})
assert.Nil(t, err)

kudoControllerFound := false
for _, ss := range statefulsets.Items {
if ss.Name == "kudo-controller-manager" {
kudoControllerFound = true
}
}
assert.True(t, kudoControllerFound, fmt.Sprintf("No kudo-controller-manager statefulset found in namespace %s", namespace))
}

func TestNoErrorOnReInit(t *testing.T) {
// if the CRD exists and we init again there should be no error
testClient, err := testutils.NewRetryClient(testenv.Config, client.Options{
Expand Down Expand Up @@ -124,7 +176,7 @@ func TestNoErrorOnReInit(t *testing.T) {

func deleteInitObjects(client *testutils.RetryClient) {
crds := cmdinit.CRDs()
prereqs := cmdinit.Prereq(cmdinit.NewOptions(""))
prereqs := cmdinit.Prereq(cmdinit.NewOptions("", ""))
deleteCRDs(crds, client)
deletePrereq(prereqs, client)
}
Expand Down

0 comments on commit a6e1755

Please sign in to comment.