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"
|
2020-07-30 22:18:38 +00:00
|
|
|
"fmt"
|
2019-02-20 10:22:34 +00:00
|
|
|
"reflect"
|
2020-02-07 14:13:44 +00:00
|
|
|
"strings"
|
2017-04-19 12:36:34 +00:00
|
|
|
"sync"
|
2016-09-28 17:29:55 +00:00
|
|
|
"time"
|
|
|
|
|
2021-06-11 16:17:59 +00:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2019-03-25 23:01:12 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-10-25 04:21:42 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2020-08-20 12:48:26 +00:00
|
|
|
"github.com/prometheus/common/config"
|
2016-09-28 17:29:55 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2020-08-20 12:48:26 +00:00
|
|
|
"github.com/prometheus/common/version"
|
2018-07-03 07:37:22 +00:00
|
|
|
apiv1 "k8s.io/api/core/v1"
|
2020-02-16 14:59:29 +00:00
|
|
|
disv1beta1 "k8s.io/api/discovery/v1beta1"
|
2020-04-06 16:23:02 +00:00
|
|
|
"k8s.io/api/networking/v1beta1"
|
2018-04-09 16:35:14 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-02-07 14:13:44 +00:00
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
2020-12-14 13:49:50 +00:00
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2018-04-09 16:35:14 +00:00
|
|
|
"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"
|
2021-06-17 10:41:50 +00:00
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
2019-02-20 10:22:34 +00:00
|
|
|
|
2020-08-20 12:48:26 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery"
|
2019-02-20 10:22:34 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
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"
|
2019-04-10 12:21:42 +00:00
|
|
|
presentValue = model.LabelValue("true")
|
2016-09-28 17:29:55 +00:00
|
|
|
)
|
|
|
|
|
2016-10-21 08:48:28 +00:00
|
|
|
var (
|
2020-07-30 22:18:38 +00:00
|
|
|
// Http header
|
|
|
|
userAgent = fmt.Sprintf("Prometheus/%s", version.Version)
|
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
|
2021-02-26 21:48:06 +00:00
|
|
|
DefaultSDConfig = SDConfig{
|
|
|
|
HTTPClientConfig: config.DefaultHTTPClientConfig,
|
|
|
|
}
|
2016-09-28 17:29:55 +00:00
|
|
|
)
|
|
|
|
|
2020-08-20 12:48:26 +00:00
|
|
|
func init() {
|
|
|
|
discovery.RegisterConfig(&SDConfig{})
|
|
|
|
prometheus.MustRegister(eventCount)
|
|
|
|
// Initialize metric vectors.
|
|
|
|
for _, role := range []string{"endpointslice", "endpoints", "node", "pod", "service", "ingress"} {
|
|
|
|
for _, evt := range []string{"add", "delete", "update"} {
|
|
|
|
eventCount.WithLabelValues(role, evt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(&clientGoRequestMetricAdapter{}).Register(prometheus.DefaultRegisterer)
|
|
|
|
(&clientGoWorkqueueMetricsProvider{}).Register(prometheus.DefaultRegisterer)
|
|
|
|
}
|
|
|
|
|
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 (
|
2020-02-16 14:59:29 +00:00
|
|
|
RoleNode Role = "node"
|
|
|
|
RolePod Role = "pod"
|
|
|
|
RoleService Role = "service"
|
|
|
|
RoleEndpoint Role = "endpoints"
|
|
|
|
RoleEndpointSlice Role = "endpointslice"
|
|
|
|
RoleIngress Role = "ingress"
|
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 *Role) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
if err := unmarshal((*string)(c)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch *c {
|
2020-07-29 20:14:23 +00:00
|
|
|
case RoleNode, RolePod, RoleService, RoleEndpoint, RoleEndpointSlice, RoleIngress:
|
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
|
|
|
return nil
|
|
|
|
default:
|
2019-03-25 23:01:12 +00:00
|
|
|
return errors.Errorf("unknown Kubernetes SD role %q", *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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SDConfig is the configuration for Kubernetes service discovery.
|
|
|
|
type SDConfig struct {
|
2020-08-20 12:48:26 +00:00
|
|
|
APIServer config.URL `yaml:"api_server,omitempty"`
|
|
|
|
Role Role `yaml:"role"`
|
2021-06-17 10:41:50 +00:00
|
|
|
KubeConfig string `yaml:"kubeconfig_file"`
|
2020-08-20 12:48:26 +00:00
|
|
|
HTTPClientConfig config.HTTPClientConfig `yaml:",inline"`
|
|
|
|
NamespaceDiscovery NamespaceDiscovery `yaml:"namespaces,omitempty"`
|
|
|
|
Selectors []SelectorConfig `yaml:"selectors,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the Config.
|
|
|
|
func (*SDConfig) Name() string { return "kubernetes" }
|
|
|
|
|
|
|
|
// NewDiscoverer returns a Discoverer for the Config.
|
|
|
|
func (c *SDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (discovery.Discoverer, error) {
|
|
|
|
return New(opts.Logger, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDirectory joins any relative file paths with dir.
|
|
|
|
func (c *SDConfig) SetDirectory(dir string) {
|
|
|
|
c.HTTPClientConfig.SetDirectory(dir)
|
2021-06-17 10:41:50 +00:00
|
|
|
c.KubeConfig = config.JoinDir(dir, c.KubeConfig)
|
2019-10-03 11:55:42 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 14:13:44 +00:00
|
|
|
type roleSelector struct {
|
2020-02-16 14:59:29 +00:00
|
|
|
node resourceSelector
|
|
|
|
pod resourceSelector
|
|
|
|
service resourceSelector
|
|
|
|
endpoints resourceSelector
|
|
|
|
endpointslice resourceSelector
|
|
|
|
ingress resourceSelector
|
2019-10-03 11:55:42 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 14:13:44 +00:00
|
|
|
type SelectorConfig struct {
|
|
|
|
Role Role `yaml:"role,omitempty"`
|
2019-10-03 11:55:42 +00:00
|
|
|
Label string `yaml:"label,omitempty"`
|
|
|
|
Field string `yaml:"field,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
|
|
|
}
|
|
|
|
|
2020-02-07 14:13:44 +00:00
|
|
|
type resourceSelector struct {
|
|
|
|
label string
|
|
|
|
field string
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-02-26 21:48:06 +00:00
|
|
|
*c = DefaultSDConfig
|
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
|
|
|
type plain SDConfig
|
|
|
|
err := unmarshal((*plain)(c))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-07 14:13:44 +00:00
|
|
|
if c.Role == "" {
|
2020-02-16 14:59:29 +00:00
|
|
|
return errors.Errorf("role missing (one of: pod, service, endpoints, endpointslice, node, ingress)")
|
2020-02-07 14:13:44 +00:00
|
|
|
}
|
2019-02-20 10:22:34 +00:00
|
|
|
err = c.HTTPClientConfig.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
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
|
|
|
}
|
2021-06-17 10:41:50 +00:00
|
|
|
if c.APIServer.URL != nil && c.KubeConfig != "" {
|
|
|
|
// Api-server and kubeconfig_file are mutually exclusive
|
|
|
|
return errors.Errorf("cannot use 'kubeconfig_file' and 'api_server' simultaneously")
|
|
|
|
}
|
|
|
|
if c.KubeConfig != "" && !reflect.DeepEqual(c.HTTPClientConfig, config.DefaultHTTPClientConfig) {
|
|
|
|
// Kubeconfig_file and custom http config are mutually exclusive
|
|
|
|
return errors.Errorf("cannot use a custom HTTP client configuration together with 'kubeconfig_file'")
|
|
|
|
}
|
2021-02-26 21:48:06 +00:00
|
|
|
if c.APIServer.URL == nil && !reflect.DeepEqual(c.HTTPClientConfig, config.DefaultHTTPClientConfig) {
|
2019-03-25 23:01:12 +00:00
|
|
|
return errors.Errorf("to use custom HTTP client configuration please provide the 'api_server' URL explicitly")
|
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
|
|
|
}
|
2020-02-07 14:13:44 +00:00
|
|
|
|
|
|
|
foundSelectorRoles := make(map[Role]struct{})
|
|
|
|
allowedSelectors := map[Role][]string{
|
2020-02-16 14:59:29 +00:00
|
|
|
RolePod: {string(RolePod)},
|
|
|
|
RoleService: {string(RoleService)},
|
|
|
|
RoleEndpointSlice: {string(RolePod), string(RoleService), string(RoleEndpointSlice)},
|
|
|
|
RoleEndpoint: {string(RolePod), string(RoleService), string(RoleEndpoint)},
|
|
|
|
RoleNode: {string(RoleNode)},
|
|
|
|
RoleIngress: {string(RoleIngress)},
|
2020-02-07 14:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, selector := range c.Selectors {
|
|
|
|
if _, ok := foundSelectorRoles[selector.Role]; ok {
|
|
|
|
return errors.Errorf("duplicated selector role: %s", selector.Role)
|
2019-10-03 11:55:42 +00:00
|
|
|
}
|
2020-02-07 14:13:44 +00:00
|
|
|
foundSelectorRoles[selector.Role] = struct{}{}
|
|
|
|
|
|
|
|
if _, ok := allowedSelectors[c.Role]; !ok {
|
2020-02-16 14:59:29 +00:00
|
|
|
return errors.Errorf("invalid role: %q, expecting one of: pod, service, endpoints, endpointslice, node or ingress", c.Role)
|
2020-02-07 14:13:44 +00:00
|
|
|
}
|
|
|
|
var allowed bool
|
|
|
|
for _, role := range allowedSelectors[c.Role] {
|
|
|
|
if role == string(selector.Role) {
|
|
|
|
allowed = true
|
|
|
|
break
|
|
|
|
}
|
2019-10-03 11:55:42 +00:00
|
|
|
}
|
2020-02-07 14:13:44 +00:00
|
|
|
|
|
|
|
if !allowed {
|
|
|
|
return errors.Errorf("%s role supports only %s selectors", c.Role, strings.Join(allowedSelectors[c.Role], ", "))
|
2019-10-03 11:55:42 +00:00
|
|
|
}
|
2020-02-07 14:13:44 +00:00
|
|
|
|
|
|
|
_, err := fields.ParseSelector(selector.Field)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-10-03 11:55:42 +00:00
|
|
|
}
|
2020-12-14 13:49:50 +00:00
|
|
|
_, err = labels.Parse(selector.Label)
|
2020-02-07 14:13:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-10-03 11:55:42 +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
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-04-09 16:35:14 +00:00
|
|
|
// 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
|
2020-08-20 12:48:26 +00:00
|
|
|
discoverers []discovery.Discoverer
|
2020-02-07 14:13:44 +00:00
|
|
|
selectors roleSelector
|
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
|
|
|
|
)
|
2021-06-17 10:41:50 +00:00
|
|
|
if conf.KubeConfig != "" {
|
|
|
|
kcfg, err = clientcmd.BuildConfigFromFlags("", conf.KubeConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else 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-08-11 18:45:52 +00:00
|
|
|
level.Info(l).Log("msg", "Using pod service account via in-cluster config")
|
2016-09-28 17:29:55 +00:00
|
|
|
} else {
|
2021-04-21 10:19:16 +00:00
|
|
|
rt, err := config.NewRoundTripperFromConfig(conf.HTTPClientConfig, "kubernetes_sd", config.WithHTTP2Disabled())
|
2019-02-20 10:22:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-09-28 17:29:55 +00:00
|
|
|
}
|
2019-02-20 10:22:34 +00:00
|
|
|
kcfg = &rest.Config{
|
|
|
|
Host: conf.APIServer.String(),
|
|
|
|
Transport: rt,
|
2016-09-28 17:29:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 22:18:38 +00:00
|
|
|
kcfg.UserAgent = userAgent
|
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,
|
2020-08-20 12:48:26 +00:00
|
|
|
discoverers: make([]discovery.Discoverer, 0),
|
2020-02-07 14:13:44 +00:00
|
|
|
selectors: mapSelector(conf.Selectors),
|
2016-09-28 17:29:55 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-02-07 14:13:44 +00:00
|
|
|
func mapSelector(rawSelector []SelectorConfig) roleSelector {
|
|
|
|
rs := roleSelector{}
|
|
|
|
for _, resourceSelectorRaw := range rawSelector {
|
|
|
|
switch resourceSelectorRaw.Role {
|
2020-02-16 14:59:29 +00:00
|
|
|
case RoleEndpointSlice:
|
|
|
|
rs.endpointslice.field = resourceSelectorRaw.Field
|
|
|
|
rs.endpointslice.label = resourceSelectorRaw.Label
|
2020-02-07 14:13:44 +00:00
|
|
|
case RoleEndpoint:
|
|
|
|
rs.endpoints.field = resourceSelectorRaw.Field
|
|
|
|
rs.endpoints.label = resourceSelectorRaw.Label
|
|
|
|
case RoleIngress:
|
|
|
|
rs.ingress.field = resourceSelectorRaw.Field
|
|
|
|
rs.ingress.label = resourceSelectorRaw.Label
|
|
|
|
case RoleNode:
|
|
|
|
rs.node.field = resourceSelectorRaw.Field
|
|
|
|
rs.node.label = resourceSelectorRaw.Label
|
|
|
|
case RolePod:
|
|
|
|
rs.pod.field = resourceSelectorRaw.Field
|
|
|
|
rs.pod.label = resourceSelectorRaw.Label
|
|
|
|
case RoleService:
|
|
|
|
rs.service.field = resourceSelectorRaw.Field
|
|
|
|
rs.service.label = resourceSelectorRaw.Label
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rs
|
|
|
|
}
|
|
|
|
|
2016-09-28 17:29:55 +00:00
|
|
|
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 {
|
2020-02-16 14:59:29 +00:00
|
|
|
case RoleEndpointSlice:
|
|
|
|
for _, namespace := range namespaces {
|
|
|
|
e := d.client.DiscoveryV1beta1().EndpointSlices(namespace)
|
|
|
|
elw := &cache.ListWatch{
|
|
|
|
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
|
|
|
options.FieldSelector = d.selectors.endpointslice.field
|
|
|
|
options.LabelSelector = d.selectors.endpointslice.label
|
|
|
|
return e.List(ctx, options)
|
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
|
|
|
options.FieldSelector = d.selectors.endpointslice.field
|
|
|
|
options.LabelSelector = d.selectors.endpointslice.label
|
|
|
|
return e.Watch(ctx, options)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
s := d.client.CoreV1().Services(namespace)
|
|
|
|
slw := &cache.ListWatch{
|
|
|
|
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
|
|
|
options.FieldSelector = d.selectors.service.field
|
|
|
|
options.LabelSelector = d.selectors.service.label
|
|
|
|
return s.List(ctx, options)
|
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
|
|
|
options.FieldSelector = d.selectors.service.field
|
|
|
|
options.LabelSelector = d.selectors.service.label
|
|
|
|
return s.Watch(ctx, options)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
p := d.client.CoreV1().Pods(namespace)
|
|
|
|
plw := &cache.ListWatch{
|
|
|
|
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
|
|
|
options.FieldSelector = d.selectors.pod.field
|
|
|
|
options.LabelSelector = d.selectors.pod.label
|
|
|
|
return p.List(ctx, options)
|
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
|
|
|
options.FieldSelector = d.selectors.pod.field
|
|
|
|
options.LabelSelector = d.selectors.pod.label
|
|
|
|
return p.Watch(ctx, options)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
eps := NewEndpointSlice(
|
|
|
|
log.With(d.logger, "role", "endpointslice"),
|
|
|
|
cache.NewSharedInformer(slw, &apiv1.Service{}, resyncPeriod),
|
|
|
|
cache.NewSharedInformer(elw, &disv1beta1.EndpointSlice{}, resyncPeriod),
|
|
|
|
cache.NewSharedInformer(plw, &apiv1.Pod{}, resyncPeriod),
|
|
|
|
)
|
|
|
|
d.discoverers = append(d.discoverers, eps)
|
|
|
|
go eps.endpointSliceInf.Run(ctx.Done())
|
|
|
|
go eps.serviceInf.Run(ctx.Done())
|
|
|
|
go eps.podInf.Run(ctx.Done())
|
|
|
|
}
|
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) {
|
2020-02-07 14:13:44 +00:00
|
|
|
options.FieldSelector = d.selectors.endpoints.field
|
|
|
|
options.LabelSelector = d.selectors.endpoints.label
|
2020-04-06 16:23:02 +00:00
|
|
|
return e.List(ctx, options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
2020-02-07 14:13:44 +00:00
|
|
|
options.FieldSelector = d.selectors.endpoints.field
|
|
|
|
options.LabelSelector = d.selectors.endpoints.label
|
2020-04-06 16:23:02 +00:00
|
|
|
return e.Watch(ctx, 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) {
|
2020-02-07 14:13:44 +00:00
|
|
|
options.FieldSelector = d.selectors.service.field
|
|
|
|
options.LabelSelector = d.selectors.service.label
|
2020-04-06 16:23:02 +00:00
|
|
|
return s.List(ctx, options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
2020-02-07 14:13:44 +00:00
|
|
|
options.FieldSelector = d.selectors.service.field
|
|
|
|
options.LabelSelector = d.selectors.service.label
|
2020-04-06 16:23:02 +00:00
|
|
|
return s.Watch(ctx, 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) {
|
2020-02-07 14:13:44 +00:00
|
|
|
options.FieldSelector = d.selectors.pod.field
|
|
|
|
options.LabelSelector = d.selectors.pod.label
|
2020-04-06 16:23:02 +00:00
|
|
|
return p.List(ctx, options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
2020-02-07 14:13:44 +00:00
|
|
|
options.FieldSelector = d.selectors.pod.field
|
|
|
|
options.LabelSelector = d.selectors.pod.label
|
2020-04-06 16:23:02 +00:00
|
|
|
return p.Watch(ctx, 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) {
|
2020-02-07 14:13:44 +00:00
|
|
|
options.FieldSelector = d.selectors.pod.field
|
|
|
|
options.LabelSelector = d.selectors.pod.label
|
2020-04-06 16:23:02 +00:00
|
|
|
return p.List(ctx, options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
2020-02-07 14:13:44 +00:00
|
|
|
options.FieldSelector = d.selectors.pod.field
|
|
|
|
options.LabelSelector = d.selectors.pod.label
|
2020-04-06 16:23:02 +00:00
|
|
|
return p.Watch(ctx, 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) {
|
2020-02-07 14:13:44 +00:00
|
|
|
options.FieldSelector = d.selectors.service.field
|
|
|
|
options.LabelSelector = d.selectors.service.label
|
2020-04-06 16:23:02 +00:00
|
|
|
return s.List(ctx, options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
2020-02-07 14:13:44 +00:00
|
|
|
options.FieldSelector = d.selectors.service.field
|
|
|
|
options.LabelSelector = d.selectors.service.label
|
2020-04-06 16:23:02 +00:00
|
|
|
return s.Watch(ctx, 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 {
|
2020-04-06 16:23:02 +00:00
|
|
|
i := d.client.NetworkingV1beta1().Ingresses(namespace)
|
2018-04-09 16:35:14 +00:00
|
|
|
ilw := &cache.ListWatch{
|
|
|
|
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
2020-02-07 14:13:44 +00:00
|
|
|
options.FieldSelector = d.selectors.ingress.field
|
|
|
|
options.LabelSelector = d.selectors.ingress.label
|
2020-04-06 16:23:02 +00:00
|
|
|
return i.List(ctx, options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
2020-02-07 14:13:44 +00:00
|
|
|
options.FieldSelector = d.selectors.ingress.field
|
|
|
|
options.LabelSelector = d.selectors.ingress.label
|
2020-04-06 16:23:02 +00:00
|
|
|
return i.Watch(ctx, 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"),
|
2020-04-06 16:23:02 +00:00
|
|
|
cache.NewSharedInformer(ilw, &v1beta1.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) {
|
2020-02-07 14:13:44 +00:00
|
|
|
options.FieldSelector = d.selectors.node.field
|
|
|
|
options.LabelSelector = d.selectors.node.label
|
2020-04-06 16:23:02 +00:00
|
|
|
return d.client.CoreV1().Nodes().List(ctx, options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
2020-02-07 14:13:44 +00:00
|
|
|
options.FieldSelector = d.selectors.node.field
|
|
|
|
options.LabelSelector = d.selectors.node.label
|
2020-04-06 16:23:02 +00:00
|
|
|
return d.client.CoreV1().Nodes().Watch(ctx, options)
|
2018-04-09 16:35:14 +00:00
|
|
|
},
|
|
|
|
}
|
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)
|
2020-08-20 12:48:26 +00:00
|
|
|
go func(d discovery.Discoverer) {
|
2018-04-09 16:35:14 +00:00
|
|
|
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
|
|
|
|
2020-02-18 16:36:57 +00:00
|
|
|
func send(ctx context.Context, ch chan<- []*targetgroup.Group, tg *targetgroup.Group) {
|
2018-04-10 08:53:00 +00:00
|
|
|
if tg == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case ch <- []*targetgroup.Group{tg}:
|
|
|
|
}
|
|
|
|
}
|