Skip to content

Commit

Permalink
add cap open command to open up k8s console in browser
Browse files Browse the repository at this point in the history
  • Loading branch information
shihyuho committed Jan 19, 2019
1 parent 756413f commit b1baa69
Show file tree
Hide file tree
Showing 14 changed files with 340 additions and 41 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protoc:
protoc -I $(protobuf) --go_out=plugins=grpc:$(proto_dst) $(protobuf)/msg.proto
protoc -I $(protobuf) --go_out=plugins=grpc:$(proto_dst) $(protobuf)/prune.proto
protoc -I $(protobuf) --go_out=plugins=grpc:$(proto_dst) $(protobuf)/version.proto
protoc -I $(protobuf) --go_out=plugins=grpc:$(proto_dst) $(protobuf)/console_url.proto

.PHONY: build
build: clean bootstrap build-caplet build-captain build-ui build-capctl
Expand Down
3 changes: 3 additions & 0 deletions api/protobuf-spec/softleader/captainkube/v2/captain.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import "image.proto";
import "msg.proto";
import "version.proto";
import "prune.proto";
import "console_url.proto";

message SyncChartRequest {
Chart chart = 1;
Expand Down Expand Up @@ -64,4 +65,6 @@ service Captain {
}
rpc Version (VersionRequest) returns (stream ChunkMessage) {
}
rpc ConsoleURL (ConsoleURLRequest) returns (ConsoleURLResponse) {
}
}
17 changes: 17 additions & 0 deletions api/protobuf-spec/softleader/captainkube/v2/console_url.proto
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;
}
1 change: 0 additions & 1 deletion api/protobuf-spec/softleader/captainkube/v2/version.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ option java_package = "tw.com.softleader.captainkube.v2";
option go_package = "captainkube.v2";

message VersionRequest {

bool full = 1;
int64 timeout = 2;
bool color = 3;
Expand Down
1 change: 1 addition & 0 deletions cmd/capctl/app/capctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func NewRootCmd(args []string, m *version.BuildMetadata) (*cobra.Command, error)
newReTagCmd(),
newSyncCmd(),
newSaveCmd(),
newOpenCmd(),
)

flags.Parse(args)
Expand Down
53 changes: 53 additions & 0 deletions cmd/capctl/app/open.go
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)
}
22 changes: 22 additions & 0 deletions cmd/captain/app/server/captain_console_url.go
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)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/nwaples/rardecode v1.0.0 // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/pierrec/lz4 v2.0.5+incompatible // indirect
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
github.com/pkg/errors v0.8.0
github.com/pmezard/go-difflib v1.0.0
github.com/sirupsen/logrus v1.2.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pierrec/lz4 v2.0.5+incompatible h1:2xWsjqPFWcplujydGg4WmhC/6fZqK42wMM8aXeqhl0I=
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4 h1:49lOXmGaUpV9Fz3gd7TFZY106KVlPVa5jcYD1gaQf98=
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4/go.mod h1:4OwLy04Bl9Ef3GJJCoec+30X3LQs/0/m4HFRt/2LUSA=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
29 changes: 29 additions & 0 deletions pkg/captain/captain_console_url.go
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
}
7 changes: 5 additions & 2 deletions pkg/ctx/flagable_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ func (e *Endpoint) String() string {
}

func (e *Endpoint) Specified() bool {
return len(e.Host) > 0 && e.Port > 0
return e.Validate() == nil
}

func (e *Endpoint) Validate() error {
if !e.Specified() {
if len(e.Host) == 0 {
return errors.New("endpoint is required")
}
if e.Port == 0 {
return errors.New("endpoint port is required")
}
return nil
}
2 changes: 1 addition & 1 deletion pkg/dur/dur.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dur

import "time"

var DefaultDeadlineSecond int64 = 300
var DefaultDeadlineSecond int64 = 1800

func Deadline(timeout int64) time.Duration {
if timeout <= 0 {
Expand Down
111 changes: 74 additions & 37 deletions pkg/proto/captain.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b1baa69

Please sign in to comment.