Skip to content

Commit

Permalink
add grafana, and cluster tunnel
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianliechti committed Jul 6, 2024
1 parent ebd3e35 commit 9c6da64
Show file tree
Hide file tree
Showing 13 changed files with 624 additions and 63 deletions.
116 changes: 116 additions & 0 deletions app/app_port.go
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
}
73 changes: 73 additions & 0 deletions app/connect/connect.go
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)
}
40 changes: 27 additions & 13 deletions app/cluster/cluster_create.go → app/create/create.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package cluster
package create

import (
"github.com/adrianliechti/devkube/app"
"github.com/adrianliechti/devkube/extension/prometheus"
"github.com/adrianliechti/devkube/app/setup"
"github.com/adrianliechti/devkube/pkg/cli"

"github.com/adrianliechti/devkube/extension/certmanager"
"github.com/adrianliechti/devkube/extension/grafana"
"github.com/adrianliechti/devkube/extension/loki"
"github.com/adrianliechti/devkube/extension/monitoring"
"github.com/adrianliechti/devkube/extension/tempo"
)

func CreateCommand() *cli.Command {
func Command() *cli.Command {
return &cli.Command{
Name: "create",
Usage: "Create cluster",
Expand All @@ -21,12 +27,24 @@ func CreateCommand() *cli.Command {

client := app.MustClient(c)

// if err := certmanager.Ensure(c.Context, client); err != nil {
// //return err
// }
if err := certmanager.Ensure(c.Context, client); err != nil {
return err
}

if err := prometheus.Ensure(c.Context, client); err != nil {
//return err
if err := monitoring.Ensure(c.Context, client); err != nil {
return err
}

if err := loki.Ensure(c.Context, client); err != nil {
return err
}

if err := tempo.Ensure(c.Context, client); err != nil {
return err
}

if err := grafana.Ensure(c.Context, client); err != nil {
return err
}

// if err := metrics.Install(c.Context, kubeconfig, app.DefaultNamespace); err != nil {
Expand All @@ -45,11 +63,7 @@ func CreateCommand() *cli.Command {
// return err
// }

// if err := observability.Install(c.Context, kubeconfig, app.DefaultNamespace); err != nil {
// return err
// }

return ExportConfig(c.Context, provider, cluster, "")
return setup.Export(c.Context, provider, cluster, "")
},
}
}
4 changes: 2 additions & 2 deletions app/cluster/cluster_delete.go → app/delete/delete.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cluster
package delete

import (
"github.com/adrianliechti/devkube/app"
"github.com/adrianliechti/devkube/pkg/cli"
)

func DeleteCommand() *cli.Command {
func Command() *cli.Command {
return &cli.Command{
Name: "delete",
Usage: "Delete cluster",
Expand Down
32 changes: 32 additions & 0 deletions app/grafana/grafana.go
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)
},
}
}
8 changes: 4 additions & 4 deletions app/cluster/cluster_setup.go → app/setup/setup.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cluster
package setup

import (
"context"
Expand All @@ -11,20 +11,20 @@ import (
"github.com/adrianliechti/loop/pkg/kubernetes"
)

func SetupCommand() *cli.Command {
func Command() *cli.Command {
return &cli.Command{
Name: "setup",
Usage: "Setup cluster",

Action: func(c *cli.Context) error {
provider, cluster := app.MustCluster(c)

return ExportConfig(c.Context, provider, cluster, "")
return Export(c.Context, provider, cluster, "")
},
}
}

func ExportConfig(ctx context.Context, provider provider.Provider, cluster string, path string) error {
func Export(ctx context.Context, provider provider.Provider, cluster string, path string) error {
if path == "" {
path = kubernetes.ConfigPath()
}
Expand Down
Loading

0 comments on commit 9c6da64

Please sign in to comment.