Skip to content

Commit

Permalink
added multi app support and proxy context injection
Browse files Browse the repository at this point in the history
  • Loading branch information
xadhatter committed Oct 23, 2023
1 parent de0ad86 commit 08f33df
Show file tree
Hide file tree
Showing 33 changed files with 468 additions and 538 deletions.
17 changes: 9 additions & 8 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,28 @@ import (
)

var cfgCmd = &cobra.Command{
Use: "config",
Args: cobra.NoArgs,
PersistentPreRun: setup,
Short: "Configure 🦊 Fox",
Use: "config",
Args: cobra.NoArgs,
Short: "Configure 🦊 Fox",
Long: `
Use the config subcommand to help setup your local environment.
`,
}

var cfgShowCmd = &cobra.Command{
Use: "show",
Args: cobra.NoArgs,
Use: "show",
Args: cobra.NoArgs,
PreRun: setup,
Run: func(cmd *cobra.Command, args []string) {
log.Marshal(cfg)
},
Short: "Show the current configuration",
}

var cfgSetupCmd = &cobra.Command{
Use: "setup",
Args: cobra.NoArgs,
Use: "setup",
Args: cobra.NoArgs,
PreRun: setup,
Run: func(cmd *cobra.Command, args []string) {
if !cfg.Fresh {
cfg.Setup()
Expand Down
3 changes: 2 additions & 1 deletion cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var docsCmd = &cobra.Command{
Short: "Generate docs for 🦊 Fox",
Long: `
Run this command to automatically generate 🦊 Fox documentation. Output is
placed in the subdirectory docs of the working directory.`,
placed in the subdirectory docs of the working directory.
`,
}

func init() {
Expand Down
76 changes: 21 additions & 55 deletions cmd/proxy.go
Original file line number Diff line number Diff line change
@@ -1,90 +1,56 @@
package cmd

import (
"context"
"errors"
"os"
"os/signal"
"strconv"
"time"

"github.com/spf13/cobra"
"github.com/xigxog/fox/internal/kubernetes"
"github.com/xigxog/fox/internal/log"
"github.com/xigxog/fox/internal/proxy"
)

var proxyCmd = &cobra.Command{
Use: "proxy [local port]",
Args: cobra.ExactArgs(1),
PreRun: setup,
Run: proxy,
Run: runProxy,
Short: "Port forward local port to broker's HTTP server adapter",
Long: `
The proxy command will inspect the Kubernetes cluster and find an available
broker to forward a local port to. This port can then be used to make HTTP
broker to proxy a local port to. This port can then be used to make HTTP
requests to the broker's HTTP server adapter. This is especially useful during
development and testing.
The optional flags 'env' and 'deployment' can be set which will automatically
inject the values as context to requests sent through the proxy. The context
can still be overridden manually by setting the header or query param on the
original request.
Examples:
# Port forward local port 8080 and wait if no brokers are available.
fox proxy 8080 --wait
fox proxy 8080 --wait 5m
# Port forward local port 8080 and inject 'my-env' and 'my-dep' context.
fox proxy 8080 --env my-env --deployment my-dep
http://127.0.0.1:8080/hello # uses my-env and my-deployment
http://127.0.0.1:8080/hello?kf-env=your-env # uses your-env and my-dep
http://127.0.0.1:8080/hello?kf-dep=your-dep # uses my-env and your-dep
`,
}

func init() {
proxyCmd.Flags().StringVarP(&cfg.Flags.Env, "env", "e", "", "environment to add to proxied requests")
proxyCmd.Flags().StringVarP(&cfg.Flags.Deployment, "deployment", "d", "", "deployment to add to proxied requests")

addCommonDeployFlags(proxyCmd)
rootCmd.AddCommand(proxyCmd)
}

func proxy(cmd *cobra.Command, args []string) {
func runProxy(cmd *cobra.Command, args []string) {
port, err := strconv.Atoi(args[0])
if err != nil {
log.Fatal("Error invalid local port '%s'.", args[0])
}

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

c := kubernetes.NewClient(cfg)

p, err := c.GetPlatform(ctx)
if err != nil {
log.Fatal("Error getting platform :%v", err)
}

pfReq := &kubernetes.PortForwardRequest{
Namespace: p.Namespace,
Platform: p.Name,
LocalPort: int32(port),
}
pf, err := c.PortForward(ctx, pfReq)
if errors.Is(err, kubernetes.ErrComponentNotRead) && cfg.Flags.WaitTime > 0 {
log.Warn("No broker pod is available.")
log.Info("Waiting for broker pod to become available...")

ctx, cancel := context.WithTimeout(context.Background(), cfg.Flags.WaitTime)
defer cancel()

err = c.WaitPodReady(ctx, p, "broker", "")
if err == nil {
pf, err = c.PortForward(ctx, pfReq)
}
}
if err != nil {
log.Fatal("Error starting proxy: %v", err)
}

interruptCh := make(chan os.Signal, 1)
signal.Notify(interruptCh, os.Interrupt)
go func() {
<-interruptCh
pf.Stop()
}()

log.Info("The proxy is ready. You can now make HTTP requests on '127.0.0.1:%d'. If you are", port)
log.Info("working on the quickstart try opening 'http://127.0.0.1:8080/hello' in your")
log.Info("browser. If you get 'route not found' you probably haven't released the app yet.")
log.Info("Try adding context to the request, 'http://localhost:30080/hello?kf-dep=my-deployment&kf-env=world'")
log.Printf("HTTP proxy started on 127.0.0.1:%d\n", port)
<-pf.Done()
proxy.Start(port, cfg)
}
19 changes: 7 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"os"
"path/filepath"
"strings"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -30,7 +28,8 @@ and release your KubeFox apps.
}

func init() {
rootCmd.PersistentFlags().StringVarP(&cfg.Flags.RepoPath, "repo", "r", pwd(), "path of Git repository to operate against")
rootCmd.PersistentFlags().StringVarP(&cfg.Flags.RepoPath, "repo", "r", "", "path to directory containing Git repository")
rootCmd.PersistentFlags().StringVarP(&cfg.Flags.AppPath, "app", "a", "", "path to directory containing KubeFox app")
rootCmd.PersistentFlags().StringVarP(&cfg.Flags.OutFormat, "output", "o", "yaml", `output format, one of ["json", "yaml"]`)
rootCmd.PersistentFlags().BoolVarP(&cfg.Flags.Info, "info", "i", false, "enable info output")
rootCmd.PersistentFlags().BoolVarP(&cfg.Flags.Verbose, "verbose", "v", false, "enable verbose output")
Expand All @@ -50,6 +49,8 @@ func initViper(cmd *cobra.Command, args []string) {
}

func Execute() {
defer log.Logger().Sync()

err := rootCmd.Execute()
if err != nil {
log.Fatal("%v", err)
Expand All @@ -63,17 +64,11 @@ func setup(cmd *cobra.Command, args []string) {
ctrl.SetLogger(logr.Logger{})

cfg.Load()

log.Verbose("gitCommit: %s, gitRef: %s", kubefox.GitCommit, kubefox.GitRef)
}

func pwd() string {
wd, err := os.Getwd()
if err != nil {
log.Fatal("Error getting working dir: %v", err)
if cfg.Fresh {
log.InfoNewline()
}

return filepath.Clean(wd)
log.Verbose("gitCommit: %s, gitRef: %s", kubefox.GitCommit, kubefox.GitRef)
}

func getOutFormat() string {
Expand Down
5 changes: 3 additions & 2 deletions docs/fox.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ and release your KubeFox apps.
### Options

```
-a, --app string path to directory containing KubeFox app
-h, --help help for fox
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path of Git repository to operate against (default "/home/xadhatter/Workspace/src/github.com/xigxog/fox")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand All @@ -32,4 +33,4 @@ and release your KubeFox apps.
* [fox release](fox_release.md) - Release app using the version from the currently checked out Git commit
* [fox version](fox_version.md) - Show version information of 🦊 Fox

###### Auto generated by spf13/cobra on 22-Oct-2023
###### Auto generated by spf13/cobra on 23-Oct-2023
5 changes: 3 additions & 2 deletions docs/fox_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ fox build [component name] [flags]
### Options inherited from parent commands

```
-a, --app string path to directory containing KubeFox app
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path of Git repository to operate against (default "/home/xadhatter/Workspace/src/github.com/xigxog/fox")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

### SEE ALSO

* [fox](fox.md) - CLI for interacting with KubeFox

###### Auto generated by spf13/cobra on 22-Oct-2023
###### Auto generated by spf13/cobra on 23-Oct-2023
5 changes: 3 additions & 2 deletions docs/fox_completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ See each sub-command's help for details on how to use the generated script.
### Options inherited from parent commands

```
-a, --app string path to directory containing KubeFox app
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path of Git repository to operate against (default "/home/xadhatter/Workspace/src/github.com/xigxog/fox")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand All @@ -31,4 +32,4 @@ See each sub-command's help for details on how to use the generated script.
* [fox completion powershell](fox_completion_powershell.md) - Generate the autocompletion script for powershell
* [fox completion zsh](fox_completion_zsh.md) - Generate the autocompletion script for zsh

###### Auto generated by spf13/cobra on 22-Oct-2023
###### Auto generated by spf13/cobra on 23-Oct-2023
5 changes: 3 additions & 2 deletions docs/fox_completion_bash.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@ fox completion bash
### Options inherited from parent commands

```
-a, --app string path to directory containing KubeFox app
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path of Git repository to operate against (default "/home/xadhatter/Workspace/src/github.com/xigxog/fox")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

### SEE ALSO

* [fox completion](fox_completion.md) - Generate the autocompletion script for the specified shell

###### Auto generated by spf13/cobra on 22-Oct-2023
###### Auto generated by spf13/cobra on 23-Oct-2023
5 changes: 3 additions & 2 deletions docs/fox_completion_fish.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ fox completion fish [flags]
### Options inherited from parent commands

```
-a, --app string path to directory containing KubeFox app
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path of Git repository to operate against (default "/home/xadhatter/Workspace/src/github.com/xigxog/fox")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

### SEE ALSO

* [fox completion](fox_completion.md) - Generate the autocompletion script for the specified shell

###### Auto generated by spf13/cobra on 22-Oct-2023
###### Auto generated by spf13/cobra on 23-Oct-2023
5 changes: 3 additions & 2 deletions docs/fox_completion_powershell.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ fox completion powershell [flags]
### Options inherited from parent commands

```
-a, --app string path to directory containing KubeFox app
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path of Git repository to operate against (default "/home/xadhatter/Workspace/src/github.com/xigxog/fox")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

### SEE ALSO

* [fox completion](fox_completion.md) - Generate the autocompletion script for the specified shell

###### Auto generated by spf13/cobra on 22-Oct-2023
###### Auto generated by spf13/cobra on 23-Oct-2023
5 changes: 3 additions & 2 deletions docs/fox_completion_zsh.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ fox completion zsh [flags]
### Options inherited from parent commands

```
-a, --app string path to directory containing KubeFox app
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path of Git repository to operate against (default "/home/xadhatter/Workspace/src/github.com/xigxog/fox")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

### SEE ALSO

* [fox completion](fox_completion.md) - Generate the autocompletion script for the specified shell

###### Auto generated by spf13/cobra on 22-Oct-2023
###### Auto generated by spf13/cobra on 23-Oct-2023
5 changes: 3 additions & 2 deletions docs/fox_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ Use the config subcommand to help setup your local environment.
### Options inherited from parent commands

```
-a, --app string path to directory containing KubeFox app
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path of Git repository to operate against (default "/home/xadhatter/Workspace/src/github.com/xigxog/fox")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

Expand All @@ -29,4 +30,4 @@ Use the config subcommand to help setup your local environment.
* [fox config setup](fox_config_setup.md) - Run setup to configure 🦊 Fox
* [fox config show](fox_config_show.md) - Show the current configuration

###### Auto generated by spf13/cobra on 22-Oct-2023
###### Auto generated by spf13/cobra on 23-Oct-2023
5 changes: 3 additions & 2 deletions docs/fox_config_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ fox config setup [flags]
### Options inherited from parent commands

```
-a, --app string path to directory containing KubeFox app
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path of Git repository to operate against (default "/home/xadhatter/Workspace/src/github.com/xigxog/fox")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

### SEE ALSO

* [fox config](fox_config.md) - Configure 🦊 Fox

###### Auto generated by spf13/cobra on 22-Oct-2023
###### Auto generated by spf13/cobra on 23-Oct-2023
5 changes: 3 additions & 2 deletions docs/fox_config_show.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ fox config show [flags]
### Options inherited from parent commands

```
-a, --app string path to directory containing KubeFox app
-i, --info enable info output
-o, --output string output format, one of ["json", "yaml"] (default "yaml")
-r, --repo string path of Git repository to operate against (default "/home/xadhatter/Workspace/src/github.com/xigxog/fox")
-r, --repo string path to directory containing Git repository
-v, --verbose enable verbose output
```

### SEE ALSO

* [fox config](fox_config.md) - Configure 🦊 Fox

###### Auto generated by spf13/cobra on 22-Oct-2023
###### Auto generated by spf13/cobra on 23-Oct-2023
Loading

0 comments on commit 08f33df

Please sign in to comment.