Skip to content

Commit

Permalink
Update Namespace.ValidateNamespace to add workspace to arguments (rad…
Browse files Browse the repository at this point in the history
…ius-project#7154)

Signed-off-by: Josh <[email protected]>

# Description

in order for the non current context to be used by ValidateNamespaces so
that it can create namespaces in the workspaces kubernetes context we
need to pass in the workspace and then use workspace.KuberneteContext()
to look up the correct context.

## Type of change

This pull request fixes a bug in Radius and has an approved issue (issue
link required).

Fixes: radius-project#5698

---------

Signed-off-by: Josh <[email protected]>
  • Loading branch information
jhandel authored and willdavsmith committed Mar 4, 2024
1 parent a3907cf commit 0c4456f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pkg/cli/cmd/env/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/cmd/env/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,13 @@ func createMocksWithInvalidResourceGroup(namespaceClient *namespace.MockInterfac

func createValidateNamespaceSuccess(namespaceClient *namespace.MockInterface) {
namespaceClient.EXPECT().
ValidateNamespace(gomock.Any(), "testingenv").
ValidateNamespace(gomock.Any(), "testingenv", gomock.Any()).
Return(nil).Times(1)
}

func createValidateNamespaceError(namespaceClient *namespace.MockInterface) {
namespaceClient.EXPECT().
ValidateNamespace(gomock.Any(), gomock.Any()).
ValidateNamespace(gomock.Any(), gomock.Any(), gomock.Any()).
Return(fmt.Errorf("failed to create namespace")).Times(1)
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/cli/cmd/env/namespace/mock_namespace.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions pkg/cli/cmd/env/namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit 0c4456f

Please sign in to comment.