2016-09-30 02:33:23 -07:00
|
|
|
// Copyright 2016 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-10-07 05:53:11 -07:00
|
|
|
package kubernetes
|
2016-09-28 10:29:55 -07:00
|
|
|
|
|
|
|
import (
|
2017-10-24 21:21:42 -07:00
|
|
|
"context"
|
2022-06-03 04:47:14 -07:00
|
|
|
"errors"
|
2020-07-30 15:18:38 -07:00
|
|
|
"fmt"
|
2024-09-09 18:41:53 -07:00
|
|
|
"log/slog"
|
2022-04-27 02:24:36 -07:00
|
|
|
"os"
|
2019-02-20 02:22:34 -08:00
|
|
|
"reflect"
|
2020-02-07 06:13:44 -08:00
|
|
|
"strings"
|
2017-04-19 05:36:34 -07:00
|
|
|
"sync"
|
2016-09-28 10:29:55 -07:00
|
|
|
"time"
|
|
|
|
|
2022-06-25 07:40:47 -07:00
|
|
|
"github.com/prometheus/prometheus/util/strutil"
|
|
|
|
|
2017-10-24 21:21:42 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2020-08-20 05:48:26 -07:00
|
|
|
"github.com/prometheus/common/config"
|
2016-09-28 10:29:55 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2024-09-09 18:41:53 -07:00
|
|
|
"github.com/prometheus/common/promslog"
|
2020-08-20 05:48:26 -07:00
|
|
|
"github.com/prometheus/common/version"
|
2018-07-03 00:37:22 -07:00
|
|
|
apiv1 "k8s.io/api/core/v1"
|
2021-10-22 12:26:31 -07:00
|
|
|
disv1 "k8s.io/api/discovery/v1"
|
2021-08-15 16:34:36 -07:00
|
|
|
networkv1 "k8s.io/api/networking/v1"
|
2018-04-09 09:35:14 -07:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-02-07 06:13:44 -08:00
|
|
|
"k8s.io/apimachinery/pkg/fields"
|
2020-12-14 05:49:50 -08:00
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2018-04-09 09:35:14 -07:00
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apimachinery/pkg/watch"
|
2017-05-11 01:29:10 -07:00
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/tools/cache"
|
2021-06-17 03:41:50 -07:00
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
2019-02-20 02:22:34 -08:00
|
|
|
|
2022-04-01 04:56:37 -07:00
|
|
|
// Required to get the GCP auth provider working.
|
|
|
|
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
|
|
|
|
|
2020-08-20 05:48:26 -07:00
|
|
|
"github.com/prometheus/prometheus/discovery"
|
2019-02-20 02:22:34 -08:00
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
2016-09-28 10:29:55 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-11-27 02:51:33 -08:00
|
|
|
// metaLabelPrefix is the meta prefix used for all meta labels.
|
2016-09-28 10:29:55 -07:00
|
|
|
// in this discovery.
|
2023-10-23 06:55:36 -07:00
|
|
|
metaLabelPrefix = model.MetaLabelPrefix + "kubernetes_"
|
|
|
|
namespaceLabel = metaLabelPrefix + "namespace"
|
|
|
|
presentValue = model.LabelValue("true")
|
2016-09-28 10:29:55 -07:00
|
|
|
)
|
|
|
|
|
2016-10-21 01:48:28 -07:00
|
|
|
var (
|
2023-10-03 13:09:25 -07:00
|
|
|
// Http header.
|
2020-07-30 15:18:38 -07:00
|
|
|
userAgent = fmt.Sprintf("Prometheus/%s", version.Version)
|
2023-10-03 13:09:25 -07:00
|
|
|
// DefaultSDConfig is the default Kubernetes SD configuration.
|
2021-02-26 13:48:06 -08:00
|
|
|
DefaultSDConfig = SDConfig{
|
|
|
|
HTTPClientConfig: config.DefaultHTTPClientConfig,
|
|
|
|
}
|
2016-09-28 10:29:55 -07:00
|
|
|
)
|
|
|
|
|
2020-08-20 05:48:26 -07:00
|
|
|
func init() {
|
|
|
|
discovery.RegisterConfig(&SDConfig{})
|
|
|
|
}
|
|
|
|
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
// Role is role of the service in Kubernetes.
|
|
|
|
type Role string
|
|
|
|
|
|
|
|
// The valid options for Role.
|
|
|
|
const (
|
2020-02-16 06:59:29 -08: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 12:01:34 -08: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 13:14:23 -07: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 12:01:34 -08:00
|
|
|
return nil
|
|
|
|
default:
|
2022-06-03 04:47:14 -07:00
|
|
|
return fmt.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 12:01:34 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-11 05:17:47 -08:00
|
|
|
func (c Role) String() string {
|
|
|
|
return string(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
MetricLabelRoleAdd = "add"
|
|
|
|
MetricLabelRoleDelete = "delete"
|
|
|
|
MetricLabelRoleUpdate = "update"
|
|
|
|
)
|
|
|
|
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
// SDConfig is the configuration for Kubernetes service discovery.
|
|
|
|
type SDConfig struct {
|
2020-08-20 05:48:26 -07:00
|
|
|
APIServer config.URL `yaml:"api_server,omitempty"`
|
|
|
|
Role Role `yaml:"role"`
|
2021-06-17 03:41:50 -07:00
|
|
|
KubeConfig string `yaml:"kubeconfig_file"`
|
2020-08-20 05:48:26 -07:00
|
|
|
HTTPClientConfig config.HTTPClientConfig `yaml:",inline"`
|
|
|
|
NamespaceDiscovery NamespaceDiscovery `yaml:"namespaces,omitempty"`
|
|
|
|
Selectors []SelectorConfig `yaml:"selectors,omitempty"`
|
2021-12-23 01:50:00 -08:00
|
|
|
AttachMetadata AttachMetadataConfig `yaml:"attach_metadata,omitempty"`
|
2020-08-20 05:48:26 -07:00
|
|
|
}
|
|
|
|
|
2024-01-23 07:53:55 -08:00
|
|
|
// NewDiscovererMetrics implements discovery.Config.
|
|
|
|
func (*SDConfig) NewDiscovererMetrics(reg prometheus.Registerer, rmi discovery.RefreshMetricsInstantiator) discovery.DiscovererMetrics {
|
|
|
|
return newDiscovererMetrics(reg, rmi)
|
|
|
|
}
|
|
|
|
|
2020-08-20 05:48:26 -07:00
|
|
|
// 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) {
|
2024-01-23 07:53:55 -08:00
|
|
|
return New(opts.Logger, opts.Metrics, c)
|
2020-08-20 05:48:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetDirectory joins any relative file paths with dir.
|
|
|
|
func (c *SDConfig) SetDirectory(dir string) {
|
|
|
|
c.HTTPClientConfig.SetDirectory(dir)
|
2021-06-17 03:41:50 -07:00
|
|
|
c.KubeConfig = config.JoinDir(dir, c.KubeConfig)
|
2019-10-03 04:55:42 -07:00
|
|
|
}
|
|
|
|
|
2020-02-07 06:13:44 -08:00
|
|
|
type roleSelector struct {
|
2020-02-16 06:59:29 -08:00
|
|
|
node resourceSelector
|
|
|
|
pod resourceSelector
|
|
|
|
service resourceSelector
|
|
|
|
endpoints resourceSelector
|
|
|
|
endpointslice resourceSelector
|
|
|
|
ingress resourceSelector
|
2019-10-03 04:55:42 -07:00
|
|
|
}
|
|
|
|
|
2020-02-07 06:13:44 -08:00
|
|
|
type SelectorConfig struct {
|
|
|
|
Role Role `yaml:"role,omitempty"`
|
2019-10-03 04:55:42 -07: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 12:01:34 -08:00
|
|
|
}
|
|
|
|
|
2020-02-07 06:13:44 -08:00
|
|
|
type resourceSelector struct {
|
|
|
|
label string
|
|
|
|
field string
|
|
|
|
}
|
|
|
|
|
2021-12-23 01:50:00 -08:00
|
|
|
// AttachMetadataConfig is the configuration for attaching additional metadata
|
|
|
|
// coming from nodes on which the targets are scheduled.
|
|
|
|
type AttachMetadataConfig struct {
|
|
|
|
Node bool `yaml:"node"`
|
|
|
|
}
|
|
|
|
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
|
|
|
func (c *SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
2021-02-26 13:48:06 -08: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 12:01:34 -08:00
|
|
|
type plain SDConfig
|
|
|
|
err := unmarshal((*plain)(c))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-07 06:13:44 -08:00
|
|
|
if c.Role == "" {
|
2024-11-03 04:15:51 -08:00
|
|
|
return errors.New("role missing (one of: pod, service, endpoints, endpointslice, node, ingress)")
|
2020-02-07 06:13:44 -08:00
|
|
|
}
|
2019-02-20 02:22:34 -08: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 12:01:34 -08:00
|
|
|
}
|
2021-06-17 03:41:50 -07:00
|
|
|
if c.APIServer.URL != nil && c.KubeConfig != "" {
|
|
|
|
// Api-server and kubeconfig_file are mutually exclusive
|
2024-11-03 04:15:51 -08:00
|
|
|
return errors.New("cannot use 'kubeconfig_file' and 'api_server' simultaneously")
|
2021-06-17 03:41:50 -07:00
|
|
|
}
|
|
|
|
if c.KubeConfig != "" && !reflect.DeepEqual(c.HTTPClientConfig, config.DefaultHTTPClientConfig) {
|
|
|
|
// Kubeconfig_file and custom http config are mutually exclusive
|
2024-11-03 04:15:51 -08:00
|
|
|
return errors.New("cannot use a custom HTTP client configuration together with 'kubeconfig_file'")
|
2021-06-17 03:41:50 -07:00
|
|
|
}
|
2021-02-26 13:48:06 -08:00
|
|
|
if c.APIServer.URL == nil && !reflect.DeepEqual(c.HTTPClientConfig, config.DefaultHTTPClientConfig) {
|
2024-11-03 04:15:51 -08:00
|
|
|
return errors.New("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 12:01:34 -08:00
|
|
|
}
|
2022-02-01 05:59:09 -08:00
|
|
|
if c.APIServer.URL != nil && c.NamespaceDiscovery.IncludeOwnNamespace {
|
2024-11-03 04:15:51 -08:00
|
|
|
return errors.New("cannot use 'api_server' and 'namespaces.own_namespace' simultaneously")
|
2022-02-01 05:59:09 -08:00
|
|
|
}
|
|
|
|
if c.KubeConfig != "" && c.NamespaceDiscovery.IncludeOwnNamespace {
|
2024-11-03 04:15:51 -08:00
|
|
|
return errors.New("cannot use 'kubeconfig_file' and 'namespaces.own_namespace' simultaneously")
|
2022-02-01 05:59:09 -08:00
|
|
|
}
|
2020-02-07 06:13:44 -08:00
|
|
|
|
|
|
|
foundSelectorRoles := make(map[Role]struct{})
|
|
|
|
allowedSelectors := map[Role][]string{
|
2020-02-16 06:59:29 -08: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 06:13:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, selector := range c.Selectors {
|
|
|
|
if _, ok := foundSelectorRoles[selector.Role]; ok {
|
2022-06-03 04:47:14 -07:00
|
|
|
return fmt.Errorf("duplicated selector role: %s", selector.Role)
|
2019-10-03 04:55:42 -07:00
|
|
|
}
|
2020-02-07 06:13:44 -08:00
|
|
|
foundSelectorRoles[selector.Role] = struct{}{}
|
|
|
|
|
|
|
|
if _, ok := allowedSelectors[c.Role]; !ok {
|
2022-06-03 04:47:14 -07:00
|
|
|
return fmt.Errorf("invalid role: %q, expecting one of: pod, service, endpoints, endpointslice, node or ingress", c.Role)
|
2020-02-07 06:13:44 -08:00
|
|
|
}
|
|
|
|
var allowed bool
|
|
|
|
for _, role := range allowedSelectors[c.Role] {
|
|
|
|
if role == string(selector.Role) {
|
|
|
|
allowed = true
|
|
|
|
break
|
|
|
|
}
|
2019-10-03 04:55:42 -07:00
|
|
|
}
|
2020-02-07 06:13:44 -08:00
|
|
|
|
|
|
|
if !allowed {
|
2022-06-03 04:47:14 -07:00
|
|
|
return fmt.Errorf("%s role supports only %s selectors", c.Role, strings.Join(allowedSelectors[c.Role], ", "))
|
2019-10-03 04:55:42 -07:00
|
|
|
}
|
2020-02-07 06:13:44 -08:00
|
|
|
|
|
|
|
_, err := fields.ParseSelector(selector.Field)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-10-03 04:55:42 -07:00
|
|
|
}
|
2020-12-14 05:49:50 -08:00
|
|
|
_, err = labels.Parse(selector.Label)
|
2020-02-07 06:13:44 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-10-03 04:55:42 -07:00
|
|
|
}
|
|
|
|
}
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NamespaceDiscovery is the configuration for discovering
|
|
|
|
// Kubernetes namespaces.
|
|
|
|
type NamespaceDiscovery struct {
|
2021-11-27 02:51:33 -08:00
|
|
|
IncludeOwnNamespace bool `yaml:"own_namespace"`
|
|
|
|
Names []string `yaml:"names"`
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
|
|
|
func (c *NamespaceDiscovery) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
*c = NamespaceDiscovery{}
|
|
|
|
type plain NamespaceDiscovery
|
2018-04-04 01:07:39 -07: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 12:01:34 -08:00
|
|
|
}
|
|
|
|
|
2018-04-09 09:35:14 -07:00
|
|
|
// Discovery implements the discoverer interface for discovering
|
2016-09-28 10:29:55 -07:00
|
|
|
// targets from Kubernetes.
|
2017-03-16 16:29:47 -07:00
|
|
|
type Discovery struct {
|
2018-04-09 09:35:14 -07:00
|
|
|
sync.RWMutex
|
2017-04-19 05:36:34 -07: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 12:01:34 -08:00
|
|
|
role Role
|
2024-09-09 18:41:53 -07:00
|
|
|
logger *slog.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 12:01:34 -08:00
|
|
|
namespaceDiscovery *NamespaceDiscovery
|
2020-08-20 05:48:26 -07:00
|
|
|
discoverers []discovery.Discoverer
|
2020-02-07 06:13:44 -08:00
|
|
|
selectors roleSelector
|
2021-11-27 02:51:33 -08:00
|
|
|
ownNamespace string
|
2021-12-23 01:50:00 -08:00
|
|
|
attachMetadata AttachMetadataConfig
|
2024-01-23 07:53:55 -08:00
|
|
|
metrics *kubernetesMetrics
|
2016-09-28 10:29:55 -07:00
|
|
|
}
|
|
|
|
|
2017-04-19 05:36:34 -07:00
|
|
|
func (d *Discovery) getNamespaces() []string {
|
|
|
|
namespaces := d.namespaceDiscovery.Names
|
2021-11-27 02:51:33 -08:00
|
|
|
includeOwnNamespace := d.namespaceDiscovery.IncludeOwnNamespace
|
|
|
|
|
|
|
|
if len(namespaces) == 0 && !includeOwnNamespace {
|
|
|
|
return []string{apiv1.NamespaceAll}
|
|
|
|
}
|
|
|
|
|
2022-02-01 01:20:03 -08:00
|
|
|
if includeOwnNamespace && d.ownNamespace != "" {
|
2021-11-27 02:51:33 -08:00
|
|
|
return append(namespaces, d.ownNamespace)
|
2017-04-19 05:36:34 -07:00
|
|
|
}
|
2021-11-27 02:51:33 -08:00
|
|
|
|
2017-04-19 05:36:34 -07:00
|
|
|
return namespaces
|
|
|
|
}
|
|
|
|
|
2016-09-28 10:29:55 -07:00
|
|
|
// New creates a new Kubernetes discovery for the given role.
|
2024-09-09 18:41:53 -07:00
|
|
|
func New(l *slog.Logger, metrics discovery.DiscovererMetrics, conf *SDConfig) (*Discovery, error) {
|
2024-01-23 07:53:55 -08:00
|
|
|
m, ok := metrics.(*kubernetesMetrics)
|
|
|
|
if !ok {
|
2024-11-03 04:15:51 -08:00
|
|
|
return nil, errors.New("invalid discovery metrics type")
|
2024-01-23 07:53:55 -08:00
|
|
|
}
|
|
|
|
|
2017-08-11 11:45:52 -07:00
|
|
|
if l == nil {
|
2024-09-09 18:41:53 -07:00
|
|
|
l = promslog.NewNopLogger()
|
2017-08-11 11:45:52 -07:00
|
|
|
}
|
2016-09-28 10:29:55 -07:00
|
|
|
var (
|
2022-02-01 01:20:03 -08:00
|
|
|
kcfg *rest.Config
|
|
|
|
err error
|
|
|
|
ownNamespace string
|
2016-09-28 10:29:55 -07:00
|
|
|
)
|
style: Replace `else if` cascades with `switch`
Wiser coders than myself have come to the conclusion that a `switch`
statement is almost always superior to a statement that includes any
`else if`.
The exceptions that I have found in our codebase are just these two:
* The `if else` is followed by an additional statement before the next
condition (separated by a `;`).
* The whole thing is within a `for` loop and `break` statements are
used. In this case, using `switch` would require tagging the `for`
loop, which probably tips the balance.
Why are `switch` statements more readable?
For one, fewer curly braces. But more importantly, the conditions all
have the same alignment, so the whole thing follows the natural flow
of going down a list of conditions. With `else if`, in contrast, all
conditions but the first are "hidden" behind `} else if `, harder to
spot and (for no good reason) presented differently from the first
condition.
I'm sure the aforemention wise coders can list even more reasons.
In any case, I like it so much that I have found myself recommending
it in code reviews. I would like to make it a habit in our code base,
without making it a hard requirement that we would test on the CI. But
for that, there has to be a role model, so this commit eliminates all
`if else` occurrences, unless it is autogenerated code or fits one of
the exceptions above.
Signed-off-by: beorn7 <beorn@grafana.com>
2023-04-12 07:14:31 -07:00
|
|
|
switch {
|
|
|
|
case conf.KubeConfig != "":
|
2021-06-17 03:41:50 -07:00
|
|
|
kcfg, err = clientcmd.BuildConfigFromFlags("", conf.KubeConfig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
style: Replace `else if` cascades with `switch`
Wiser coders than myself have come to the conclusion that a `switch`
statement is almost always superior to a statement that includes any
`else if`.
The exceptions that I have found in our codebase are just these two:
* The `if else` is followed by an additional statement before the next
condition (separated by a `;`).
* The whole thing is within a `for` loop and `break` statements are
used. In this case, using `switch` would require tagging the `for`
loop, which probably tips the balance.
Why are `switch` statements more readable?
For one, fewer curly braces. But more importantly, the conditions all
have the same alignment, so the whole thing follows the natural flow
of going down a list of conditions. With `else if`, in contrast, all
conditions but the first are "hidden" behind `} else if `, harder to
spot and (for no good reason) presented differently from the first
condition.
I'm sure the aforemention wise coders can list even more reasons.
In any case, I like it so much that I have found myself recommending
it in code reviews. I would like to make it a habit in our code base,
without making it a hard requirement that we would test on the CI. But
for that, there has to be a role model, so this commit eliminates all
`if else` occurrences, unless it is autogenerated code or fits one of
the exceptions above.
Signed-off-by: beorn7 <beorn@grafana.com>
2023-04-12 07:14:31 -07:00
|
|
|
case conf.APIServer.URL == nil:
|
2017-01-19 01:52:52 -08:00
|
|
|
// Use the Kubernetes provided pod service account
|
2024-04-06 04:48:24 -07:00
|
|
|
// as described in https://kubernetes.io/docs/tasks/run-application/access-api-from-pod/#using-official-client-libraries
|
2016-09-28 10:29:55 -07:00
|
|
|
kcfg, err = rest.InClusterConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-01 01:20:03 -08:00
|
|
|
|
2022-02-01 05:59:09 -08:00
|
|
|
if conf.NamespaceDiscovery.IncludeOwnNamespace {
|
2022-04-27 02:24:36 -07:00
|
|
|
ownNamespaceContents, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
|
2022-02-01 05:59:09 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not determine the pod's namespace: %w", err)
|
|
|
|
}
|
|
|
|
if len(ownNamespaceContents) == 0 {
|
|
|
|
return nil, errors.New("could not read own namespace name (empty file)")
|
|
|
|
}
|
|
|
|
ownNamespace = string(ownNamespaceContents)
|
2022-02-01 01:20:03 -08:00
|
|
|
}
|
|
|
|
|
2024-09-09 18:41:53 -07:00
|
|
|
l.Info("Using pod service account via in-cluster config")
|
style: Replace `else if` cascades with `switch`
Wiser coders than myself have come to the conclusion that a `switch`
statement is almost always superior to a statement that includes any
`else if`.
The exceptions that I have found in our codebase are just these two:
* The `if else` is followed by an additional statement before the next
condition (separated by a `;`).
* The whole thing is within a `for` loop and `break` statements are
used. In this case, using `switch` would require tagging the `for`
loop, which probably tips the balance.
Why are `switch` statements more readable?
For one, fewer curly braces. But more importantly, the conditions all
have the same alignment, so the whole thing follows the natural flow
of going down a list of conditions. With `else if`, in contrast, all
conditions but the first are "hidden" behind `} else if `, harder to
spot and (for no good reason) presented differently from the first
condition.
I'm sure the aforemention wise coders can list even more reasons.
In any case, I like it so much that I have found myself recommending
it in code reviews. I would like to make it a habit in our code base,
without making it a hard requirement that we would test on the CI. But
for that, there has to be a role model, so this commit eliminates all
`if else` occurrences, unless it is autogenerated code or fits one of
the exceptions above.
Signed-off-by: beorn7 <beorn@grafana.com>
2023-04-12 07:14:31 -07:00
|
|
|
default:
|
2021-09-26 14:16:12 -07:00
|
|
|
rt, err := config.NewRoundTripperFromConfig(conf.HTTPClientConfig, "kubernetes_sd")
|
2019-02-20 02:22:34 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-09-28 10:29:55 -07:00
|
|
|
}
|
2019-02-20 02:22:34 -08:00
|
|
|
kcfg = &rest.Config{
|
|
|
|
Host: conf.APIServer.String(),
|
|
|
|
Transport: rt,
|
2016-09-28 10:29:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 15:18:38 -07:00
|
|
|
kcfg.UserAgent = userAgent
|
2022-09-26 05:53:39 -07:00
|
|
|
kcfg.ContentType = "application/vnd.kubernetes.protobuf"
|
2016-09-28 10:29:55 -07:00
|
|
|
|
|
|
|
c, err := kubernetes.NewForConfig(kcfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-27 02:51:33 -08:00
|
|
|
|
2023-10-23 06:55:36 -07:00
|
|
|
d := &Discovery{
|
2017-04-19 05:36:34 -07:00
|
|
|
client: c,
|
|
|
|
logger: l,
|
|
|
|
role: conf.Role,
|
|
|
|
namespaceDiscovery: &conf.NamespaceDiscovery,
|
2020-08-20 05:48:26 -07:00
|
|
|
discoverers: make([]discovery.Discoverer, 0),
|
2020-02-07 06:13:44 -08:00
|
|
|
selectors: mapSelector(conf.Selectors),
|
2022-02-01 01:20:03 -08:00
|
|
|
ownNamespace: ownNamespace,
|
2021-12-23 01:50:00 -08:00
|
|
|
attachMetadata: conf.AttachMetadata,
|
2024-01-23 07:53:55 -08:00
|
|
|
metrics: m,
|
2023-10-23 06:55:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return d, nil
|
2016-09-28 10:29:55 -07:00
|
|
|
}
|
|
|
|
|
2020-02-07 06:13:44 -08:00
|
|
|
func mapSelector(rawSelector []SelectorConfig) roleSelector {
|
|
|
|
rs := roleSelector{}
|
|
|
|
for _, resourceSelectorRaw := range rawSelector {
|
|
|
|
switch resourceSelectorRaw.Role {
|
2020-02-16 06:59:29 -08:00
|
|
|
case RoleEndpointSlice:
|
|
|
|
rs.endpointslice.field = resourceSelectorRaw.Field
|
|
|
|
rs.endpointslice.label = resourceSelectorRaw.Label
|
2020-02-07 06:13:44 -08: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
|
|
|
|
}
|
|
|
|
|
2022-12-20 03:40:04 -08:00
|
|
|
// Disable the informer's resync, which just periodically resends already processed updates and distort SD metrics.
|
|
|
|
const resyncDisabled = 0
|
2016-09-28 10:29:55 -07:00
|
|
|
|
2018-04-09 09:35:14 -07:00
|
|
|
// Run implements the discoverer interface.
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
func (d *Discovery) Run(ctx context.Context, ch chan<- []*targetgroup.Group) {
|
2018-04-09 09:35:14 -07:00
|
|
|
d.Lock()
|
2023-10-23 06:55:36 -07:00
|
|
|
|
2017-04-19 05:36:34 -07:00
|
|
|
namespaces := d.getNamespaces()
|
|
|
|
|
2017-03-16 16:29:47 -07:00
|
|
|
switch d.role {
|
2020-02-16 06:59:29 -08:00
|
|
|
case RoleEndpointSlice:
|
|
|
|
for _, namespace := range namespaces {
|
2022-05-27 02:45:09 -07:00
|
|
|
var informer cache.SharedIndexInformer
|
2024-06-28 07:04:30 -07:00
|
|
|
e := d.client.DiscoveryV1().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)
|
|
|
|
},
|
2020-02-16 06:59:29 -08:00
|
|
|
}
|
2024-06-28 07:04:30 -07:00
|
|
|
informer = d.newEndpointSlicesByNodeInformer(elw, &disv1.EndpointSlice{})
|
2021-10-22 12:26:31 -07:00
|
|
|
|
2020-02-16 06:59:29 -08:00
|
|
|
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)
|
|
|
|
},
|
|
|
|
}
|
2022-05-27 02:45:09 -07:00
|
|
|
var nodeInf cache.SharedInformer
|
|
|
|
if d.attachMetadata.Node {
|
|
|
|
nodeInf = d.newNodeInformer(context.Background())
|
|
|
|
go nodeInf.Run(ctx.Done())
|
|
|
|
}
|
2020-02-16 06:59:29 -08:00
|
|
|
eps := NewEndpointSlice(
|
2024-09-09 18:41:53 -07:00
|
|
|
d.logger.With("role", "endpointslice"),
|
2021-10-22 12:26:31 -07:00
|
|
|
informer,
|
2024-02-07 03:38:40 -08:00
|
|
|
d.mustNewSharedInformer(slw, &apiv1.Service{}, resyncDisabled),
|
|
|
|
d.mustNewSharedInformer(plw, &apiv1.Pod{}, resyncDisabled),
|
2022-05-27 02:45:09 -07:00
|
|
|
nodeInf,
|
2024-01-23 07:53:55 -08:00
|
|
|
d.metrics.eventCount,
|
2020-02-16 06:59:29 -08:00
|
|
|
)
|
|
|
|
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 01:53:00 -07:00
|
|
|
case RoleEndpoint:
|
2017-04-19 05:36:34 -07:00
|
|
|
for _, namespace := range namespaces {
|
2018-06-14 07:49:43 -07:00
|
|
|
e := d.client.CoreV1().Endpoints(namespace)
|
2018-04-09 09:35:14 -07:00
|
|
|
elw := &cache.ListWatch{
|
|
|
|
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
2020-02-07 06:13:44 -08:00
|
|
|
options.FieldSelector = d.selectors.endpoints.field
|
|
|
|
options.LabelSelector = d.selectors.endpoints.label
|
2020-04-06 09:23:02 -07:00
|
|
|
return e.List(ctx, options)
|
2018-04-09 09:35:14 -07:00
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
2020-02-07 06:13:44 -08:00
|
|
|
options.FieldSelector = d.selectors.endpoints.field
|
|
|
|
options.LabelSelector = d.selectors.endpoints.label
|
2020-04-06 09:23:02 -07:00
|
|
|
return e.Watch(ctx, options)
|
2018-04-09 09:35:14 -07:00
|
|
|
},
|
|
|
|
}
|
2018-06-14 07:49:43 -07:00
|
|
|
s := d.client.CoreV1().Services(namespace)
|
2018-04-09 09:35:14 -07:00
|
|
|
slw := &cache.ListWatch{
|
|
|
|
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
2020-02-07 06:13:44 -08:00
|
|
|
options.FieldSelector = d.selectors.service.field
|
|
|
|
options.LabelSelector = d.selectors.service.label
|
2020-04-06 09:23:02 -07:00
|
|
|
return s.List(ctx, options)
|
2018-04-09 09:35:14 -07:00
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
2020-02-07 06:13:44 -08:00
|
|
|
options.FieldSelector = d.selectors.service.field
|
|
|
|
options.LabelSelector = d.selectors.service.label
|
2020-04-06 09:23:02 -07:00
|
|
|
return s.Watch(ctx, options)
|
2018-04-09 09:35:14 -07:00
|
|
|
},
|
|
|
|
}
|
2018-06-14 07:49:43 -07:00
|
|
|
p := d.client.CoreV1().Pods(namespace)
|
2018-04-09 09:35:14 -07:00
|
|
|
plw := &cache.ListWatch{
|
|
|
|
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
2020-02-07 06:13:44 -08:00
|
|
|
options.FieldSelector = d.selectors.pod.field
|
|
|
|
options.LabelSelector = d.selectors.pod.label
|
2020-04-06 09:23:02 -07:00
|
|
|
return p.List(ctx, options)
|
2018-04-09 09:35:14 -07:00
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
2020-02-07 06:13:44 -08:00
|
|
|
options.FieldSelector = d.selectors.pod.field
|
|
|
|
options.LabelSelector = d.selectors.pod.label
|
2020-04-06 09:23:02 -07:00
|
|
|
return p.Watch(ctx, options)
|
2018-04-09 09:35:14 -07:00
|
|
|
},
|
|
|
|
}
|
2022-05-27 02:45:09 -07:00
|
|
|
var nodeInf cache.SharedInformer
|
|
|
|
if d.attachMetadata.Node {
|
|
|
|
nodeInf = d.newNodeInformer(ctx)
|
|
|
|
go nodeInf.Run(ctx.Done())
|
|
|
|
}
|
|
|
|
|
2017-04-19 05:36:34 -07:00
|
|
|
eps := NewEndpoints(
|
2024-09-09 18:41:53 -07:00
|
|
|
d.logger.With("role", "endpoint"),
|
2022-05-27 02:45:09 -07:00
|
|
|
d.newEndpointsByNodeInformer(elw),
|
2024-02-07 03:38:40 -08:00
|
|
|
d.mustNewSharedInformer(slw, &apiv1.Service{}, resyncDisabled),
|
|
|
|
d.mustNewSharedInformer(plw, &apiv1.Pod{}, resyncDisabled),
|
2022-05-27 02:45:09 -07:00
|
|
|
nodeInf,
|
2024-01-23 07:53:55 -08:00
|
|
|
d.metrics.eventCount,
|
2017-04-19 05:36:34 -07:00
|
|
|
)
|
2018-04-09 09:35:14 -07:00
|
|
|
d.discoverers = append(d.discoverers, eps)
|
2017-04-19 05:36:34 -07:00
|
|
|
go eps.endpointsInf.Run(ctx.Done())
|
|
|
|
go eps.serviceInf.Run(ctx.Done())
|
|
|
|
go eps.podInf.Run(ctx.Done())
|
|
|
|
}
|
2018-04-10 01:53:00 -07:00
|
|
|
case RolePod:
|
2021-12-23 01:50:00 -08:00
|
|
|
var nodeInformer cache.SharedInformer
|
|
|
|
if d.attachMetadata.Node {
|
|
|
|
nodeInformer = d.newNodeInformer(ctx)
|
|
|
|
go nodeInformer.Run(ctx.Done())
|
|
|
|
}
|
|
|
|
|
2017-04-19 05:36:34 -07:00
|
|
|
for _, namespace := range namespaces {
|
2018-06-14 07:49:43 -07:00
|
|
|
p := d.client.CoreV1().Pods(namespace)
|
2018-04-09 09:35:14 -07:00
|
|
|
plw := &cache.ListWatch{
|
|
|
|
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
2020-02-07 06:13:44 -08:00
|
|
|
options.FieldSelector = d.selectors.pod.field
|
|
|
|
options.LabelSelector = d.selectors.pod.label
|
2020-04-06 09:23:02 -07:00
|
|
|
return p.List(ctx, options)
|
2018-04-09 09:35:14 -07:00
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
2020-02-07 06:13:44 -08:00
|
|
|
options.FieldSelector = d.selectors.pod.field
|
|
|
|
options.LabelSelector = d.selectors.pod.label
|
2020-04-06 09:23:02 -07:00
|
|
|
return p.Watch(ctx, options)
|
2018-04-09 09:35:14 -07:00
|
|
|
},
|
|
|
|
}
|
2017-04-19 05:36:34 -07:00
|
|
|
pod := NewPod(
|
2024-09-09 18:41:53 -07:00
|
|
|
d.logger.With("role", "pod"),
|
2022-03-10 06:33:34 -08:00
|
|
|
d.newPodsByNodeInformer(plw),
|
2021-12-23 01:50:00 -08:00
|
|
|
nodeInformer,
|
2024-01-23 07:53:55 -08:00
|
|
|
d.metrics.eventCount,
|
2017-04-19 05:36:34 -07:00
|
|
|
)
|
2018-04-09 09:35:14 -07:00
|
|
|
d.discoverers = append(d.discoverers, pod)
|
2021-12-23 01:50:00 -08:00
|
|
|
go pod.podInf.Run(ctx.Done())
|
2016-09-28 10:29:55 -07:00
|
|
|
}
|
2018-04-10 01:53:00 -07:00
|
|
|
case RoleService:
|
2017-04-19 05:36:34 -07:00
|
|
|
for _, namespace := range namespaces {
|
2018-06-14 07:49:43 -07:00
|
|
|
s := d.client.CoreV1().Services(namespace)
|
2018-04-09 09:35:14 -07:00
|
|
|
slw := &cache.ListWatch{
|
|
|
|
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
2020-02-07 06:13:44 -08:00
|
|
|
options.FieldSelector = d.selectors.service.field
|
|
|
|
options.LabelSelector = d.selectors.service.label
|
2020-04-06 09:23:02 -07:00
|
|
|
return s.List(ctx, options)
|
2018-04-09 09:35:14 -07:00
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
2020-02-07 06:13:44 -08:00
|
|
|
options.FieldSelector = d.selectors.service.field
|
|
|
|
options.LabelSelector = d.selectors.service.label
|
2020-04-06 09:23:02 -07:00
|
|
|
return s.Watch(ctx, options)
|
2018-04-09 09:35:14 -07:00
|
|
|
},
|
|
|
|
}
|
2017-04-19 05:36:34 -07:00
|
|
|
svc := NewService(
|
2024-09-09 18:41:53 -07:00
|
|
|
d.logger.With("role", "service"),
|
2024-02-07 03:38:40 -08:00
|
|
|
d.mustNewSharedInformer(slw, &apiv1.Service{}, resyncDisabled),
|
2024-01-23 07:53:55 -08:00
|
|
|
d.metrics.eventCount,
|
2017-04-19 05:36:34 -07:00
|
|
|
)
|
2018-04-09 09:35:14 -07:00
|
|
|
d.discoverers = append(d.discoverers, svc)
|
2017-04-19 05:36:34 -07:00
|
|
|
go svc.informer.Run(ctx.Done())
|
2016-10-06 07:28:59 -07:00
|
|
|
}
|
2018-04-10 01:53:00 -07:00
|
|
|
case RoleIngress:
|
2017-09-04 04:10:44 -07:00
|
|
|
for _, namespace := range namespaces {
|
2021-08-15 16:34:36 -07:00
|
|
|
var informer cache.SharedInformer
|
2024-06-28 07:04:30 -07:00
|
|
|
i := d.client.NetworkingV1().Ingresses(namespace)
|
|
|
|
ilw := &cache.ListWatch{
|
|
|
|
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
|
|
|
options.FieldSelector = d.selectors.ingress.field
|
|
|
|
options.LabelSelector = d.selectors.ingress.label
|
|
|
|
return i.List(ctx, options)
|
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
|
|
|
options.FieldSelector = d.selectors.ingress.field
|
|
|
|
options.LabelSelector = d.selectors.ingress.label
|
|
|
|
return i.Watch(ctx, options)
|
|
|
|
},
|
2018-04-09 09:35:14 -07:00
|
|
|
}
|
2024-06-28 07:04:30 -07:00
|
|
|
informer = d.mustNewSharedInformer(ilw, &networkv1.Ingress{}, resyncDisabled)
|
2017-09-04 04:10:44 -07:00
|
|
|
ingress := NewIngress(
|
2024-09-09 18:41:53 -07:00
|
|
|
d.logger.With("role", "ingress"),
|
2021-08-15 16:34:36 -07:00
|
|
|
informer,
|
2024-01-23 07:53:55 -08:00
|
|
|
d.metrics.eventCount,
|
2017-09-04 04:10:44 -07:00
|
|
|
)
|
2018-04-09 09:35:14 -07:00
|
|
|
d.discoverers = append(d.discoverers, ingress)
|
2017-09-04 04:10:44 -07:00
|
|
|
go ingress.informer.Run(ctx.Done())
|
|
|
|
}
|
2018-04-10 01:53:00 -07:00
|
|
|
case RoleNode:
|
2021-12-23 01:50:00 -08:00
|
|
|
nodeInformer := d.newNodeInformer(ctx)
|
2024-09-09 18:41:53 -07:00
|
|
|
node := NewNode(d.logger.With("role", "node"), nodeInformer, d.metrics.eventCount)
|
2018-04-09 09:35:14 -07:00
|
|
|
d.discoverers = append(d.discoverers, node)
|
2016-10-07 04:33:04 -07:00
|
|
|
go node.informer.Run(ctx.Done())
|
2016-09-28 10:29:55 -07:00
|
|
|
default:
|
2024-09-09 18:41:53 -07:00
|
|
|
d.logger.Error("unknown Kubernetes discovery kind", "role", d.role)
|
2016-09-28 10:29:55 -07:00
|
|
|
}
|
|
|
|
|
2018-04-09 09:35:14 -07:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, dd := range d.discoverers {
|
|
|
|
wg.Add(1)
|
2020-08-20 05:48:26 -07:00
|
|
|
go func(d discovery.Discoverer) {
|
2018-04-09 09:35:14 -07:00
|
|
|
defer wg.Done()
|
|
|
|
d.Run(ctx, ch)
|
|
|
|
}(dd)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Unlock()
|
2018-08-17 06:20:22 -07:00
|
|
|
|
|
|
|
wg.Wait()
|
2016-09-28 10:29:55 -07:00
|
|
|
<-ctx.Done()
|
|
|
|
}
|
|
|
|
|
|
|
|
func lv(s string) model.LabelValue {
|
|
|
|
return model.LabelValue(s)
|
|
|
|
}
|
2018-04-10 01:53:00 -07:00
|
|
|
|
2020-02-18 08:36:57 -08:00
|
|
|
func send(ctx context.Context, ch chan<- []*targetgroup.Group, tg *targetgroup.Group) {
|
2018-04-10 01:53:00 -07:00
|
|
|
if tg == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case ch <- []*targetgroup.Group{tg}:
|
|
|
|
}
|
|
|
|
}
|
2021-08-15 16:34:36 -07:00
|
|
|
|
|
|
|
func retryOnError(ctx context.Context, interval time.Duration, f func() error) (canceled bool) {
|
|
|
|
var err error
|
|
|
|
err = f()
|
|
|
|
for {
|
|
|
|
if err == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return true
|
|
|
|
case <-time.After(interval):
|
|
|
|
err = f()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 01:50:00 -08:00
|
|
|
func (d *Discovery) newNodeInformer(ctx context.Context) cache.SharedInformer {
|
|
|
|
nlw := &cache.ListWatch{
|
|
|
|
ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
|
|
|
|
options.FieldSelector = d.selectors.node.field
|
|
|
|
options.LabelSelector = d.selectors.node.label
|
|
|
|
return d.client.CoreV1().Nodes().List(ctx, options)
|
|
|
|
},
|
|
|
|
WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
|
|
|
|
options.FieldSelector = d.selectors.node.field
|
|
|
|
options.LabelSelector = d.selectors.node.label
|
|
|
|
return d.client.CoreV1().Nodes().Watch(ctx, options)
|
|
|
|
},
|
|
|
|
}
|
2024-02-07 03:38:40 -08:00
|
|
|
return d.mustNewSharedInformer(nlw, &apiv1.Node{}, resyncDisabled)
|
2021-12-23 01:50:00 -08:00
|
|
|
}
|
2022-03-10 06:33:34 -08:00
|
|
|
|
|
|
|
func (d *Discovery) newPodsByNodeInformer(plw *cache.ListWatch) cache.SharedIndexInformer {
|
|
|
|
indexers := make(map[string]cache.IndexFunc)
|
|
|
|
if d.attachMetadata.Node {
|
|
|
|
indexers[nodeIndex] = func(obj interface{}) ([]string, error) {
|
|
|
|
pod, ok := obj.(*apiv1.Pod)
|
|
|
|
if !ok {
|
2024-11-03 04:15:51 -08:00
|
|
|
return nil, errors.New("object is not a pod")
|
2022-03-10 06:33:34 -08:00
|
|
|
}
|
|
|
|
return []string{pod.Spec.NodeName}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-07 03:38:40 -08:00
|
|
|
return d.mustNewSharedIndexInformer(plw, &apiv1.Pod{}, resyncDisabled, indexers)
|
2022-03-10 06:33:34 -08:00
|
|
|
}
|
2021-10-22 12:26:31 -07:00
|
|
|
|
2022-05-27 02:45:09 -07:00
|
|
|
func (d *Discovery) newEndpointsByNodeInformer(plw *cache.ListWatch) cache.SharedIndexInformer {
|
|
|
|
indexers := make(map[string]cache.IndexFunc)
|
2024-02-01 04:34:37 -08:00
|
|
|
indexers[podIndex] = func(obj interface{}) ([]string, error) {
|
|
|
|
e, ok := obj.(*apiv1.Endpoints)
|
|
|
|
if !ok {
|
2024-11-03 04:15:51 -08:00
|
|
|
return nil, errors.New("object is not endpoints")
|
2024-02-01 04:34:37 -08:00
|
|
|
}
|
|
|
|
var pods []string
|
|
|
|
for _, target := range e.Subsets {
|
|
|
|
for _, addr := range target.Addresses {
|
|
|
|
if addr.TargetRef != nil && addr.TargetRef.Kind == "Pod" {
|
|
|
|
pods = append(pods, namespacedName(addr.TargetRef.Namespace, addr.TargetRef.Name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pods, nil
|
|
|
|
}
|
2022-05-27 02:45:09 -07:00
|
|
|
if !d.attachMetadata.Node {
|
2024-02-07 03:38:40 -08:00
|
|
|
return d.mustNewSharedIndexInformer(plw, &apiv1.Endpoints{}, resyncDisabled, indexers)
|
2022-05-27 02:45:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
indexers[nodeIndex] = func(obj interface{}) ([]string, error) {
|
|
|
|
e, ok := obj.(*apiv1.Endpoints)
|
|
|
|
if !ok {
|
2024-11-03 04:15:51 -08:00
|
|
|
return nil, errors.New("object is not endpoints")
|
2022-05-27 02:45:09 -07:00
|
|
|
}
|
|
|
|
var nodes []string
|
|
|
|
for _, target := range e.Subsets {
|
|
|
|
for _, addr := range target.Addresses {
|
2023-05-03 13:46:13 -07:00
|
|
|
if addr.TargetRef != nil {
|
|
|
|
switch addr.TargetRef.Kind {
|
|
|
|
case "Pod":
|
|
|
|
if addr.NodeName != nil {
|
|
|
|
nodes = append(nodes, *addr.NodeName)
|
|
|
|
}
|
|
|
|
case "Node":
|
|
|
|
nodes = append(nodes, addr.TargetRef.Name)
|
|
|
|
}
|
2022-05-27 02:45:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
|
2024-02-07 03:38:40 -08:00
|
|
|
return d.mustNewSharedIndexInformer(plw, &apiv1.Endpoints{}, resyncDisabled, indexers)
|
2022-05-27 02:45:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Discovery) newEndpointSlicesByNodeInformer(plw *cache.ListWatch, object runtime.Object) cache.SharedIndexInformer {
|
|
|
|
indexers := make(map[string]cache.IndexFunc)
|
|
|
|
if !d.attachMetadata.Node {
|
2024-02-07 03:38:40 -08:00
|
|
|
return d.mustNewSharedIndexInformer(plw, object, resyncDisabled, indexers)
|
2022-05-27 02:45:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
indexers[nodeIndex] = func(obj interface{}) ([]string, error) {
|
|
|
|
var nodes []string
|
|
|
|
switch e := obj.(type) {
|
|
|
|
case *disv1.EndpointSlice:
|
|
|
|
for _, target := range e.Endpoints {
|
2023-05-03 13:46:13 -07:00
|
|
|
if target.TargetRef != nil {
|
|
|
|
switch target.TargetRef.Kind {
|
|
|
|
case "Pod":
|
|
|
|
if target.NodeName != nil {
|
|
|
|
nodes = append(nodes, *target.NodeName)
|
|
|
|
}
|
|
|
|
case "Node":
|
|
|
|
nodes = append(nodes, target.TargetRef.Name)
|
|
|
|
}
|
2022-05-27 02:45:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
2024-11-03 04:15:51 -08:00
|
|
|
return nil, errors.New("object is not an endpointslice")
|
2022-05-27 02:45:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
|
2024-02-07 03:38:40 -08:00
|
|
|
return d.mustNewSharedIndexInformer(plw, object, resyncDisabled, indexers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Discovery) informerWatchErrorHandler(r *cache.Reflector, err error) {
|
|
|
|
d.metrics.failuresCount.Inc()
|
|
|
|
cache.DefaultWatchErrorHandler(r, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Discovery) mustNewSharedInformer(lw cache.ListerWatcher, exampleObject runtime.Object, defaultEventHandlerResyncPeriod time.Duration) cache.SharedInformer {
|
|
|
|
informer := cache.NewSharedInformer(lw, exampleObject, defaultEventHandlerResyncPeriod)
|
|
|
|
// Invoking SetWatchErrorHandler should fail only if the informer has been started beforehand.
|
|
|
|
// Such a scenario would suggest an incorrect use of the API, thus the panic.
|
|
|
|
if err := informer.SetWatchErrorHandler(d.informerWatchErrorHandler); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return informer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Discovery) mustNewSharedIndexInformer(lw cache.ListerWatcher, exampleObject runtime.Object, defaultEventHandlerResyncPeriod time.Duration, indexers cache.Indexers) cache.SharedIndexInformer {
|
|
|
|
informer := cache.NewSharedIndexInformer(lw, exampleObject, defaultEventHandlerResyncPeriod, indexers)
|
|
|
|
// Invoking SetWatchErrorHandler should fail only if the informer has been started beforehand.
|
|
|
|
// Such a scenario would suggest an incorrect use of the API, thus the panic.
|
|
|
|
if err := informer.SetWatchErrorHandler(d.informerWatchErrorHandler); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return informer
|
2022-05-27 02:45:09 -07:00
|
|
|
}
|
|
|
|
|
2023-05-30 05:13:00 -07:00
|
|
|
func addObjectMetaLabels(labelSet model.LabelSet, objectMeta metav1.ObjectMeta, role Role) {
|
|
|
|
labelSet[model.LabelName(metaLabelPrefix+string(role)+"_name")] = lv(objectMeta.Name)
|
|
|
|
|
|
|
|
for k, v := range objectMeta.Labels {
|
|
|
|
ln := strutil.SanitizeLabelName(k)
|
|
|
|
labelSet[model.LabelName(metaLabelPrefix+string(role)+"_label_"+ln)] = lv(v)
|
|
|
|
labelSet[model.LabelName(metaLabelPrefix+string(role)+"_labelpresent_"+ln)] = presentValue
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range objectMeta.Annotations {
|
|
|
|
ln := strutil.SanitizeLabelName(k)
|
|
|
|
labelSet[model.LabelName(metaLabelPrefix+string(role)+"_annotation_"+ln)] = lv(v)
|
|
|
|
labelSet[model.LabelName(metaLabelPrefix+string(role)+"_annotationpresent_"+ln)] = presentValue
|
|
|
|
}
|
|
|
|
}
|
2024-02-01 04:34:37 -08:00
|
|
|
|
|
|
|
func namespacedName(namespace, name string) string {
|
|
|
|
return namespace + "/" + name
|
|
|
|
}
|
2024-10-15 11:29:07 -07:00
|
|
|
|
|
|
|
// nodeName knows how to handle the cache.DeletedFinalStateUnknown tombstone.
|
|
|
|
// It assumes the MetaNamespaceKeyFunc keyFunc is used, which uses the node name as the tombstone key.
|
|
|
|
func nodeName(o interface{}) (string, error) {
|
|
|
|
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(o)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return key, nil
|
|
|
|
}
|