diff --git a/cmd/login.go b/cmd/login.go index 118037929e01..b97a751b2c8b 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -25,7 +25,6 @@ import ( k8Client "github.com/okteto/okteto/pkg/k8s/client" "github.com/okteto/okteto/pkg/log" "github.com/okteto/okteto/pkg/okteto" - "github.com/skratchdot/open-golang/open" "github.com/spf13/cobra" ) @@ -74,7 +73,7 @@ to log in to a Okteto Enterprise instance running at okteto.example.com. u, err = login.WithToken(ctx, oktetoURL, token) } else { log.Debugf("authenticating with the browser") - u, err = withBrowser(ctx, oktetoURL) + u, err = login.WithBrowser(ctx, oktetoURL) } if err != nil { @@ -90,7 +89,7 @@ to log in to a Okteto Enterprise instance running at okteto.example.com. log.Success("Logged in as %s @ %s", u.GithubID, oktetoURL) } - err = namespace.RunNamespace(ctx, "") + err = namespace.RunNamespace(ctx, "", "") if err != nil { log.Infof("error fetching your Kubernetes credentials: %s", err) log.Hint(" Run `okteto namespace` to switch your context and download your Kubernetes credentials.") @@ -109,25 +108,6 @@ to log in to a Okteto Enterprise instance running at okteto.example.com. return cmd } -func withBrowser(ctx context.Context, oktetoURL string) (*okteto.User, error) { - h, err := login.StartWithBrowser(ctx, oktetoURL) - if err != nil { - log.Infof("couldn't start the login process: %s", err) - return nil, fmt.Errorf("couldn't start the login process, please try again") - } - - authorizationURL := h.AuthorizationURL() - fmt.Println("Authentication will continue in your default browser") - if err := open.Start(authorizationURL); err != nil { - log.Errorf("Something went wrong opening your browser: %s\n", err) - } - - fmt.Printf("You can also open a browser and navigate to the following address:\n") - fmt.Println(authorizationURL) - - return login.EndWithBrowser(ctx, h) -} - func parseURL(u string) (string, error) { url, err := url.Parse(u) if err != nil { diff --git a/cmd/namespace/create.go b/cmd/namespace/create.go index 6c11210f19d4..8652137d3b46 100644 --- a/cmd/namespace/create.go +++ b/cmd/namespace/create.go @@ -68,7 +68,7 @@ func executeCreateNamespace(ctx context.Context, namespace string, members *[]st } } - if err := RunNamespace(ctx, namespace); err != nil { + if err := RunNamespace(ctx, namespace, ""); err != nil { return fmt.Errorf("failed to activate your new namespace: %s", err) } diff --git a/cmd/namespace/namespace.go b/cmd/namespace/namespace.go index 9099cfb96858..1b6c091d0018 100644 --- a/cmd/namespace/namespace.go +++ b/cmd/namespace/namespace.go @@ -28,6 +28,7 @@ import ( //Namespace fetch credentials for a cluster namespace func Namespace(ctx context.Context) *cobra.Command { + var oktetoURL string cmd := &cobra.Command{ Use: "namespace [name]", Short: "Downloads k8s credentials for a namespace", @@ -43,16 +44,31 @@ func Namespace(ctx context.Context) *cobra.Command { return err } - err := RunNamespace(ctx, namespace) + err := RunNamespace(ctx, namespace, oktetoURL) analytics.TrackNamespace(err == nil) return err }, } + cmd.Flags().StringVarP(&oktetoURL, "url", "u", "", "Okteto URL (optional)") return cmd } //RunNamespace starts the kubeconfig sequence -func RunNamespace(ctx context.Context, namespace string) error { +func RunNamespace(ctx context.Context, namespace, oktetoURL string) error { + if oktetoURL != "" && okteto.GetURL() != oktetoURL { + u, err := login.WithBrowser(ctx, oktetoURL) + if err != nil { + return err + } + log.Infof("authenticated user %s", u.ID) + + if oktetoURL == okteto.CloudURL { + log.Success("Logged in as %s", u.GithubID) + } else { + log.Success("Logged in as %s @ %s", u.GithubID, oktetoURL) + } + } + cred, err := okteto.GetCredentials(ctx, namespace) if err != nil { return err diff --git a/pkg/cmd/login/login.go b/pkg/cmd/login/login.go index 61cc25fa7474..913758baf978 100644 --- a/pkg/cmd/login/login.go +++ b/pkg/cmd/login/login.go @@ -23,6 +23,7 @@ import ( "github.com/okteto/okteto/pkg/log" "github.com/okteto/okteto/pkg/model" "github.com/okteto/okteto/pkg/okteto" + "github.com/skratchdot/open-golang/open" ) // WithEnvVarIfAvailable authenticates the user with OKTETO_TOKEN value @@ -51,6 +52,26 @@ func WithToken(ctx context.Context, url, token string) (*okteto.User, error) { return okteto.AuthWithToken(ctx, url, token) } +//WithBrowser authenticates the user with the brower +func WithBrowser(ctx context.Context, oktetoURL string) (*okteto.User, error) { + h, err := StartWithBrowser(ctx, oktetoURL) + if err != nil { + log.Infof("couldn't start the login process: %s", err) + return nil, fmt.Errorf("couldn't start the login process, please try again") + } + + authorizationURL := h.AuthorizationURL() + fmt.Println("Authentication will continue in your default browser") + if err := open.Start(authorizationURL); err != nil { + log.Errorf("Something went wrong opening your browser: %s\n", err) + } + + fmt.Printf("You can also open a browser and navigate to the following address:\n") + fmt.Println(authorizationURL) + + return EndWithBrowser(ctx, h) +} + // StartWithBrowser starts the authentication of the user with the IDP via a browser func StartWithBrowser(ctx context.Context, url string) (*Handler, error) { state, err := randToken()