2020-06-26 10:25:58 +00:00
|
|
|
// Copyright 2020 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.
|
|
|
|
|
2021-04-13 07:33:54 +00:00
|
|
|
package moby
|
2020-06-26 10:25:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
2020-11-09 11:41:02 +00:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2020-06-26 10:25:58 +00:00
|
|
|
"github.com/docker/docker/client"
|
|
|
|
"github.com/go-kit/kit/log"
|
2020-08-20 12:48:26 +00:00
|
|
|
"github.com/prometheus/common/config"
|
2020-06-26 10:25:58 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2020-07-30 22:18:38 +00:00
|
|
|
"github.com/prometheus/common/version"
|
2020-06-26 10:25:58 +00:00
|
|
|
|
2020-08-20 12:48:26 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery"
|
2020-06-26 10:25:58 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery/refresh"
|
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
swarmLabel = model.MetaLabelPrefix + "dockerswarm_"
|
|
|
|
)
|
|
|
|
|
2020-07-30 22:18:38 +00:00
|
|
|
var userAgent = fmt.Sprintf("Prometheus/%s", version.Version)
|
|
|
|
|
2021-04-13 07:33:54 +00:00
|
|
|
// DefaultDockerSwarmSDConfig is the default Docker Swarm SD configuration.
|
|
|
|
var DefaultDockerSwarmSDConfig = DockerSwarmSDConfig{
|
2021-02-26 21:48:06 +00:00
|
|
|
RefreshInterval: model.Duration(60 * time.Second),
|
|
|
|
Port: 80,
|
|
|
|
Filters: []Filter{},
|
|
|
|
HTTPClientConfig: config.DefaultHTTPClientConfig,
|
2020-06-26 10:25:58 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 12:48:26 +00:00
|
|
|
func init() {
|
2021-04-13 07:33:54 +00:00
|
|
|
discovery.RegisterConfig(&DockerSwarmSDConfig{})
|
2020-08-20 12:48:26 +00:00
|
|
|
}
|
|
|
|
|
2021-04-13 07:33:54 +00:00
|
|
|
// DockerSwarmSDConfig is the configuration for Docker Swarm based service discovery.
|
|
|
|
type DockerSwarmSDConfig struct {
|
2020-08-20 12:48:26 +00:00
|
|
|
HTTPClientConfig config.HTTPClientConfig `yaml:",inline"`
|
2020-06-26 10:25:58 +00:00
|
|
|
|
2020-11-09 11:41:02 +00:00
|
|
|
Host string `yaml:"host"`
|
|
|
|
Role string `yaml:"role"`
|
|
|
|
Port int `yaml:"port"`
|
|
|
|
Filters []Filter `yaml:"filters"`
|
2020-06-26 10:25:58 +00:00
|
|
|
|
|
|
|
RefreshInterval model.Duration `yaml:"refresh_interval"`
|
|
|
|
}
|
|
|
|
|
2020-11-09 11:41:02 +00:00
|
|
|
// Filter represent a filter that can be passed to Docker Swarm to reduce the
|
|
|
|
// amount of data received.
|
|
|
|
type Filter struct {
|
|
|
|
Name string `yaml:"name"`
|
|
|
|
Values []string `yaml:"values"`
|
|
|
|
}
|
|
|
|
|
2020-08-20 12:48:26 +00:00
|
|
|
// Name returns the name of the Config.
|
2021-04-13 07:33:54 +00:00
|
|
|
func (*DockerSwarmSDConfig) Name() string { return "dockerswarm" }
|
2020-08-20 12:48:26 +00:00
|
|
|
|
|
|
|
// NewDiscoverer returns a Discoverer for the Config.
|
2021-04-13 07:33:54 +00:00
|
|
|
func (c *DockerSwarmSDConfig) NewDiscoverer(opts discovery.DiscovererOptions) (discovery.Discoverer, error) {
|
2020-08-20 12:48:26 +00:00
|
|
|
return NewDiscovery(c, opts.Logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDirectory joins any relative file paths with dir.
|
2021-04-13 07:33:54 +00:00
|
|
|
func (c *DockerSwarmSDConfig) SetDirectory(dir string) {
|
2020-08-20 12:48:26 +00:00
|
|
|
c.HTTPClientConfig.SetDirectory(dir)
|
|
|
|
}
|
|
|
|
|
2020-06-26 10:25:58 +00:00
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
2021-04-13 07:33:54 +00:00
|
|
|
func (c *DockerSwarmSDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
*c = DefaultDockerSwarmSDConfig
|
|
|
|
type plain DockerSwarmSDConfig
|
2020-06-26 10:25:58 +00:00
|
|
|
err := unmarshal((*plain)(c))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if c.Host == "" {
|
|
|
|
return fmt.Errorf("host missing")
|
|
|
|
}
|
2020-07-21 21:24:43 +00:00
|
|
|
if _, err = url.Parse(c.Host); err != nil {
|
2020-06-26 10:25:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch c.Role {
|
|
|
|
case "services", "nodes", "tasks":
|
|
|
|
case "":
|
|
|
|
return fmt.Errorf("role missing (one of: tasks, services, nodes)")
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid role %s, expected tasks, services, or nodes", c.Role)
|
|
|
|
}
|
2021-02-18 22:14:49 +00:00
|
|
|
return c.HTTPClientConfig.Validate()
|
2020-06-26 10:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Discovery periodically performs Docker Swarm requests. It implements
|
|
|
|
// the Discoverer interface.
|
|
|
|
type Discovery struct {
|
|
|
|
*refresh.Discovery
|
2020-11-09 11:41:02 +00:00
|
|
|
client *client.Client
|
|
|
|
role string
|
|
|
|
port int
|
|
|
|
filters filters.Args
|
2020-06-26 10:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDiscovery returns a new Discovery which periodically refreshes its targets.
|
2021-04-13 07:33:54 +00:00
|
|
|
func NewDiscovery(conf *DockerSwarmSDConfig, logger log.Logger) (*Discovery, error) {
|
2020-07-17 15:38:02 +00:00
|
|
|
var err error
|
2020-07-21 21:24:43 +00:00
|
|
|
|
2020-06-26 10:25:58 +00:00
|
|
|
d := &Discovery{
|
|
|
|
port: conf.Port,
|
2020-07-09 11:01:08 +00:00
|
|
|
role: conf.Role,
|
2020-06-26 10:25:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 21:24:43 +00:00
|
|
|
hostURL, err := url.Parse(conf.Host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-06-26 10:25:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-17 15:38:02 +00:00
|
|
|
opts := []client.Opt{
|
2020-06-26 10:25:58 +00:00
|
|
|
client.WithHost(conf.Host),
|
|
|
|
client.WithAPIVersionNegotiation(),
|
2020-07-17 15:38:02 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 11:41:02 +00:00
|
|
|
d.filters = filters.NewArgs()
|
|
|
|
for _, f := range conf.Filters {
|
|
|
|
for _, v := range f.Values {
|
|
|
|
d.filters.Add(f.Name, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 15:48:05 +00:00
|
|
|
// There are other protocols than HTTP supported by the Docker daemon, like
|
2020-07-17 15:41:02 +00:00
|
|
|
// unix, which are not supported by the HTTP client. Passing HTTP client
|
|
|
|
// options to the Docker client makes those non-HTTP requests fail.
|
2020-07-21 21:24:43 +00:00
|
|
|
if hostURL.Scheme == "http" || hostURL.Scheme == "https" {
|
2021-04-21 10:19:16 +00:00
|
|
|
rt, err := config.NewRoundTripperFromConfig(conf.HTTPClientConfig, "dockerswarm_sd", config.WithHTTP2Disabled())
|
2020-07-17 15:38:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
opts = append(opts,
|
|
|
|
client.WithHTTPClient(&http.Client{
|
|
|
|
Transport: rt,
|
|
|
|
Timeout: time.Duration(conf.RefreshInterval),
|
|
|
|
}),
|
2020-07-21 21:24:43 +00:00
|
|
|
client.WithScheme(hostURL.Scheme),
|
2020-07-30 22:18:38 +00:00
|
|
|
client.WithHTTPHeaders(map[string]string{
|
|
|
|
"User-Agent": userAgent,
|
|
|
|
}),
|
2020-07-17 15:38:02 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.client, err = client.NewClientWithOpts(opts...)
|
2020-06-26 10:25:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error setting up docker swarm client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Discovery = refresh.NewDiscovery(
|
|
|
|
logger,
|
|
|
|
"dockerswarm",
|
|
|
|
time.Duration(conf.RefreshInterval),
|
2020-07-09 11:01:08 +00:00
|
|
|
d.refresh,
|
2020-06-26 10:25:58 +00:00
|
|
|
)
|
|
|
|
return d, nil
|
|
|
|
}
|
2020-07-09 11:01:08 +00:00
|
|
|
|
|
|
|
func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) {
|
|
|
|
switch d.role {
|
|
|
|
case "services":
|
|
|
|
return d.refreshServices(ctx)
|
|
|
|
case "nodes":
|
|
|
|
return d.refreshNodes(ctx)
|
|
|
|
case "tasks":
|
|
|
|
return d.refreshTasks(ctx)
|
|
|
|
default:
|
|
|
|
panic(fmt.Errorf("unexpected role %s", d.role))
|
|
|
|
}
|
|
|
|
}
|