-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ebd3e35
commit 9c6da64
Showing
13 changed files
with
624 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package app | ||
|
||
import ( | ||
"errors" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/adrianliechti/loop/pkg/cli" | ||
"github.com/adrianliechti/loop/pkg/system" | ||
) | ||
|
||
var PortFlag = &cli.IntFlag{ | ||
Name: "port", | ||
Usage: "port", | ||
} | ||
|
||
func Port(c *cli.Context) int { | ||
return c.Int(PortFlag.Name) | ||
} | ||
|
||
func MustPort(c *cli.Context) int { | ||
port := Port(c) | ||
|
||
if port <= 0 { | ||
cli.Fatal(errors.New("port missing")) | ||
} | ||
|
||
return port | ||
} | ||
|
||
var PortsFlag = &cli.StringSliceFlag{ | ||
Name: "port", | ||
Usage: "port mappings", | ||
} | ||
|
||
func Ports(c *cli.Context) (map[int]int, error) { | ||
s := c.StringSlice(PortsFlag.Name) | ||
|
||
result := map[int]int{} | ||
|
||
for _, p := range s { | ||
pair := strings.Split(p, ":") | ||
|
||
if len(pair) > 2 { | ||
return nil, errors.New("invalid port mapping") | ||
} | ||
|
||
if len(pair) == 1 { | ||
pair = []string{pair[0], pair[0]} | ||
} | ||
|
||
source, err := strconv.Atoi(pair[0]) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
target, err := strconv.Atoi(pair[1]) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
result[source] = target | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func MustPorts(c *cli.Context) map[int]int { | ||
ports, err := Ports(c) | ||
|
||
if err != nil { | ||
cli.Fatal(err) | ||
} | ||
|
||
if len(ports) == 0 { | ||
cli.Fatal(errors.New("ports missing")) | ||
} | ||
|
||
return ports | ||
} | ||
|
||
func PortOrRandom(c *cli.Context, preference int) (int, error) { | ||
port := Port(c) | ||
|
||
if port > 0 { | ||
return port, nil | ||
} | ||
|
||
return system.FreePort(preference) | ||
} | ||
|
||
func MustPortOrRandom(c *cli.Context, preference int) int { | ||
port, err := PortOrRandom(c, preference) | ||
|
||
if err != nil { | ||
cli.Fatal(err) | ||
} | ||
|
||
return port | ||
} | ||
|
||
func RandomPort(c *cli.Context, preference int) (int, error) { | ||
return system.FreePort(preference) | ||
} | ||
|
||
func MustRandomPort(c *cli.Context, preference int) int { | ||
port, err := RandomPort(c, preference) | ||
|
||
if err != nil { | ||
cli.Fatal(err) | ||
} | ||
|
||
return port | ||
} |
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,73 @@ | ||
package connect | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/adrianliechti/devkube/app" | ||
"github.com/adrianliechti/devkube/pkg/cli" | ||
"github.com/adrianliechti/loop/pkg/catapult" | ||
"github.com/adrianliechti/loop/pkg/kubernetes" | ||
"github.com/adrianliechti/loop/pkg/system" | ||
) | ||
|
||
func Command() *cli.Command { | ||
return &cli.Command{ | ||
Name: "connect", | ||
Usage: "forward cluster workload traffic", | ||
|
||
Flags: []cli.Flag{ | ||
&cli.StringSliceFlag{ | ||
Name: "namespace", | ||
Usage: "filter namespace(s)", | ||
}, | ||
|
||
&cli.StringFlag{ | ||
Name: "scope", | ||
Usage: "set namespace scope", | ||
}, | ||
}, | ||
|
||
Action: func(c *cli.Context) error { | ||
elevated, err := system.IsElevated() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
if !elevated { | ||
cli.Fatal("This command must be run as root!") | ||
} | ||
|
||
client := app.MustClient(c) | ||
|
||
var tunnelScope string = "default" | ||
var tunnelNamespaces []string = nil | ||
|
||
if val := c.StringSlice("namespace"); len(val) > 0 { | ||
tunnelScope = val[0] | ||
tunnelNamespaces = val | ||
} | ||
|
||
if val := c.String("scope"); val != "" { | ||
tunnelScope = val | ||
} | ||
|
||
return Catapult(c.Context, client, tunnelNamespaces, tunnelScope) | ||
}, | ||
} | ||
} | ||
|
||
func Catapult(ctx context.Context, client kubernetes.Client, namespaces []string, scope string) error { | ||
catapult, err := catapult.New(client, catapult.CatapultOptions{ | ||
Scope: scope, | ||
Namespaces: namespaces, | ||
|
||
IncludeIngress: true, | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return catapult.Start(ctx) | ||
} |
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,32 @@ | ||
package grafana | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/adrianliechti/devkube/app" | ||
"github.com/adrianliechti/devkube/pkg/cli" | ||
) | ||
|
||
func Command() *cli.Command { | ||
return &cli.Command{ | ||
Name: "grafana", | ||
Usage: "Open Grafana", | ||
|
||
Action: func(c *cli.Context) error { | ||
client := app.MustClient(c) | ||
|
||
port := app.MustPortOrRandom(c, 3000) | ||
|
||
ready := make(chan struct{}) | ||
|
||
go func() { | ||
<-ready | ||
|
||
url := fmt.Sprintf("http://127.0.0.1:%d", port) | ||
cli.OpenURL(url) | ||
}() | ||
|
||
return client.ServicePortForward(c.Context, "monitoring", "grafana", "", map[int]int{port: 3000}, ready) | ||
}, | ||
} | ||
} |
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
Oops, something went wrong.