2016-07-01 03:17:17 -07:00
|
|
|
// Copyright 2016 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 (
|
2017-10-24 21:21:42 -07:00
|
|
|
"context"
|
2016-07-01 03:17:17 -07:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2017-08-11 11:45:52 -07:00
|
|
|
"github.com/go-kit/kit/log"
|
|
|
|
"github.com/go-kit/kit/log/level"
|
2016-07-01 03:17:17 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2017-05-11 01:29:10 -07:00
|
|
|
"k8s.io/client-go/pkg/api"
|
|
|
|
apiv1 "k8s.io/client-go/pkg/api/v1"
|
|
|
|
"k8s.io/client-go/tools/cache"
|
2017-10-24 21:21:42 -07:00
|
|
|
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
2017-10-24 21:21:42 -07:00
|
|
|
"github.com/prometheus/prometheus/util/strutil"
|
2016-07-01 03:17:17 -07:00
|
|
|
)
|
|
|
|
|
2016-10-07 05:53:11 -07:00
|
|
|
// Pod discovers new pod targets.
|
|
|
|
type Pod struct {
|
|
|
|
informer cache.SharedInformer
|
|
|
|
store cache.Store
|
|
|
|
logger log.Logger
|
2016-07-01 03:17:17 -07:00
|
|
|
}
|
|
|
|
|
2016-10-17 02:05:13 -07:00
|
|
|
// NewPod creates a new pod discovery.
|
|
|
|
func NewPod(l log.Logger, pods cache.SharedInformer) *Pod {
|
2017-08-11 11:45:52 -07:00
|
|
|
if l == nil {
|
|
|
|
l = log.NewNopLogger()
|
|
|
|
}
|
2016-10-07 05:53:11 -07:00
|
|
|
return &Pod{
|
|
|
|
informer: pods,
|
|
|
|
store: pods.GetStore(),
|
|
|
|
logger: l,
|
2016-07-01 03:17:17 -07:00
|
|
|
}
|
2016-10-07 05:53:11 -07:00
|
|
|
}
|
2016-07-01 03:17:17 -07:00
|
|
|
|
2018-01-08 15:59:18 -08:00
|
|
|
// Run implements the Discoverer interface.
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
func (p *Pod) Run(ctx context.Context, ch chan<- []*targetgroup.Group) {
|
2016-10-07 05:53:11 -07:00
|
|
|
// Send full initial set of pod targets.
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
var initial []*targetgroup.Group
|
2016-10-07 05:53:11 -07:00
|
|
|
for _, o := range p.store.List() {
|
|
|
|
tg := p.buildPod(o.(*apiv1.Pod))
|
|
|
|
initial = append(initial, tg)
|
2016-07-01 03:17:17 -07:00
|
|
|
|
2017-08-11 11:45:52 -07:00
|
|
|
level.Debug(p.logger).Log("msg", "initial pod", "tg", fmt.Sprintf("%#v", tg))
|
2016-10-07 05:53:11 -07:00
|
|
|
}
|
2016-07-01 03:17:17 -07:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2016-10-07 05:53:11 -07:00
|
|
|
case ch <- initial:
|
2016-07-01 03:17:17 -07:00
|
|
|
}
|
|
|
|
|
2016-10-07 05:53:11 -07:00
|
|
|
// Send target groups for pod updates.
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
send := func(tg *targetgroup.Group) {
|
2018-01-30 07:00:33 -08:00
|
|
|
if tg == nil {
|
|
|
|
return
|
|
|
|
}
|
2017-08-11 11:45:52 -07:00
|
|
|
level.Debug(p.logger).Log("msg", "pod update", "tg", fmt.Sprintf("%#v", tg))
|
2016-07-01 03:17:17 -07:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
case ch <- []*targetgroup.Group{tg}:
|
2016-07-01 03:17:17 -07:00
|
|
|
}
|
|
|
|
}
|
2016-10-07 05:53:11 -07:00
|
|
|
p.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
|
|
|
|
AddFunc: func(o interface{}) {
|
2016-10-21 01:48:28 -07:00
|
|
|
eventCount.WithLabelValues("pod", "add").Inc()
|
2016-11-21 02:44:48 -08:00
|
|
|
|
2016-11-14 07:21:38 -08:00
|
|
|
pod, err := convertToPod(o)
|
|
|
|
if err != nil {
|
2017-08-11 11:45:52 -07:00
|
|
|
level.Error(p.logger).Log("msg", "converting to Pod object failed", "err", err)
|
2016-11-14 07:21:38 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
send(p.buildPod(pod))
|
2016-10-07 05:53:11 -07:00
|
|
|
},
|
|
|
|
DeleteFunc: func(o interface{}) {
|
2016-10-21 01:48:28 -07:00
|
|
|
eventCount.WithLabelValues("pod", "delete").Inc()
|
2016-11-21 02:44:48 -08:00
|
|
|
|
2016-11-14 07:21:38 -08:00
|
|
|
pod, err := convertToPod(o)
|
|
|
|
if err != nil {
|
2017-08-11 11:45:52 -07:00
|
|
|
level.Error(p.logger).Log("msg", "converting to Pod object failed", "err", err)
|
2016-11-14 07:21:38 -08:00
|
|
|
return
|
|
|
|
}
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
send(&targetgroup.Group{Source: podSource(pod)})
|
2016-10-07 05:53:11 -07:00
|
|
|
},
|
|
|
|
UpdateFunc: func(_, o interface{}) {
|
2016-10-21 01:48:28 -07:00
|
|
|
eventCount.WithLabelValues("pod", "update").Inc()
|
2016-11-21 02:44:48 -08:00
|
|
|
|
2016-11-14 07:21:38 -08:00
|
|
|
pod, err := convertToPod(o)
|
|
|
|
if err != nil {
|
2017-08-11 11:45:52 -07:00
|
|
|
level.Error(p.logger).Log("msg", "converting to Pod object failed", "err", err)
|
2016-11-14 07:21:38 -08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
send(p.buildPod(pod))
|
2016-10-07 05:53:11 -07:00
|
|
|
},
|
|
|
|
})
|
2016-07-01 03:17:17 -07:00
|
|
|
|
2016-10-07 05:53:11 -07:00
|
|
|
// Block until the target provider is explicitly canceled.
|
|
|
|
<-ctx.Done()
|
2016-07-01 03:17:17 -07:00
|
|
|
}
|
|
|
|
|
2016-11-14 07:21:38 -08:00
|
|
|
func convertToPod(o interface{}) (*apiv1.Pod, error) {
|
2017-09-04 04:10:44 -07:00
|
|
|
pod, ok := o.(*apiv1.Pod)
|
|
|
|
if ok {
|
|
|
|
return pod, nil
|
2016-11-14 07:21:38 -08:00
|
|
|
}
|
|
|
|
|
2017-09-04 04:10:44 -07:00
|
|
|
deletedState, ok := o.(cache.DeletedFinalStateUnknown)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("Received unexpected object: %v", o)
|
|
|
|
}
|
|
|
|
pod, ok = deletedState.Obj.(*apiv1.Pod)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("DeletedFinalStateUnknown contained non-Pod object: %v", deletedState.Obj)
|
|
|
|
}
|
2016-11-14 07:21:38 -08:00
|
|
|
return pod, nil
|
|
|
|
}
|
|
|
|
|
2016-10-07 05:53:11 -07:00
|
|
|
const (
|
|
|
|
podNameLabel = metaLabelPrefix + "pod_name"
|
|
|
|
podIPLabel = metaLabelPrefix + "pod_ip"
|
|
|
|
podContainerNameLabel = metaLabelPrefix + "pod_container_name"
|
|
|
|
podContainerPortNameLabel = metaLabelPrefix + "pod_container_port_name"
|
|
|
|
podContainerPortNumberLabel = metaLabelPrefix + "pod_container_port_number"
|
|
|
|
podContainerPortProtocolLabel = metaLabelPrefix + "pod_container_port_protocol"
|
|
|
|
podReadyLabel = metaLabelPrefix + "pod_ready"
|
|
|
|
podLabelPrefix = metaLabelPrefix + "pod_label_"
|
|
|
|
podAnnotationPrefix = metaLabelPrefix + "pod_annotation_"
|
|
|
|
podNodeNameLabel = metaLabelPrefix + "pod_node_name"
|
|
|
|
podHostIPLabel = metaLabelPrefix + "pod_host_ip"
|
2017-11-07 08:24:23 -08:00
|
|
|
podUID = metaLabelPrefix + "pod_uid"
|
2016-10-07 05:53:11 -07:00
|
|
|
)
|
2016-07-01 03:17:17 -07:00
|
|
|
|
2016-10-07 05:53:11 -07:00
|
|
|
func podLabels(pod *apiv1.Pod) model.LabelSet {
|
|
|
|
ls := model.LabelSet{
|
|
|
|
podNameLabel: lv(pod.ObjectMeta.Name),
|
|
|
|
podIPLabel: lv(pod.Status.PodIP),
|
|
|
|
podReadyLabel: podReady(pod),
|
|
|
|
podNodeNameLabel: lv(pod.Spec.NodeName),
|
|
|
|
podHostIPLabel: lv(pod.Status.HostIP),
|
2017-11-07 08:24:23 -08:00
|
|
|
podUID: lv(string(pod.ObjectMeta.UID)),
|
2016-07-01 03:17:17 -07:00
|
|
|
}
|
|
|
|
|
2016-10-07 05:53:11 -07:00
|
|
|
for k, v := range pod.Labels {
|
2016-10-11 00:22:56 -07:00
|
|
|
ln := strutil.SanitizeLabelName(podLabelPrefix + k)
|
2016-10-07 05:53:11 -07:00
|
|
|
ls[model.LabelName(ln)] = lv(v)
|
2016-07-01 03:17:17 -07:00
|
|
|
}
|
|
|
|
|
2016-10-07 05:53:11 -07:00
|
|
|
for k, v := range pod.Annotations {
|
2016-10-11 00:22:56 -07:00
|
|
|
ln := strutil.SanitizeLabelName(podAnnotationPrefix + k)
|
2016-10-07 05:53:11 -07:00
|
|
|
ls[model.LabelName(ln)] = lv(v)
|
2016-07-01 03:17:17 -07:00
|
|
|
}
|
|
|
|
|
2016-10-07 05:53:11 -07:00
|
|
|
return ls
|
2016-07-01 03:17:17 -07:00
|
|
|
}
|
|
|
|
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
func (p *Pod) buildPod(pod *apiv1.Pod) *targetgroup.Group {
|
2016-10-07 05:53:11 -07:00
|
|
|
// During startup the pod may not have an IP yet. This does not even allow
|
|
|
|
// for an up metric, so we skip the target.
|
|
|
|
if len(pod.Status.PodIP) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
tg := &targetgroup.Group{
|
2016-10-07 05:53:11 -07:00
|
|
|
Source: podSource(pod),
|
2016-07-01 03:17:17 -07:00
|
|
|
}
|
2016-10-07 05:53:11 -07:00
|
|
|
tg.Labels = podLabels(pod)
|
|
|
|
tg.Labels[namespaceLabel] = lv(pod.Namespace)
|
|
|
|
|
|
|
|
for _, c := range pod.Spec.Containers {
|
|
|
|
// If no ports are defined for the container, create an anonymous
|
|
|
|
// target per container.
|
|
|
|
if len(c.Ports) == 0 {
|
|
|
|
// We don't have a port so we just set the address label to the pod IP.
|
|
|
|
// The user has to add a port manually.
|
|
|
|
tg.Targets = append(tg.Targets, model.LabelSet{
|
|
|
|
model.AddressLabel: lv(pod.Status.PodIP),
|
|
|
|
podContainerNameLabel: lv(c.Name),
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Otherwise create one target for each container/port combination.
|
|
|
|
for _, port := range c.Ports {
|
2016-10-17 02:05:13 -07:00
|
|
|
ports := strconv.FormatUint(uint64(port.ContainerPort), 10)
|
2016-10-07 05:53:11 -07:00
|
|
|
addr := net.JoinHostPort(pod.Status.PodIP, ports)
|
2016-07-01 03:17:17 -07:00
|
|
|
|
2016-10-07 05:53:11 -07:00
|
|
|
tg.Targets = append(tg.Targets, model.LabelSet{
|
|
|
|
model.AddressLabel: lv(addr),
|
|
|
|
podContainerNameLabel: lv(c.Name),
|
|
|
|
podContainerPortNumberLabel: lv(ports),
|
|
|
|
podContainerPortNameLabel: lv(port.Name),
|
|
|
|
podContainerPortProtocolLabel: lv(string(port.Protocol)),
|
|
|
|
})
|
2016-07-01 03:17:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tg
|
|
|
|
}
|
|
|
|
|
2016-10-07 05:53:11 -07:00
|
|
|
func podSource(pod *apiv1.Pod) string {
|
|
|
|
return "pod/" + pod.Namespace + "/" + pod.Name
|
2016-07-01 03:17:17 -07:00
|
|
|
}
|
|
|
|
|
2016-10-07 05:53:11 -07:00
|
|
|
func podReady(pod *apiv1.Pod) model.LabelValue {
|
|
|
|
for _, cond := range pod.Status.Conditions {
|
|
|
|
if cond.Type == apiv1.PodReady {
|
|
|
|
return lv(strings.ToLower(string(cond.Status)))
|
2016-07-01 03:17:17 -07:00
|
|
|
}
|
|
|
|
}
|
2016-10-07 05:53:11 -07:00
|
|
|
return lv(strings.ToLower(string(api.ConditionUnknown)))
|
2016-07-01 03:17:17 -07:00
|
|
|
}
|