2016-12-27 18:16:47 -08:00
|
|
|
// Copyright 2017 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 triton
|
|
|
|
|
|
|
|
import (
|
2017-10-24 21:21:42 -07:00
|
|
|
"context"
|
2016-12-27 18:16:47 -08:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-05-25 03:27:12 -07:00
|
|
|
"io"
|
2016-12-27 18:16:47 -08:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2018-10-10 02:03:34 -07:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2016-12-27 18:16:47 -08:00
|
|
|
"time"
|
|
|
|
|
2017-08-11 11:45:52 -07:00
|
|
|
"github.com/go-kit/kit/log"
|
2019-03-25 16:01:12 -07:00
|
|
|
conntrack "github.com/mwitkow/go-conntrack"
|
|
|
|
"github.com/pkg/errors"
|
2019-03-25 03:54:22 -07:00
|
|
|
config_util "github.com/prometheus/common/config"
|
2016-12-27 18:16:47 -08:00
|
|
|
"github.com/prometheus/common/model"
|
2017-10-24 21:21:42 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
"github.com/prometheus/prometheus/discovery/refresh"
|
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"
|
2016-12-27 18:16:47 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tritonLabel = model.MetaLabelPrefix + "triton_"
|
2018-10-10 02:03:34 -07:00
|
|
|
tritonLabelGroups = tritonLabel + "groups"
|
2017-10-23 06:57:30 -07:00
|
|
|
tritonLabelMachineID = tritonLabel + "machine_id"
|
2016-12-27 18:16:47 -08:00
|
|
|
tritonLabelMachineAlias = tritonLabel + "machine_alias"
|
2017-04-06 14:09:23 -07:00
|
|
|
tritonLabelMachineBrand = tritonLabel + "machine_brand"
|
2016-12-27 18:16:47 -08:00
|
|
|
tritonLabelMachineImage = tritonLabel + "machine_image"
|
2017-10-23 06:57:30 -07:00
|
|
|
tritonLabelServerID = tritonLabel + "server_id"
|
2016-12-27 18:16:47 -08:00
|
|
|
)
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
// DefaultSDConfig is the default Triton SD configuration.
|
|
|
|
var DefaultSDConfig = SDConfig{
|
2020-05-22 08:19:21 -07:00
|
|
|
Role: "container",
|
2019-03-25 03:54:22 -07:00
|
|
|
Port: 9163,
|
|
|
|
RefreshInterval: model.Duration(60 * time.Second),
|
|
|
|
Version: 1,
|
|
|
|
}
|
2016-12-27 18:16:47 -08: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
|
|
|
// SDConfig is the configuration for Triton based service discovery.
|
|
|
|
type SDConfig struct {
|
|
|
|
Account string `yaml:"account"`
|
2020-05-22 08:19:21 -07:00
|
|
|
Role string `yaml:"role"`
|
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
|
|
|
DNSSuffix string `yaml:"dns_suffix"`
|
|
|
|
Endpoint string `yaml:"endpoint"`
|
2018-10-10 02:03:34 -07:00
|
|
|
Groups []string `yaml:"groups,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
|
|
|
Port int `yaml:"port"`
|
|
|
|
RefreshInterval model.Duration `yaml:"refresh_interval,omitempty"`
|
|
|
|
TLSConfig config_util.TLSConfig `yaml:"tls_config,omitempty"`
|
|
|
|
Version int `yaml:"version"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the yaml.Unmarshaler interface.
|
|
|
|
func (c *SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
*c = DefaultSDConfig
|
|
|
|
type plain SDConfig
|
|
|
|
err := unmarshal((*plain)(c))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-22 08:19:21 -07:00
|
|
|
if c.Role != "container" && c.Role != "cn" {
|
|
|
|
return errors.New("triton SD configuration requires role to be 'container' or 'cn'")
|
|
|
|
}
|
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
|
|
|
if c.Account == "" {
|
2019-03-25 16:01:12 -07:00
|
|
|
return errors.New("triton SD configuration requires an account")
|
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
|
|
|
}
|
|
|
|
if c.DNSSuffix == "" {
|
2019-03-25 16:01:12 -07:00
|
|
|
return errors.New("triton SD configuration requires a dns_suffix")
|
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
|
|
|
}
|
|
|
|
if c.Endpoint == "" {
|
2019-03-25 16:01:12 -07:00
|
|
|
return errors.New("triton SD configuration requires an endpoint")
|
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
|
|
|
}
|
|
|
|
if c.RefreshInterval <= 0 {
|
2019-03-25 16:01:12 -07:00
|
|
|
return errors.New("triton SD configuration requires RefreshInterval to be a positive integer")
|
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-04 01:07:39 -07:00
|
|
|
return nil
|
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
|
|
|
}
|
|
|
|
|
2017-10-23 06:57:30 -07:00
|
|
|
// DiscoveryResponse models a JSON response from the Triton discovery.
|
2020-05-22 08:19:21 -07:00
|
|
|
type DiscoveryResponse struct {
|
2016-12-27 18:16:47 -08:00
|
|
|
Containers []struct {
|
2018-10-10 02:03:34 -07:00
|
|
|
Groups []string `json:"groups"`
|
|
|
|
ServerUUID string `json:"server_uuid"`
|
|
|
|
VMAlias string `json:"vm_alias"`
|
|
|
|
VMBrand string `json:"vm_brand"`
|
|
|
|
VMImageUUID string `json:"vm_image_uuid"`
|
|
|
|
VMUUID string `json:"vm_uuid"`
|
2016-12-27 18:16:47 -08:00
|
|
|
} `json:"containers"`
|
|
|
|
}
|
|
|
|
|
2020-05-22 08:19:21 -07:00
|
|
|
// ComputeNodeDiscoveryResponse models a JSON response from the Triton discovery /gz/ endpoint.
|
|
|
|
type ComputeNodeDiscoveryResponse struct {
|
|
|
|
ComputeNodes []struct {
|
|
|
|
ServerUUID string `json:"server_uuid"`
|
|
|
|
ServerHostname string `json:"server_hostname"`
|
|
|
|
} `json:"cns"`
|
|
|
|
}
|
|
|
|
|
2016-12-27 18:16:47 -08:00
|
|
|
// Discovery periodically performs Triton-SD requests. It implements
|
2018-01-08 15:59:18 -08:00
|
|
|
// the Discoverer interface.
|
2016-12-27 18:16:47 -08:00
|
|
|
type Discovery struct {
|
2019-03-25 03:54:22 -07:00
|
|
|
*refresh.Discovery
|
2016-12-27 18:16:47 -08:00
|
|
|
client *http.Client
|
|
|
|
interval time.Duration
|
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 *SDConfig
|
2016-12-27 18:16:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new Discovery which periodically refreshes its targets.
|
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 New(logger log.Logger, conf *SDConfig) (*Discovery, error) {
|
2018-04-25 10:19:06 -07:00
|
|
|
tls, err := config_util.NewTLSConfig(&conf.TLSConfig)
|
2016-12-27 18:16:47 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-10-06 03:22:19 -07:00
|
|
|
transport := &http.Transport{
|
|
|
|
TLSClientConfig: tls,
|
|
|
|
DialContext: conntrack.NewDialContextFunc(
|
|
|
|
conntrack.DialWithTracing(),
|
|
|
|
conntrack.DialWithName("triton_sd"),
|
|
|
|
),
|
|
|
|
}
|
2016-12-27 18:16:47 -08:00
|
|
|
client := &http.Client{Transport: transport}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
d := &Discovery{
|
2016-12-27 18:16:47 -08:00
|
|
|
client: client,
|
|
|
|
interval: time.Duration(conf.RefreshInterval),
|
|
|
|
sdConfig: conf,
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
d.Discovery = refresh.NewDiscovery(
|
|
|
|
logger,
|
|
|
|
"triton",
|
|
|
|
time.Duration(conf.RefreshInterval),
|
|
|
|
d.refresh,
|
|
|
|
)
|
|
|
|
return d, nil
|
2016-12-27 18:16:47 -08:00
|
|
|
}
|
|
|
|
|
2020-05-22 08:19:21 -07:00
|
|
|
// triton-cmon has two discovery endpoints:
|
|
|
|
// https://github.com/joyent/triton-cmon/blob/master/lib/endpoints/discover.js
|
|
|
|
//
|
|
|
|
// The default endpoint exposes "containers", otherwise called "virtual machines" in triton,
|
|
|
|
// which are (branded) zones running on the triton platform.
|
|
|
|
//
|
|
|
|
// The /gz/ endpoint exposes "compute nodes", also known as "servers" or "global zones",
|
|
|
|
// on which the "containers" are running.
|
|
|
|
//
|
|
|
|
// As triton is not internally consistent in using these names,
|
|
|
|
// the terms as used in triton-cmon are used here.
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) {
|
2020-05-22 08:19:21 -07:00
|
|
|
var endpointFormat string
|
|
|
|
switch d.sdConfig.Role {
|
|
|
|
case "container":
|
|
|
|
endpointFormat = "https://%s:%d/v%d/discover"
|
|
|
|
case "cn":
|
|
|
|
endpointFormat = "https://%s:%d/v%d/gz/discover"
|
|
|
|
default:
|
|
|
|
return nil, errors.New(fmt.Sprintf("unknown role '%s' in configuration", d.sdConfig.Role))
|
|
|
|
}
|
|
|
|
var endpoint = fmt.Sprintf(endpointFormat, d.sdConfig.Endpoint, d.sdConfig.Port, d.sdConfig.Version)
|
2018-10-10 02:03:34 -07:00
|
|
|
if len(d.sdConfig.Groups) > 0 {
|
|
|
|
groups := url.QueryEscape(strings.Join(d.sdConfig.Groups, ","))
|
|
|
|
endpoint = fmt.Sprintf("%s?groups=%s", endpoint, groups)
|
|
|
|
}
|
|
|
|
|
2019-02-26 05:47:04 -08:00
|
|
|
req, err := http.NewRequest("GET", endpoint, nil)
|
2016-12-27 18:16:47 -08:00
|
|
|
if err != nil {
|
2019-02-26 05:47:04 -08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
resp, err := d.client.Do(req)
|
|
|
|
if err != nil {
|
2019-03-25 16:01:12 -07:00
|
|
|
return nil, errors.Wrap(err, "an error occurred when requesting targets from the discovery endpoint")
|
2016-12-27 18:16:47 -08:00
|
|
|
}
|
|
|
|
|
2019-05-25 03:27:12 -07:00
|
|
|
defer func() {
|
|
|
|
io.Copy(ioutil.Discard, resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
}()
|
2016-12-27 18:16:47 -08:00
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2019-03-25 16:01:12 -07:00
|
|
|
return nil, errors.Wrap(err, "an error occurred when reading the response body")
|
2016-12-27 18:16:47 -08:00
|
|
|
}
|
|
|
|
|
2020-05-22 08:19:21 -07:00
|
|
|
// The JSON response body is different so it needs to be processed/mapped separately.
|
|
|
|
switch d.sdConfig.Role {
|
|
|
|
case "container":
|
|
|
|
return d.processContainerResponse(data, endpoint)
|
|
|
|
case "cn":
|
|
|
|
return d.processComputeNodeResponse(data, endpoint)
|
|
|
|
default:
|
|
|
|
return nil, errors.New(fmt.Sprintf("unknown role '%s' in configuration", d.sdConfig.Role))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Discovery) processContainerResponse(data []byte, endpoint string) ([]*targetgroup.Group, error) {
|
|
|
|
tg := &targetgroup.Group{
|
|
|
|
Source: endpoint,
|
|
|
|
}
|
|
|
|
|
|
|
|
dr := DiscoveryResponse{}
|
|
|
|
err := json.Unmarshal(data, &dr)
|
2016-12-27 18:16:47 -08:00
|
|
|
if err != nil {
|
2019-03-25 16:01:12 -07:00
|
|
|
return nil, errors.Wrap(err, "an error occurred unmarshaling the discovery response json")
|
2016-12-27 18:16:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, container := range dr.Containers {
|
|
|
|
labels := model.LabelSet{
|
2017-10-23 06:57:30 -07:00
|
|
|
tritonLabelMachineID: model.LabelValue(container.VMUUID),
|
2016-12-27 18:16:47 -08:00
|
|
|
tritonLabelMachineAlias: model.LabelValue(container.VMAlias),
|
2017-04-06 14:09:23 -07:00
|
|
|
tritonLabelMachineBrand: model.LabelValue(container.VMBrand),
|
2016-12-27 18:16:47 -08:00
|
|
|
tritonLabelMachineImage: model.LabelValue(container.VMImageUUID),
|
2017-10-23 06:57:30 -07:00
|
|
|
tritonLabelServerID: model.LabelValue(container.ServerUUID),
|
2016-12-27 18:16:47 -08:00
|
|
|
}
|
|
|
|
addr := fmt.Sprintf("%s.%s:%d", container.VMUUID, d.sdConfig.DNSSuffix, d.sdConfig.Port)
|
|
|
|
labels[model.AddressLabel] = model.LabelValue(addr)
|
2018-10-10 02:03:34 -07:00
|
|
|
|
|
|
|
if len(container.Groups) > 0 {
|
|
|
|
name := "," + strings.Join(container.Groups, ",") + ","
|
|
|
|
labels[tritonLabelGroups] = model.LabelValue(name)
|
|
|
|
}
|
|
|
|
|
2016-12-27 18:16:47 -08:00
|
|
|
tg.Targets = append(tg.Targets, labels)
|
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
return []*targetgroup.Group{tg}, nil
|
2016-12-27 18:16:47 -08:00
|
|
|
}
|
2020-05-22 08:19:21 -07:00
|
|
|
|
|
|
|
func (d *Discovery) processComputeNodeResponse(data []byte, endpoint string) ([]*targetgroup.Group, error) {
|
|
|
|
tg := &targetgroup.Group{
|
|
|
|
Source: endpoint,
|
|
|
|
}
|
|
|
|
|
|
|
|
dr := ComputeNodeDiscoveryResponse{}
|
|
|
|
err := json.Unmarshal(data, &dr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "an error occurred unmarshaling the compute node discovery response json")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cn := range dr.ComputeNodes {
|
|
|
|
labels := model.LabelSet{
|
|
|
|
tritonLabelMachineID: model.LabelValue(cn.ServerUUID),
|
|
|
|
tritonLabelMachineAlias: model.LabelValue(cn.ServerHostname),
|
|
|
|
}
|
|
|
|
addr := fmt.Sprintf("%s.%s:%d", cn.ServerUUID, d.sdConfig.DNSSuffix, d.sdConfig.Port)
|
|
|
|
labels[model.AddressLabel] = model.LabelValue(addr)
|
|
|
|
|
|
|
|
tg.Targets = append(tg.Targets, labels)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []*targetgroup.Group{tg}, nil
|
|
|
|
}
|