2015-07-18 14:23:58 -07:00
|
|
|
// Copyright 2015 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 (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2015-10-03 01:21:43 -07:00
|
|
|
"github.com/prometheus/common/log"
|
2015-08-20 08:18:46 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2016-02-18 08:26:27 -08:00
|
|
|
"golang.org/x/net/context"
|
2015-08-22 00:42:45 -07:00
|
|
|
|
2015-07-18 14:23:58 -07:00
|
|
|
"github.com/prometheus/prometheus/config"
|
|
|
|
"github.com/prometheus/prometheus/util/httputil"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// kubernetesMetaLabelPrefix is the meta prefix used for all meta labels.
|
|
|
|
// in this discovery.
|
2015-08-20 08:18:46 -07:00
|
|
|
metaLabelPrefix = model.MetaLabelPrefix + "kubernetes_"
|
2016-05-18 17:18:52 -07:00
|
|
|
|
|
|
|
// roleLabel is the name for the label containing a target's role.
|
|
|
|
roleLabel = metaLabelPrefix + "role"
|
|
|
|
|
|
|
|
sourcePodPrefix = "pods"
|
|
|
|
// podsTargetGroupNAme is the name given to the target group for pods
|
|
|
|
podsTargetGroupName = "pods"
|
|
|
|
// podNamespaceLabel is the name for the label containing a target pod's namespace
|
|
|
|
podNamespaceLabel = metaLabelPrefix + "pod_namespace"
|
|
|
|
// podNameLabel is the name for the label containing a target pod's name
|
|
|
|
podNameLabel = metaLabelPrefix + "pod_name"
|
|
|
|
// podAddressLabel is the name for the label containing a target pod's IP address (the PodIP)
|
|
|
|
podAddressLabel = metaLabelPrefix + "pod_address"
|
|
|
|
// podContainerNameLabel is the name for the label containing a target's container name
|
|
|
|
podContainerNameLabel = metaLabelPrefix + "pod_container_name"
|
|
|
|
// podContainerPortNameLabel is the name for the label containing the name of the port selected for a target
|
|
|
|
podContainerPortNameLabel = metaLabelPrefix + "pod_container_port_name"
|
|
|
|
// PodContainerPortListLabel is the name for the label containing a list of all TCP ports on the target container
|
|
|
|
podContainerPortListLabel = metaLabelPrefix + "pod_container_port_list"
|
2016-05-19 10:37:11 -07:00
|
|
|
// PodContainerPortMapPrefix is the prefix used to create the names of labels that associate container port names to port values
|
|
|
|
// Such labels will be named (podContainerPortMapPrefix)_(PortName) = (ContainerPort)
|
|
|
|
podContainerPortMapPrefix = metaLabelPrefix + "pod_container_port_map_"
|
2016-05-18 17:18:52 -07:00
|
|
|
// podReadyLabel is the name for the label containing the 'Ready' status (true/false/unknown) for a target
|
|
|
|
podReadyLabel = metaLabelPrefix + "pod_ready"
|
|
|
|
// podLabelPrefix is the prefix for prom label names corresponding to k8s labels for a target pod
|
|
|
|
podLabelPrefix = metaLabelPrefix + "pod_label_"
|
|
|
|
// podAnnotationPrefix is the prefix for prom label names corresponding to k8s annotations for a target pod
|
|
|
|
podAnnotationPrefix = metaLabelPrefix + "pod_annotation_"
|
|
|
|
|
|
|
|
sourceServicePrefix = "services"
|
2015-07-18 14:23:58 -07:00
|
|
|
// serviceNamespaceLabel is the name for the label containing a target's service namespace.
|
|
|
|
serviceNamespaceLabel = metaLabelPrefix + "service_namespace"
|
|
|
|
// serviceNameLabel is the name for the label containing a target's service name.
|
|
|
|
serviceNameLabel = metaLabelPrefix + "service_name"
|
|
|
|
// serviceLabelPrefix is the prefix for the service labels.
|
|
|
|
serviceLabelPrefix = metaLabelPrefix + "service_label_"
|
|
|
|
// serviceAnnotationPrefix is the prefix for the service annotations.
|
|
|
|
serviceAnnotationPrefix = metaLabelPrefix + "service_annotation_"
|
2016-05-18 17:18:52 -07:00
|
|
|
|
2015-07-18 14:23:58 -07:00
|
|
|
// nodesTargetGroupName is the name given to the target group for nodes.
|
|
|
|
nodesTargetGroupName = "nodes"
|
2016-05-18 17:18:52 -07:00
|
|
|
// nodeLabelPrefix is the prefix for the node labels.
|
|
|
|
nodeLabelPrefix = metaLabelPrefix + "node_label_"
|
2016-06-07 03:31:29 -07:00
|
|
|
// nodeAddressPrefix is the prefix for the node addresses.
|
|
|
|
nodeAddressPrefix = metaLabelPrefix + "node_address_"
|
|
|
|
// nodePortLabel is the name of the label for the node port.
|
|
|
|
nodePortLabel = metaLabelPrefix + "node_port"
|
2016-05-18 17:18:52 -07:00
|
|
|
|
2015-10-24 06:41:14 -07:00
|
|
|
// apiServersTargetGroupName is the name given to the target group for API servers.
|
|
|
|
apiServersTargetGroupName = "apiServers"
|
2015-07-18 14:23:58 -07:00
|
|
|
|
|
|
|
serviceAccountToken = "/var/run/secrets/kubernetes.io/serviceaccount/token"
|
|
|
|
serviceAccountCACert = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
|
|
|
|
|
|
|
|
apiVersion = "v1"
|
2015-09-03 02:47:09 -07:00
|
|
|
apiPrefix = "/api/" + apiVersion
|
2015-07-18 14:23:58 -07:00
|
|
|
nodesURL = apiPrefix + "/nodes"
|
2016-05-18 17:18:52 -07:00
|
|
|
podsURL = apiPrefix + "/pods"
|
2015-07-18 14:23:58 -07:00
|
|
|
servicesURL = apiPrefix + "/services"
|
|
|
|
endpointsURL = apiPrefix + "/endpoints"
|
|
|
|
serviceEndpointsURL = apiPrefix + "/namespaces/%s/endpoints/%s"
|
|
|
|
)
|
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
// Discovery implements a TargetProvider for Kubernetes services.
|
|
|
|
type Discovery struct {
|
2015-07-18 14:23:58 -07:00
|
|
|
client *http.Client
|
|
|
|
Conf *config.KubernetesSDConfig
|
|
|
|
|
2016-01-15 04:36:55 -08:00
|
|
|
apiServers []config.URL
|
|
|
|
apiServersMu sync.RWMutex
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
2015-08-18 05:37:28 -07:00
|
|
|
// Initialize sets up the discovery for usage.
|
2015-08-24 06:07:27 -07:00
|
|
|
func (kd *Discovery) Initialize() error {
|
2015-07-18 14:23:58 -07:00
|
|
|
client, err := newKubernetesHTTPClient(kd.Conf)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-24 06:41:14 -07:00
|
|
|
kd.apiServers = kd.Conf.APIServers
|
2015-07-18 14:23:58 -07:00
|
|
|
kd.client = client
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run implements the TargetProvider interface.
|
2016-02-18 08:26:27 -08:00
|
|
|
func (kd *Discovery) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
|
2016-07-05 01:13:24 -07:00
|
|
|
log.Debugf("Start Kubernetes service discovery")
|
2015-07-18 14:23:58 -07:00
|
|
|
defer close(ch)
|
|
|
|
|
2016-07-01 10:28:29 -07:00
|
|
|
switch kd.Conf.Role {
|
|
|
|
case config.KubernetesRolePod, config.KubernetesRoleContainer:
|
|
|
|
pd := &podDiscovery{
|
|
|
|
retryInterval: time.Duration(kd.Conf.RetryInterval),
|
|
|
|
kd: kd,
|
|
|
|
}
|
2016-07-01 03:17:17 -07:00
|
|
|
pd.run(ctx, ch)
|
2016-07-01 10:28:29 -07:00
|
|
|
case config.KubernetesRoleNode:
|
|
|
|
nd := &nodeDiscovery{
|
|
|
|
retryInterval: time.Duration(kd.Conf.RetryInterval),
|
|
|
|
kd: kd,
|
|
|
|
}
|
2016-07-01 07:55:37 -07:00
|
|
|
nd.run(ctx, ch)
|
2016-07-01 10:28:29 -07:00
|
|
|
case config.KubernetesRoleService, config.KubernetesRoleEndpoint:
|
|
|
|
sd := &serviceDiscovery{
|
|
|
|
retryInterval: time.Duration(kd.Conf.RetryInterval),
|
|
|
|
kd: kd,
|
|
|
|
}
|
2016-07-01 08:45:07 -07:00
|
|
|
sd.run(ctx, ch)
|
2016-07-01 10:28:29 -07:00
|
|
|
case config.KubernetesRoleAPIServer:
|
|
|
|
select {
|
|
|
|
case ch <- []*config.TargetGroup{kd.updateAPIServersTargetGroup()}:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
log.Errorf("unknown Kubernetes discovery kind %q", kd.Conf.Role)
|
2016-02-18 08:26:27 -08:00
|
|
|
return
|
2015-09-03 02:47:09 -07:00
|
|
|
}
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
2015-10-24 06:41:14 -07:00
|
|
|
func (kd *Discovery) queryAPIServerPath(path string) (*http.Response, error) {
|
2015-09-03 02:47:09 -07:00
|
|
|
req, err := http.NewRequest("GET", path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-24 06:41:14 -07:00
|
|
|
return kd.queryAPIServerReq(req)
|
2015-09-03 02:47:09 -07:00
|
|
|
}
|
|
|
|
|
2015-10-24 06:41:14 -07:00
|
|
|
func (kd *Discovery) queryAPIServerReq(req *http.Request) (*http.Response, error) {
|
|
|
|
// Lock in case we need to rotate API servers to request.
|
|
|
|
kd.apiServersMu.Lock()
|
|
|
|
defer kd.apiServersMu.Unlock()
|
|
|
|
var lastErr error
|
|
|
|
for i := 0; i < len(kd.apiServers); i++ {
|
2015-09-03 02:47:09 -07:00
|
|
|
cloneReq := *req
|
2015-10-24 06:41:14 -07:00
|
|
|
cloneReq.URL.Host = kd.apiServers[0].Host
|
|
|
|
cloneReq.URL.Scheme = kd.apiServers[0].Scheme
|
2015-09-03 02:47:09 -07:00
|
|
|
res, err := kd.client.Do(&cloneReq)
|
|
|
|
if err == nil {
|
|
|
|
return res, nil
|
|
|
|
}
|
2015-10-24 06:41:14 -07:00
|
|
|
lastErr = err
|
|
|
|
kd.rotateAPIServers()
|
2015-09-03 02:47:09 -07:00
|
|
|
}
|
2016-05-18 17:18:52 -07:00
|
|
|
return nil, fmt.Errorf("unable to query any API servers: %v", lastErr)
|
2015-09-03 02:47:09 -07:00
|
|
|
}
|
|
|
|
|
2015-10-24 06:41:14 -07:00
|
|
|
func (kd *Discovery) rotateAPIServers() {
|
|
|
|
if len(kd.apiServers) > 1 {
|
|
|
|
kd.apiServers = append(kd.apiServers[1:], kd.apiServers[0])
|
2015-09-03 02:47:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-24 06:41:14 -07:00
|
|
|
func (kd *Discovery) updateAPIServersTargetGroup() *config.TargetGroup {
|
2015-09-03 02:47:09 -07:00
|
|
|
tg := &config.TargetGroup{
|
2015-10-24 06:41:14 -07:00
|
|
|
Source: apiServersTargetGroupName,
|
2015-09-03 02:47:09 -07:00
|
|
|
Labels: model.LabelSet{
|
2015-10-24 06:41:14 -07:00
|
|
|
roleLabel: model.LabelValue("apiserver"),
|
2015-09-03 02:47:09 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-10-24 06:41:14 -07:00
|
|
|
for _, apiServer := range kd.apiServers {
|
|
|
|
apiServerAddress := apiServer.Host
|
|
|
|
_, _, err := net.SplitHostPort(apiServerAddress)
|
2015-09-03 02:47:09 -07:00
|
|
|
// If error then no port is specified - use default for scheme.
|
|
|
|
if err != nil {
|
2015-10-24 06:41:14 -07:00
|
|
|
switch apiServer.Scheme {
|
2015-09-03 02:47:09 -07:00
|
|
|
case "http":
|
2015-10-24 06:41:14 -07:00
|
|
|
apiServerAddress = net.JoinHostPort(apiServerAddress, "80")
|
2015-09-03 02:47:09 -07:00
|
|
|
case "https":
|
2015-10-24 06:41:14 -07:00
|
|
|
apiServerAddress = net.JoinHostPort(apiServerAddress, "443")
|
2015-09-03 02:47:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t := model.LabelSet{
|
2015-10-24 06:41:14 -07:00
|
|
|
model.AddressLabel: model.LabelValue(apiServerAddress),
|
|
|
|
model.SchemeLabel: model.LabelValue(apiServer.Scheme),
|
2015-09-03 02:47:09 -07:00
|
|
|
}
|
|
|
|
tg.Targets = append(tg.Targets, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tg
|
|
|
|
}
|
|
|
|
|
2015-07-18 14:23:58 -07:00
|
|
|
func newKubernetesHTTPClient(conf *config.KubernetesSDConfig) (*http.Client, error) {
|
|
|
|
bearerTokenFile := conf.BearerTokenFile
|
2015-09-06 16:07:44 -07:00
|
|
|
caFile := conf.TLSConfig.CAFile
|
2015-07-18 14:23:58 -07:00
|
|
|
if conf.InCluster {
|
|
|
|
if len(bearerTokenFile) == 0 {
|
|
|
|
bearerTokenFile = serviceAccountToken
|
|
|
|
}
|
|
|
|
if len(caFile) == 0 {
|
2015-09-29 01:38:58 -07:00
|
|
|
// With recent versions, the CA certificate is mounted as a secret
|
2015-07-18 14:23:58 -07:00
|
|
|
// but we need to handle older versions too. In this case, don't
|
2015-09-06 16:07:44 -07:00
|
|
|
// set the CAFile & the configuration will have to use InsecureSkipVerify.
|
2015-07-18 14:23:58 -07:00
|
|
|
if _, err := os.Stat(serviceAccountCACert); err == nil {
|
|
|
|
caFile = serviceAccountCACert
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 16:07:44 -07:00
|
|
|
tlsOpts := httputil.TLSOptions{
|
|
|
|
InsecureSkipVerify: conf.TLSConfig.InsecureSkipVerify,
|
|
|
|
CAFile: caFile,
|
|
|
|
CertFile: conf.TLSConfig.CertFile,
|
|
|
|
KeyFile: conf.TLSConfig.KeyFile,
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
2015-09-06 16:07:44 -07:00
|
|
|
tlsConfig, err := httputil.NewTLSConfig(tlsOpts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
2015-09-06 16:07:44 -07:00
|
|
|
var rt http.RoundTripper = &http.Transport{
|
2015-07-18 14:23:58 -07:00
|
|
|
Dial: func(netw, addr string) (c net.Conn, err error) {
|
|
|
|
c, err = net.DialTimeout(netw, addr, time.Duration(conf.RequestTimeout))
|
|
|
|
return
|
|
|
|
},
|
2015-09-06 16:07:44 -07:00
|
|
|
TLSClientConfig: tlsConfig,
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
2015-10-23 08:47:10 -07:00
|
|
|
// If a bearer token is provided, create a round tripper that will set the
|
|
|
|
// Authorization header correctly on each request.
|
|
|
|
bearerToken := conf.BearerToken
|
|
|
|
if len(bearerToken) == 0 && len(bearerTokenFile) > 0 {
|
|
|
|
b, err := ioutil.ReadFile(bearerTokenFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to read bearer token file %s: %s", bearerTokenFile, err)
|
|
|
|
}
|
|
|
|
bearerToken = string(b)
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
if len(bearerToken) > 0 {
|
2015-10-23 08:47:10 -07:00
|
|
|
rt = httputil.NewBearerAuthRoundTripper(bearerToken, rt)
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
2015-10-23 08:47:10 -07:00
|
|
|
|
|
|
|
if conf.BasicAuth != nil {
|
|
|
|
rt = httputil.NewBasicAuthRoundTripper(conf.BasicAuth.Username, conf.BasicAuth.Password, rt)
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return &http.Client{
|
|
|
|
Transport: rt,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Until loops until stop channel is closed, running f every period.
|
|
|
|
// f may not be invoked if stop channel is already closed.
|
|
|
|
func until(f func(), period time.Duration, stopCh <-chan struct{}) {
|
|
|
|
select {
|
|
|
|
case <-stopCh:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stopCh:
|
|
|
|
return
|
|
|
|
case <-time.After(period):
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|