diff --git a/cmd/context/create.go b/cmd/context/create.go index ae64a97bf0f1..bc419cb40b4a 100644 --- a/cmd/context/create.go +++ b/cmd/context/create.go @@ -137,8 +137,12 @@ func (c *ContextUse) UseContext(ctx context.Context, ctxOptions *ContextOptions) return err } } - if ctxOptions.isOkteto && ctxOptions.Save { - hasAccess, err := utils.HasAccessToNamespace(ctx, ctxOptions.Namespace) + if ctxOptions.IsOkteto && ctxOptions.Save { + client, err := c.oktetoClientProvider.NewOktetoNamespaceClient() + if err != nil { + return err + } + hasAccess, err := utils.HasAccessToNamespace(ctx, ctxOptions.Namespace, client) if err != nil { return err } diff --git a/cmd/context/create_test.go b/cmd/context/create_test.go index 15507a64f7f6..a7a862967ea3 100644 --- a/cmd/context/create_test.go +++ b/cmd/context/create_test.go @@ -40,6 +40,46 @@ func Test_createContext(t *testing.T) { user *types.User fakeObjects []runtime.Object }{ + { + name: "change namespace", + ctxStore: &okteto.OktetoContextStore{ + Contexts: map[string]*okteto.OktetoContext{ + "https://okteto.cloud.com": {}, + }, + CurrentContext: "https://okteto.cloud.com", + }, + ctxOptions: &ContextOptions{ + IsOkteto: true, + Save: true, + Context: "https://okteto.cloud.com", + Namespace: "test", + }, + user: &types.User{ + Token: "test", + }, + kubeconfigCtx: kubeconfigFields{[]string{"cloud_okteto_com"}, []string{"test"}, ""}, + expectedErr: false, + }, + { + name: "change namespace forbidden", + ctxStore: &okteto.OktetoContextStore{ + Contexts: map[string]*okteto.OktetoContext{ + "https://okteto.cloud.com": {}, + }, + CurrentContext: "https://okteto.cloud.com", + }, + ctxOptions: &ContextOptions{ + IsOkteto: true, + Save: true, + Context: "https://okteto.cloud.com", + Namespace: "not-found", + }, + user: &types.User{ + Token: "test", + }, + kubeconfigCtx: kubeconfigFields{[]string{"cloud_okteto_com"}, []string{"test"}, ""}, + expectedErr: true, + }, { name: "transform k8s to url and create okteto context -> namespace with label", ctxStore: &okteto.OktetoContextStore{ @@ -186,7 +226,6 @@ func Test_createContext(t *testing.T) { kubeconfigCtx: kubeconfigFields{[]string{"cloud_okteto_com"}, []string{"test"}, ""}, expectedErr: false, }, - { name: "empty ctx create url", ctxStore: &okteto.OktetoContextStore{ @@ -215,7 +254,7 @@ func Test_createContext(t *testing.T) { ctxController := ContextUse{ k8sClientProvider: test.NewFakeK8sProvider(tt.fakeObjects), loginController: test.NewFakeLoginController(tt.user, nil), - oktetoClientProvider: test.NewFakeOktetoClientProvider(&types.UserContext{User: *tt.user}, nil), + oktetoClientProvider: test.NewFakeOktetoClientProvider(&types.UserContext{User: *tt.user}, []types.Namespace{{ID: "test"}}, nil), } okteto.CurrentStore = tt.ctxStore diff --git a/cmd/utils/okteto.go b/cmd/utils/okteto.go index ced280236289..4cca55abfb30 100644 --- a/cmd/utils/okteto.go +++ b/cmd/utils/okteto.go @@ -16,14 +16,11 @@ package utils import ( "context" - "github.com/okteto/okteto/pkg/okteto" + "github.com/okteto/okteto/pkg/types" ) -func HasAccessToNamespace(ctx context.Context, namespace string) (bool, error) { - oktetoClient, err := okteto.NewOktetoClient() - if err != nil { - return false, nil - } +func HasAccessToNamespace(ctx context.Context, namespace string, oktetoClient types.NamespaceInterface) (bool, error) { + nList, err := oktetoClient.ListNamespaces(ctx) if err != nil { return false, err diff --git a/internal/test/okteto_client.go b/internal/test/okteto_client.go index 85f9d97bfbba..6b91f728deae 100644 --- a/internal/test/okteto_client.go +++ b/internal/test/okteto_client.go @@ -22,16 +22,21 @@ import ( type FakeOktetoClientProvider struct { UserContext *types.UserContext Err error + Namespaces []types.Namespace } -func NewFakeOktetoClientProvider(userContext *types.UserContext, err error) *FakeOktetoClientProvider { - return &FakeOktetoClientProvider{UserContext: userContext, Err: err} +func NewFakeOktetoClientProvider(userContext *types.UserContext, namespaces []types.Namespace, err error) *FakeOktetoClientProvider { + return &FakeOktetoClientProvider{UserContext: userContext, Namespaces: namespaces, Err: err} } func (f FakeOktetoClientProvider) NewOktetoUserClient() (types.UserInterface, error) { return FakeUserClient{UserContext: f.UserContext, err: f.Err}, nil } +func (f FakeOktetoClientProvider) NewOktetoNamespaceClient() (types.NamespaceInterface, error) { + return FakeNamespaceClient{namespaces: f.Namespaces}, nil +} + type FakeUserClient struct { UserContext *types.UserContext err error @@ -41,3 +46,13 @@ type FakeUserClient struct { func (f FakeUserClient) GetUserContext(ctx context.Context) (*types.UserContext, error) { return f.UserContext, f.err } + +type FakeNamespaceClient struct { + namespaces []types.Namespace + err error +} + +// GetUserContext get user context +func (f FakeNamespaceClient) ListNamespaces(ctx context.Context) ([]types.Namespace, error) { + return f.namespaces, f.err +} diff --git a/pkg/okteto/namespace.go b/pkg/okteto/namespace.go index 0ba072b02219..449048e1b6f8 100644 --- a/pkg/okteto/namespace.go +++ b/pkg/okteto/namespace.go @@ -19,6 +19,7 @@ import ( "regexp" "github.com/okteto/okteto/pkg/errors" + "github.com/okteto/okteto/pkg/types" "github.com/shurcooL/graphql" ) @@ -27,10 +28,8 @@ const ( MAX_ALLOWED_CHARS = 63 ) -//Namespace represents an Okteto k8s namespace -type Namespace struct { - ID string `json:"id" yaml:"id"` - Sleeping bool `json:"sleeping" yaml:"sleeping"` +func (c OktetoClientProvider) NewOktetoNamespaceClient() (types.NamespaceInterface, error) { + return NewOktetoClient() } // CreateNamespace creates a namespace @@ -52,7 +51,7 @@ func (c *OktetoClient) CreateNamespace(ctx context.Context, namespace string) (s } // ListNamespaces list namespaces -func (c *OktetoClient) ListNamespaces(ctx context.Context) ([]Namespace, error) { +func (c *OktetoClient) ListNamespaces(ctx context.Context) ([]types.Namespace, error) { var query struct { Spaces []struct { Id graphql.String @@ -65,9 +64,9 @@ func (c *OktetoClient) ListNamespaces(ctx context.Context) ([]Namespace, error) return nil, err } - result := make([]Namespace, 0) + result := make([]types.Namespace, 0) for _, space := range query.Spaces { - result = append(result, Namespace{ + result = append(result, types.Namespace{ ID: string(space.Id), Sleeping: bool(space.Sleeping), }) diff --git a/pkg/types/interface.go b/pkg/types/interface.go index 50fe7001e7c8..f2743adc5e78 100644 --- a/pkg/types/interface.go +++ b/pkg/types/interface.go @@ -19,6 +19,11 @@ type UserInterface interface { GetUserContext(ctx context.Context) (*UserContext, error) } +type NamespaceInterface interface { + ListNamespaces(ctx context.Context) ([]Namespace, error) +} + type OktetoUserClientProvider interface { NewOktetoUserClient() (UserInterface, error) + NewOktetoNamespaceClient() (NamespaceInterface, error) } diff --git a/pkg/types/namespace.go b/pkg/types/namespace.go new file mode 100644 index 000000000000..79c21bf11677 --- /dev/null +++ b/pkg/types/namespace.go @@ -0,0 +1,20 @@ +// Copyright 2021 The Okteto Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package types + +//Namespace represents an Okteto k8s namespace +type Namespace struct { + ID string `json:"id" yaml:"id"` + Sleeping bool `json:"sleeping" yaml:"sleeping"` +} diff --git a/rollback_actions.sh b/rollback_actions.sh index 1749d3182afd..ce103b2742f3 100755 --- a/rollback_actions.sh +++ b/rollback_actions.sh @@ -22,7 +22,7 @@ actionsRepos=(delete-namespace destroy-stack apply context - ) +) for repo in "${actionsRepos[@]}"; do echo "$repo" diff --git a/update_actions.sh b/update_actions.sh index 2c5506ed2123..f5dcffbbd0d4 100755 --- a/update_actions.sh +++ b/update_actions.sh @@ -21,7 +21,8 @@ actionsRepos=(delete-namespace login destroy-stack apply - context) + context +) for repo in "${actionsRepos[@]}"; do echo "$repo"