2015-07-18 14:23:58 -07:00
|
|
|
// Copyright 2015 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 (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2015-10-03 01:21:43 -07:00
|
|
|
"github.com/prometheus/common/log"
|
2015-08-20 08:18:46 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2016-02-18 08:26:27 -08:00
|
|
|
"golang.org/x/net/context"
|
2015-08-22 00:42:45 -07:00
|
|
|
|
2015-07-18 14:23:58 -07:00
|
|
|
"github.com/prometheus/prometheus/config"
|
|
|
|
"github.com/prometheus/prometheus/util/httputil"
|
|
|
|
"github.com/prometheus/prometheus/util/strutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
sourceServicePrefix = "services"
|
|
|
|
|
|
|
|
// kubernetesMetaLabelPrefix is the meta prefix used for all meta labels.
|
|
|
|
// in this discovery.
|
2015-08-20 08:18:46 -07:00
|
|
|
metaLabelPrefix = model.MetaLabelPrefix + "kubernetes_"
|
2015-07-18 14:23:58 -07:00
|
|
|
// serviceNamespaceLabel is the name for the label containing a target's service namespace.
|
|
|
|
serviceNamespaceLabel = metaLabelPrefix + "service_namespace"
|
|
|
|
// serviceNameLabel is the name for the label containing a target's service name.
|
|
|
|
serviceNameLabel = metaLabelPrefix + "service_name"
|
|
|
|
// nodeLabelPrefix is the prefix for the node labels.
|
|
|
|
nodeLabelPrefix = metaLabelPrefix + "node_label_"
|
|
|
|
// serviceLabelPrefix is the prefix for the service labels.
|
|
|
|
serviceLabelPrefix = metaLabelPrefix + "service_label_"
|
|
|
|
// serviceAnnotationPrefix is the prefix for the service annotations.
|
|
|
|
serviceAnnotationPrefix = metaLabelPrefix + "service_annotation_"
|
|
|
|
// nodesTargetGroupName is the name given to the target group for nodes.
|
|
|
|
nodesTargetGroupName = "nodes"
|
2015-10-24 06:41:14 -07:00
|
|
|
// apiServersTargetGroupName is the name given to the target group for API servers.
|
|
|
|
apiServersTargetGroupName = "apiServers"
|
2015-09-03 02:47:09 -07:00
|
|
|
// roleLabel is the name for the label containing a target's role.
|
|
|
|
roleLabel = metaLabelPrefix + "role"
|
2015-07-18 14:23:58 -07:00
|
|
|
|
|
|
|
serviceAccountToken = "/var/run/secrets/kubernetes.io/serviceaccount/token"
|
|
|
|
serviceAccountCACert = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
|
|
|
|
|
|
|
|
apiVersion = "v1"
|
2015-09-03 02:47:09 -07:00
|
|
|
apiPrefix = "/api/" + apiVersion
|
2015-07-18 14:23:58 -07:00
|
|
|
nodesURL = apiPrefix + "/nodes"
|
|
|
|
servicesURL = apiPrefix + "/services"
|
|
|
|
endpointsURL = apiPrefix + "/endpoints"
|
|
|
|
serviceEndpointsURL = apiPrefix + "/namespaces/%s/endpoints/%s"
|
|
|
|
)
|
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
// Discovery implements a TargetProvider for Kubernetes services.
|
|
|
|
type Discovery struct {
|
2015-07-18 14:23:58 -07:00
|
|
|
client *http.Client
|
|
|
|
Conf *config.KubernetesSDConfig
|
|
|
|
|
2016-01-15 04:36:55 -08:00
|
|
|
apiServers []config.URL
|
|
|
|
apiServersMu sync.RWMutex
|
|
|
|
nodes map[string]*Node
|
|
|
|
services map[string]map[string]*Service
|
|
|
|
nodesMu sync.RWMutex
|
|
|
|
servicesMu sync.RWMutex
|
|
|
|
runDone chan struct{}
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
2015-08-18 05:37:28 -07:00
|
|
|
// Initialize sets up the discovery for usage.
|
2015-08-24 06:07:27 -07:00
|
|
|
func (kd *Discovery) Initialize() error {
|
2015-07-18 14:23:58 -07:00
|
|
|
client, err := newKubernetesHTTPClient(kd.Conf)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-24 06:41:14 -07:00
|
|
|
kd.apiServers = kd.Conf.APIServers
|
2015-07-18 14:23:58 -07:00
|
|
|
kd.client = client
|
|
|
|
kd.runDone = make(chan struct{})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run implements the TargetProvider interface.
|
2016-02-18 08:26:27 -08:00
|
|
|
func (kd *Discovery) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
|
2015-07-18 14:23:58 -07:00
|
|
|
defer close(ch)
|
|
|
|
|
2016-02-18 08:26:27 -08:00
|
|
|
// Send an initial full view.
|
|
|
|
// TODO(fabxc): this does not include all available services and service
|
|
|
|
// endpoints yet. Service endpoints were also missing in the previous Sources() method.
|
|
|
|
var all []*config.TargetGroup
|
|
|
|
|
|
|
|
all = append(all, kd.updateAPIServersTargetGroup())
|
|
|
|
all = append(all, kd.updateNodesTargetGroup())
|
|
|
|
|
|
|
|
select {
|
|
|
|
case ch <- all:
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2015-09-03 02:47:09 -07:00
|
|
|
}
|
|
|
|
|
2015-07-18 14:23:58 -07:00
|
|
|
retryInterval := time.Duration(kd.Conf.RetryInterval)
|
|
|
|
|
|
|
|
update := make(chan interface{}, 10)
|
|
|
|
|
2016-02-18 08:26:27 -08:00
|
|
|
go kd.watchNodes(update, ctx.Done(), retryInterval)
|
|
|
|
go kd.startServiceWatch(update, ctx.Done(), retryInterval)
|
2015-07-18 14:23:58 -07:00
|
|
|
|
2015-08-14 04:04:23 -07:00
|
|
|
var tg *config.TargetGroup
|
2015-07-18 14:23:58 -07:00
|
|
|
for {
|
|
|
|
select {
|
2016-02-18 08:26:27 -08:00
|
|
|
case <-ctx.Done():
|
2015-07-18 14:23:58 -07:00
|
|
|
return
|
|
|
|
case event := <-update:
|
|
|
|
switch obj := event.(type) {
|
|
|
|
case *nodeEvent:
|
|
|
|
kd.updateNode(obj.Node, obj.EventType)
|
2015-08-14 04:04:23 -07:00
|
|
|
tg = kd.updateNodesTargetGroup()
|
2015-07-18 14:23:58 -07:00
|
|
|
case *serviceEvent:
|
2015-08-14 04:04:23 -07:00
|
|
|
tg = kd.updateService(obj.Service, obj.EventType)
|
2015-07-18 14:23:58 -07:00
|
|
|
case *endpointsEvent:
|
2015-08-14 04:04:23 -07:00
|
|
|
tg = kd.updateServiceEndpoints(obj.Endpoints, obj.EventType)
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
}
|
2015-08-14 04:04:23 -07:00
|
|
|
|
2015-10-08 09:06:58 -07:00
|
|
|
if tg == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-08-14 04:04:23 -07:00
|
|
|
select {
|
2016-02-18 08:26:27 -08:00
|
|
|
case ch <- []*config.TargetGroup{tg}:
|
|
|
|
case <-ctx.Done():
|
2015-08-14 04:04:23 -07:00
|
|
|
return
|
|
|
|
}
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-24 06:41:14 -07:00
|
|
|
func (kd *Discovery) queryAPIServerPath(path string) (*http.Response, error) {
|
2015-09-03 02:47:09 -07:00
|
|
|
req, err := http.NewRequest("GET", path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-24 06:41:14 -07:00
|
|
|
return kd.queryAPIServerReq(req)
|
2015-09-03 02:47:09 -07:00
|
|
|
}
|
|
|
|
|
2015-10-24 06:41:14 -07:00
|
|
|
func (kd *Discovery) queryAPIServerReq(req *http.Request) (*http.Response, error) {
|
|
|
|
// Lock in case we need to rotate API servers to request.
|
|
|
|
kd.apiServersMu.Lock()
|
|
|
|
defer kd.apiServersMu.Unlock()
|
|
|
|
var lastErr error
|
|
|
|
for i := 0; i < len(kd.apiServers); i++ {
|
2015-09-03 02:47:09 -07:00
|
|
|
cloneReq := *req
|
2015-10-24 06:41:14 -07:00
|
|
|
cloneReq.URL.Host = kd.apiServers[0].Host
|
|
|
|
cloneReq.URL.Scheme = kd.apiServers[0].Scheme
|
2015-09-03 02:47:09 -07:00
|
|
|
res, err := kd.client.Do(&cloneReq)
|
|
|
|
if err == nil {
|
|
|
|
return res, nil
|
|
|
|
}
|
2015-10-24 06:41:14 -07:00
|
|
|
lastErr = err
|
|
|
|
kd.rotateAPIServers()
|
2015-09-03 02:47:09 -07:00
|
|
|
}
|
2015-10-24 06:41:14 -07:00
|
|
|
return nil, fmt.Errorf("Unable to query any API servers: %v", lastErr)
|
2015-09-03 02:47:09 -07:00
|
|
|
}
|
|
|
|
|
2015-10-24 06:41:14 -07:00
|
|
|
func (kd *Discovery) rotateAPIServers() {
|
|
|
|
if len(kd.apiServers) > 1 {
|
|
|
|
kd.apiServers = append(kd.apiServers[1:], kd.apiServers[0])
|
2015-09-03 02:47:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-24 06:41:14 -07:00
|
|
|
func (kd *Discovery) updateAPIServersTargetGroup() *config.TargetGroup {
|
2015-09-03 02:47:09 -07:00
|
|
|
tg := &config.TargetGroup{
|
2015-10-24 06:41:14 -07:00
|
|
|
Source: apiServersTargetGroupName,
|
2015-09-03 02:47:09 -07:00
|
|
|
Labels: model.LabelSet{
|
2015-10-24 06:41:14 -07:00
|
|
|
roleLabel: model.LabelValue("apiserver"),
|
2015-09-03 02:47:09 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-10-24 06:41:14 -07:00
|
|
|
for _, apiServer := range kd.apiServers {
|
|
|
|
apiServerAddress := apiServer.Host
|
|
|
|
_, _, err := net.SplitHostPort(apiServerAddress)
|
2015-09-03 02:47:09 -07:00
|
|
|
// If error then no port is specified - use default for scheme.
|
|
|
|
if err != nil {
|
2015-10-24 06:41:14 -07:00
|
|
|
switch apiServer.Scheme {
|
2015-09-03 02:47:09 -07:00
|
|
|
case "http":
|
2015-10-24 06:41:14 -07:00
|
|
|
apiServerAddress = net.JoinHostPort(apiServerAddress, "80")
|
2015-09-03 02:47:09 -07:00
|
|
|
case "https":
|
2015-10-24 06:41:14 -07:00
|
|
|
apiServerAddress = net.JoinHostPort(apiServerAddress, "443")
|
2015-09-03 02:47:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t := model.LabelSet{
|
2015-10-24 06:41:14 -07:00
|
|
|
model.AddressLabel: model.LabelValue(apiServerAddress),
|
|
|
|
model.SchemeLabel: model.LabelValue(apiServer.Scheme),
|
2015-09-03 02:47:09 -07:00
|
|
|
}
|
|
|
|
tg.Targets = append(tg.Targets, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tg
|
|
|
|
}
|
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
func (kd *Discovery) updateNodesTargetGroup() *config.TargetGroup {
|
2016-01-15 04:36:55 -08:00
|
|
|
kd.nodesMu.RLock()
|
|
|
|
defer kd.nodesMu.RUnlock()
|
2015-07-18 14:23:58 -07:00
|
|
|
|
2015-09-03 02:47:09 -07:00
|
|
|
tg := &config.TargetGroup{
|
|
|
|
Source: nodesTargetGroupName,
|
|
|
|
Labels: model.LabelSet{
|
|
|
|
roleLabel: model.LabelValue("node"),
|
|
|
|
},
|
|
|
|
}
|
2015-07-18 14:23:58 -07:00
|
|
|
|
|
|
|
// Now let's loop through the nodes & add them to the target group with appropriate labels.
|
|
|
|
for nodeName, node := range kd.nodes {
|
2016-03-01 04:02:43 -08:00
|
|
|
nodeAddress, err := nodeHostIP(node)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Skipping node %s: %s", node.Name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
address := fmt.Sprintf("%s:%d", nodeAddress.String(), kd.Conf.KubeletPort)
|
2015-07-18 14:23:58 -07:00
|
|
|
|
2015-08-20 08:18:46 -07:00
|
|
|
t := model.LabelSet{
|
2015-10-12 13:26:09 -07:00
|
|
|
model.AddressLabel: model.LabelValue(address),
|
|
|
|
model.InstanceLabel: model.LabelValue(nodeName),
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
for k, v := range node.ObjectMeta.Labels {
|
|
|
|
labelName := strutil.SanitizeLabelName(nodeLabelPrefix + k)
|
2015-08-20 08:18:46 -07:00
|
|
|
t[model.LabelName(labelName)] = model.LabelValue(v)
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
tg.Targets = append(tg.Targets, t)
|
|
|
|
}
|
|
|
|
|
2015-08-14 04:04:23 -07:00
|
|
|
return tg
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
func (kd *Discovery) updateNode(node *Node, eventType EventType) {
|
2015-07-18 14:23:58 -07:00
|
|
|
kd.nodesMu.Lock()
|
|
|
|
defer kd.nodesMu.Unlock()
|
|
|
|
updatedNodeName := node.ObjectMeta.Name
|
|
|
|
switch eventType {
|
|
|
|
case deleted:
|
|
|
|
// Deleted - remove from nodes map.
|
|
|
|
delete(kd.nodes, updatedNodeName)
|
|
|
|
case added, modified:
|
|
|
|
// Added/Modified - update the node in the nodes map.
|
|
|
|
kd.nodes[updatedNodeName] = node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-15 04:36:55 -08:00
|
|
|
func (kd *Discovery) getNodes() (map[string]*Node, string, error) {
|
|
|
|
res, err := kd.queryAPIServerPath(nodesURL)
|
|
|
|
if err != nil {
|
|
|
|
// If we can't list nodes then we can't watch them. Assume this is a misconfiguration
|
|
|
|
// & return error.
|
|
|
|
return nil, "", fmt.Errorf("Unable to list Kubernetes nodes: %s", err)
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
|
|
return nil, "", fmt.Errorf("Unable to list Kubernetes nodes. Unexpected response: %d %s", res.StatusCode, res.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
var nodes NodeList
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&nodes); err != nil {
|
|
|
|
body, _ := ioutil.ReadAll(res.Body)
|
|
|
|
return nil, "", fmt.Errorf("Unable to list Kubernetes nodes. Unexpected response body: %s", string(body))
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeMap := map[string]*Node{}
|
|
|
|
for idx, node := range nodes.Items {
|
|
|
|
nodeMap[node.ObjectMeta.Name] = &nodes.Items[idx]
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeMap, nodes.ResourceVersion, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (kd *Discovery) getServices() (map[string]map[string]*Service, string, error) {
|
|
|
|
res, err := kd.queryAPIServerPath(servicesURL)
|
|
|
|
if err != nil {
|
|
|
|
// If we can't list services then we can't watch them. Assume this is a misconfiguration
|
|
|
|
// & return error.
|
|
|
|
return nil, "", fmt.Errorf("Unable to list Kubernetes services: %s", err)
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
|
|
return nil, "", fmt.Errorf("Unable to list Kubernetes services. Unexpected response: %d %s", res.StatusCode, res.Status)
|
|
|
|
}
|
|
|
|
var services ServiceList
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&services); err != nil {
|
|
|
|
body, _ := ioutil.ReadAll(res.Body)
|
|
|
|
return nil, "", fmt.Errorf("Unable to list Kubernetes services. Unexpected response body: %s", string(body))
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceMap := map[string]map[string]*Service{}
|
|
|
|
for idx, service := range services.Items {
|
|
|
|
namespace, ok := serviceMap[service.ObjectMeta.Namespace]
|
|
|
|
if !ok {
|
|
|
|
namespace = map[string]*Service{}
|
|
|
|
serviceMap[service.ObjectMeta.Namespace] = namespace
|
|
|
|
}
|
|
|
|
namespace[service.ObjectMeta.Name] = &services.Items[idx]
|
|
|
|
}
|
|
|
|
|
|
|
|
return serviceMap, services.ResourceVersion, nil
|
|
|
|
}
|
|
|
|
|
2015-07-18 14:23:58 -07:00
|
|
|
// watchNodes watches nodes as they come & go.
|
2015-08-24 06:07:27 -07:00
|
|
|
func (kd *Discovery) watchNodes(events chan interface{}, done <-chan struct{}, retryInterval time.Duration) {
|
2015-07-18 14:23:58 -07:00
|
|
|
until(func() {
|
2016-01-15 04:36:55 -08:00
|
|
|
nodes, resourceVersion, err := kd.getNodes()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Cannot initialize nodes collection: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the known nodes.
|
|
|
|
kd.nodes = map[string]*Node{}
|
|
|
|
|
|
|
|
for _, node := range nodes {
|
|
|
|
events <- &nodeEvent{added, node}
|
|
|
|
}
|
|
|
|
|
2015-09-03 02:47:09 -07:00
|
|
|
req, err := http.NewRequest("GET", nodesURL, nil)
|
2015-07-18 14:23:58 -07:00
|
|
|
if err != nil {
|
2016-01-15 04:36:55 -08:00
|
|
|
log.Errorf("Cannot create nodes request: %s", err)
|
2015-07-18 14:23:58 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
values := req.URL.Query()
|
|
|
|
values.Add("watch", "true")
|
2016-01-15 04:36:55 -08:00
|
|
|
values.Add("resourceVersion", resourceVersion)
|
2015-07-18 14:23:58 -07:00
|
|
|
req.URL.RawQuery = values.Encode()
|
2015-10-24 06:41:14 -07:00
|
|
|
res, err := kd.queryAPIServerReq(req)
|
2015-07-18 14:23:58 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to watch nodes: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2015-09-11 03:44:28 -07:00
|
|
|
defer res.Body.Close()
|
2015-07-18 14:23:58 -07:00
|
|
|
if res.StatusCode != http.StatusOK {
|
2015-08-25 17:04:01 -07:00
|
|
|
log.Errorf("Failed to watch nodes: %d", res.StatusCode)
|
2015-07-18 14:23:58 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
d := json.NewDecoder(res.Body)
|
|
|
|
|
|
|
|
for {
|
|
|
|
var event nodeEvent
|
|
|
|
if err := d.Decode(&event); err != nil {
|
2016-01-15 04:36:55 -08:00
|
|
|
log.Errorf("Watch nodes unexpectedly closed: %s", err)
|
2015-07-18 14:23:58 -07:00
|
|
|
return
|
|
|
|
}
|
2015-08-14 04:04:23 -07:00
|
|
|
|
|
|
|
select {
|
|
|
|
case events <- &event:
|
|
|
|
case <-done:
|
|
|
|
}
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
2015-08-14 04:04:23 -07:00
|
|
|
}, retryInterval, done)
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// watchServices watches services as they come & go.
|
2016-01-15 04:36:55 -08:00
|
|
|
func (kd *Discovery) startServiceWatch(events chan<- interface{}, done <-chan struct{}, retryInterval time.Duration) {
|
2015-07-18 14:23:58 -07:00
|
|
|
until(func() {
|
2016-01-15 04:36:55 -08:00
|
|
|
// We use separate target groups for each discovered service so we'll need to clean up any if they've been deleted
|
|
|
|
// in Kubernetes while we couldn't connect - small chance of this, but worth dealing with.
|
|
|
|
existingServices := kd.services
|
|
|
|
|
|
|
|
// Reset the known services.
|
|
|
|
kd.services = map[string]map[string]*Service{}
|
|
|
|
|
|
|
|
services, resourceVersion, err := kd.getServices()
|
2015-07-18 14:23:58 -07:00
|
|
|
if err != nil {
|
2016-01-15 04:36:55 -08:00
|
|
|
log.Errorf("Cannot initialize services collection: %s", err)
|
2015-07-18 14:23:58 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-15 04:36:55 -08:00
|
|
|
// Now let's loop through the old services & see if they still exist in here
|
|
|
|
for oldNSName, oldNS := range existingServices {
|
|
|
|
if ns, ok := services[oldNSName]; !ok {
|
|
|
|
for _, service := range existingServices[oldNSName] {
|
|
|
|
events <- &serviceEvent{deleted, service}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for oldServiceName, oldService := range oldNS {
|
|
|
|
if _, ok := ns[oldServiceName]; !ok {
|
|
|
|
events <- &serviceEvent{deleted, oldService}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Discard the existing services map for GC.
|
|
|
|
existingServices = nil
|
|
|
|
|
|
|
|
for _, ns := range services {
|
|
|
|
for _, service := range ns {
|
|
|
|
events <- &serviceEvent{added, service}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
kd.watchServices(resourceVersion, events, done)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
kd.watchServiceEndpoints(resourceVersion, events, done)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}, retryInterval, done)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (kd *Discovery) watchServices(resourceVersion string, events chan<- interface{}, done <-chan struct{}) {
|
|
|
|
req, err := http.NewRequest("GET", servicesURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to create services request: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
values := req.URL.Query()
|
|
|
|
values.Add("watch", "true")
|
|
|
|
values.Add("resourceVersion", resourceVersion)
|
|
|
|
req.URL.RawQuery = values.Encode()
|
|
|
|
|
|
|
|
res, err := kd.queryAPIServerReq(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to watch services: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
|
|
log.Errorf("Failed to watch services: %d", res.StatusCode)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
d := json.NewDecoder(res.Body)
|
|
|
|
|
|
|
|
for {
|
|
|
|
var event serviceEvent
|
|
|
|
if err := d.Decode(&event); err != nil {
|
|
|
|
log.Errorf("Watch services unexpectedly closed: %s", err)
|
2015-07-18 14:23:58 -07:00
|
|
|
return
|
|
|
|
}
|
2016-01-15 04:36:55 -08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case events <- &event:
|
|
|
|
case <-done:
|
2015-07-18 14:23:58 -07:00
|
|
|
return
|
|
|
|
}
|
2016-01-15 04:36:55 -08:00
|
|
|
}
|
|
|
|
}
|
2015-07-18 14:23:58 -07:00
|
|
|
|
2016-01-15 04:36:55 -08:00
|
|
|
// watchServiceEndpoints watches service endpoints as they come & go.
|
|
|
|
func (kd *Discovery) watchServiceEndpoints(resourceVersion string, events chan<- interface{}, done <-chan struct{}) {
|
|
|
|
req, err := http.NewRequest("GET", endpointsURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to create service endpoints request: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
values := req.URL.Query()
|
|
|
|
values.Add("watch", "true")
|
|
|
|
values.Add("resourceVersion", resourceVersion)
|
|
|
|
req.URL.RawQuery = values.Encode()
|
2015-07-18 14:23:58 -07:00
|
|
|
|
2016-01-15 04:36:55 -08:00
|
|
|
res, err := kd.queryAPIServerReq(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Failed to watch service endpoints: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
|
|
log.Errorf("Failed to watch service endpoints: %d", res.StatusCode)
|
|
|
|
return
|
|
|
|
}
|
2015-08-14 04:04:23 -07:00
|
|
|
|
2016-01-15 04:36:55 -08:00
|
|
|
d := json.NewDecoder(res.Body)
|
|
|
|
|
|
|
|
for {
|
|
|
|
var event endpointsEvent
|
|
|
|
if err := d.Decode(&event); err != nil {
|
|
|
|
log.Errorf("Watch service endpoints unexpectedly closed: %s", err)
|
|
|
|
return
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
2016-01-15 04:36:55 -08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case events <- &event:
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
}
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
func (kd *Discovery) updateService(service *Service, eventType EventType) *config.TargetGroup {
|
2015-07-18 14:23:58 -07:00
|
|
|
kd.servicesMu.Lock()
|
|
|
|
defer kd.servicesMu.Unlock()
|
2015-08-14 04:04:23 -07:00
|
|
|
|
2015-07-18 14:23:58 -07:00
|
|
|
switch eventType {
|
|
|
|
case deleted:
|
2016-01-15 04:36:55 -08:00
|
|
|
return kd.deleteService(service)
|
2015-07-18 14:23:58 -07:00
|
|
|
case added, modified:
|
2015-08-14 04:04:23 -07:00
|
|
|
return kd.addService(service)
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
2015-08-14 04:04:23 -07:00
|
|
|
return nil
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
func (kd *Discovery) deleteService(service *Service) *config.TargetGroup {
|
2015-07-18 14:23:58 -07:00
|
|
|
tg := &config.TargetGroup{Source: serviceSource(service)}
|
2015-08-14 04:04:23 -07:00
|
|
|
|
2015-07-18 14:23:58 -07:00
|
|
|
delete(kd.services[service.ObjectMeta.Namespace], service.ObjectMeta.Name)
|
|
|
|
if len(kd.services[service.ObjectMeta.Namespace]) == 0 {
|
|
|
|
delete(kd.services, service.ObjectMeta.Namespace)
|
|
|
|
}
|
2015-08-14 04:04:23 -07:00
|
|
|
|
|
|
|
return tg
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
func (kd *Discovery) addService(service *Service) *config.TargetGroup {
|
2015-07-18 14:23:58 -07:00
|
|
|
namespace, ok := kd.services[service.ObjectMeta.Namespace]
|
|
|
|
if !ok {
|
|
|
|
namespace = map[string]*Service{}
|
|
|
|
kd.services[service.ObjectMeta.Namespace] = namespace
|
|
|
|
}
|
2015-08-14 04:04:23 -07:00
|
|
|
|
2015-07-18 14:23:58 -07:00
|
|
|
namespace[service.ObjectMeta.Name] = service
|
|
|
|
endpointURL := fmt.Sprintf(serviceEndpointsURL, service.ObjectMeta.Namespace, service.ObjectMeta.Name)
|
2015-08-14 04:04:23 -07:00
|
|
|
|
2015-10-24 06:41:14 -07:00
|
|
|
res, err := kd.queryAPIServerPath(endpointURL)
|
2015-07-18 14:23:58 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Error getting service endpoints: %s", err)
|
2015-08-14 04:04:23 -07:00
|
|
|
return nil
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
2015-09-11 03:44:28 -07:00
|
|
|
defer res.Body.Close()
|
2015-07-18 14:23:58 -07:00
|
|
|
if res.StatusCode != http.StatusOK {
|
2015-08-25 17:04:01 -07:00
|
|
|
log.Errorf("Failed to get service endpoints: %d", res.StatusCode)
|
2015-08-14 04:04:23 -07:00
|
|
|
return nil
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
var eps Endpoints
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&eps); err != nil {
|
2015-07-18 14:23:58 -07:00
|
|
|
log.Errorf("Error getting service endpoints: %s", err)
|
2015-08-14 04:04:23 -07:00
|
|
|
return nil
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
return kd.updateServiceTargetGroup(service, &eps)
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
func (kd *Discovery) updateServiceTargetGroup(service *Service, eps *Endpoints) *config.TargetGroup {
|
2015-07-18 14:23:58 -07:00
|
|
|
tg := &config.TargetGroup{
|
|
|
|
Source: serviceSource(service),
|
2015-08-20 08:18:46 -07:00
|
|
|
Labels: model.LabelSet{
|
|
|
|
serviceNamespaceLabel: model.LabelValue(service.ObjectMeta.Namespace),
|
|
|
|
serviceNameLabel: model.LabelValue(service.ObjectMeta.Name),
|
2015-07-18 14:23:58 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range service.ObjectMeta.Labels {
|
|
|
|
labelName := strutil.SanitizeLabelName(serviceLabelPrefix + k)
|
2015-08-20 08:18:46 -07:00
|
|
|
tg.Labels[model.LabelName(labelName)] = model.LabelValue(v)
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range service.ObjectMeta.Annotations {
|
|
|
|
labelName := strutil.SanitizeLabelName(serviceAnnotationPrefix + k)
|
2015-08-20 08:18:46 -07:00
|
|
|
tg.Labels[model.LabelName(labelName)] = model.LabelValue(v)
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
2015-12-08 02:29:40 -08:00
|
|
|
serviceAddress := service.ObjectMeta.Name + "." + service.ObjectMeta.Namespace + ".svc"
|
|
|
|
|
|
|
|
// Append the first TCP service port if one exists.
|
|
|
|
for _, port := range service.Spec.Ports {
|
|
|
|
if port.Protocol == ProtocolTCP {
|
|
|
|
serviceAddress += fmt.Sprintf(":%d", port.Port)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-16 06:18:18 -08:00
|
|
|
t := model.LabelSet{
|
2015-12-08 02:29:40 -08:00
|
|
|
model.AddressLabel: model.LabelValue(serviceAddress),
|
2015-11-16 06:18:18 -08:00
|
|
|
roleLabel: model.LabelValue("service"),
|
|
|
|
}
|
|
|
|
tg.Targets = append(tg.Targets, t)
|
|
|
|
|
2015-07-18 14:23:58 -07:00
|
|
|
// Now let's loop through the endpoints & add them to the target group with appropriate labels.
|
2015-08-24 06:07:27 -07:00
|
|
|
for _, ss := range eps.Subsets {
|
|
|
|
epPort := ss.Ports[0].Port
|
2015-07-18 14:23:58 -07:00
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
for _, addr := range ss.Addresses {
|
2015-07-18 14:23:58 -07:00
|
|
|
ipAddr := addr.IP
|
|
|
|
if len(ipAddr) == net.IPv6len {
|
|
|
|
ipAddr = "[" + ipAddr + "]"
|
|
|
|
}
|
|
|
|
address := fmt.Sprintf("%s:%d", ipAddr, epPort)
|
|
|
|
|
2015-11-16 06:18:18 -08:00
|
|
|
t := model.LabelSet{
|
|
|
|
model.AddressLabel: model.LabelValue(address),
|
|
|
|
roleLabel: model.LabelValue("endpoint"),
|
|
|
|
}
|
2015-07-18 14:23:58 -07:00
|
|
|
|
|
|
|
tg.Targets = append(tg.Targets, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-14 04:04:23 -07:00
|
|
|
return tg
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
2015-08-24 06:07:27 -07:00
|
|
|
func (kd *Discovery) updateServiceEndpoints(endpoints *Endpoints, eventType EventType) *config.TargetGroup {
|
2015-07-18 14:23:58 -07:00
|
|
|
kd.servicesMu.Lock()
|
|
|
|
defer kd.servicesMu.Unlock()
|
2015-08-14 04:04:23 -07:00
|
|
|
|
2015-07-18 14:23:58 -07:00
|
|
|
serviceNamespace := endpoints.ObjectMeta.Namespace
|
|
|
|
serviceName := endpoints.ObjectMeta.Name
|
2015-08-14 04:04:23 -07:00
|
|
|
|
2015-07-18 14:23:58 -07:00
|
|
|
if service, ok := kd.services[serviceNamespace][serviceName]; ok {
|
2015-08-14 04:04:23 -07:00
|
|
|
return kd.updateServiceTargetGroup(service, endpoints)
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
2015-08-14 04:04:23 -07:00
|
|
|
return nil
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func newKubernetesHTTPClient(conf *config.KubernetesSDConfig) (*http.Client, error) {
|
|
|
|
bearerTokenFile := conf.BearerTokenFile
|
2015-09-06 16:07:44 -07:00
|
|
|
caFile := conf.TLSConfig.CAFile
|
2015-07-18 14:23:58 -07:00
|
|
|
if conf.InCluster {
|
|
|
|
if len(bearerTokenFile) == 0 {
|
|
|
|
bearerTokenFile = serviceAccountToken
|
|
|
|
}
|
|
|
|
if len(caFile) == 0 {
|
2015-09-29 01:38:58 -07:00
|
|
|
// With recent versions, the CA certificate is mounted as a secret
|
2015-07-18 14:23:58 -07:00
|
|
|
// but we need to handle older versions too. In this case, don't
|
2015-09-06 16:07:44 -07:00
|
|
|
// set the CAFile & the configuration will have to use InsecureSkipVerify.
|
2015-07-18 14:23:58 -07:00
|
|
|
if _, err := os.Stat(serviceAccountCACert); err == nil {
|
|
|
|
caFile = serviceAccountCACert
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 16:07:44 -07:00
|
|
|
tlsOpts := httputil.TLSOptions{
|
|
|
|
InsecureSkipVerify: conf.TLSConfig.InsecureSkipVerify,
|
|
|
|
CAFile: caFile,
|
|
|
|
CertFile: conf.TLSConfig.CertFile,
|
|
|
|
KeyFile: conf.TLSConfig.KeyFile,
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
2015-09-06 16:07:44 -07:00
|
|
|
tlsConfig, err := httputil.NewTLSConfig(tlsOpts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
2015-09-06 16:07:44 -07:00
|
|
|
var rt http.RoundTripper = &http.Transport{
|
2015-07-18 14:23:58 -07:00
|
|
|
Dial: func(netw, addr string) (c net.Conn, err error) {
|
|
|
|
c, err = net.DialTimeout(netw, addr, time.Duration(conf.RequestTimeout))
|
|
|
|
return
|
|
|
|
},
|
2015-09-06 16:07:44 -07:00
|
|
|
TLSClientConfig: tlsConfig,
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
2015-10-23 08:47:10 -07:00
|
|
|
// If a bearer token is provided, create a round tripper that will set the
|
|
|
|
// Authorization header correctly on each request.
|
|
|
|
bearerToken := conf.BearerToken
|
|
|
|
if len(bearerToken) == 0 && len(bearerTokenFile) > 0 {
|
|
|
|
b, err := ioutil.ReadFile(bearerTokenFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to read bearer token file %s: %s", bearerTokenFile, err)
|
|
|
|
}
|
|
|
|
bearerToken = string(b)
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
if len(bearerToken) > 0 {
|
2015-10-23 08:47:10 -07:00
|
|
|
rt = httputil.NewBearerAuthRoundTripper(bearerToken, rt)
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
2015-10-23 08:47:10 -07:00
|
|
|
|
|
|
|
if conf.BasicAuth != nil {
|
|
|
|
rt = httputil.NewBasicAuthRoundTripper(conf.BasicAuth.Username, conf.BasicAuth.Password, rt)
|
2015-07-18 14:23:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return &http.Client{
|
|
|
|
Transport: rt,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func serviceSource(service *Service) string {
|
|
|
|
return sourceServicePrefix + ":" + service.ObjectMeta.Namespace + "/" + service.ObjectMeta.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Until loops until stop channel is closed, running f every period.
|
|
|
|
// f may not be invoked if stop channel is already closed.
|
|
|
|
func until(f func(), period time.Duration, stopCh <-chan struct{}) {
|
|
|
|
select {
|
|
|
|
case <-stopCh:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stopCh:
|
|
|
|
return
|
|
|
|
case <-time.After(period):
|
|
|
|
f()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-01 04:02:43 -08:00
|
|
|
|
|
|
|
// nodeHostIP returns the provided node's address, based on the priority:
|
|
|
|
// 1. NodeInternalIP
|
|
|
|
// 2. NodeExternalIP
|
|
|
|
// 3. NodeLegacyHostIP
|
|
|
|
//
|
|
|
|
// Copied from k8s.io/kubernetes/pkg/util/node/node.go
|
|
|
|
func nodeHostIP(node *Node) (net.IP, error) {
|
|
|
|
addresses := node.Status.Addresses
|
|
|
|
addressMap := make(map[NodeAddressType][]NodeAddress)
|
|
|
|
for i := range addresses {
|
|
|
|
addressMap[addresses[i].Type] = append(addressMap[addresses[i].Type], addresses[i])
|
|
|
|
}
|
|
|
|
if addresses, ok := addressMap[NodeInternalIP]; ok {
|
|
|
|
return net.ParseIP(addresses[0].Address), nil
|
|
|
|
}
|
|
|
|
if addresses, ok := addressMap[NodeExternalIP]; ok {
|
|
|
|
return net.ParseIP(addresses[0].Address), nil
|
|
|
|
}
|
|
|
|
if addresses, ok := addressMap[NodeLegacyHostIP]; ok {
|
|
|
|
return net.ParseIP(addresses[0].Address), nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("host IP unknown; known addresses: %v", addresses)
|
|
|
|
}
|