Skip to content

Commit

Permalink
#1570. Update start.go by moving the tunnels routes under packages, u…
Browse files Browse the repository at this point in the history
…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
mike-winberry committed May 2, 2023
1 parent 7926f94 commit a7b25b8
Show file tree
Hide file tree
Showing 18 changed files with 506 additions and 436 deletions.
78 changes: 78 additions & 0 deletions docs/4-user-guide/3-zarf-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,84 @@ Must be one of:
</blockquote>
</details>

<details open>
<summary>
<strong> <a name="metadata_connectStrings"></a>connectStrings</strong>
</summary>
&nbsp;
<blockquote>

## metadata > connectStrings

**Description:** List of connection strings for the package

| | |
| ------------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| **Type** | `object` |
| **Additional properties** | [![Any type: allowed](https://img.shields.io/badge/Any%20type-allowed-green)](# "Additional Properties of any type are allowed.") |

<details open>
<summary>
<strong> <a name="metadata_connectStrings_pattern1"></a>Pattern Property ConnectString</strong>
</summary>
&nbsp;
<blockquote>

## metadata > connectStrings > ConnectString
:::note
All properties whose name matches the regular expression
```.*``` ([Test](https://regex101.com/?regex=.%2A))
must respect the following conditions
:::

| | |
| ------------------------- | -------------------------------------------------------------------------------------------------------- |
| **Type** | `object` |
| **Additional properties** | [![Not allowed](https://img.shields.io/badge/Not%20allowed-red)](# "Additional Properties not allowed.") |
| **Defined in** | #/definitions/ConnectString |

<details>
<summary>
<strong> <a name="metadata_connectStrings_pattern1_description"></a>description *</strong>
</summary>
&nbsp;
<blockquote>

![Required](https://img.shields.io/badge/Required-red)

**Description:** Descriptive text that explains what the resource you would be connecting to is used for

| | |
| -------- | -------- |
| **Type** | `string` |

</blockquote>
</details>

<details>
<summary>
<strong> <a name="metadata_connectStrings_pattern1_url"></a>url *</strong>
</summary>
&nbsp;
<blockquote>

![Required](https://img.shields.io/badge/Required-red)

**Description:** URL path that gets appended to the k8s port-forward result

| | |
| -------- | -------- |
| **Type** | `string` |

</blockquote>
</details>

</blockquote>
</details>

</blockquote>
</details>

</blockquote>
</details>

Expand Down
59 changes: 0 additions & 59 deletions src/internal/api/packages/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ package packages
import (
"net/http"

"github.com/defenseunicorns/zarf/src/config"
"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"
)

// ListDeployedPackages writes a list of packages that have been deployed to the connected cluster.
Expand All @@ -31,59 +28,3 @@ func ListDeployedPackages(w http.ResponseWriter, _ *http.Request) {

common.WriteJSONResponse(w, deployedPackages, http.StatusOK)
}

// ListPackageConnections lists the zarf connections for a package.
func ListPackageConnections(w http.ResponseWriter, r *http.Request) {
data := types.APIPackageConnections{}

pkgName := chi.URLParam(r, "name")

c, err := cluster.NewCluster()

if err != nil {
message.ErrorWebf(err, w, "Could not connect to cluster")
return
}

// Get the package from the cluster.
pkg, err := c.GetDeployedPackage(pkgName)

if err != nil {
message.ErrorWebf(err, w, "Unable to get package %s", pkgName)
return
}

// Get a list of namespaces from the package component charts.
namespaces := make(map[string]string)
for _, component := range pkg.DeployedComponents {
for _, chart := range component.InstalledCharts {
namespaces[chart.Namespace] = chart.Namespace
}
}

// Get a list of zarf connections from the namespaces.
connections := make(types.ConnectStrings)
for namespace := range namespaces {
// Get a list of services in the namespace with the zarf connect label.
serviceList, err := c.Kube.GetServicesByLabelExists(namespace, config.ZarfConnectLabelName)

if err != nil {
message.ErrorWebf(err, w, "Unable to get a list of the zarf connections for package %s", pkgName)
return
}

for _, svc := range serviceList.Items {
name := svc.Labels[config.ZarfConnectLabelName]

// Add the connectString.
connections[name] = types.ConnectString{
Description: svc.Annotations[config.ZarfConnectAnnotationDescription],
URL: svc.Annotations[config.ZarfConnectAnnotationURL],
}
}

}
data.ConnectStrings = connections

common.WriteJSONResponse(w, data, http.StatusOK)
}
109 changes: 109 additions & 0 deletions src/internal/api/packages/tunnels.go
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)
}
12 changes: 4 additions & 8 deletions src/internal/api/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/defenseunicorns/zarf/src/internal/api/cluster"
"github.com/defenseunicorns/zarf/src/internal/api/components"
"github.com/defenseunicorns/zarf/src/internal/api/packages"
"github.com/defenseunicorns/zarf/src/internal/api/tunnels"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/exec"
Expand Down Expand Up @@ -92,21 +91,18 @@ func LaunchAPIServer() {
r.Get("/find-init", packages.FindInitPackage)
r.Get("/read/{path}", packages.Read)
r.Get("/list", packages.ListDeployedPackages)
r.Get("/list/connections/{name}", packages.ListPackageConnections)
r.Put("/deploy", packages.DeployPackage)
r.Get("/deploy-stream", packages.StreamDeployPackage)
r.Delete("/remove/{name}", packages.RemovePackage)
r.Put("/{pkg}/connect/{name}", packages.ConnectTunnel)
r.Delete("/{pkg}/disconnect/{name}", packages.DisconnectTunnel)
r.Get("/{pkg}/connections", packages.ListPackageConnections)
r.Get("/connections", packages.ListConnections)
})

r.Route("/components", func(r chi.Router) {
r.Get("/deployed", components.ListDeployingComponents)
})

r.Route("/tunnels", func(r chi.Router) {
r.Get("/list", tunnels.ListTunnels)
r.Put("/connect/{name}", tunnels.ConnectTunnel)
r.Delete("/disconnect/{name}", tunnels.DisconnectTunnel)
})
})

// If no dev port specified, use the server port for the URL and try to open it
Expand Down
87 changes: 0 additions & 87 deletions src/internal/api/tunnels/tunnels.go

This file was deleted.

Loading

0 comments on commit a7b25b8

Please sign in to comment.