Skip to content

Commit

Permalink
Accept single string as command in the okteto manifest (#916) (#919)
Browse files Browse the repository at this point in the history
* Accept single string as command in the okteto manifest

Signed-off-by: Pablo Chico de Guzman <pchico83@gmail.com>

* Remove unneeded check from marshalling

Signed-off-by: Pablo Chico de Guzman <pchico83@gmail.com>
  • Loading branch information
pchico83 committed Jun 16, 2020
1 parent 7210fb5 commit 46d541e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 25 deletions.
24 changes: 2 additions & 22 deletions cmd/login.go
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand All @@ -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.")
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/namespace/create.go
Expand Up @@ -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)
}

Expand Down
20 changes: 18 additions & 2 deletions cmd/namespace/namespace.go
Expand Up @@ -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",
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions pkg/cmd/login/login.go
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 46d541e

Please sign in to comment.