-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1570. Update start.go by moving the tunnels routes under packages, u…
…pdated the routes to take in a pkg and connection name instead of just connection so that tunnels can be created and removed by package. Remove api/packages list.go ListPackageConnections as it is no longer needed. Move api/tunnels/tunnels.go to api/packages/tunnels.go. Update api/packages tunnels.go with new typing and logic to connect, disconnect, and get connections by package, added ListConnections method that returns a map of package names to APIDeployedPackageConnections types, and removed the launch logic from the backend to be handled in the front end. Update packager deploy.go to add the connect strings to the pkg metadata prior to saving the deployed package information to k8s. Update types package.go ZarfMetadata with ConnectStrings field of Type ConnectStrings. Update types api.go with connection types for package connections/tunnels. Update ui api.ts with new endpoints and removed the tunnel endpoints. Update ui store.ts with update logic for the stores that use polling, added PackageTunnels interface. Update deployed-package-table with keys to prevent misrendering after adding a package. Update ui ConnectDeployedPackageDialog to use the new connections logic and api calls as well as opening the connection url in a new tab upon successfull connection. Update DeployedPackageMenu removed unused import. Update DisconnectDeployedPackageDialog to use new endpoints and logic for removing a connection. Update routes +page by extracting the asynchronous store methods into store.ts and added a poll for connections.
- Loading branch information
1 parent
7926f94
commit a7b25b8
Showing
18 changed files
with
506 additions
and
436 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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors | ||
|
||
// Package packages provides api functions for managing Zarf packages. | ||
package packages | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/defenseunicorns/zarf/src/internal/api/common" | ||
"github.com/defenseunicorns/zarf/src/internal/cluster" | ||
"github.com/defenseunicorns/zarf/src/pkg/message" | ||
"github.com/defenseunicorns/zarf/src/types" | ||
"github.com/go-chi/chi/v5" | ||
) | ||
|
||
type PackageTunnel struct { | ||
tunnel *cluster.Tunnel | ||
Connection types.APIDeployedPackageConnection `json:"connection,omitempty"` | ||
} | ||
type PackageTunnels map[string]map[string]PackageTunnel | ||
|
||
// tunnels is a map of package names to tunnel objects used for storing connected tunnels | ||
var tunnels = make(PackageTunnels) | ||
|
||
// ListConnections returns a map of pkgName to a list of connections | ||
func ListConnections(w http.ResponseWriter, _ *http.Request) { | ||
allConnections := make(types.APIConnections) | ||
for name, pkgTunnels := range tunnels { | ||
for _, pkgTunnel := range pkgTunnels { | ||
if allConnections[name] == nil { | ||
allConnections[name] = make(types.APIDeployedPackageConnections, 0) | ||
} | ||
allConnections[name] = append(allConnections[name], pkgTunnel.Connection) | ||
} | ||
} | ||
common.WriteJSONResponse(w, allConnections, http.StatusOK) | ||
} | ||
|
||
// ListPackageConnections lists all tunnel names | ||
func ListPackageConnections(w http.ResponseWriter, r *http.Request) { | ||
pkgName := chi.URLParam(r, "pkg") | ||
if tunnels[pkgName] == nil { | ||
message.ErrorWebf(errors.New("No tunnels for package %s"), w, pkgName) | ||
return | ||
} | ||
pkgTunnels := make(types.APIDeployedPackageConnections, 0, len(tunnels[pkgName])) | ||
for _, pkgTunnel := range tunnels[pkgName] { | ||
pkgTunnels = append(pkgTunnels, pkgTunnel.Connection) | ||
} | ||
|
||
common.WriteJSONResponse(w, pkgTunnels, http.StatusOK) | ||
} | ||
|
||
// ConnectTunnel establishes a tunnel for the requested resource | ||
func ConnectTunnel(w http.ResponseWriter, r *http.Request) { | ||
pkgName := chi.URLParam(r, "pkg") | ||
connectionName := chi.URLParam(r, "name") | ||
|
||
if tunnels[pkgName] == nil { | ||
tunnels[pkgName] = make(map[string]PackageTunnel) | ||
} | ||
|
||
pkgTunnels := tunnels[pkgName] | ||
|
||
if pkgTunnels[connectionName].tunnel != nil { | ||
common.WriteJSONResponse(w, tunnels[pkgName][connectionName].Connection, http.StatusOK) | ||
return | ||
} | ||
|
||
tunnel, err := cluster.NewZarfTunnel() | ||
|
||
if err != nil { | ||
message.ErrorWebf(err, w, "Failed to create tunnel for %s", connectionName) | ||
return | ||
} | ||
|
||
err = tunnel.Connect(connectionName, false) | ||
if err != nil { | ||
message.ErrorWebf(err, w, "Failed to connect to %s", connectionName) | ||
return | ||
} | ||
|
||
tunnels[pkgName][connectionName] = PackageTunnel{ | ||
tunnel: tunnel, | ||
Connection: types.APIDeployedPackageConnection{ | ||
Name: connectionName, | ||
URL: tunnel.FullURL(), | ||
}, | ||
} | ||
common.WriteJSONResponse(w, tunnels[pkgName][connectionName].Connection, http.StatusCreated) | ||
} | ||
|
||
// DisconnectTunnel closes the tunnel for the requested resource | ||
func DisconnectTunnel(w http.ResponseWriter, r *http.Request) { | ||
pkgName := chi.URLParam(r, "pkg") | ||
connectionName := chi.URLParam(r, "name") | ||
pkgTunnel := tunnels[pkgName][connectionName] | ||
if pkgTunnel.tunnel == nil { | ||
message.ErrorWebf(errors.New("Tunnel not found"), w, "Failed to disconnect from %s", connectionName) | ||
return | ||
} | ||
|
||
pkgTunnel.tunnel.Close() | ||
delete(tunnels[pkgName], connectionName) | ||
|
||
common.WriteJSONResponse(w, true, http.StatusOK) | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.