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"
|
2017-09-04 04:10:44 -07:00
|
|
|
"fmt"
|
|
|
|
|
2017-09-15 10:45:27 -07:00
|
|
|
"github.com/go-kit/kit/log"
|
|
|
|
"github.com/go-kit/kit/log/level"
|
2017-09-04 04:10:44 -07:00
|
|
|
"github.com/prometheus/common/model"
|
Refactor SD configuration to remove `config` dependency (#3629)
* refactor: move targetGroup struct and CheckOverflow() to their own package
* refactor: move auth and security related structs to a utility package, fix import error in utility package
* refactor: Azure SD, remove SD struct from config
* refactor: DNS SD, remove SD struct from config into dns package
* refactor: ec2 SD, move SD struct from config into the ec2 package
* refactor: file SD, move SD struct from config to file discovery package
* refactor: gce, move SD struct from config to gce discovery package
* refactor: move HTTPClientConfig and URL into util/config, fix import error in httputil
* refactor: consul, move SD struct from config into consul discovery package
* refactor: marathon, move SD struct from config into marathon discovery package
* refactor: triton, move SD struct from config to triton discovery package, fix test
* refactor: zookeeper, move SD structs from config to zookeeper discovery package
* refactor: openstack, remove SD struct from config, move into openstack discovery package
* refactor: kubernetes, move SD struct from config into kubernetes discovery package
* refactor: notifier, use targetgroup package instead of config
* refactor: tests for file, marathon, triton SD - use targetgroup package instead of config.TargetGroup
* refactor: retrieval, use targetgroup package instead of config.TargetGroup
* refactor: storage, use config util package
* refactor: discovery manager, use targetgroup package instead of config.TargetGroup
* refactor: use HTTPClient and TLS config from configUtil instead of config
* refactor: tests, use targetgroup package instead of config.TargetGroup
* refactor: fix tagetgroup.Group pointers that were removed by mistake
* refactor: openstack, kubernetes: drop prefixes
* refactor: remove import aliases forced due to vscode bug
* refactor: move main SD struct out of config into discovery/config
* refactor: rename configUtil to config_util
* refactor: rename yamlUtil to yaml_config
* refactor: kubernetes, remove prefixes
* refactor: move the TargetGroup package to discovery/
* refactor: fix order of imports
2017-12-29 12:01:34 -08:00
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
2017-09-04 04:10:44 -07:00
|
|
|
"github.com/prometheus/prometheus/util/strutil"
|
|
|
|
"k8s.io/client-go/pkg/apis/extensions/v1beta1"
|
|
|
|
"k8s.io/client-go/tools/cache"
|
2018-04-09 09:35:14 -07:00
|
|
|
"k8s.io/client-go/util/workqueue"
|
2017-09-04 04:10:44 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Ingress implements discovery of Kubernetes ingresss.
|
|
|
|
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.
|
|
|
|
func NewIngress(l log.Logger, inf cache.SharedInformer) *Ingress {
|
2018-04-09 09:35:14 -07:00
|
|
|
s := &Ingress{logger: l, informer: inf, store: inf.GetStore(), queue: workqueue.NewNamed("ingress")}
|
|
|
|
s.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
|
|
|
|
AddFunc: func(o interface{}) {
|
|
|
|
eventCount.WithLabelValues("ingress", "add").Inc()
|
|
|
|
s.enqueue(o)
|
|
|
|
},
|
|
|
|
DeleteFunc: func(o interface{}) {
|
|
|
|
eventCount.WithLabelValues("ingress", "delete").Inc()
|
|
|
|
s.enqueue(o)
|
|
|
|
},
|
|
|
|
UpdateFunc: func(_, o interface{}) {
|
|
|
|
eventCount.WithLabelValues("ingress", "update").Inc()
|
|
|
|
s.enqueue(o)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
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) {
|
|
|
|
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() {
|
2018-07-17 21:07:33 -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 {
|
2018-07-17 21:07:33 -07:00
|
|
|
send(ctx, i.logger, RoleIngress, ch, &targetgroup.Group{Source: ingressSourceFromNamespaceAndName(namespace, name)})
|
2018-04-09 09:35:14 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
eps, err := convertToIngress(o)
|
|
|
|
if err != nil {
|
2018-07-17 21:07:33 -07:00
|
|
|
level.Error(i.logger).Log("msg", "converting to Ingress object failed", "err", err)
|
2018-04-09 09:35:14 -07:00
|
|
|
return true
|
|
|
|
}
|
2018-07-17 21:07:33 -07:00
|
|
|
send(ctx, i.logger, RoleIngress, ch, i.buildIngress(eps))
|
2018-04-09 09:35:14 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-09-04 04:10:44 -07:00
|
|
|
func convertToIngress(o interface{}) (*v1beta1.Ingress, error) {
|
|
|
|
ingress, ok := o.(*v1beta1.Ingress)
|
|
|
|
if ok {
|
|
|
|
return ingress, nil
|
|
|
|
}
|
|
|
|
|
2018-04-09 09:35:14 -07:00
|
|
|
return nil, fmt.Errorf("Received unexpected object: %v", o)
|
2017-09-04 04:10:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func ingressSource(s *v1beta1.Ingress) string {
|
2018-04-25 09:36:22 -07:00
|
|
|
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 (
|
|
|
|
ingressNameLabel = metaLabelPrefix + "ingress_name"
|
|
|
|
ingressLabelPrefix = metaLabelPrefix + "ingress_label_"
|
|
|
|
ingressAnnotationPrefix = metaLabelPrefix + "ingress_annotation_"
|
|
|
|
ingressSchemeLabel = metaLabelPrefix + "ingress_scheme"
|
|
|
|
ingressHostLabel = metaLabelPrefix + "ingress_host"
|
|
|
|
ingressPathLabel = metaLabelPrefix + "ingress_path"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ingressLabels(ingress *v1beta1.Ingress) model.LabelSet {
|
|
|
|
ls := make(model.LabelSet, len(ingress.Labels)+len(ingress.Annotations)+2)
|
|
|
|
ls[ingressNameLabel] = lv(ingress.Name)
|
|
|
|
ls[namespaceLabel] = lv(ingress.Namespace)
|
|
|
|
|
|
|
|
for k, v := range ingress.Labels {
|
|
|
|
ln := strutil.SanitizeLabelName(ingressLabelPrefix + k)
|
|
|
|
ls[model.LabelName(ln)] = lv(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range ingress.Annotations {
|
|
|
|
ln := strutil.SanitizeLabelName(ingressAnnotationPrefix + k)
|
|
|
|
ls[model.LabelName(ln)] = lv(v)
|
|
|
|
}
|
|
|
|
return ls
|
|
|
|
}
|
|
|
|
|
|
|
|
func pathsFromIngressRule(rv *v1beta1.IngressRuleValue) []string {
|
|
|
|
if rv.HTTP == nil {
|
|
|
|
return []string{"/"}
|
|
|
|
}
|
|
|
|
paths := make([]string, len(rv.HTTP.Paths))
|
|
|
|
for n, p := range rv.HTTP.Paths {
|
|
|
|
path := p.Path
|
|
|
|
if path == "" {
|
|
|
|
path = "/"
|
|
|
|
}
|
|
|
|
paths[n] = path
|
|
|
|
}
|
|
|
|
return paths
|
|
|
|
}
|
|
|
|
|
2018-07-17 21:07:33 -07:00
|
|
|
func (i *Ingress) buildIngress(ingress *v1beta1.Ingress) *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)
|
|
|
|
|
2018-07-02 03:02:40 -07:00
|
|
|
tlsHosts := make(map[string]struct{})
|
2018-07-04 05:46:48 -07:00
|
|
|
for _, tls := range ingress.Spec.TLS {
|
|
|
|
for _, host := range tls.Hosts {
|
|
|
|
tlsHosts[host] = struct{}{}
|
2018-07-02 03:02:40 -07:00
|
|
|
}
|
2017-09-04 04:10:44 -07:00
|
|
|
}
|
2018-07-02 03:02:40 -07:00
|
|
|
|
2017-09-04 04:10:44 -07:00
|
|
|
for _, rule := range ingress.Spec.Rules {
|
|
|
|
paths := pathsFromIngressRule(&rule.IngressRuleValue)
|
|
|
|
|
2018-07-12 07:12:32 -07:00
|
|
|
scheme := "http"
|
2018-07-02 03:02:40 -07:00
|
|
|
_, isTLS := tlsHosts[rule.Host]
|
|
|
|
if isTLS {
|
2018-07-12 07:12:32 -07:00
|
|
|
scheme = "https"
|
2018-07-02 03:02:40 -07:00
|
|
|
}
|
|
|
|
|
2017-09-04 04:10:44 -07:00
|
|
|
for _, path := range paths {
|
|
|
|
tg.Targets = append(tg.Targets, model.LabelSet{
|
|
|
|
model.AddressLabel: lv(rule.Host),
|
2018-07-12 07:12:32 -07:00
|
|
|
ingressSchemeLabel: lv(scheme),
|
2017-09-04 04:10:44 -07:00
|
|
|
ingressHostLabel: lv(rule.Host),
|
|
|
|
ingressPathLabel: lv(path),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tg
|
|
|
|
}
|