Skip to content

Commit

Permalink
feat: proxy support enabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
beneiltis committed Oct 27, 2023
1 parent 3c52b14 commit c9234cb
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 6 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ After successfully installing punq, you're just a few steps away from harnessing

To install punq on your cluster within the current context and set up ingress routing, enabling you to access punq via your custom domain:

### Without Ingress (local / not exposed to the internet)

```
punq install
punq proxy
```

### With Ingress
```
punq install -i punq.yourdomain.com
```
Expand Down
22 changes: 18 additions & 4 deletions cmd/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var proxyCmd = &cobra.Command{
// FORWARD BACKEND
readyBackendCh := make(chan struct{})
stopBackendCh := make(chan struct{}, 1)
backendUrl := fmt.Sprintf("http://%s:%d/version", utils.CONFIG.Backend.Host, utils.CONFIG.Backend.Port)
backendUrl := fmt.Sprintf("http://%s:%d", utils.CONFIG.Backend.Host, utils.CONFIG.Backend.Port)
go kubernetes.StartPortForward(utils.CONFIG.Backend.Port, utils.CONFIG.Backend.Port, readyBackendCh, stopBackendCh, &contextId)

// FORWARD FRONTEND
Expand All @@ -40,6 +40,12 @@ var proxyCmd = &cobra.Command{
frontendUrl := fmt.Sprintf("http://%s:%d", utils.CONFIG.Frontend.Host, utils.CONFIG.Frontend.Port)
go kubernetes.StartPortForward(utils.CONFIG.Frontend.Port, utils.CONFIG.Frontend.Port, readyFrontendCh, stopFrontendCh, &contextId)

// FORWARD WEBSOCKET
readyWebsocketCh := make(chan struct{})
stopWebsocketCh := make(chan struct{}, 1)
websocketUrl := fmt.Sprintf("ws://%s:%d", utils.CONFIG.Websocket.Host, utils.CONFIG.Websocket.Port)
go kubernetes.StartPortForward(utils.CONFIG.Websocket.Port, utils.CONFIG.Websocket.Port, readyWebsocketCh, stopWebsocketCh, &contextId)

select {
case <-readyBackendCh:
fmt.Printf("Backend %s is ready! 🚀🚀🚀\n", backendUrl)
Expand All @@ -50,21 +56,29 @@ var proxyCmd = &cobra.Command{

select {
case <-readyFrontendCh:

fmt.Printf("Frontend %s is ready! 🚀🚀🚀\n", frontendUrl)
utils.OpenBrowser(frontendUrl)
utils.OpenBrowser("http://localhost:8888")
break
case <-stopFrontendCh:
break
}

select {
case <-readyWebsocketCh:
fmt.Printf("Websocket %s is ready! 🚀🚀🚀\n", websocketUrl)
break
case <-stopWebsocketCh:
break
}

kubernetes.Proxy(backendUrl, frontendUrl, websocketUrl)

quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
},
}

func init() {
proxyCmd.Hidden = true
rootCmd.AddCommand(proxyCmd)
}
3 changes: 3 additions & 0 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ var updateOperatorImageCmd = &cobra.Command{
}

imageName := fmt.Sprintf("ghcr.io/mogenius/punq:%s", vers)
if utils.CONFIG.Misc.Stage != "production" {
imageName = fmt.Sprintf("ghcr.io/mogenius/punq-dev:%s", vers)
}
err = kubernetes.UpdateDeploymentImage(utils.CONFIG.Kubernetes.OwnNamespace, version.Name, imageName, nil)
if err != nil {
fmt.Printf("Error: %s\n", err.Error())
Expand Down
6 changes: 5 additions & 1 deletion kubernetes/addResources.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,11 @@ func addDeployment(provider *KubeProvider) {
deploymentContainer.WithName(version.Name)
deploymentContainer.WithImage(version.OperatorImage)

deploymentContainer.WithPorts(applyconfcore.ContainerPort().WithContainerPort(int32(utils.CONFIG.Backend.Port)).WithContainerPort(int32(utils.CONFIG.Frontend.Port)).WithContainerPort(int32(utils.CONFIG.Websocket.Port)))
deploymentContainer.WithPorts(
applyconfcore.ContainerPort().WithContainerPort(int32(utils.CONFIG.Backend.Port)),
applyconfcore.ContainerPort().WithContainerPort(int32(utils.CONFIG.Frontend.Port)),
applyconfcore.ContainerPort().WithContainerPort(int32(utils.CONFIG.Websocket.Port)),
)

envVars := []applyconfcore.EnvVarApplyConfiguration{}
envVars = append(envVars, applyconfcore.EnvVarApplyConfiguration{
Expand Down
46 changes: 46 additions & 0 deletions kubernetes/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/signal"
Expand Down Expand Up @@ -112,3 +113,48 @@ func portForwardAPod(req PortForwardAPodRequest, contextId *string) error {
}
return fw.ForwardPorts()
}

func Proxy(backendUrl string, frontendUrl string, websocketUrl string) {
localPort := ":8888"

backendURL, err := url.Parse(backendUrl)
if err != nil {
utils.FatalError(fmt.Sprintf("Error parsing backend url: %s", err.Error()))
}
frontendURL, err := url.Parse(frontendUrl)
if err != nil {
utils.FatalError(fmt.Sprintf("Error parsing frontend url: %s", err.Error()))
}
websocketURL, err := url.Parse(websocketUrl)
if err != nil {
utils.FatalError(fmt.Sprintf("Error parsing websocket url: %s", err.Error()))
}

backendProxy := httputil.NewSingleHostReverseProxy(backendURL)
frontendProxy := httputil.NewSingleHostReverseProxy(frontendURL)
websocketProxy := httputil.NewSingleHostReverseProxy(websocketURL)

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
orig := r.URL
fmt.Printf("FRONTEND: localhost%s%s -> %s%s\n", localPort, orig, frontendURL, r.URL)
frontendProxy.ServeHTTP(w, r)
})

http.HandleFunc("/backend/", func(w http.ResponseWriter, r *http.Request) {
orig := r.URL
r.URL.Path = strings.Replace(r.URL.Path, "/backend", "", 1)
fmt.Printf("BACKEND : localhost%s%s -> %s%s\n", localPort, orig, backendURL, r.URL)
backendProxy.ServeHTTP(w, r)
})

http.HandleFunc("/websocket", func(w http.ResponseWriter, r *http.Request) {
orig := r.URL
r.URL.Path = strings.Replace(r.URL.Path, "/websocket", "", 1)
fmt.Printf("WEBSOCKET: localhost%s%s -> %s%s\n", localPort, orig, websocketURL, r.URL)
websocketProxy.ServeHTTP(w, r)
})

if err = http.ListenAndServe(localPort, nil); err != nil {
utils.FatalError(fmt.Sprintf("Error starting proxy: %s", err.Error()))
}
}
8 changes: 7 additions & 1 deletion welcome.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Install punq on your cluster in your current context. This will also set up the ingress to deliver punq on your own domain. You'll be asked to confirm with "Y".
punq install -i punq.yourdomain.com
## Without Ingress (local / not exposed to the internet):
punq install
punq proxy

## With Ingress
punq install -i punq.yourdomain.com
open https://punq.yourdomain.com

- In your domain's DNS settings, add an A-Record for the punq domain which points to your cluster loadbalancer IP, e.g. A: 123.123.123.123 -> punq.yourdomain.com.
- Open punq in your browser.
Expand Down

0 comments on commit c9234cb

Please sign in to comment.