Skip to content

Commit

Permalink
Add error if user has not the permissions for ns (#1428)
Browse files Browse the repository at this point in the history
* Add error if user has not the permissions for ns

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

* Update message

Signed-off-by: Javier López Barba <javier@okteto.com>
  • Loading branch information
jLopezbarb committed Apr 20, 2021
1 parent 3570b78 commit 7377c87
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions cmd/namespace/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"github.com/okteto/okteto/pkg/cmd/login"
"github.com/okteto/okteto/pkg/config"
"github.com/okteto/okteto/pkg/errors"
k8Client "github.com/okteto/okteto/pkg/k8s/client"
"github.com/okteto/okteto/pkg/k8s/namespaces"
"github.com/okteto/okteto/pkg/log"
"github.com/okteto/okteto/pkg/okteto"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -87,6 +89,10 @@ func RunNamespace(ctx context.Context, namespace string) error {
kubeConfigFile := config.GetKubeConfigFile()
clusterContext := okteto.GetClusterContext()

if err = isNamespaceAvailable(ctx, namespace); err != nil {
return err
}

if err := okteto.SetKubeConfig(cred, kubeConfigFile, namespace, okteto.GetUserID(), clusterContext, true); err != nil {
return err
}
Expand Down Expand Up @@ -120,3 +126,16 @@ func askOktetoURL() (string, error) {

return oktetoURL, nil
}

func isNamespaceAvailable(ctx context.Context, namespace string) error {
client, _, err := k8Client.GetLocal()
if err != nil {
return err
}

_, err = namespaces.Get(ctx, namespace, client)
if err != nil {
return fmt.Errorf("Namespace '%s' not found. Please verify that the namespace exists and that you have access to it.", namespace)
}
return nil
}

0 comments on commit 7377c87

Please sign in to comment.