Skip to content

Commit

Permalink
build a pod target if there is no declared ports (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyam8 authored Sep 11, 2020
1 parent 7546080 commit a43e133
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ One of the following role types can be configured to discover targets:

The pod role discovers all pods and exposes their containers as targets.
For each declared port of a container, it generates single target.
If there is no declared port it generates one target with empty `Port`, `PortName` and `PortProtocol` fields.

Available pod target fields:

Expand Down
35 changes: 33 additions & 2 deletions pipeline/discovery/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,33 @@ func (p Pod) buildTargets(pod *apiv1.Pod) (targets []model.Target) {
for _, container := range pod.Spec.Containers {
env := p.collectEnv(pod.Namespace, container)

if len(container.Ports) == 0 {
target := &PodTarget{
tuid: podTUID(pod, container),
Address: pod.Status.PodIP,
Namespace: pod.Namespace,
Name: pod.Name,
Annotations: toMapInterface(pod.Annotations),
Labels: toMapInterface(pod.Labels),
NodeName: pod.Spec.NodeName,
PodIP: pod.Status.PodIP,
ContName: container.Name,
Image: container.Image,
Env: toMapInterface(env),
}
hash, err := calcHash(target)
if err != nil {
continue
}
target.hash = hash

return []model.Target{target}
}

for _, port := range container.Ports {
portNum := strconv.FormatUint(uint64(port.ContainerPort), 10)
target := &PodTarget{
tuid: podTUID(pod, container, port),
tuid: podTUIDWithPort(pod, container, port),
Address: net.JoinHostPort(pod.Status.PodIP, portNum),
Namespace: pod.Namespace,
Name: pod.Name,
Expand Down Expand Up @@ -299,7 +322,15 @@ func (p Pod) envFromSecret(vars map[string]string, ns string, src apiv1.EnvFromS
}
}

func podTUID(pod *apiv1.Pod, container apiv1.Container, port apiv1.ContainerPort) string {
func podTUID(pod *apiv1.Pod, container apiv1.Container) string {
return fmt.Sprintf("%s_%s_%s",
pod.Namespace,
pod.Name,
container.Name,
)
}

func podTUIDWithPort(pod *apiv1.Pod, container apiv1.Container, port apiv1.ContainerPort) string {
return fmt.Sprintf("%s_%s_%s_%s_%s",
pod.Namespace,
pod.Name,
Expand Down
2 changes: 1 addition & 1 deletion pipeline/discovery/kubernetes/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ func preparePodGroup(pod *apiv1.Pod) *podGroup {
for _, port := range container.Ports {
portNum := strconv.FormatUint(uint64(port.ContainerPort), 10)
target := &PodTarget{
tuid: podTUID(pod, container, port),
tuid: podTUIDWithPort(pod, container, port),
Address: net.JoinHostPort(pod.Status.PodIP, portNum),
Namespace: pod.Namespace,
Name: pod.Name,
Expand Down

0 comments on commit a43e133

Please sign in to comment.