mirror of
https://github.com/prometheus/prometheus.git
synced 2025-02-21 03:16:00 -08:00
sd k8s: support sidecar containers in endpoint discovery
Sidecar containers are a newish feature in k8s. They're implemented similar to init containers but actually stay running and allow you to delay startup of your application pod until the sidecar started (like init containers always do). This adds the ports of the sidecar container to the list of discovered endpoint(slice), allowing you to target those containers as well. The implementation is a copy of that of Pod discovery fixes: #14927 Signed-off-by: bas smit <bsmit@bol.com>
This commit is contained in:
parent
7a90d73fa6
commit
a10dc9298e
|
@ -361,16 +361,19 @@ func (e *Endpoints) buildEndpoints(eps *apiv1.Endpoints) *targetgroup.Group {
|
|||
target = target.Merge(podLabels(pod))
|
||||
|
||||
// Attach potential container port labels matching the endpoint port.
|
||||
for _, c := range pod.Spec.Containers {
|
||||
containers := append(pod.Spec.Containers, pod.Spec.InitContainers...)
|
||||
for i, c := range containers {
|
||||
for _, cport := range c.Ports {
|
||||
if port.Port == cport.ContainerPort {
|
||||
ports := strconv.FormatUint(uint64(port.Port), 10)
|
||||
isInit := i >= len(pod.Spec.Containers)
|
||||
|
||||
target[podContainerNameLabel] = lv(c.Name)
|
||||
target[podContainerImageLabel] = lv(c.Image)
|
||||
target[podContainerPortNameLabel] = lv(cport.Name)
|
||||
target[podContainerPortNumberLabel] = lv(ports)
|
||||
target[podContainerPortProtocolLabel] = lv(string(port.Protocol))
|
||||
target[podContainerIsInit] = lv(strconv.FormatBool(isInit))
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -411,7 +414,8 @@ func (e *Endpoints) buildEndpoints(eps *apiv1.Endpoints) *targetgroup.Group {
|
|||
continue
|
||||
}
|
||||
|
||||
for _, c := range pe.pod.Spec.Containers {
|
||||
containers := append(pe.pod.Spec.Containers, pe.pod.Spec.InitContainers...)
|
||||
for i, c := range containers {
|
||||
for _, cport := range c.Ports {
|
||||
hasSeenPort := func() bool {
|
||||
for _, eport := range pe.servicePorts {
|
||||
|
@ -428,6 +432,7 @@ func (e *Endpoints) buildEndpoints(eps *apiv1.Endpoints) *targetgroup.Group {
|
|||
a := net.JoinHostPort(pe.pod.Status.PodIP, strconv.FormatUint(uint64(cport.ContainerPort), 10))
|
||||
ports := strconv.FormatUint(uint64(cport.ContainerPort), 10)
|
||||
|
||||
isInit := i >= len(pe.pod.Spec.Containers)
|
||||
target := model.LabelSet{
|
||||
model.AddressLabel: lv(a),
|
||||
podContainerNameLabel: lv(c.Name),
|
||||
|
@ -435,6 +440,7 @@ func (e *Endpoints) buildEndpoints(eps *apiv1.Endpoints) *targetgroup.Group {
|
|||
podContainerPortNameLabel: lv(cport.Name),
|
||||
podContainerPortNumberLabel: lv(ports),
|
||||
podContainerPortProtocolLabel: lv(string(cport.Protocol)),
|
||||
podContainerIsInit: lv(strconv.FormatBool(isInit)),
|
||||
}
|
||||
tg.Targets = append(tg.Targets, target.Merge(podLabels(pe.pod)))
|
||||
}
|
||||
|
|
|
@ -377,19 +377,23 @@ func (e *EndpointSlice) buildEndpointSlice(eps endpointSliceAdaptor) *targetgrou
|
|||
target = target.Merge(podLabels(pod))
|
||||
|
||||
// Attach potential container port labels matching the endpoint port.
|
||||
for _, c := range pod.Spec.Containers {
|
||||
containers := append(pod.Spec.Containers, pod.Spec.InitContainers...)
|
||||
for i, c := range containers {
|
||||
for _, cport := range c.Ports {
|
||||
if port.port() == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if *port.port() == cport.ContainerPort {
|
||||
ports := strconv.FormatUint(uint64(*port.port()), 10)
|
||||
isInit := i >= len(pod.Spec.Containers)
|
||||
|
||||
target[podContainerNameLabel] = lv(c.Name)
|
||||
target[podContainerImageLabel] = lv(c.Image)
|
||||
target[podContainerPortNameLabel] = lv(cport.Name)
|
||||
target[podContainerPortNumberLabel] = lv(ports)
|
||||
target[podContainerPortProtocolLabel] = lv(string(cport.Protocol))
|
||||
target[podContainerIsInit] = lv(strconv.FormatBool(isInit))
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -417,7 +421,8 @@ func (e *EndpointSlice) buildEndpointSlice(eps endpointSliceAdaptor) *targetgrou
|
|||
continue
|
||||
}
|
||||
|
||||
for _, c := range pe.pod.Spec.Containers {
|
||||
containers := append(pe.pod.Spec.Containers, pe.pod.Spec.InitContainers...)
|
||||
for i, c := range containers {
|
||||
for _, cport := range c.Ports {
|
||||
hasSeenPort := func() bool {
|
||||
for _, eport := range pe.servicePorts {
|
||||
|
@ -437,6 +442,7 @@ func (e *EndpointSlice) buildEndpointSlice(eps endpointSliceAdaptor) *targetgrou
|
|||
a := net.JoinHostPort(pe.pod.Status.PodIP, strconv.FormatUint(uint64(cport.ContainerPort), 10))
|
||||
ports := strconv.FormatUint(uint64(cport.ContainerPort), 10)
|
||||
|
||||
isInit := i >= len(pe.pod.Spec.Containers)
|
||||
target := model.LabelSet{
|
||||
model.AddressLabel: lv(a),
|
||||
podContainerNameLabel: lv(c.Name),
|
||||
|
@ -444,6 +450,7 @@ func (e *EndpointSlice) buildEndpointSlice(eps endpointSliceAdaptor) *targetgrou
|
|||
podContainerPortNameLabel: lv(cport.Name),
|
||||
podContainerPortNumberLabel: lv(ports),
|
||||
podContainerPortProtocolLabel: lv(string(cport.Protocol)),
|
||||
podContainerIsInit: lv(strconv.FormatBool(isInit)),
|
||||
}
|
||||
tg.Targets = append(tg.Targets, target.Merge(podLabels(pe.pod)))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue