2016-09-30 09:33:23 +00: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 12:53:11 +00:00
|
|
|
package kubernetes
|
2016-09-28 17:29:55 +00:00
|
|
|
|
|
|
|
import (
|
2017-10-25 04:21:42 +00:00
|
|
|
"context"
|
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 20:01:34 +00:00
|
|
|
"fmt"
|
2016-09-28 17:29:55 +00:00
|
|
|
"io/ioutil"
|
2017-04-19 12:36:34 +00:00
|
|
|
"sync"
|
2016-09-28 17:29:55 +00:00
|
|
|
"time"
|
|
|
|
|
2017-08-11 18:45:52 +00:00
|
|
|
"github.com/go-kit/kit/log"
|
|
|
|
"github.com/go-kit/kit/log/level"
|
2017-10-25 04:21:42 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2018-01-11 15:10:25 +00:00
|
|
|
config_util "github.com/prometheus/common/config"
|
2016-09-28 17:29:55 +00:00
|
|
|
"github.com/prometheus/common/model"
|
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 20:01:34 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
|
|
|
|
2018-07-03 07:37:22 +00:00
|
|
|
apiv1 "k8s.io/api/core/v1"
|
|
|
|
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
|
2018-04-09 16:35:14 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/watch"
|
2017-05-11 08:29:10 +00:00
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/tools/cache"
|
2016-09-28 17:29:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// kubernetesMetaLabelPrefix is the meta prefix used for all meta labels.
|
|
|
|
// in this discovery.
|
2018-08-22 19:33:23 +00:00
|
|
|
metaLabelPrefix = model.MetaLabelPrefix + "kubernetes_"
|
|
|
|
namespaceLabel = metaLabelPrefix + "namespace"
|
|
|
|
metricsNamespace = "prometheus_sd_kubernetes"
|
2016-09-28 17:29:55 +00:00
|
|
|
)
|
|
|
|
|
2016-10-21 08:48:28 +00:00
|
|
|
var (
|
2018-08-22 19:33:23 +00:00
|
|
|
// Custom events metric
|
2016-10-21 08:48:28 +00:00
|
|
|
eventCount = prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
2018-08-22 19:33:23 +00:00
|
|
|
Namespace: metricsNamespace,
|
|
|
|
Name: "events_total",
|
|
|
|
Help: "The number of Kubernetes events handled.",
|
2016-10-21 08:48:28 +00:00
|
|
|
},
|
2016-10-21 10:11:15 +00:00
|
|
|
[]string{"role", "event"},
|
2016-10-21 08:48:28 +00: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 20:01:34 +00:00
|
|
|
// DefaultSDConfig is the default Kubernetes SD configuration
|
|
|
|
DefaultSDConfig = SDConfig{}
|
2016-09-28 17:29:55 +00: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 20:01:34 +00:00
|
|
|
// Role is role of the service in Kubernetes.
|
|
|
|
type Role string
|
|
|
|
|
|
|
|
// The valid options for Role.
|
|
|
|
const (
|
|
|
|
RoleNode Role = "node"
|
|
|
|
RolePod Role = "pod"
|
|
|
|
RoleService Role = "service"
|
|
|
|
RoleEndpoint Role = "endpoints"
|
|
|
|
RoleIngress Role = "ingress"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
|
|
|
func (c *Role) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
if err := unmarshal((*string)(c)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch *c {
|
|
|
|
case RoleNode, RolePod, RoleService, RoleEndpoint, RoleIngress:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unknown Kubernetes SD role %q", *c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SDConfig is the configuration for Kubernetes service discovery.
|
|
|
|
type SDConfig struct {
|
2018-07-03 11:43:41 +00:00
|
|
|
APIServer config_util.URL `yaml:"api_server,omitempty"`
|
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 20:01:34 +00:00
|
|
|
Role Role `yaml:"role"`
|
|
|
|
BasicAuth *config_util.BasicAuth `yaml:"basic_auth,omitempty"`
|
|
|
|
BearerToken config_util.Secret `yaml:"bearer_token,omitempty"`
|
|
|
|
BearerTokenFile string `yaml:"bearer_token_file,omitempty"`
|
|
|
|
TLSConfig config_util.TLSConfig `yaml:"tls_config,omitempty"`
|
2018-07-03 11:43:41 +00:00
|
|
|
NamespaceDiscovery NamespaceDiscovery `yaml:"namespaces,omitempty"`
|
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 20:01:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
|
|
|
func (c *SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
*c = SDConfig{}
|
|
|
|
type plain SDConfig
|
|
|
|
err := unmarshal((*plain)(c))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if c.Role == "" {
|
|
|
|
return fmt.Errorf("role missing (one of: pod, service, endpoints, node)")
|
|
|
|
}
|
|
|
|
if len(c.BearerToken) > 0 && len(c.BearerTokenFile) > 0 {
|
|
|
|
return fmt.Errorf("at most one of bearer_token & bearer_token_file must be configured")
|
|
|
|
}
|
|
|
|
if c.BasicAuth != nil && (len(c.BearerToken) > 0 || len(c.BearerTokenFile) > 0) {
|
|
|
|
return fmt.Errorf("at most one of basic_auth, bearer_token & bearer_token_file must be configured")
|
|
|
|
}
|
|
|
|
if c.APIServer.URL == nil &&
|
|
|
|
(c.BasicAuth != nil || c.BearerToken != "" || c.BearerTokenFile != "" ||
|
|
|
|
c.TLSConfig.CAFile != "" || c.TLSConfig.CertFile != "" || c.TLSConfig.KeyFile != "") {
|
|
|
|
return fmt.Errorf("to use custom authentication please provide the 'api_server' URL explicitly")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NamespaceDiscovery is the configuration for discovering
|
|
|
|
// Kubernetes namespaces.
|
|
|
|
type NamespaceDiscovery struct {
|
|
|
|
Names []string `yaml:"names"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
|
|
|
func (c *NamespaceDiscovery) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
*c = NamespaceDiscovery{}
|
|
|
|
type plain NamespaceDiscovery
|
2018-04-04 08:07:39 +00:00
|
|
|
return unmarshal((*plain)(c))
|
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 20:01:34 +00:00
|
|
|
}
|
|
|
|
|
2016-10-21 08:48:28 +00:00
|
|
|
func init() {
|
|
|
|
prometheus.MustRegister(eventCount)
|
|
|
|
|
|
|
|
// Initialize metric vectors.
|
2016-11-21 10:44:48 +00:00
|
|
|
for _, role := range []string{"endpoints", "node", "pod", "service"} {
|
|
|
|
for _, evt := range []string{"add", "delete", "update"} {
|
|
|
|
eventCount.WithLabelValues(role, evt)
|
|
|
|
}
|
|
|
|
}
|
2018-08-22 19:33:23 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
clientGoRequestMetricAdapterInstance = clientGoRequestMetricAdapter{}
|
|
|
|
clientGoCacheMetricsProviderInstance = clientGoCacheMetricsProvider{}
|
|
|
|
clientGoWorkqueueMetricsProviderInstance = clientGoWorkqueueMetricsProvider{}
|
|
|
|
)
|
|
|
|
|
|
|
|
clientGoRequestMetricAdapterInstance.Register(prometheus.DefaultRegisterer)
|
|
|
|
clientGoCacheMetricsProviderInstance.Register(prometheus.DefaultRegisterer)
|
|
|
|
clientGoWorkqueueMetricsProviderInstance.Register(prometheus.DefaultRegisterer)
|
|
|
|
|
2016-10-21 08:48:28 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 16:35:14 +00:00
|
|
|
// This is only for internal use.
|
|
|
|
type discoverer interface {
|
|
|
|
Run(ctx context.Context, up chan<- []*targetgroup.Group)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discovery implements the discoverer interface for discovering
|
2016-09-28 17:29:55 +00:00
|
|
|
// targets from Kubernetes.
|
2017-03-16 23:29:47 +00:00
|
|
|
type Discovery struct {
|
2018-04-09 16:35:14 +00:00
|
|
|
sync.RWMutex
|
2017-04-19 12:36:34 +00:00
|
|
|
client kubernetes.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 20:01:34 +00:00
|
|
|
role Role
|
2017-04-19 12:36:34 +00:00
|
|
|
logger log.Logger
|
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 20:01:34 +00:00
|
|
|
namespaceDiscovery *NamespaceDiscovery
|
2018-04-09 16:35:14 +00:00
|
|
|
discoverers []discoverer
|
2016-09-28 17:29:55 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:36:34 +00:00
|
|
|
func (d *Discovery) getNamespaces() []string {
|
|
|
|
namespaces := d.namespaceDiscovery.Names
|
|
|
|
if len(namespaces) == 0 {
|
2018-07-03 07:37:22 +00:00
|
|
|
namespaces = []string{apiv1.NamespaceAll}
|
2017-04-19 12:36:34 +00:00
|
|
|
}
|
|
|
|
return namespaces
|
|
|
|
}
|
|
|
|
|
2016-09-28 17:29:55 +00:00
|
|
|
// New creates a new Kubernetes discovery for the given role.
|
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 20:01:34 +00:00
|
|
|
func New(l log.Logger, conf *SDConfig) (*Discovery, error) {
|
2017-08-11 18:45:52 +00:00
|
|
|
if l == nil {
|
|
|
|
l = log.NewNopLogger()
|
|
|
|
}
|
2016-09-28 17:29:55 +00:00
|
|
|
var (
|
|
|
|
kcfg *rest.Config
|
|
|
|
err error
|
|
|
|
)
|
2016-10-07 12:53:11 +00:00
|
|
|
if conf.APIServer.URL == nil {
|
2017-01-19 09:52:52 +00:00
|
|
|
// Use the Kubernetes provided pod service account
|
|
|
|
// as described in https://kubernetes.io/docs/admin/service-accounts-admin/
|
2016-09-28 17:29:55 +00:00
|
|
|
kcfg, err = rest.InClusterConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-19 09:52:52 +00: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
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Info(l).Log("msg", "Using pod service account via in-cluster config")
|
|
|
|
|
2017-01-19 09:52:52 +00:00
|
|
|
if conf.TLSConfig.CAFile != "" {
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Warn(l).Log("msg", "Configured TLS CA file is ignored when using pod service account")
|
2017-01-19 09:52:52 +00:00
|
|
|
}
|
|
|
|
if conf.TLSConfig.CertFile != "" || conf.TLSConfig.KeyFile != "" {
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Warn(l).Log("msg", "Configured TLS client certificate is ignored when using pod service account")
|
2017-01-19 09:52:52 +00:00
|
|
|
}
|
|
|
|
if conf.BearerToken != "" {
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Warn(l).Log("msg", "Configured auth token is ignored when using pod service account")
|
2017-01-19 09:52:52 +00:00
|
|
|
}
|
|
|
|
if conf.BasicAuth != nil {
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Warn(l).Log("msg", "Configured basic authentication credentials are ignored when using pod service account")
|
2017-01-19 09:52:52 +00:00
|
|
|
}
|
2016-09-28 17:29:55 +00:00
|
|
|
} else {
|
2017-01-19 09:52:52 +00: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 08:29:10 +00:00
|
|
|
Insecure: conf.TLSConfig.InsecureSkipVerify,
|
2017-01-19 09:52:52 +00:00
|
|
|
},
|
|
|
|
}
|
2017-05-29 11:46:23 +00:00
|
|
|
token := string(conf.BearerToken)
|
2016-09-28 17:29:55 +00:00
|
|
|
if conf.BearerTokenFile != "" {
|
|
|
|
bf, err := ioutil.ReadFile(conf.BearerTokenFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
token = string(bf)
|
|
|
|
}
|
2017-01-19 09:52:52 +00:00
|
|
|
kcfg.BearerToken = token
|
2016-09-28 17:29:55 +00:00
|
|
|
|
2017-01-19 09:52:52 +00:00
|
|
|
if conf.BasicAuth != nil {
|
|
|
|
kcfg.Username = conf.BasicAuth.Username
|
2017-05-29 11:46:23 +00:00
|
|
|
kcfg.Password = string(conf.BasicAuth.Password)
|
2016-09-28 17:29:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-23 14:49:49 +00:00
|
|
|
kcfg.UserAgent = "Prometheus/discovery"
|
2016-09-28 17:29:55 +00:00
|
|
|
|
|
|
|
c, err := kubernetes.NewForConfig(kcfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-16 23:29:47 +00:00
|
|
|
return &Discovery{
|
2017-04-19 12:36:34 +00:00
|
|
|
client: c,
|
|
|
|
logger: l,
|
|
|
|
role: conf.Role,
|
|
|
|
namespaceDiscovery: &conf.NamespaceDiscovery,
|
2018-04-09 16:35:14 +00:00
|
|
|
discoverers: make([]discoverer, 0),
|
2016-09-28 17:29:55 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const resyncPeriod = 10 * time.Minute
|
|
|
|
|
2018-04-09 16:35:14 +00: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 20:01:34 +00:00
|
|
|
func (d *Discovery) Run(ctx context.Context, ch chan<- []*targetgroup.Group) {
|
2018-04-09 16:35:14 +00:00
|
|
|
d.Lock()
|
2017-04-19 12:36:34 +00:00
|
|
|
namespaces := d.getNamespaces()
|
|
|
|
|
2017-03-16 23:29:47 +00:00
|
|
|
switch d.role {
|
2018-04-10 08:53:00 +00:00
|
|
|
case RoleEndpoint:
|
2017-04-19 12:36:34 +00:00
|
|
|
for _, namespace := range namespaces {
|
2018-06-14 14:49:43 +00:00
|
|
|
e := d.client.CoreV1().Endpoints(namespace)
|
2018-04-09 16:35:14 +00:00
|
|
|
elw := &cache.ListWatch{
|
|
|
|
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
2018-06-14 14:49:43 +00:00
|
|
|
return e.List(options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
2018-06-14 14:49:43 +00:00
|
|
|
return e.Watch(options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
}
|
2018-06-14 14:49:43 +00:00
|
|
|
s := d.client.CoreV1().Services(namespace)
|
2018-04-09 16:35:14 +00:00
|
|
|
slw := &cache.ListWatch{
|
|
|
|
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
2018-06-14 14:49:43 +00:00
|
|
|
return s.List(options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
2018-06-14 14:49:43 +00:00
|
|
|
return s.Watch(options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
}
|
2018-06-14 14:49:43 +00:00
|
|
|
p := d.client.CoreV1().Pods(namespace)
|
2018-04-09 16:35:14 +00:00
|
|
|
plw := &cache.ListWatch{
|
|
|
|
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
2018-06-14 14:49:43 +00:00
|
|
|
return p.List(options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
2018-06-14 14:49:43 +00:00
|
|
|
return p.Watch(options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
}
|
2017-04-19 12:36:34 +00:00
|
|
|
eps := NewEndpoints(
|
2017-09-08 16:34:20 +00:00
|
|
|
log.With(d.logger, "role", "endpoint"),
|
2017-04-19 12:36:34 +00:00
|
|
|
cache.NewSharedInformer(slw, &apiv1.Service{}, resyncPeriod),
|
|
|
|
cache.NewSharedInformer(elw, &apiv1.Endpoints{}, resyncPeriod),
|
|
|
|
cache.NewSharedInformer(plw, &apiv1.Pod{}, resyncPeriod),
|
|
|
|
)
|
2018-04-09 16:35:14 +00:00
|
|
|
d.discoverers = append(d.discoverers, eps)
|
2017-04-19 12:36:34 +00:00
|
|
|
go eps.endpointsInf.Run(ctx.Done())
|
|
|
|
go eps.serviceInf.Run(ctx.Done())
|
|
|
|
go eps.podInf.Run(ctx.Done())
|
|
|
|
}
|
2018-04-10 08:53:00 +00:00
|
|
|
case RolePod:
|
2017-04-19 12:36:34 +00:00
|
|
|
for _, namespace := range namespaces {
|
2018-06-14 14:49:43 +00:00
|
|
|
p := d.client.CoreV1().Pods(namespace)
|
2018-04-09 16:35:14 +00:00
|
|
|
plw := &cache.ListWatch{
|
|
|
|
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
2018-06-14 14:49:43 +00:00
|
|
|
return p.List(options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
2018-06-14 14:49:43 +00:00
|
|
|
return p.Watch(options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
}
|
2017-04-19 12:36:34 +00:00
|
|
|
pod := NewPod(
|
2017-09-15 17:45:27 +00:00
|
|
|
log.With(d.logger, "role", "pod"),
|
2017-04-19 12:36:34 +00:00
|
|
|
cache.NewSharedInformer(plw, &apiv1.Pod{}, resyncPeriod),
|
|
|
|
)
|
2018-04-09 16:35:14 +00:00
|
|
|
d.discoverers = append(d.discoverers, pod)
|
2017-04-19 12:36:34 +00:00
|
|
|
go pod.informer.Run(ctx.Done())
|
2016-09-28 17:29:55 +00:00
|
|
|
}
|
2018-04-10 08:53:00 +00:00
|
|
|
case RoleService:
|
2017-04-19 12:36:34 +00:00
|
|
|
for _, namespace := range namespaces {
|
2018-06-14 14:49:43 +00:00
|
|
|
s := d.client.CoreV1().Services(namespace)
|
2018-04-09 16:35:14 +00:00
|
|
|
slw := &cache.ListWatch{
|
|
|
|
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
2018-06-14 14:49:43 +00:00
|
|
|
return s.List(options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
2018-06-14 14:49:43 +00:00
|
|
|
return s.Watch(options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
}
|
2017-04-19 12:36:34 +00:00
|
|
|
svc := NewService(
|
2017-09-15 17:45:27 +00:00
|
|
|
log.With(d.logger, "role", "service"),
|
2017-04-19 12:36:34 +00:00
|
|
|
cache.NewSharedInformer(slw, &apiv1.Service{}, resyncPeriod),
|
|
|
|
)
|
2018-04-09 16:35:14 +00:00
|
|
|
d.discoverers = append(d.discoverers, svc)
|
2017-04-19 12:36:34 +00:00
|
|
|
go svc.informer.Run(ctx.Done())
|
2016-10-06 14:28:59 +00:00
|
|
|
}
|
2018-04-10 08:53:00 +00:00
|
|
|
case RoleIngress:
|
2017-09-04 11:10:44 +00:00
|
|
|
for _, namespace := range namespaces {
|
2018-06-14 14:49:43 +00:00
|
|
|
i := d.client.ExtensionsV1beta1().Ingresses(namespace)
|
2018-04-09 16:35:14 +00:00
|
|
|
ilw := &cache.ListWatch{
|
|
|
|
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
2018-06-14 14:49:43 +00:00
|
|
|
return i.List(options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
2018-06-14 14:49:43 +00:00
|
|
|
return i.Watch(options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
}
|
2017-09-04 11:10:44 +00:00
|
|
|
ingress := NewIngress(
|
2017-09-15 17:45:27 +00:00
|
|
|
log.With(d.logger, "role", "ingress"),
|
2017-09-06 10:47:03 +00:00
|
|
|
cache.NewSharedInformer(ilw, &extensionsv1beta1.Ingress{}, resyncPeriod),
|
2017-09-04 11:10:44 +00:00
|
|
|
)
|
2018-04-09 16:35:14 +00:00
|
|
|
d.discoverers = append(d.discoverers, ingress)
|
2017-09-04 11:10:44 +00:00
|
|
|
go ingress.informer.Run(ctx.Done())
|
|
|
|
}
|
2018-04-10 08:53:00 +00:00
|
|
|
case RoleNode:
|
2018-04-09 16:35:14 +00:00
|
|
|
nlw := &cache.ListWatch{
|
|
|
|
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
|
|
|
return d.client.CoreV1().Nodes().List(options)
|
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
|
|
|
return d.client.CoreV1().Nodes().Watch(options)
|
|
|
|
},
|
|
|
|
}
|
2016-10-07 11:33:04 +00:00
|
|
|
node := NewNode(
|
2017-09-15 17:45:27 +00:00
|
|
|
log.With(d.logger, "role", "node"),
|
2016-10-07 11:33:04 +00:00
|
|
|
cache.NewSharedInformer(nlw, &apiv1.Node{}, resyncPeriod),
|
|
|
|
)
|
2018-04-09 16:35:14 +00:00
|
|
|
d.discoverers = append(d.discoverers, node)
|
2016-10-07 11:33:04 +00:00
|
|
|
go node.informer.Run(ctx.Done())
|
2016-09-28 17:29:55 +00:00
|
|
|
default:
|
2017-08-11 18:45:52 +00:00
|
|
|
level.Error(d.logger).Log("msg", "unknown Kubernetes discovery kind", "role", d.role)
|
2016-09-28 17:29:55 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 16:35:14 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, dd := range d.discoverers {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(d discoverer) {
|
|
|
|
defer wg.Done()
|
|
|
|
d.Run(ctx, ch)
|
|
|
|
}(dd)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Unlock()
|
2018-08-17 13:20:22 +00:00
|
|
|
|
|
|
|
wg.Wait()
|
2016-09-28 17:29:55 +00:00
|
|
|
<-ctx.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
func lv(s string) model.LabelValue {
|
|
|
|
return model.LabelValue(s)
|
|
|
|
}
|
2018-04-10 08:53:00 +00:00
|
|
|
|
|
|
|
func send(ctx context.Context, l log.Logger, role Role, ch chan<- []*targetgroup.Group, tg *targetgroup.Group) {
|
|
|
|
if tg == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case ch <- []*targetgroup.Group{tg}:
|
|
|
|
}
|
|
|
|
}
|