Skip to content

Commit

Permalink
Move find port function into library code (#2049)
Browse files Browse the repository at this point in the history
## 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 <[email protected]>
  • Loading branch information
Racer159 and Noxsios authored Oct 3, 2023
1 parent d2b868b commit 67170fb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
24 changes: 1 addition & 23 deletions src/internal/cluster/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
24 changes: 24 additions & 0 deletions src/pkg/k8s/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 67170fb

Please sign in to comment.