Skip to content

Commit

Permalink
Fix master build (#1977)
Browse files Browse the repository at this point in the history
* Fix master build

Signed-off-by: Javier López Barba <javier@okteto.com>

* Add test no namespace found

Signed-off-by: Javier López Barba <javier@okteto.com>

* Fix script lint

Signed-off-by: Javier López Barba <javier@okteto.com>

* Fix script linter

Signed-off-by: Javier López Barba <javier@okteto.com>
  • Loading branch information
jLopezbarb committed Nov 16, 2021
1 parent e015a1a commit 24f194e
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 21 deletions.
8 changes: 6 additions & 2 deletions cmd/context/create.go
Expand Up @@ -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
}
Expand Down
43 changes: 41 additions & 2 deletions cmd/context/create_test.go
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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

Expand Down
9 changes: 3 additions & 6 deletions cmd/utils/okteto.go
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions internal/test/okteto_client.go
Expand Up @@ -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
Expand All @@ -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
}
13 changes: 6 additions & 7 deletions pkg/okteto/namespace.go
Expand Up @@ -19,6 +19,7 @@ import (
"regexp"

"github.com/okteto/okteto/pkg/errors"
"github.com/okteto/okteto/pkg/types"
"github.com/shurcooL/graphql"
)

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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),
})
Expand Down
5 changes: 5 additions & 0 deletions pkg/types/interface.go
Expand Up @@ -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)
}
20 changes: 20 additions & 0 deletions 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"`
}
2 changes: 1 addition & 1 deletion rollback_actions.sh
Expand Up @@ -22,7 +22,7 @@ actionsRepos=(delete-namespace
destroy-stack
apply
context
)
)

for repo in "${actionsRepos[@]}"; do
echo "$repo"
Expand Down
3 changes: 2 additions & 1 deletion update_actions.sh
Expand Up @@ -21,7 +21,8 @@ actionsRepos=(delete-namespace
login
destroy-stack
apply
context)
context
)

for repo in "${actionsRepos[@]}"; do
echo "$repo"
Expand Down

0 comments on commit 24f194e

Please sign in to comment.