2020-02-16 06:59:29 -08:00
|
|
|
// Copyright 2020 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-31 04:46:55 -07:00
|
|
|
"errors"
|
2022-06-03 04:47:14 -07:00
|
|
|
"fmt"
|
2020-02-16 06:59:29 -08:00
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
|
2021-06-11 09:17:59 -07:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2023-10-23 06:55:36 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2020-02-16 06:59:29 -08:00
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
apiv1 "k8s.io/api/core/v1"
|
2021-10-22 12:26:31 -07:00
|
|
|
v1 "k8s.io/api/discovery/v1"
|
|
|
|
"k8s.io/api/discovery/v1beta1"
|
2020-02-16 06:59:29 -08:00
|
|
|
"k8s.io/client-go/tools/cache"
|
|
|
|
"k8s.io/client-go/util/workqueue"
|
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
|
|
|
"github.com/prometheus/prometheus/util/strutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EndpointSlice discovers new endpoint targets.
|
|
|
|
type EndpointSlice struct {
|
|
|
|
logger log.Logger
|
|
|
|
|
2022-05-27 02:45:09 -07:00
|
|
|
endpointSliceInf cache.SharedIndexInformer
|
2020-02-16 06:59:29 -08:00
|
|
|
serviceInf cache.SharedInformer
|
|
|
|
podInf cache.SharedInformer
|
2022-05-27 02:45:09 -07:00
|
|
|
nodeInf cache.SharedInformer
|
|
|
|
withNodeMetadata bool
|
2020-02-16 06:59:29 -08:00
|
|
|
|
|
|
|
podStore cache.Store
|
|
|
|
endpointSliceStore cache.Store
|
|
|
|
serviceStore cache.Store
|
|
|
|
|
|
|
|
queue *workqueue.Type
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEndpointSlice returns a new endpointslice discovery.
|
2023-10-23 06:55:36 -07:00
|
|
|
func NewEndpointSlice(l log.Logger, eps cache.SharedIndexInformer, svc, pod, node cache.SharedInformer, eventCount *prometheus.CounterVec) *EndpointSlice {
|
2020-02-16 06:59:29 -08:00
|
|
|
if l == nil {
|
|
|
|
l = log.NewNopLogger()
|
|
|
|
}
|
2023-12-11 05:12:43 -08:00
|
|
|
|
|
|
|
epslAddCount := eventCount.WithLabelValues("endpointslice", "add")
|
|
|
|
epslUpdateCount := eventCount.WithLabelValues("endpointslice", "update")
|
|
|
|
epslDeleteCount := eventCount.WithLabelValues("endpointslice", "delete")
|
|
|
|
|
|
|
|
svcAddCount := eventCount.WithLabelValues("service", "add")
|
|
|
|
svcUpdateCount := eventCount.WithLabelValues("service", "update")
|
|
|
|
svcDeleteCount := eventCount.WithLabelValues("service", "delete")
|
|
|
|
|
2020-02-16 06:59:29 -08:00
|
|
|
e := &EndpointSlice{
|
|
|
|
logger: l,
|
|
|
|
endpointSliceInf: eps,
|
|
|
|
endpointSliceStore: eps.GetStore(),
|
|
|
|
serviceInf: svc,
|
|
|
|
serviceStore: svc.GetStore(),
|
|
|
|
podInf: pod,
|
|
|
|
podStore: pod.GetStore(),
|
2022-05-27 02:45:09 -07:00
|
|
|
nodeInf: node,
|
|
|
|
withNodeMetadata: node != nil,
|
2020-02-16 06:59:29 -08:00
|
|
|
queue: workqueue.NewNamed("endpointSlice"),
|
|
|
|
}
|
|
|
|
|
2022-12-14 01:43:53 -08:00
|
|
|
_, err := e.endpointSliceInf.AddEventHandler(cache.ResourceEventHandlerFuncs{
|
2020-02-16 06:59:29 -08:00
|
|
|
AddFunc: func(o interface{}) {
|
2023-12-11 05:12:43 -08:00
|
|
|
epslAddCount.Inc()
|
2020-02-16 06:59:29 -08:00
|
|
|
e.enqueue(o)
|
|
|
|
},
|
|
|
|
UpdateFunc: func(_, o interface{}) {
|
2023-12-11 05:12:43 -08:00
|
|
|
epslUpdateCount.Inc()
|
2020-02-16 06:59:29 -08:00
|
|
|
e.enqueue(o)
|
|
|
|
},
|
|
|
|
DeleteFunc: func(o interface{}) {
|
2023-12-11 05:12:43 -08:00
|
|
|
epslDeleteCount.Inc()
|
2020-02-16 06:59:29 -08:00
|
|
|
e.enqueue(o)
|
|
|
|
},
|
|
|
|
})
|
2022-12-14 01:43:53 -08:00
|
|
|
if err != nil {
|
|
|
|
level.Error(l).Log("msg", "Error adding endpoint slices event handler.", "err", err)
|
|
|
|
}
|
2020-02-16 06:59:29 -08:00
|
|
|
|
|
|
|
serviceUpdate := func(o interface{}) {
|
|
|
|
svc, err := convertToService(o)
|
|
|
|
if err != nil {
|
|
|
|
level.Error(e.logger).Log("msg", "converting to Service object failed", "err", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(brancz): use cache.Indexer to index endpoints by
|
|
|
|
// disv1beta1.LabelServiceName so this operation doesn't have to
|
|
|
|
// iterate over all endpoint objects.
|
|
|
|
for _, obj := range e.endpointSliceStore.List() {
|
2021-10-22 12:26:31 -07:00
|
|
|
esa, err := e.getEndpointSliceAdaptor(obj)
|
|
|
|
if err != nil {
|
|
|
|
level.Error(e.logger).Log("msg", "converting to EndpointSlice object failed", "err", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if lv, exists := esa.labels()[esa.labelServiceName()]; exists && lv == svc.Name {
|
|
|
|
e.enqueue(esa.get())
|
2020-02-16 06:59:29 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-14 01:43:53 -08:00
|
|
|
_, err = e.serviceInf.AddEventHandler(cache.ResourceEventHandlerFuncs{
|
2020-02-16 06:59:29 -08:00
|
|
|
AddFunc: func(o interface{}) {
|
2023-12-11 05:12:43 -08:00
|
|
|
svcAddCount.Inc()
|
2020-02-16 06:59:29 -08:00
|
|
|
serviceUpdate(o)
|
|
|
|
},
|
|
|
|
UpdateFunc: func(_, o interface{}) {
|
2023-12-11 05:12:43 -08:00
|
|
|
svcUpdateCount.Inc()
|
2020-02-16 06:59:29 -08:00
|
|
|
serviceUpdate(o)
|
|
|
|
},
|
|
|
|
DeleteFunc: func(o interface{}) {
|
2023-12-11 05:12:43 -08:00
|
|
|
svcDeleteCount.Inc()
|
2020-02-16 06:59:29 -08:00
|
|
|
serviceUpdate(o)
|
|
|
|
},
|
|
|
|
})
|
2022-12-14 01:43:53 -08:00
|
|
|
if err != nil {
|
|
|
|
level.Error(l).Log("msg", "Error adding services event handler.", "err", err)
|
|
|
|
}
|
2020-02-16 06:59:29 -08:00
|
|
|
|
2022-05-27 02:45:09 -07:00
|
|
|
if e.withNodeMetadata {
|
2022-12-14 01:43:53 -08:00
|
|
|
_, err = e.nodeInf.AddEventHandler(cache.ResourceEventHandlerFuncs{
|
2022-05-27 02:45:09 -07:00
|
|
|
AddFunc: func(o interface{}) {
|
|
|
|
node := o.(*apiv1.Node)
|
|
|
|
e.enqueueNode(node.Name)
|
|
|
|
},
|
|
|
|
UpdateFunc: func(_, o interface{}) {
|
|
|
|
node := o.(*apiv1.Node)
|
|
|
|
e.enqueueNode(node.Name)
|
|
|
|
},
|
|
|
|
DeleteFunc: func(o interface{}) {
|
|
|
|
node := o.(*apiv1.Node)
|
|
|
|
e.enqueueNode(node.Name)
|
|
|
|
},
|
|
|
|
})
|
2022-12-14 01:43:53 -08:00
|
|
|
if err != nil {
|
|
|
|
level.Error(l).Log("msg", "Error adding nodes event handler.", "err", err)
|
|
|
|
}
|
2022-05-27 02:45:09 -07:00
|
|
|
}
|
|
|
|
|
2020-02-16 06:59:29 -08:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2022-05-27 02:45:09 -07:00
|
|
|
func (e *EndpointSlice) enqueueNode(nodeName string) {
|
|
|
|
endpoints, err := e.endpointSliceInf.GetIndexer().ByIndex(nodeIndex, nodeName)
|
|
|
|
if err != nil {
|
|
|
|
level.Error(e.logger).Log("msg", "Error getting endpoints for node", "node", nodeName, "err", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, endpoint := range endpoints {
|
|
|
|
e.enqueue(endpoint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 06:59:29 -08:00
|
|
|
func (e *EndpointSlice) enqueue(obj interface{}) {
|
|
|
|
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
e.queue.Add(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run implements the Discoverer interface.
|
|
|
|
func (e *EndpointSlice) Run(ctx context.Context, ch chan<- []*targetgroup.Group) {
|
|
|
|
defer e.queue.ShutDown()
|
|
|
|
|
2022-05-27 02:45:09 -07:00
|
|
|
cacheSyncs := []cache.InformerSynced{e.endpointSliceInf.HasSynced, e.serviceInf.HasSynced, e.podInf.HasSynced}
|
|
|
|
if e.withNodeMetadata {
|
|
|
|
cacheSyncs = append(cacheSyncs, e.nodeInf.HasSynced)
|
|
|
|
}
|
|
|
|
if !cache.WaitForCacheSync(ctx.Done(), cacheSyncs...) {
|
2023-10-31 04:46:55 -07:00
|
|
|
if !errors.Is(ctx.Err(), context.Canceled) {
|
2020-02-16 06:59:29 -08:00
|
|
|
level.Error(e.logger).Log("msg", "endpointslice informer unable to sync cache")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2023-10-31 04:35:13 -07:00
|
|
|
for e.process(ctx, ch) {
|
2020-02-16 06:59:29 -08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Block until the target provider is explicitly canceled.
|
|
|
|
<-ctx.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EndpointSlice) process(ctx context.Context, ch chan<- []*targetgroup.Group) bool {
|
|
|
|
keyObj, quit := e.queue.Get()
|
|
|
|
if quit {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer e.queue.Done(keyObj)
|
|
|
|
key := keyObj.(string)
|
|
|
|
|
|
|
|
namespace, name, err := cache.SplitMetaNamespaceKey(key)
|
|
|
|
if err != nil {
|
|
|
|
level.Error(e.logger).Log("msg", "splitting key failed", "key", key)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
o, exists, err := e.endpointSliceStore.GetByKey(key)
|
|
|
|
if err != nil {
|
|
|
|
level.Error(e.logger).Log("msg", "getting object from store failed", "key", key)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
send(ctx, ch, &targetgroup.Group{Source: endpointSliceSourceFromNamespaceAndName(namespace, name)})
|
|
|
|
return true
|
|
|
|
}
|
2021-10-22 12:26:31 -07:00
|
|
|
|
|
|
|
esa, err := e.getEndpointSliceAdaptor(o)
|
2020-02-16 06:59:29 -08:00
|
|
|
if err != nil {
|
|
|
|
level.Error(e.logger).Log("msg", "converting to EndpointSlice object failed", "err", err)
|
|
|
|
return true
|
|
|
|
}
|
2021-10-22 12:26:31 -07:00
|
|
|
|
|
|
|
send(ctx, ch, e.buildEndpointSlice(esa))
|
2020-02-16 06:59:29 -08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-10-22 12:26:31 -07:00
|
|
|
func (e *EndpointSlice) getEndpointSliceAdaptor(o interface{}) (endpointSliceAdaptor, error) {
|
|
|
|
switch endpointSlice := o.(type) {
|
|
|
|
case *v1.EndpointSlice:
|
|
|
|
return newEndpointSliceAdaptorFromV1(endpointSlice), nil
|
|
|
|
case *v1beta1.EndpointSlice:
|
|
|
|
return newEndpointSliceAdaptorFromV1beta1(endpointSlice), nil
|
|
|
|
default:
|
2022-06-03 04:47:14 -07:00
|
|
|
return nil, fmt.Errorf("received unexpected object: %v", o)
|
2020-02-16 06:59:29 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 12:26:31 -07:00
|
|
|
func endpointSliceSource(ep endpointSliceAdaptor) string {
|
|
|
|
return endpointSliceSourceFromNamespaceAndName(ep.namespace(), ep.name())
|
2020-02-16 06:59:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func endpointSliceSourceFromNamespaceAndName(namespace, name string) string {
|
|
|
|
return "endpointslice/" + namespace + "/" + name
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
endpointSliceAddressTypeLabel = metaLabelPrefix + "endpointslice_address_type"
|
|
|
|
endpointSlicePortNameLabel = metaLabelPrefix + "endpointslice_port_name"
|
|
|
|
endpointSlicePortProtocolLabel = metaLabelPrefix + "endpointslice_port_protocol"
|
|
|
|
endpointSlicePortLabel = metaLabelPrefix + "endpointslice_port"
|
|
|
|
endpointSlicePortAppProtocol = metaLabelPrefix + "endpointslice_port_app_protocol"
|
|
|
|
endpointSliceEndpointConditionsReadyLabel = metaLabelPrefix + "endpointslice_endpoint_conditions_ready"
|
2023-01-19 08:56:53 -08:00
|
|
|
endpointSliceEndpointConditionsServingLabel = metaLabelPrefix + "endpointslice_endpoint_conditions_serving"
|
|
|
|
endpointSliceEndpointConditionsTerminatingLabel = metaLabelPrefix + "endpointslice_endpoint_conditions_terminating"
|
2020-02-16 06:59:29 -08:00
|
|
|
endpointSliceEndpointHostnameLabel = metaLabelPrefix + "endpointslice_endpoint_hostname"
|
|
|
|
endpointSliceAddressTargetKindLabel = metaLabelPrefix + "endpointslice_address_target_kind"
|
|
|
|
endpointSliceAddressTargetNameLabel = metaLabelPrefix + "endpointslice_address_target_name"
|
|
|
|
endpointSliceEndpointTopologyLabelPrefix = metaLabelPrefix + "endpointslice_endpoint_topology_"
|
|
|
|
endpointSliceEndpointTopologyLabelPresentPrefix = metaLabelPrefix + "endpointslice_endpoint_topology_present_"
|
|
|
|
)
|
|
|
|
|
2021-10-22 12:26:31 -07:00
|
|
|
func (e *EndpointSlice) buildEndpointSlice(eps endpointSliceAdaptor) *targetgroup.Group {
|
2020-02-16 06:59:29 -08:00
|
|
|
tg := &targetgroup.Group{
|
|
|
|
Source: endpointSliceSource(eps),
|
|
|
|
}
|
|
|
|
tg.Labels = model.LabelSet{
|
2021-10-22 12:26:31 -07:00
|
|
|
namespaceLabel: lv(eps.namespace()),
|
|
|
|
endpointSliceAddressTypeLabel: lv(eps.addressType()),
|
2020-02-16 06:59:29 -08:00
|
|
|
}
|
2023-05-30 05:13:00 -07:00
|
|
|
|
|
|
|
addObjectMetaLabels(tg.Labels, eps.getObjectMeta(), RoleEndpointSlice)
|
|
|
|
|
2020-02-16 06:59:29 -08:00
|
|
|
e.addServiceLabels(eps, tg)
|
|
|
|
|
|
|
|
type podEntry struct {
|
|
|
|
pod *apiv1.Pod
|
2021-10-22 12:26:31 -07:00
|
|
|
servicePorts []endpointSlicePortAdaptor
|
2020-02-16 06:59:29 -08:00
|
|
|
}
|
|
|
|
seenPods := map[string]*podEntry{}
|
|
|
|
|
2021-10-22 12:26:31 -07:00
|
|
|
add := func(addr string, ep endpointSliceEndpointAdaptor, port endpointSlicePortAdaptor) {
|
2020-02-16 06:59:29 -08:00
|
|
|
a := addr
|
2021-10-22 12:26:31 -07:00
|
|
|
if port.port() != nil {
|
|
|
|
a = net.JoinHostPort(addr, strconv.FormatUint(uint64(*port.port()), 10))
|
2020-02-16 06:59:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
target := model.LabelSet{
|
|
|
|
model.AddressLabel: lv(a),
|
|
|
|
}
|
|
|
|
|
2021-10-22 12:26:31 -07:00
|
|
|
if port.name() != nil {
|
|
|
|
target[endpointSlicePortNameLabel] = lv(*port.name())
|
2020-02-16 06:59:29 -08:00
|
|
|
}
|
|
|
|
|
2021-10-22 12:26:31 -07:00
|
|
|
if port.protocol() != nil {
|
2023-04-09 00:08:40 -07:00
|
|
|
target[endpointSlicePortProtocolLabel] = lv(*port.protocol())
|
2020-02-16 06:59:29 -08:00
|
|
|
}
|
|
|
|
|
2021-10-22 12:26:31 -07:00
|
|
|
if port.port() != nil {
|
|
|
|
target[endpointSlicePortLabel] = lv(strconv.FormatUint(uint64(*port.port()), 10))
|
2020-02-16 06:59:29 -08:00
|
|
|
}
|
|
|
|
|
2021-10-22 12:26:31 -07:00
|
|
|
if port.appProtocol() != nil {
|
|
|
|
target[endpointSlicePortAppProtocol] = lv(*port.appProtocol())
|
2020-02-16 06:59:29 -08:00
|
|
|
}
|
|
|
|
|
2021-10-22 12:26:31 -07:00
|
|
|
if ep.conditions().ready() != nil {
|
|
|
|
target[endpointSliceEndpointConditionsReadyLabel] = lv(strconv.FormatBool(*ep.conditions().ready()))
|
2020-02-16 06:59:29 -08:00
|
|
|
}
|
|
|
|
|
2023-01-19 08:56:53 -08:00
|
|
|
if ep.conditions().serving() != nil {
|
|
|
|
target[endpointSliceEndpointConditionsServingLabel] = lv(strconv.FormatBool(*ep.conditions().serving()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if ep.conditions().terminating() != nil {
|
|
|
|
target[endpointSliceEndpointConditionsTerminatingLabel] = lv(strconv.FormatBool(*ep.conditions().terminating()))
|
|
|
|
}
|
|
|
|
|
2021-10-22 12:26:31 -07:00
|
|
|
if ep.hostname() != nil {
|
|
|
|
target[endpointSliceEndpointHostnameLabel] = lv(*ep.hostname())
|
2020-02-16 06:59:29 -08:00
|
|
|
}
|
|
|
|
|
2021-10-22 12:26:31 -07:00
|
|
|
if ep.targetRef() != nil {
|
|
|
|
target[model.LabelName(endpointSliceAddressTargetKindLabel)] = lv(ep.targetRef().Kind)
|
|
|
|
target[model.LabelName(endpointSliceAddressTargetNameLabel)] = lv(ep.targetRef().Name)
|
2020-02-16 06:59:29 -08:00
|
|
|
}
|
|
|
|
|
2021-10-22 12:26:31 -07:00
|
|
|
for k, v := range ep.topology() {
|
2020-02-16 06:59:29 -08:00
|
|
|
ln := strutil.SanitizeLabelName(k)
|
|
|
|
target[model.LabelName(endpointSliceEndpointTopologyLabelPrefix+ln)] = lv(v)
|
|
|
|
target[model.LabelName(endpointSliceEndpointTopologyLabelPresentPrefix+ln)] = presentValue
|
|
|
|
}
|
|
|
|
|
2022-05-27 02:45:09 -07:00
|
|
|
if e.withNodeMetadata {
|
2023-05-03 13:46:13 -07:00
|
|
|
if ep.targetRef() != nil && ep.targetRef().Kind == "Node" {
|
|
|
|
target = addNodeLabels(target, e.nodeInf, e.logger, &ep.targetRef().Name)
|
|
|
|
} else {
|
|
|
|
target = addNodeLabels(target, e.nodeInf, e.logger, ep.nodename())
|
|
|
|
}
|
2022-05-27 02:45:09 -07:00
|
|
|
}
|
|
|
|
|
2021-10-22 12:26:31 -07:00
|
|
|
pod := e.resolvePodRef(ep.targetRef())
|
2020-02-16 06:59:29 -08:00
|
|
|
if pod == nil {
|
|
|
|
// This target is not a Pod, so don't continue with Pod specific logic.
|
|
|
|
tg.Targets = append(tg.Targets, target)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s := pod.Namespace + "/" + pod.Name
|
|
|
|
|
|
|
|
sp, ok := seenPods[s]
|
|
|
|
if !ok {
|
|
|
|
sp = &podEntry{pod: pod}
|
|
|
|
seenPods[s] = sp
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attach standard pod labels.
|
|
|
|
target = target.Merge(podLabels(pod))
|
|
|
|
|
|
|
|
// Attach potential container port labels matching the endpoint port.
|
|
|
|
for _, c := range pod.Spec.Containers {
|
|
|
|
for _, cport := range c.Ports {
|
2021-10-22 12:26:31 -07:00
|
|
|
if port.port() == nil {
|
2020-02-16 06:59:29 -08:00
|
|
|
continue
|
|
|
|
}
|
2021-10-22 12:26:31 -07:00
|
|
|
if *port.port() == cport.ContainerPort {
|
|
|
|
ports := strconv.FormatUint(uint64(*port.port()), 10)
|
2020-02-16 06:59:29 -08:00
|
|
|
|
|
|
|
target[podContainerNameLabel] = lv(c.Name)
|
2022-07-18 05:31:45 -07:00
|
|
|
target[podContainerImageLabel] = lv(c.Image)
|
2020-02-16 06:59:29 -08:00
|
|
|
target[podContainerPortNameLabel] = lv(cport.Name)
|
|
|
|
target[podContainerPortNumberLabel] = lv(ports)
|
|
|
|
target[podContainerPortProtocolLabel] = lv(string(cport.Protocol))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add service port so we know that we have already generated a target
|
|
|
|
// for it.
|
|
|
|
sp.servicePorts = append(sp.servicePorts, port)
|
|
|
|
tg.Targets = append(tg.Targets, target)
|
|
|
|
}
|
|
|
|
|
2021-10-22 12:26:31 -07:00
|
|
|
for _, ep := range eps.endpoints() {
|
|
|
|
for _, port := range eps.ports() {
|
|
|
|
for _, addr := range ep.addresses() {
|
2020-02-16 06:59:29 -08:00
|
|
|
add(addr, ep, port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For all seen pods, check all container ports. If they were not covered
|
|
|
|
// by one of the service endpoints, generate targets for them.
|
|
|
|
for _, pe := range seenPods {
|
|
|
|
for _, c := range pe.pod.Spec.Containers {
|
|
|
|
for _, cport := range c.Ports {
|
|
|
|
hasSeenPort := func() bool {
|
|
|
|
for _, eport := range pe.servicePorts {
|
2021-10-22 12:26:31 -07:00
|
|
|
if eport.port() == nil {
|
2020-02-16 06:59:29 -08:00
|
|
|
continue
|
|
|
|
}
|
2021-10-22 12:26:31 -07:00
|
|
|
if cport.ContainerPort == *eport.port() {
|
2020-02-16 06:59:29 -08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if hasSeenPort() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-11-29 16:08:01 -08:00
|
|
|
// PodIP can be empty when a pod is starting or has been evicted.
|
|
|
|
if len(pe.pod.Status.PodIP) != 0 {
|
|
|
|
a := net.JoinHostPort(pe.pod.Status.PodIP, strconv.FormatUint(uint64(cport.ContainerPort), 10))
|
|
|
|
ports := strconv.FormatUint(uint64(cport.ContainerPort), 10)
|
|
|
|
|
|
|
|
target := model.LabelSet{
|
|
|
|
model.AddressLabel: lv(a),
|
|
|
|
podContainerNameLabel: lv(c.Name),
|
|
|
|
podContainerImageLabel: lv(c.Image),
|
|
|
|
podContainerPortNameLabel: lv(cport.Name),
|
|
|
|
podContainerPortNumberLabel: lv(ports),
|
|
|
|
podContainerPortProtocolLabel: lv(string(cport.Protocol)),
|
|
|
|
}
|
|
|
|
tg.Targets = append(tg.Targets, target.Merge(podLabels(pe.pod)))
|
2020-02-16 06:59:29 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tg
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *EndpointSlice) resolvePodRef(ref *apiv1.ObjectReference) *apiv1.Pod {
|
|
|
|
if ref == nil || ref.Kind != "Pod" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
p := &apiv1.Pod{}
|
|
|
|
p.Namespace = ref.Namespace
|
|
|
|
p.Name = ref.Name
|
|
|
|
|
|
|
|
obj, exists, err := e.podStore.Get(p)
|
|
|
|
if err != nil {
|
|
|
|
level.Error(e.logger).Log("msg", "resolving pod ref failed", "err", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return obj.(*apiv1.Pod)
|
|
|
|
}
|
|
|
|
|
2021-10-22 12:26:31 -07:00
|
|
|
func (e *EndpointSlice) addServiceLabels(esa endpointSliceAdaptor, tg *targetgroup.Group) {
|
2020-02-16 06:59:29 -08:00
|
|
|
var (
|
|
|
|
svc = &apiv1.Service{}
|
|
|
|
found bool
|
|
|
|
)
|
2021-10-22 12:26:31 -07:00
|
|
|
svc.Namespace = esa.namespace()
|
2020-02-16 06:59:29 -08:00
|
|
|
|
|
|
|
// Every EndpointSlice object has the Service they belong to in the
|
|
|
|
// kubernetes.io/service-name label.
|
2021-10-22 12:26:31 -07:00
|
|
|
svc.Name, found = esa.labels()[esa.labelServiceName()]
|
2020-02-16 06:59:29 -08:00
|
|
|
if !found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, exists, err := e.serviceStore.Get(svc)
|
|
|
|
if err != nil {
|
|
|
|
level.Error(e.logger).Log("msg", "retrieving service failed", "err", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
svc = obj.(*apiv1.Service)
|
|
|
|
|
|
|
|
tg.Labels = tg.Labels.Merge(serviceLabels(svc))
|
|
|
|
}
|