Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: override tunnel details with user-provided settings #2707

Closed
wants to merge 9 commits into from
8 changes: 4 additions & 4 deletions site/src/content/docs/commands/zarf_connect.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ zarf connect { REGISTRY | GIT | connect-name } [flags]
```
--cli-only Disable browser auto-open
-h, --help help for connect
--local-port int (Optional, autogenerated if not provided) Specify the local port to bind to. E.g. local-port=42000. Ignored if --name is unset.
--local-port int (Optional, autogenerated if not provided) Specify the local port to bind to. E.g. local-port=42000.
--name string Specify the resource name. E.g. name=unicorns or name=unicorn-pod-7448499f4d-b5bk6.
--namespace string Specify the namespace. E.g. namespace=default. Ignored if --name is unset. (default "zarf")
--remote-port int Specify the remote port of the resource to bind to. E.g. remote-port=8080. Ignored if --name is unset.
--type string Specify the resource type. E.g. type=svc or type=pod. Ignored if --name is unset. (default "svc")
--namespace string Specify the namespace. E.g. namespace=default. (default "zarf")
--remote-port int Specify the remote port of the resource to bind to. E.g. remote-port=8080.
--type string Specify the resource type. E.g. type=svc or type=pod. (default "svc")
```

### Options inherited from parent commands
Expand Down
44 changes: 30 additions & 14 deletions src/cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@
)

var (
connectResourceName string
connectNamespace string
connectResourceType string
connectLocalPort int
connectRemotePort int
cliOnly bool
cliOnly bool
zt cluster.TunnelInfo

connectCmd = &cobra.Command{
Use: "connect { REGISTRY | GIT | connect-name }",
Expand All @@ -43,12 +39,32 @@
ctx := cmd.Context()

var tunnel *cluster.Tunnel
if connectResourceName != "" {
zt := cluster.NewTunnelInfo(connectNamespace, connectResourceType, connectResourceName, "", connectLocalPort, connectRemotePort)
if target == "" {

Check warning on line 42 in src/cmd/connect.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/connect.go#L42

Added line #L42 was not covered by tests
tunnel, err = c.ConnectTunnelInfo(ctx, zt)
} else {
tunnel, err = c.Connect(ctx, target)
var ti cluster.TunnelInfo
ti, err = c.NewTargetTunnelInfo(ctx, target)
if err != nil {
return fmt.Errorf("unable to create tunnel: %w", err)

Check warning on line 48 in src/cmd/connect.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/connect.go#L45-L48

Added lines #L45 - L48 were not covered by tests
}
if zt.ResourceType != cluster.SvcResource {
ti.ResourceType = zt.ResourceType

Check warning on line 51 in src/cmd/connect.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/connect.go#L50-L51

Added lines #L50 - L51 were not covered by tests
}
if zt.ResourceName != "" {
ti.ResourceName = zt.ResourceName

Check warning on line 54 in src/cmd/connect.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/connect.go#L53-L54

Added lines #L53 - L54 were not covered by tests
}
if zt.Namespace != cluster.ZarfNamespaceName {
ti.Namespace = zt.Namespace

Check warning on line 57 in src/cmd/connect.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/connect.go#L56-L57

Added lines #L56 - L57 were not covered by tests
}
if zt.LocalPort != 0 {
ti.LocalPort = zt.LocalPort

Check warning on line 60 in src/cmd/connect.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/connect.go#L59-L60

Added lines #L59 - L60 were not covered by tests
}
if zt.RemotePort != 0 {
ti.RemotePort = zt.RemotePort

Check warning on line 63 in src/cmd/connect.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/connect.go#L62-L63

Added lines #L62 - L63 were not covered by tests
}
tunnel, err = c.ConnectTunnelInfo(ctx, ti)

Check warning on line 65 in src/cmd/connect.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/connect.go#L65

Added line #L65 was not covered by tests
}

if err != nil {
return fmt.Errorf("unable to connect to the service: %w", err)
}
Expand Down Expand Up @@ -104,10 +120,10 @@
rootCmd.AddCommand(connectCmd)
connectCmd.AddCommand(connectListCmd)

connectCmd.Flags().StringVar(&connectResourceName, "name", "", lang.CmdConnectFlagName)
connectCmd.Flags().StringVar(&connectNamespace, "namespace", cluster.ZarfNamespaceName, lang.CmdConnectFlagNamespace)
connectCmd.Flags().StringVar(&connectResourceType, "type", cluster.SvcResource, lang.CmdConnectFlagType)
connectCmd.Flags().IntVar(&connectLocalPort, "local-port", 0, lang.CmdConnectFlagLocalPort)
connectCmd.Flags().IntVar(&connectRemotePort, "remote-port", 0, lang.CmdConnectFlagRemotePort)
connectCmd.Flags().StringVar(&zt.ResourceName, "name", "", lang.CmdConnectFlagName)
connectCmd.Flags().StringVar(&zt.Namespace, "namespace", cluster.ZarfNamespaceName, lang.CmdConnectFlagNamespace)
connectCmd.Flags().StringVar(&zt.ResourceType, "type", cluster.SvcResource, lang.CmdConnectFlagType)
connectCmd.Flags().IntVar(&zt.LocalPort, "local-port", 0, lang.CmdConnectFlagLocalPort)
connectCmd.Flags().IntVar(&zt.RemotePort, "remote-port", 0, lang.CmdConnectFlagRemotePort)

Check warning on line 127 in src/cmd/connect.go

View check run for this annotation

Codecov / codecov/patch

src/cmd/connect.go#L123-L127

Added lines #L123 - L127 were not covered by tests
connectCmd.Flags().BoolVar(&cliOnly, "cli-only", false, lang.CmdConnectFlagCliOnly)
}
8 changes: 4 additions & 4 deletions src/config/lang/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ const (
CmdConnectListShort = "Lists all available connection shortcuts"

CmdConnectFlagName = "Specify the resource name. E.g. name=unicorns or name=unicorn-pod-7448499f4d-b5bk6."
CmdConnectFlagNamespace = "Specify the namespace. E.g. namespace=default. Ignored if --name is unset."
CmdConnectFlagType = "Specify the resource type. E.g. type=svc or type=pod. Ignored if --name is unset."
CmdConnectFlagLocalPort = "(Optional, autogenerated if not provided) Specify the local port to bind to. E.g. local-port=42000. Ignored if --name is unset."
CmdConnectFlagRemotePort = "Specify the remote port of the resource to bind to. E.g. remote-port=8080. Ignored if --name is unset."
chaospuppy marked this conversation as resolved.
Show resolved Hide resolved
CmdConnectFlagNamespace = "Specify the namespace. E.g. namespace=default."
CmdConnectFlagType = "Specify the resource type. E.g. type=svc or type=pod."
CmdConnectFlagLocalPort = "(Optional, autogenerated if not provided) Specify the local port to bind to. E.g. local-port=42000."
CmdConnectFlagRemotePort = "Specify the remote port of the resource to bind to. E.g. remote-port=8080."
CmdConnectFlagCliOnly = "Disable browser auto-open"

CmdConnectPreparingTunnel = "Preparing a tunnel to connect to %s"
Expand Down
75 changes: 42 additions & 33 deletions src/pkg/cluster/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,23 @@

// TunnelInfo is a struct that contains the necessary info to create a new Tunnel
type TunnelInfo struct {
localPort int
remotePort int
namespace string
resourceType string
resourceName string
LocalPort int
RemotePort int
Namespace string
ResourceType string
ResourceName string
urlSuffix string
}

// NewTunnelInfo returns a new TunnelInfo object for connecting to a cluster
func NewTunnelInfo(namespace, resourceType, resourceName, urlSuffix string, localPort, remotePort int) TunnelInfo {
return TunnelInfo{
namespace: namespace,
resourceType: resourceType,
resourceName: resourceName,
Namespace: namespace,
ResourceType: resourceType,
ResourceName: resourceName,

Check warning on line 60 in src/pkg/cluster/tunnel.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/tunnel.go#L58-L60

Added lines #L58 - L60 were not covered by tests
urlSuffix: urlSuffix,
localPort: localPort,
remotePort: remotePort,
LocalPort: localPort,
RemotePort: remotePort,

Check warning on line 63 in src/pkg/cluster/tunnel.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/tunnel.go#L62-L63

Added lines #L62 - L63 were not covered by tests
}
}
chaospuppy marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -93,44 +93,53 @@
return nil
}

// Connect will establish a tunnel to the specified target.
func (c *Cluster) Connect(ctx context.Context, target string) (*Tunnel, error) {
// NewTargetTunnelInfo returns a new TunnelInfo object for the specified target.
func (c *Cluster) NewTargetTunnelInfo(ctx context.Context, target string) (TunnelInfo, error) {

Check warning on line 97 in src/pkg/cluster/tunnel.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/tunnel.go#L97

Added line #L97 was not covered by tests
var err error
zt := TunnelInfo{
namespace: ZarfNamespaceName,
resourceType: SvcResource,
Namespace: ZarfNamespaceName,
ResourceType: SvcResource,

Check warning on line 101 in src/pkg/cluster/tunnel.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/tunnel.go#L100-L101

Added lines #L100 - L101 were not covered by tests
}

switch strings.ToUpper(target) {
case ZarfRegistry:
zt.resourceName = ZarfRegistryName
zt.remotePort = ZarfRegistryPort
zt.ResourceName = ZarfRegistryName
zt.RemotePort = ZarfRegistryPort

Check warning on line 107 in src/pkg/cluster/tunnel.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/tunnel.go#L106-L107

Added lines #L106 - L107 were not covered by tests
zt.urlSuffix = `/v2/_catalog`
case ZarfGit:
zt.resourceName = ZarfGitServerName
zt.remotePort = ZarfGitServerPort
zt.ResourceName = ZarfGitServerName
zt.RemotePort = ZarfGitServerPort

Check warning on line 111 in src/pkg/cluster/tunnel.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/tunnel.go#L110-L111

Added lines #L110 - L111 were not covered by tests
case ZarfInjector:
zt.resourceName = ZarfInjectorName
zt.remotePort = ZarfInjectorPort
zt.ResourceName = ZarfInjectorName
zt.RemotePort = ZarfInjectorPort

Check warning on line 114 in src/pkg/cluster/tunnel.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/tunnel.go#L113-L114

Added lines #L113 - L114 were not covered by tests
default:
if target != "" {
if zt, err = c.checkForZarfConnectLabel(ctx, target); err != nil {
return nil, fmt.Errorf("problem looking for a zarf connect label in the cluster: %s", err.Error())
return TunnelInfo{}, fmt.Errorf("problem looking for a zarf connect label in the cluster: %s", err.Error())

Check warning on line 118 in src/pkg/cluster/tunnel.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/tunnel.go#L118

Added line #L118 was not covered by tests
}
}
if zt.resourceName == "" {
return nil, fmt.Errorf("missing resource name")
if zt.ResourceName == "" {
return TunnelInfo{}, fmt.Errorf("missing resource name")

Check warning on line 122 in src/pkg/cluster/tunnel.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/tunnel.go#L121-L122

Added lines #L121 - L122 were not covered by tests
}
if zt.remotePort < 1 {
return nil, fmt.Errorf("missing remote port")
if zt.RemotePort < 1 {
return TunnelInfo{}, fmt.Errorf("missing remote port")

Check warning on line 125 in src/pkg/cluster/tunnel.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/tunnel.go#L124-L125

Added lines #L124 - L125 were not covered by tests
}
}
return zt, err

Check warning on line 128 in src/pkg/cluster/tunnel.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/tunnel.go#L128

Added line #L128 was not covered by tests
}

// Connect will establish a tunnel to the specified target.
func (c *Cluster) Connect(ctx context.Context, target string) (*Tunnel, error) {
zt, err := c.NewTargetTunnelInfo(ctx, target)
if err != nil {
return nil, err

Check warning on line 135 in src/pkg/cluster/tunnel.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/tunnel.go#L132-L135

Added lines #L132 - L135 were not covered by tests
}
return c.ConnectTunnelInfo(ctx, zt)
}

// ConnectTunnelInfo connects to the cluster with the provided TunnelInfo
func (c *Cluster) ConnectTunnelInfo(ctx context.Context, zt TunnelInfo) (*Tunnel, error) {
tunnel, err := c.NewTunnel(zt.namespace, zt.resourceType, zt.resourceName, zt.urlSuffix, zt.localPort, zt.remotePort)
tunnel, err := c.NewTunnel(zt.Namespace, zt.ResourceType, zt.ResourceName, zt.urlSuffix, zt.LocalPort, zt.RemotePort)

Check warning on line 142 in src/pkg/cluster/tunnel.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/tunnel.go#L142

Added line #L142 was not covered by tests
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -206,25 +215,25 @@
svc := serviceList.Items[0]

// Reset based on the matched params.
zt.resourceType = SvcResource
zt.resourceName = svc.Name
zt.namespace = svc.Namespace
zt.ResourceType = SvcResource
zt.ResourceName = svc.Name
zt.Namespace = svc.Namespace

Check warning on line 220 in src/pkg/cluster/tunnel.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/tunnel.go#L218-L220

Added lines #L218 - L220 were not covered by tests
// Only support a service with a single port.
zt.remotePort = svc.Spec.Ports[0].TargetPort.IntValue()
zt.RemotePort = svc.Spec.Ports[0].TargetPort.IntValue()

Check warning on line 222 in src/pkg/cluster/tunnel.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/tunnel.go#L222

Added line #L222 was not covered by tests
// if targetPort == 0, look for Port (which is required)
if zt.remotePort == 0 {
if zt.RemotePort == 0 {

Check warning on line 224 in src/pkg/cluster/tunnel.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/tunnel.go#L224

Added line #L224 was not covered by tests
// TODO: Need a check for if container port is not found
remotePort, err := c.findPodContainerPort(ctx, svc)
if err != nil {
return TunnelInfo{}, err
}
zt.remotePort = remotePort
zt.RemotePort = remotePort

Check warning on line 230 in src/pkg/cluster/tunnel.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/tunnel.go#L230

Added line #L230 was not covered by tests
}

// Add the url suffix too.
zt.urlSuffix = svc.Annotations[config.ZarfConnectAnnotationURL]

message.Debugf("tunnel connection match: %s/%s on port %d", svc.Namespace, svc.Name, zt.remotePort)
message.Debugf("tunnel connection match: %s/%s on port %d", svc.Namespace, svc.Name, zt.RemotePort)

Check warning on line 236 in src/pkg/cluster/tunnel.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/cluster/tunnel.go#L236

Added line #L236 was not covered by tests
} else {
return zt, fmt.Errorf("no matching services found for %s", name)
}
Expand Down
Loading