2017-09-04 04:10:44 -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.
|
|
|
|
|
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2017-10-24 21:21:42 -07:00
|
|
|
"context"
|
2022-06-03 04:47:14 -07:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-08-26 06:31:05 -07:00
|
|
|
"strings"
|
2017-09-04 04:10:44 -07:00
|
|
|
|
2021-06-11 09:17:59 -07:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2023-10-23 06:55:36 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-09-04 04:10:44 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2021-08-15 16:34:36 -07:00
|
|
|
v1 "k8s.io/api/networking/v1"
|
2017-09-04 04:10:44 -07:00
|
|
|
"k8s.io/client-go/tools/cache"
|
2018-04-09 09:35:14 -07:00
|
|
|
"k8s.io/client-go/util/workqueue"
|
2019-03-25 16:01:12 -07:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
2017-09-04 04:10:44 -07:00
|
|
|
)
|
|
|
|
|
2019-07-27 11:07:23 -07:00
|
|
|
// Ingress implements discovery of Kubernetes ingress.
|
2017-09-04 04:10:44 -07:00
|
|
|
type Ingress struct {
|
|
|
|
logger log.Logger
|
|
|
|
informer cache.SharedInformer
|
|
|
|
store cache.Store
|
2018-04-09 09:35:14 -07:00
|
|
|
queue *workqueue.Type
|
2017-09-04 04:10:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewIngress returns a new ingress discovery.
|
2023-10-23 06:55:36 -07:00
|
|
|
func NewIngress(l log.Logger, inf cache.SharedInformer, eventCount *prometheus.CounterVec) *Ingress {
|
2023-12-11 05:17:47 -08:00
|
|
|
ingressAddCount := eventCount.WithLabelValues(RoleIngress.String(), MetricLabelRoleAdd)
|
|
|
|
ingressUpdateCount := eventCount.WithLabelValues(RoleIngress.String(), MetricLabelRoleUpdate)
|
|
|
|
ingressDeleteCount := eventCount.WithLabelValues(RoleIngress.String(), MetricLabelRoleDelete)
|
2023-12-11 05:12:43 -08:00
|
|
|
|
|
|
|
s := &Ingress{
|
|
|
|
logger: l,
|
|
|
|
informer: inf,
|
|
|
|
store: inf.GetStore(),
|
2023-12-11 05:17:47 -08:00
|
|
|
queue: workqueue.NewNamed(RoleIngress.String()),
|
2023-12-11 05:12:43 -08:00
|
|
|
}
|
|
|
|
|
2022-12-14 01:43:53 -08:00
|
|
|
_, err := s.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
|
2018-04-09 09:35:14 -07:00
|
|
|
AddFunc: func(o interface{}) {
|
2023-12-11 05:12:43 -08:00
|
|
|
ingressAddCount.Inc()
|
2018-04-09 09:35:14 -07:00
|
|
|
s.enqueue(o)
|
|
|
|
},
|
|
|
|
DeleteFunc: func(o interface{}) {
|
2023-12-11 05:12:43 -08:00
|
|
|
ingressDeleteCount.Inc()
|
2018-04-09 09:35:14 -07:00
|
|
|
s.enqueue(o)
|
|
|
|
},
|
|
|
|
UpdateFunc: func(_, o interface{}) {
|
2023-12-11 05:12:43 -08:00
|
|
|
ingressUpdateCount.Inc()
|
2018-04-09 09:35:14 -07:00
|
|
|
s.enqueue(o)
|
|
|
|
},
|
|
|
|
})
|
2022-12-14 01:43:53 -08:00
|
|
|
if err != nil {
|
|
|
|
level.Error(l).Log("msg", "Error adding ingresses event handler.", "err", err)
|
|
|
|
}
|
2018-04-09 09:35:14 -07:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2018-07-17 21:07:33 -07:00
|
|
|
func (i *Ingress) enqueue(obj interface{}) {
|
2018-04-09 09:35:14 -07:00
|
|
|
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(obj)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-17 21:07:33 -07:00
|
|
|
i.queue.Add(key)
|
2017-09-04 04:10:44 -07:00
|
|
|
}
|
|
|
|
|
2018-01-08 15:59:18 -08:00
|
|
|
// Run implements the Discoverer interface.
|
2018-07-17 21:07:33 -07:00
|
|
|
func (i *Ingress) Run(ctx context.Context, ch chan<- []*targetgroup.Group) {
|
|
|
|
defer i.queue.ShutDown()
|
2018-04-09 09:35:14 -07:00
|
|
|
|
2018-07-17 21:07:33 -07:00
|
|
|
if !cache.WaitForCacheSync(ctx.Done(), i.informer.HasSynced) {
|
2022-06-03 04:47:14 -07:00
|
|
|
if !errors.Is(ctx.Err(), context.Canceled) {
|
2019-10-09 02:51:38 -07:00
|
|
|
level.Error(i.logger).Log("msg", "ingress informer unable to sync cache")
|
|
|
|
}
|
2017-09-04 04:10:44 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-09 09:35:14 -07:00
|
|
|
go func() {
|
2023-10-31 04:35:13 -07:00
|
|
|
for i.process(ctx, ch) {
|
2018-04-09 09:35:14 -07:00
|
|
|
}
|
|
|
|
}()
|
2017-09-04 04:10:44 -07:00
|
|
|
|
|
|
|
// Block until the target provider is explicitly canceled.
|
|
|
|
<-ctx.Done()
|
|
|
|
}
|
|
|
|
|
2018-07-17 21:07:33 -07:00
|
|
|
func (i *Ingress) process(ctx context.Context, ch chan<- []*targetgroup.Group) bool {
|
|
|
|
keyObj, quit := i.queue.Get()
|
2018-04-09 09:35:14 -07:00
|
|
|
if quit {
|
|
|
|
return false
|
|
|
|
}
|
2018-07-17 21:07:33 -07:00
|
|
|
defer i.queue.Done(keyObj)
|
2018-04-09 09:35:14 -07:00
|
|
|
key := keyObj.(string)
|
|
|
|
|
|
|
|
namespace, name, err := cache.SplitMetaNamespaceKey(key)
|
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-07-17 21:07:33 -07:00
|
|
|
o, exists, err := i.store.GetByKey(key)
|
2018-04-09 09:35:14 -07:00
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if !exists {
|
2020-02-18 08:36:57 -08:00
|
|
|
send(ctx, ch, &targetgroup.Group{Source: ingressSourceFromNamespaceAndName(namespace, name)})
|
2018-04-09 09:35:14 -07:00
|
|
|
return true
|
|
|
|
}
|
2021-08-15 16:34:36 -07:00
|
|
|
|
2021-08-19 21:31:33 -07:00
|
|
|
var ia ingressAdaptor
|
2021-08-15 16:34:36 -07:00
|
|
|
switch ingress := o.(type) {
|
|
|
|
case *v1.Ingress:
|
2021-08-19 21:31:33 -07:00
|
|
|
ia = newIngressAdaptorFromV1(ingress)
|
|
|
|
default:
|
|
|
|
level.Error(i.logger).Log("msg", "converting to Ingress object failed", "err",
|
2022-06-03 04:47:14 -07:00
|
|
|
fmt.Errorf("received unexpected object: %v", o))
|
2018-04-09 09:35:14 -07:00
|
|
|
return true
|
|
|
|
}
|
2021-08-19 21:31:33 -07:00
|
|
|
send(ctx, ch, i.buildIngress(ia))
|
2018-04-09 09:35:14 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:31:33 -07:00
|
|
|
func ingressSource(s ingressAdaptor) string {
|
|
|
|
return ingressSourceFromNamespaceAndName(s.namespace(), s.name())
|
2017-09-04 04:10:44 -07:00
|
|
|
}
|
|
|
|
|
2018-04-09 09:35:14 -07:00
|
|
|
func ingressSourceFromNamespaceAndName(namespace, name string) string {
|
|
|
|
return "ingress/" + namespace + "/" + name
|
|
|
|
}
|
|
|
|
|
2017-09-04 04:10:44 -07:00
|
|
|
const (
|
2023-05-30 05:13:00 -07:00
|
|
|
ingressSchemeLabel = metaLabelPrefix + "ingress_scheme"
|
|
|
|
ingressHostLabel = metaLabelPrefix + "ingress_host"
|
|
|
|
ingressPathLabel = metaLabelPrefix + "ingress_path"
|
|
|
|
ingressClassNameLabel = metaLabelPrefix + "ingress_class_name"
|
2017-09-04 04:10:44 -07:00
|
|
|
)
|
|
|
|
|
2021-08-19 21:31:33 -07:00
|
|
|
func ingressLabels(ingress ingressAdaptor) model.LabelSet {
|
2019-10-25 19:06:00 -07:00
|
|
|
// Each label and annotation will create two key-value pairs in the map.
|
2023-05-30 05:13:00 -07:00
|
|
|
ls := make(model.LabelSet)
|
2021-08-19 21:31:33 -07:00
|
|
|
ls[namespaceLabel] = lv(ingress.namespace())
|
|
|
|
if cls := ingress.ingressClassName(); cls != nil {
|
|
|
|
ls[ingressClassNameLabel] = lv(*cls)
|
2021-06-10 10:02:42 -07:00
|
|
|
}
|
2017-09-04 04:10:44 -07:00
|
|
|
|
2023-05-30 05:13:00 -07:00
|
|
|
addObjectMetaLabels(ls, ingress.getObjectMeta(), RoleIngress)
|
2017-09-04 04:10:44 -07:00
|
|
|
|
|
|
|
return ls
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:31:33 -07:00
|
|
|
func pathsFromIngressPaths(ingressPaths []string) []string {
|
|
|
|
if ingressPaths == nil {
|
2021-08-15 16:34:36 -07:00
|
|
|
return []string{"/"}
|
|
|
|
}
|
2021-08-19 21:31:33 -07:00
|
|
|
paths := make([]string, len(ingressPaths))
|
|
|
|
for n, p := range ingressPaths {
|
|
|
|
path := p
|
|
|
|
if p == "" {
|
2021-08-15 16:34:36 -07:00
|
|
|
path = "/"
|
|
|
|
}
|
|
|
|
paths[n] = path
|
|
|
|
}
|
|
|
|
return paths
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:31:33 -07:00
|
|
|
func (i *Ingress) buildIngress(ingress ingressAdaptor) *targetgroup.Group {
|
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
|
|
|
tg := &targetgroup.Group{
|
2017-09-04 04:10:44 -07:00
|
|
|
Source: ingressSource(ingress),
|
|
|
|
}
|
|
|
|
tg.Labels = ingressLabels(ingress)
|
|
|
|
|
2021-08-19 21:31:33 -07:00
|
|
|
for _, rule := range ingress.rules() {
|
2021-08-26 06:31:05 -07:00
|
|
|
scheme := "http"
|
2021-08-19 21:31:33 -07:00
|
|
|
paths := pathsFromIngressPaths(rule.paths())
|
2021-08-15 16:34:36 -07:00
|
|
|
|
2021-08-26 06:31:05 -07:00
|
|
|
out:
|
|
|
|
for _, pattern := range ingress.tlsHosts() {
|
|
|
|
if matchesHostnamePattern(pattern, rule.host()) {
|
|
|
|
scheme = "https"
|
|
|
|
break out
|
|
|
|
}
|
2021-08-15 16:34:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, path := range paths {
|
|
|
|
tg.Targets = append(tg.Targets, model.LabelSet{
|
2021-08-19 21:31:33 -07:00
|
|
|
model.AddressLabel: lv(rule.host()),
|
2021-08-15 16:34:36 -07:00
|
|
|
ingressSchemeLabel: lv(scheme),
|
2021-08-19 21:31:33 -07:00
|
|
|
ingressHostLabel: lv(rule.host()),
|
2021-08-15 16:34:36 -07:00
|
|
|
ingressPathLabel: lv(path),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tg
|
|
|
|
}
|
2021-08-26 06:31:05 -07:00
|
|
|
|
|
|
|
// matchesHostnamePattern returns true if the host matches a wildcard DNS
|
2021-08-30 13:34:01 -07:00
|
|
|
// pattern or pattern and host are equal.
|
2021-08-26 06:31:05 -07:00
|
|
|
func matchesHostnamePattern(pattern, host string) bool {
|
|
|
|
if pattern == host {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
patternParts := strings.Split(pattern, ".")
|
|
|
|
hostParts := strings.Split(host, ".")
|
|
|
|
|
2021-08-30 13:34:01 -07:00
|
|
|
// If the first element of the pattern is not a wildcard, give up.
|
2021-08-26 06:31:05 -07:00
|
|
|
if len(patternParts) == 0 || patternParts[0] != "*" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-08-30 13:34:01 -07:00
|
|
|
// A wildcard match require the pattern to have the same length as the host
|
|
|
|
// path.
|
2021-08-26 06:31:05 -07:00
|
|
|
if len(patternParts) != len(hostParts) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i < len(patternParts); i++ {
|
|
|
|
if patternParts[i] != hostParts[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|