-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added multi app support and proxy context injection
- Loading branch information
Showing
33 changed files
with
468 additions
and
538 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
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 |
---|---|---|
@@ -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) | ||
} |
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
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
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
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
Oops, something went wrong.