From 67170fb322c593f99cecc1c34fdf8d3260dbf175 Mon Sep 17 00:00:00 2001 From: Wayne Starr Date: Tue, 3 Oct 2023 17:25:45 -0500 Subject: [PATCH] Move find port function into library code (#2049) ## Description This moves a helper function for finding a containers port from a service pod into library code to be used by other binaries. ## Related Issue Relates to #1814 ## Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [X] Other (security config, docs update, etc) ## Checklist before merging - [x] Test, docs, adr added or updated as needed - [x] [Contributor Guide Steps](https://github.com/defenseunicorns/zarf/blob/main/CONTRIBUTING.md#developer-workflow) followed --------- Co-authored-by: razzle --- src/internal/cluster/tunnel.go | 24 +----------------------- src/pkg/k8s/pods.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/internal/cluster/tunnel.go b/src/internal/cluster/tunnel.go index 046b1b3d74..d52c606ecc 100644 --- a/src/internal/cluster/tunnel.go +++ b/src/internal/cluster/tunnel.go @@ -195,7 +195,7 @@ func (c *Cluster) checkForZarfConnectLabel(name string) (TunnelInfo, error) { zt.remotePort = svc.Spec.Ports[0].TargetPort.IntValue() // if targetPort == 0, look for Port (which is required) if zt.remotePort == 0 { - zt.remotePort = c.findPodContainerPort(svc) + zt.remotePort = c.FindPodContainerPort(svc) } // Add the url suffix too. @@ -208,25 +208,3 @@ func (c *Cluster) checkForZarfConnectLabel(name string) (TunnelInfo, error) { return zt, nil } - -// findPodContainerPort will find the container port in the pod and assign it to tunnel's remotePort. -func (c *Cluster) findPodContainerPort(svc v1.Service) int { - selectorLabelsOfPods := k8s.MakeLabels(svc.Spec.Selector) - pods := c.WaitForPodsAndContainers(k8s.PodLookup{ - Namespace: svc.Namespace, - Selector: selectorLabelsOfPods, - }, nil) - - for _, pod := range pods { - // Find the matching name on the port in the pod - for _, container := range pod.Spec.Containers { - for _, port := range container.Ports { - if port.Name == svc.Spec.Ports[0].TargetPort.String() { - return int(port.ContainerPort) - } - } - } - } - - return 0 -} diff --git a/src/pkg/k8s/pods.go b/src/pkg/k8s/pods.go index 3160065317..1b8eb75a40 100644 --- a/src/pkg/k8s/pods.go +++ b/src/pkg/k8s/pods.go @@ -160,3 +160,27 @@ func (k *K8s) WaitForPodsAndContainers(target PodLookup, include PodFilter) []co return []corev1.Pod{} } + +// FindPodContainerPort will find a pod's container port from a service and return it. +// +// Returns 0 if no port is found. +func (k *K8s) FindPodContainerPort(svc corev1.Service) int { + selectorLabelsOfPods := MakeLabels(svc.Spec.Selector) + pods := k.WaitForPodsAndContainers(PodLookup{ + Namespace: svc.Namespace, + Selector: selectorLabelsOfPods, + }, nil) + + for _, pod := range pods { + // Find the matching name on the port in the pod + for _, container := range pod.Spec.Containers { + for _, port := range container.Ports { + if port.Name == svc.Spec.Ports[0].TargetPort.String() { + return int(port.ContainerPort) + } + } + } + } + + return 0 +}