2016-09-30 02:33:23 -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.
|
|
|
|
|
2016-10-07 05:53:11 -07:00
|
|
|
package kubernetes
|
2016-09-28 10:29:55 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2017-04-19 05:36:34 -07:00
|
|
|
"sync"
|
2016-09-28 10:29:55 -07:00
|
|
|
"time"
|
|
|
|
|
2016-10-21 01:48:28 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2016-09-28 10:29:55 -07:00
|
|
|
"github.com/prometheus/prometheus/config"
|
|
|
|
|
|
|
|
"github.com/prometheus/common/log"
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"golang.org/x/net/context"
|
2017-05-11 01:29:10 -07:00
|
|
|
"k8s.io/apimachinery/pkg/util/runtime"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/client-go/pkg/api"
|
|
|
|
apiv1 "k8s.io/client-go/pkg/api/v1"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/tools/cache"
|
2016-09-28 10:29:55 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// kubernetesMetaLabelPrefix is the meta prefix used for all meta labels.
|
|
|
|
// in this discovery.
|
|
|
|
metaLabelPrefix = model.MetaLabelPrefix + "kubernetes_"
|
|
|
|
namespaceLabel = metaLabelPrefix + "namespace"
|
|
|
|
)
|
|
|
|
|
2016-10-21 01:48:28 -07:00
|
|
|
var (
|
|
|
|
eventCount = prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
2016-11-21 02:44:48 -08:00
|
|
|
Name: "prometheus_sd_kubernetes_events_total",
|
|
|
|
Help: "The number of Kubernetes events handled.",
|
2016-10-21 01:48:28 -07:00
|
|
|
},
|
2016-10-21 03:11:15 -07:00
|
|
|
[]string{"role", "event"},
|
2016-10-21 01:48:28 -07:00
|
|
|
)
|
2016-09-28 10:29:55 -07:00
|
|
|
)
|
|
|
|
|
2016-10-21 01:48:28 -07:00
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(eventCount)
|
|
|
|
|
|
|
|
// Initialize metric vectors.
|
2016-11-21 02:44:48 -08:00
|
|
|
for _, role := range []string{"endpoints", "node", "pod", "service"} {
|
|
|
|
for _, evt := range []string{"add", "delete", "update"} {
|
|
|
|
eventCount.WithLabelValues(role, evt)
|
|
|
|
}
|
|
|
|
}
|
2016-10-21 01:48:28 -07:00
|
|
|
}
|
|
|
|
|
2017-03-16 16:29:47 -07:00
|
|
|
// Discovery implements the TargetProvider interface for discovering
|
2016-09-28 10:29:55 -07:00
|
|
|
// targets from Kubernetes.
|
2017-03-16 16:29:47 -07:00
|
|
|
type Discovery struct {
|
2017-04-19 05:36:34 -07:00
|
|
|
client kubernetes.Interface
|
|
|
|
role config.KubernetesRole
|
|
|
|
logger log.Logger
|
|
|
|
namespaceDiscovery *config.KubernetesNamespaceDiscovery
|
2016-09-28 10:29:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-11-12 05:13:38 -08:00
|
|
|
runtime.ErrorHandlers = []func(error){
|
|
|
|
func(err error) {
|
|
|
|
log.With("component", "kube_client_runtime").Errorln(err)
|
|
|
|
},
|
|
|
|
}
|
2016-09-28 10:29:55 -07:00
|
|
|
}
|
|
|
|
|
2017-04-19 05:36:34 -07:00
|
|
|
func (d *Discovery) getNamespaces() []string {
|
|
|
|
namespaces := d.namespaceDiscovery.Names
|
|
|
|
if len(namespaces) == 0 {
|
|
|
|
namespaces = []string{api.NamespaceAll}
|
|
|
|
}
|
|
|
|
return namespaces
|
|
|
|
}
|
|
|
|
|
2016-09-28 10:29:55 -07:00
|
|
|
// New creates a new Kubernetes discovery for the given role.
|
2017-03-16 16:29:47 -07:00
|
|
|
func New(l log.Logger, conf *config.KubernetesSDConfig) (*Discovery, error) {
|
2016-09-28 10:29:55 -07:00
|
|
|
var (
|
|
|
|
kcfg *rest.Config
|
|
|
|
err error
|
|
|
|
)
|
2016-10-07 05:53:11 -07:00
|
|
|
if conf.APIServer.URL == nil {
|
2017-01-19 01:52:52 -08:00
|
|
|
// Use the Kubernetes provided pod service account
|
|
|
|
// as described in https://kubernetes.io/docs/admin/service-accounts-admin/
|
2016-09-28 10:29:55 -07:00
|
|
|
kcfg, err = rest.InClusterConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-19 01:52:52 -08:00
|
|
|
// Because the handling of configuration parameters changes
|
|
|
|
// we should inform the user when their currently configured values
|
|
|
|
// will be ignored due to precedence of InClusterConfig
|
|
|
|
l.Info("Using pod service account via in-cluster config")
|
|
|
|
if conf.TLSConfig.CAFile != "" {
|
|
|
|
l.Warn("Configured TLS CA file is ignored when using pod service account")
|
|
|
|
}
|
|
|
|
if conf.TLSConfig.CertFile != "" || conf.TLSConfig.KeyFile != "" {
|
|
|
|
l.Warn("Configured TLS client certificate is ignored when using pod service account")
|
|
|
|
}
|
|
|
|
if conf.BearerToken != "" {
|
|
|
|
l.Warn("Configured auth token is ignored when using pod service account")
|
|
|
|
}
|
|
|
|
if conf.BasicAuth != nil {
|
|
|
|
l.Warn("Configured basic authentication credentials are ignored when using pod service account")
|
|
|
|
}
|
2016-09-28 10:29:55 -07:00
|
|
|
} else {
|
2017-01-19 01:52:52 -08:00
|
|
|
kcfg = &rest.Config{
|
|
|
|
Host: conf.APIServer.String(),
|
|
|
|
TLSClientConfig: rest.TLSClientConfig{
|
|
|
|
CAFile: conf.TLSConfig.CAFile,
|
|
|
|
CertFile: conf.TLSConfig.CertFile,
|
|
|
|
KeyFile: conf.TLSConfig.KeyFile,
|
2017-05-11 01:29:10 -07:00
|
|
|
Insecure: conf.TLSConfig.InsecureSkipVerify,
|
2017-01-19 01:52:52 -08:00
|
|
|
},
|
|
|
|
}
|
2017-05-29 04:46:23 -07:00
|
|
|
token := string(conf.BearerToken)
|
2016-09-28 10:29:55 -07:00
|
|
|
if conf.BearerTokenFile != "" {
|
|
|
|
bf, err := ioutil.ReadFile(conf.BearerTokenFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
token = string(bf)
|
|
|
|
}
|
2017-01-19 01:52:52 -08:00
|
|
|
kcfg.BearerToken = token
|
2016-09-28 10:29:55 -07:00
|
|
|
|
2017-01-19 01:52:52 -08:00
|
|
|
if conf.BasicAuth != nil {
|
|
|
|
kcfg.Username = conf.BasicAuth.Username
|
2017-05-29 04:46:23 -07:00
|
|
|
kcfg.Password = string(conf.BasicAuth.Password)
|
2016-09-28 10:29:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 01:52:52 -08:00
|
|
|
kcfg.UserAgent = "prometheus/discovery"
|
2016-09-28 10:29:55 -07:00
|
|
|
|
|
|
|
c, err := kubernetes.NewForConfig(kcfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-16 16:29:47 -07:00
|
|
|
return &Discovery{
|
2017-04-19 05:36:34 -07:00
|
|
|
client: c,
|
|
|
|
logger: l,
|
|
|
|
role: conf.Role,
|
|
|
|
namespaceDiscovery: &conf.NamespaceDiscovery,
|
2016-09-28 10:29:55 -07:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const resyncPeriod = 10 * time.Minute
|
|
|
|
|
|
|
|
// Run implements the TargetProvider interface.
|
2017-03-16 16:29:47 -07:00
|
|
|
func (d *Discovery) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
|
2017-05-11 05:29:49 -07:00
|
|
|
rclient := d.client.Core().RESTClient()
|
2016-09-28 10:29:55 -07:00
|
|
|
|
2017-04-19 05:36:34 -07:00
|
|
|
namespaces := d.getNamespaces()
|
|
|
|
|
2017-03-16 16:29:47 -07:00
|
|
|
switch d.role {
|
2016-10-18 07:20:26 -07:00
|
|
|
case "endpoints":
|
2017-04-19 05:36:34 -07:00
|
|
|
var wg sync.WaitGroup
|
2016-09-30 01:42:07 -07:00
|
|
|
|
2017-04-19 05:36:34 -07:00
|
|
|
for _, namespace := range namespaces {
|
|
|
|
elw := cache.NewListWatchFromClient(rclient, "endpoints", namespace, nil)
|
|
|
|
slw := cache.NewListWatchFromClient(rclient, "services", namespace, nil)
|
|
|
|
plw := cache.NewListWatchFromClient(rclient, "pods", namespace, nil)
|
|
|
|
eps := NewEndpoints(
|
|
|
|
d.logger.With("kubernetes_sd", "endpoint"),
|
|
|
|
cache.NewSharedInformer(slw, &apiv1.Service{}, resyncPeriod),
|
|
|
|
cache.NewSharedInformer(elw, &apiv1.Endpoints{}, resyncPeriod),
|
|
|
|
cache.NewSharedInformer(plw, &apiv1.Pod{}, resyncPeriod),
|
|
|
|
)
|
|
|
|
go eps.endpointsInf.Run(ctx.Done())
|
|
|
|
go eps.serviceInf.Run(ctx.Done())
|
|
|
|
go eps.podInf.Run(ctx.Done())
|
2016-09-30 01:42:07 -07:00
|
|
|
|
2017-04-19 05:36:34 -07:00
|
|
|
for !eps.serviceInf.HasSynced() {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
for !eps.endpointsInf.HasSynced() {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
for !eps.podInf.HasSynced() {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
eps.Run(ctx, ch)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2016-09-28 10:29:55 -07:00
|
|
|
case "pod":
|
2017-04-19 05:36:34 -07:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, namespace := range namespaces {
|
|
|
|
plw := cache.NewListWatchFromClient(rclient, "pods", namespace, nil)
|
|
|
|
pod := NewPod(
|
|
|
|
d.logger.With("kubernetes_sd", "pod"),
|
|
|
|
cache.NewSharedInformer(plw, &apiv1.Pod{}, resyncPeriod),
|
|
|
|
)
|
|
|
|
go pod.informer.Run(ctx.Done())
|
2016-09-28 10:29:55 -07:00
|
|
|
|
2017-04-19 05:36:34 -07:00
|
|
|
for !pod.informer.HasSynced() {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
pod.Run(ctx, ch)
|
|
|
|
}()
|
2016-09-28 10:29:55 -07:00
|
|
|
}
|
2017-04-19 05:36:34 -07:00
|
|
|
wg.Wait()
|
2016-10-06 07:28:59 -07:00
|
|
|
case "service":
|
2017-04-19 05:36:34 -07:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, namespace := range namespaces {
|
|
|
|
slw := cache.NewListWatchFromClient(rclient, "services", namespace, nil)
|
|
|
|
svc := NewService(
|
|
|
|
d.logger.With("kubernetes_sd", "service"),
|
|
|
|
cache.NewSharedInformer(slw, &apiv1.Service{}, resyncPeriod),
|
|
|
|
)
|
|
|
|
go svc.informer.Run(ctx.Done())
|
2016-10-06 07:28:59 -07:00
|
|
|
|
2017-04-19 05:36:34 -07:00
|
|
|
for !svc.informer.HasSynced() {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
svc.Run(ctx, ch)
|
|
|
|
}()
|
2016-10-06 07:28:59 -07:00
|
|
|
}
|
2017-04-19 05:36:34 -07:00
|
|
|
wg.Wait()
|
2016-10-07 04:33:04 -07:00
|
|
|
case "node":
|
|
|
|
nlw := cache.NewListWatchFromClient(rclient, "nodes", api.NamespaceAll, nil)
|
|
|
|
node := NewNode(
|
2017-03-16 16:29:47 -07:00
|
|
|
d.logger.With("kubernetes_sd", "node"),
|
2016-10-07 04:33:04 -07:00
|
|
|
cache.NewSharedInformer(nlw, &apiv1.Node{}, resyncPeriod),
|
|
|
|
)
|
|
|
|
go node.informer.Run(ctx.Done())
|
|
|
|
|
|
|
|
for !node.informer.HasSynced() {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
node.Run(ctx, ch)
|
|
|
|
|
2016-09-28 10:29:55 -07:00
|
|
|
default:
|
2017-03-16 16:29:47 -07:00
|
|
|
d.logger.Errorf("unknown Kubernetes discovery kind %q", d.role)
|
2016-09-28 10:29:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
<-ctx.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
func lv(s string) model.LabelValue {
|
|
|
|
return model.LabelValue(s)
|
|
|
|
}
|