Skip to content

Commit

Permalink
added proxy command to port forward to brokers
Browse files Browse the repository at this point in the history
  • Loading branch information
xadhatter committed Oct 22, 2023
1 parent 2484951 commit de0ad86
Show file tree
Hide file tree
Showing 27 changed files with 549 additions and 218 deletions.
18 changes: 14 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@
"request": "launch",
"mode": "auto",
"program": "main.go",
"args": ["publish",
"--verbose",
"--repo=/home/xadhatter/Workspace/src/github.com/xadhatter-org/hello-world",
"--skip-deploy"]
"args": [
"publish",
"--verbose",
"--repo=/home/xadhatter/Workspace/src/github.com/xadhatter-org/hello-world",
"--skip-deploy"
]
},
{
"name": "proxy",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "main.go",
"args": ["proxy", "--verbose", "8080"]
}
]
}
2 changes: 1 addition & 1 deletion cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func init() {
}

func addCommonDeployFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&cfg.Flags.Platform, "platform", "p", "", "platform to run components with")
cmd.Flags().StringVarP(&cfg.Flags.Namespace, "namespace", "n", "", "namespace of platform")
cmd.Flags().StringVarP(&cfg.Flags.Platform, "platform", "p", "", "name of platform to utilize")
cmd.Flags().DurationVarP(&cfg.Flags.WaitTime, "wait", "", 0, "wait up the specified time for components to be ready")
}

Expand Down
90 changes: 90 additions & 0 deletions cmd/proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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"
)

var proxyCmd = &cobra.Command{
Use: "proxy [local port]",
Args: cobra.ExactArgs(1),
PreRun: setup,
Run: proxy,
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
requests to the broker's HTTP server adapter. This is especially useful during
development and testing.
Examples:
# Port forward local port 8080 and wait if no brokers are available.
fox proxy 8080 --wait
`,
}

func init() {
addCommonDeployFlags(proxyCmd)
rootCmd.AddCommand(proxyCmd)
}

func proxy(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()
}
3 changes: 2 additions & 1 deletion docs/fox.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ and release your KubeFox apps.
* [fox deploy](fox_deploy.md) - Deploy KubeFox app using the version from the currently checked out Git commit
* [fox docs](fox_docs.md) - Generate docs for 🦊 Fox
* [fox init](fox_init.md) - Initialize a KubeFox app
* [fox proxy](fox_proxy.md) - Port forward local port to broker's HTTP server adapter
* [fox publish](fox_publish.md) - Builds, pushes, and deploys KubeFox apps using the version of the currently checked out Git commit
* [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 20-Oct-2023
###### Auto generated by spf13/cobra on 22-Oct-2023
2 changes: 1 addition & 1 deletion docs/fox_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ fox build [component name] [flags]

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

###### Auto generated by spf13/cobra on 20-Oct-2023
###### Auto generated by spf13/cobra on 22-Oct-2023
2 changes: 1 addition & 1 deletion docs/fox_completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,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 20-Oct-2023
###### Auto generated by spf13/cobra on 22-Oct-2023
2 changes: 1 addition & 1 deletion docs/fox_completion_bash.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ fox completion bash

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

###### Auto generated by spf13/cobra on 20-Oct-2023
###### Auto generated by spf13/cobra on 22-Oct-2023
2 changes: 1 addition & 1 deletion docs/fox_completion_fish.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ fox completion fish [flags]

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

###### Auto generated by spf13/cobra on 20-Oct-2023
###### Auto generated by spf13/cobra on 22-Oct-2023
2 changes: 1 addition & 1 deletion docs/fox_completion_powershell.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ fox completion powershell [flags]

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

###### Auto generated by spf13/cobra on 20-Oct-2023
###### Auto generated by spf13/cobra on 22-Oct-2023
2 changes: 1 addition & 1 deletion docs/fox_completion_zsh.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ fox completion zsh [flags]

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

###### Auto generated by spf13/cobra on 20-Oct-2023
###### Auto generated by spf13/cobra on 22-Oct-2023
2 changes: 1 addition & 1 deletion docs/fox_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,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 20-Oct-2023
###### Auto generated by spf13/cobra on 22-Oct-2023
2 changes: 1 addition & 1 deletion docs/fox_config_setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ fox config setup [flags]

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

###### Auto generated by spf13/cobra on 20-Oct-2023
###### Auto generated by spf13/cobra on 22-Oct-2023
2 changes: 1 addition & 1 deletion docs/fox_config_show.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ fox config show [flags]

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

###### Auto generated by spf13/cobra on 20-Oct-2023
###### Auto generated by spf13/cobra on 22-Oct-2023
4 changes: 2 additions & 2 deletions docs/fox_deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fox deploy [deployment name] [flags]
```
-h, --help help for deploy
-n, --namespace string namespace of platform
-p, --platform string platform to run components with
-p, --platform string name of platform to utilize
--wait duration wait up the specified time for components to be ready
```

Expand All @@ -28,4 +28,4 @@ fox deploy [deployment name] [flags]

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

###### Auto generated by spf13/cobra on 20-Oct-2023
###### Auto generated by spf13/cobra on 22-Oct-2023
2 changes: 1 addition & 1 deletion docs/fox_docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ fox docs [flags]

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

###### Auto generated by spf13/cobra on 20-Oct-2023
###### Auto generated by spf13/cobra on 22-Oct-2023
2 changes: 1 addition & 1 deletion docs/fox_init.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ fox init [flags]

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

###### Auto generated by spf13/cobra on 20-Oct-2023
###### Auto generated by spf13/cobra on 22-Oct-2023
44 changes: 44 additions & 0 deletions docs/fox_proxy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
## fox proxy

Port forward local port to broker's HTTP server adapter

### Synopsis


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
requests to the broker's HTTP server adapter. This is especially useful during
development and testing.

Examples:
# Port forward local port 8080 and wait if no brokers are available.
fox proxy 8080 --wait


```
fox proxy [local port] [flags]
```

### Options

```
-h, --help help for proxy
-n, --namespace string namespace of platform
-p, --platform string name of platform to utilize
--wait duration wait up the specified time for components to be ready
```

### Options inherited from parent commands

```
-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")
-v, --verbose enable verbose output
```

### SEE ALSO

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

###### Auto generated by spf13/cobra on 22-Oct-2023
4 changes: 2 additions & 2 deletions docs/fox_publish.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fox publish [deploy-name] [flags]
-k, --kind string if provided the built image will be loaded into the Kind cluster
-n, --namespace string namespace of platform
--no-cache do not use cache when building image
-p, --platform string platform to run components with
-p, --platform string name of platform to utilize
--skip-deploy do not perform deployment after build
--skip-push do not push image after build
--wait duration wait up the specified time for components to be ready
Expand All @@ -33,4 +33,4 @@ fox publish [deploy-name] [flags]

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

###### Auto generated by spf13/cobra on 20-Oct-2023
###### Auto generated by spf13/cobra on 22-Oct-2023
4 changes: 2 additions & 2 deletions docs/fox_release.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fox release [release name] [flags]
-k, --kind string if provided the built image will be loaded into the Kind cluster
-n, --namespace string namespace of platform
--no-cache do not use cache when building image
-p, --platform string platform to run components with
-p, --platform string name of platform to utilize
--wait duration wait up the specified time for components to be ready
```

Expand All @@ -47,4 +47,4 @@ fox release [release name] [flags]

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

###### Auto generated by spf13/cobra on 20-Oct-2023
###### Auto generated by spf13/cobra on 22-Oct-2023
2 changes: 1 addition & 1 deletion docs/fox_version.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ fox version [flags]

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

###### Auto generated by spf13/cobra on 20-Oct-2023
###### Auto generated by spf13/cobra on 22-Oct-2023
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
Expand Down Expand Up @@ -267,6 +268,8 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/moby/patternmatcher v0.6.0 h1:GmP9lR19aU5GqSSFko+5pRqHi+Ohk1O69aFiKkVGiPk=
github.com/moby/patternmatcher v0.6.0/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc=
github.com/moby/spdystream v0.2.0 h1:cjW1zVyyoiM0T7b6UoySUFqzXMoqRckQtXwGPiBhOM8=
github.com/moby/spdystream v0.2.0/go.mod h1:f7i0iNDQJ059oMTcWxx8MA/zKFIuD/lY+0GqbN2Wy8c=
github.com/moby/sys/sequential v0.5.0 h1:OPvI35Lzn9K04PBbCLW0g4LcFAJgHsvXsRyewg5lXtc=
github.com/moby/sys/sequential v0.5.0/go.mod h1:tH2cOOs5V9MlPiXcQzRC+eEyab644PWKGRYaaV5ZZlo=
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 h1:HfkjXDfhgVaN5rmueG8cL8KKeFNecRCXFhaJ2qZ5SKA=
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type ContainerRegistry struct {
Token string `json:"token"`
}

func (cfg *Config) IsLocalRegistry() bool {
func (cfg *Config) IsRegistryLocal() bool {
return strings.HasPrefix(cfg.ContainerRegistry.Address, LocalRegistry)
}

Expand Down
Loading

0 comments on commit de0ad86

Please sign in to comment.