Skip to content

Commit

Permalink
#1570. Fix api/cluster tunnels.go with correct typing for tunnels map…
Browse files Browse the repository at this point in the history
…. Update types runetime.go by removing ZarfTunnels type to fix import error
  • Loading branch information
mike-winberry committed May 1, 2023
1 parent 6412b7b commit 11450dc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
25 changes: 11 additions & 14 deletions src/internal/api/cluster/tunnels.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import (
"github.com/defenseunicorns/zarf/src/internal/cluster"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils/exec"
"github.com/defenseunicorns/zarf/src/types"
"github.com/go-chi/chi"
)

var tunnels types.ZarfTunnels
var tunnels map[string]*cluster.Tunnel

// ConnectTunnel establishes a tunnel for the requested resource
func ConnectTunnel(w http.ResponseWriter, r *http.Request) {
Expand All @@ -26,12 +25,11 @@ func ConnectTunnel(w http.ResponseWriter, r *http.Request) {
if tunnels != nil {
if tunnels[name] != nil {
launchTunnelUrl(tunnels[name], w, name)
message.Debug("Tunnel already exists for %s", name)
common.WriteJSONResponse(w, true, http.StatusCreated)
return
}
} else {
tunnels = make(types.ZarfTunnels)
tunnels = make(map[string]*cluster.Tunnel)
// Keep this open until an interrupt signal is received.
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
Expand All @@ -50,7 +48,6 @@ func ConnectTunnel(w http.ResponseWriter, r *http.Request) {
}

tunnel, err := cluster.NewZarfTunnel()
tunnel.EnableAutoOpen()

if err != nil {
message.ErrorWebf(err, w, "Failed to create tunnel for %s", name)
Expand All @@ -60,20 +57,12 @@ func ConnectTunnel(w http.ResponseWriter, r *http.Request) {
if err != nil {
message.ErrorWebf(err, w, "Failed to connect to %s", name)
}
tunnels[name] = tunnel
launchTunnelUrl(tunnel, w, name)

tunnels[name] = tunnel
common.WriteJSONResponse(w, true, http.StatusCreated)
}

// launchTunnelUrl launches the tunnel URL in the default browser
func launchTunnelUrl(tunnel *cluster.Tunnel, w http.ResponseWriter, name string) {
if err := exec.LaunchURL(tunnel.HTTPEndpoint()); err != nil {
message.ErrorWebf(err, w, "Failed to launch browser for %s", name)

}
}

// DisconnectTunnel closes the tunnel for the requested resource
func DisconnectTunnel(w http.ResponseWriter, r *http.Request) {
name := chi.URLParam(r, "name")
Expand All @@ -83,6 +72,14 @@ func DisconnectTunnel(w http.ResponseWriter, r *http.Request) {
common.WriteJSONResponse(w, true, http.StatusOK)
}

// launchTunnelUrl launches the tunnel URL in the default browser
func launchTunnelUrl(tunnel *cluster.Tunnel, w http.ResponseWriter, name string) {
if err := exec.LaunchURL(tunnel.HTTPEndpoint()); err != nil {
message.ErrorWebf(err, w, "Failed to launch browser for %s", name)

}
}

// closeTunnel closes the tunnel for the requested resource and removes it from the tunnels map
func closeTunnel(name string) {
if tunnels != nil {
Expand Down
4 changes: 0 additions & 4 deletions src/types/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package types

import (
"github.com/defenseunicorns/zarf/src/internal/cluster"
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/registry"
)
Expand Down Expand Up @@ -97,9 +96,6 @@ type ConnectString struct {
// ConnectStrings is a map of connect names to connection information.
type ConnectStrings map[string]ConnectString

// ZarfTunnels is a map of resource names to the open tunnels.
type ZarfTunnels map[string]*cluster.Tunnel

// ComponentSBOM contains information related to the files SBOM'ed from a component.
type ComponentSBOM struct {
Files []string
Expand Down

0 comments on commit 11450dc

Please sign in to comment.