-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add cap open command to open up k8s console in browser
- Loading branch information
Showing
14 changed files
with
340 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
api/protobuf-spec/softleader/captainkube/v2/console_url.proto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
syntax = "proto3"; | ||
|
||
// 依照 https://cloud.google.com/apis/design/naming_convention 規範 | ||
package softleader.captainkube.v2; | ||
|
||
// Specifies Java package name, using the standard prefix "tw.com." | ||
option java_package = "tw.com.softleader.captainkube.v2"; | ||
option go_package = "captainkube.v2"; | ||
|
||
message ConsoleURLRequest { | ||
string host = 1; | ||
} | ||
|
||
message ConsoleURLResponse { | ||
string vendor = 1; | ||
string url = 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package app | ||
|
||
import ( | ||
"github.com/pkg/browser" | ||
"github.com/sirupsen/logrus" | ||
"github.com/softleader/captain-kube/pkg/captain" | ||
"github.com/softleader/captain-kube/pkg/ctx" | ||
"github.com/softleader/captain-kube/pkg/proto" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type openCmd struct { | ||
endpoint *ctx.Endpoint | ||
} | ||
|
||
func newOpenCmd() *cobra.Command { | ||
c := openCmd{ | ||
endpoint: activeCtx.Endpoint, | ||
} | ||
|
||
cmd := &cobra.Command{ | ||
Use: "open", | ||
Short: "open endpoint Kubernetes console in browser", | ||
Long: "Open endpoint Kubernetes console in browser", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if err := c.endpoint.Validate(); err != nil { | ||
return err | ||
} | ||
return c.run() | ||
}, | ||
} | ||
|
||
f := cmd.Flags() | ||
c.endpoint.AddFlags(f) | ||
|
||
return cmd | ||
} | ||
|
||
func (c *openCmd) run() error { | ||
resp, err := captain.ConsoleURL( | ||
logrus.StandardLogger(), | ||
c.endpoint.String(), | ||
&captainkube_v2.ConsoleURLRequest{ | ||
Host: c.endpoint.Host, | ||
}, | ||
settings.Timeout) | ||
if err != nil { | ||
return err | ||
} | ||
url := resp.GetUrl() | ||
logrus.Debugf("opening %q in browser", url) | ||
return browser.OpenURL(url) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/softleader/captain-kube/pkg/proto" | ||
) | ||
|
||
func (s *CaptainServer) ConsoleURL(ctx context.Context, req *captainkube_v2.ConsoleURLRequest) (*captainkube_v2.ConsoleURLResponse, error) { | ||
resp := &captainkube_v2.ConsoleURLResponse{ | ||
Vendor: s.K8s, | ||
} | ||
switch v := s.K8s; v { | ||
case "icp": | ||
resp.Url = fmt.Sprintf("https://%s:%v", req.GetHost(), 8443) | ||
return resp, nil | ||
case "gcp": | ||
return nil, fmt.Errorf("GCP is not supported yet") | ||
default: | ||
return nil, fmt.Errorf("unsupported kubernetes vendor: %v", v) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package captain | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/sirupsen/logrus" | ||
"github.com/softleader/captain-kube/pkg/dur" | ||
"github.com/softleader/captain-kube/pkg/proto" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
func ConsoleURL(log *logrus.Logger, url string, req *captainkube_v2.ConsoleURLRequest, timeout int64) (*captainkube_v2.ConsoleURLResponse, error) { | ||
log.Debugf("dialing %q with insecure", url) | ||
conn, err := grpc.Dial(url, grpc.WithInsecure()) | ||
if err != nil { | ||
return nil, fmt.Errorf("did not connect: %v", err) | ||
} | ||
defer conn.Close() | ||
c := captainkube_v2.NewCaptainClient(conn) | ||
deadline := dur.Deadline(timeout) | ||
log.Debugf("setting context with timeout %v", deadline) | ||
ctx, cancel := context.WithTimeout(context.Background(), deadline) | ||
defer cancel() | ||
resq, err := c.ConsoleURL(ctx, req) | ||
if err != nil { | ||
return nil, fmt.Errorf("%v.ConsoleURL(%v) = _, %v", c, req, err) | ||
} | ||
return resq, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.