From 384d1e3fefec945f6e62cd03d4784d30ee94370b Mon Sep 17 00:00:00 2001 From: Josh Date: Fri, 9 Feb 2024 11:54:50 -0600 Subject: [PATCH] Update Namespace.ValidateNamespace to add workspace to arguments Signed-off-by: Josh --- pkg/cli/cmd/env/create/create.go | 2 +- pkg/cli/cmd/env/namespace/mock_namespace.go | 4 +++- pkg/cli/cmd/env/namespace/namespace.go | 13 ++++++++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pkg/cli/cmd/env/create/create.go b/pkg/cli/cmd/env/create/create.go index c185402c42..f0e38879c5 100644 --- a/pkg/cli/cmd/env/create/create.go +++ b/pkg/cli/cmd/env/create/create.go @@ -141,7 +141,7 @@ func (r *Runner) Validate(cmd *cobra.Command, args []string) error { return err } - err = r.NamespaceInterface.ValidateNamespace(cmd.Context(), r.Namespace) + err = r.NamespaceInterface.ValidateNamespace(cmd.Context(), r.Namespace, *r.Workspace) if err != nil { return err } diff --git a/pkg/cli/cmd/env/namespace/mock_namespace.go b/pkg/cli/cmd/env/namespace/mock_namespace.go index f03b7878f0..e0eb0e25fa 100644 --- a/pkg/cli/cmd/env/namespace/mock_namespace.go +++ b/pkg/cli/cmd/env/namespace/mock_namespace.go @@ -9,6 +9,8 @@ import ( reflect "reflect" gomock "github.com/golang/mock/gomock" + + "github.com/radius-project/radius/pkg/cli/workspaces" ) // MockInterface is a mock of Interface interface. @@ -35,7 +37,7 @@ func (m *MockInterface) EXPECT() *MockInterfaceMockRecorder { } // ValidateNamespace mocks base method. -func (m *MockInterface) ValidateNamespace(arg0 context.Context, arg1 string) error { +func (m *MockInterface) ValidateNamespace(arg0 context.Context, arg1 string, workspace workspaces.Workspace) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ValidateNamespace", arg0, arg1) ret0, _ := ret[0].(error) diff --git a/pkg/cli/cmd/env/namespace/namespace.go b/pkg/cli/cmd/env/namespace/namespace.go index ac983ef0da..5fb35e43e6 100644 --- a/pkg/cli/cmd/env/namespace/namespace.go +++ b/pkg/cli/cmd/env/namespace/namespace.go @@ -19,12 +19,14 @@ package namespace import ( "context" + "github.com/radius-project/radius/pkg/cli/clierrors" "github.com/radius-project/radius/pkg/cli/kubernetes" + "github.com/radius-project/radius/pkg/cli/workspaces" ) //go:generate mockgen -destination=./mock_namespace.go -package=namespace -self_package github.com/radius-project/radius/pkg/cli/cmd/env/namespace github.com/radius-project/radius/pkg/cli/cmd/env/namespace Interface type Interface interface { - ValidateNamespace(ctx context.Context, namespace string) error + ValidateNamespace(ctx context.Context, namespace string, workspace workspaces.Workspace) error } type Impl struct { @@ -35,8 +37,13 @@ type Impl struct { // ValidateNamespace creates a Kubernetes client and checks if the given namespace exists. If it does not exist, creates it. // If unsuccessful, returns an error. -func (i *Impl) ValidateNamespace(ctx context.Context, namespace string) error { - client, _, err := kubernetes.NewClientset("") +func (i *Impl) ValidateNamespace(ctx context.Context, namespace string, workspace workspaces.Workspace) error { + // get the current kubernetes context from the workspace + kubernetesContext, hasContext := workspace.KubernetesContext() + if !hasContext { + return clierrors.Message("no kubernetes context found in the current workspace") + } + client, _, err := kubernetes.NewClientset(kubernetesContext) if err != nil { return err }