Add ability to filter triton_sd targets by pre-defined groups (#4701)

Additionally, add triton groups metadata to the discovery reponse
and correct a documentation error regarding the triton server id
metadata.

Signed-off-by: Richard Kiene <richard.kiene@joyent.com>
This commit is contained in:
Richard Kiene 2018-10-10 02:03:34 -07:00 committed by Brian Brazil
parent eb4cc37e50
commit b537f6047a
3 changed files with 55 additions and 7 deletions

View file

@ -19,6 +19,8 @@ import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/go-kit/kit/log"
@ -33,6 +35,7 @@ import (
const (
tritonLabel = model.MetaLabelPrefix + "triton_"
tritonLabelGroups = tritonLabel + "groups"
tritonLabelMachineID = tritonLabel + "machine_id"
tritonLabelMachineAlias = tritonLabel + "machine_alias"
tritonLabelMachineBrand = tritonLabel + "machine_brand"
@ -65,6 +68,7 @@ type SDConfig struct {
Account string `yaml:"account"`
DNSSuffix string `yaml:"dns_suffix"`
Endpoint string `yaml:"endpoint"`
Groups []string `yaml:"groups,omitempty"`
Port int `yaml:"port"`
RefreshInterval model.Duration `yaml:"refresh_interval,omitempty"`
TLSConfig config_util.TLSConfig `yaml:"tls_config,omitempty"`
@ -102,11 +106,12 @@ func init() {
// DiscoveryResponse models a JSON response from the Triton discovery.
type DiscoveryResponse struct {
Containers []struct {
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"`
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"`
} `json:"containers"`
}
@ -187,6 +192,11 @@ func (d *Discovery) refresh() (tg *targetgroup.Group, err error) {
}()
var endpoint = fmt.Sprintf("https://%s:%d/v%d/discover", d.sdConfig.Endpoint, d.sdConfig.Port, d.sdConfig.Version)
if len(d.sdConfig.Groups) > 0 {
groups := url.QueryEscape(strings.Join(d.sdConfig.Groups, ","))
endpoint = fmt.Sprintf("%s?groups=%s", endpoint, groups)
}
tg = &targetgroup.Group{
Source: endpoint,
}
@ -219,6 +229,12 @@ func (d *Discovery) refresh() (tg *targetgroup.Group, err error) {
}
addr := fmt.Sprintf("%s.%s:%d", container.VMUUID, d.sdConfig.DNSSuffix, d.sdConfig.Port)
labels[model.AddressLabel] = model.LabelValue(addr)
if len(container.Groups) > 0 {
name := "," + strings.Join(container.Groups, ",") + ","
labels[tritonLabelGroups] = model.LabelValue(name)
}
tg.Targets = append(tg.Targets, labels)
}

View file

@ -55,6 +55,16 @@ var (
CertFile: "shouldnotexist.cert",
},
}
groupsconf = SDConfig{
Account: "testAccount",
DNSSuffix: "triton.example.com",
Endpoint: "127.0.0.1",
Groups: []string{"foo", "bar"},
Port: 443,
Version: 1,
RefreshInterval: 1,
TLSConfig: config.TLSConfig{InsecureSkipVerify: true},
}
)
func TestTritonSDNew(t *testing.T) {
@ -76,6 +86,20 @@ func TestTritonSDNewBadConfig(t *testing.T) {
testutil.Assert(t, td == nil, "")
}
func TestTritonSDNewGroupsConfig(t *testing.T) {
td, err := New(nil, &groupsconf)
testutil.Ok(t, err)
testutil.Assert(t, td != nil, "")
testutil.Assert(t, td.client != nil, "")
testutil.Assert(t, td.interval != 0, "")
testutil.Assert(t, td.sdConfig != nil, "")
testutil.Equals(t, groupsconf.Account, td.sdConfig.Account)
testutil.Equals(t, groupsconf.DNSSuffix, td.sdConfig.DNSSuffix)
testutil.Equals(t, groupsconf.Endpoint, td.sdConfig.Endpoint)
testutil.Equals(t, groupsconf.Groups, td.sdConfig.Groups)
testutil.Equals(t, groupsconf.Port, td.sdConfig.Port)
}
func TestTritonSDRun(t *testing.T) {
var (
td, err = New(nil, &conf)
@ -112,6 +136,7 @@ func TestTritonSDRefreshMultipleTargets(t *testing.T) {
var (
dstr = `{"containers":[
{
"groups":["foo","bar","baz"],
"server_uuid":"44454c4c-5000-104d-8037-b7c04f5a5131",
"vm_alias":"server01",
"vm_brand":"lx",

View file

@ -935,10 +935,12 @@ discovery endpoints.
The following meta labels are available on targets during relabeling:
* `__meta_triton_machine_id`: the UUID of the target container
* `__meta_triton_groups`: the list of groups belonging to the target joined by a comma separator
* `__meta_triton_machine_alias`: the alias of the target container
* `__meta_triton_machine_brand`: the brand of the target container
* `__meta_triton_machine_id`: the UUID of the target container
* `__meta_triton_machine_image`: the target containers image type
* `__meta_triton_machine_server_id`: the server UUID for the target container
* `__meta_triton_server_id`: the server UUID for the target container
```yaml
# The information to access the Triton discovery API.
@ -953,6 +955,11 @@ dns_suffix: <string>
# often the same value as dns_suffix.
endpoint: <string>
# A list of groups for which targets are retrieved. If omitted, all containers
# available to the requesting account are scraped.
groups:
[ - <string> ... ]
# The port to use for discovery and metric scraping.
[ port: <int> | default = 9163 ]