2015-08-17 17:49:10 +00: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.
|
|
|
|
|
2016-04-25 13:30:11 +00:00
|
|
|
package marathon
|
2015-07-16 12:02:07 +00:00
|
|
|
|
|
|
|
import (
|
2017-10-25 04:21:42 +00:00
|
|
|
"context"
|
2015-07-16 12:02:07 +00:00
|
|
|
"errors"
|
2018-04-17 08:28:06 +00:00
|
|
|
"io"
|
2016-09-29 12:57:28 +00:00
|
|
|
"net/http"
|
2018-04-17 08:28:06 +00:00
|
|
|
"net/http/httptest"
|
2015-07-16 12:02:07 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-08-20 15:18:46 +00: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 20:01:34 +00:00
|
|
|
"github.com/prometheus/prometheus/discovery/targetgroup"
|
2015-07-16 12:02:07 +00:00
|
|
|
)
|
|
|
|
|
2016-06-14 09:35:21 +00:00
|
|
|
var (
|
|
|
|
marathonValidLabel = map[string]string{"prometheus": "yes"}
|
|
|
|
testServers = []string{"http://localhost:8080"}
|
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 20:01:34 +00:00
|
|
|
conf = SDConfig{Servers: testServers}
|
2016-06-14 09:35:21 +00:00
|
|
|
)
|
2015-07-16 12:02:07 +00: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 20:01:34 +00:00
|
|
|
func testUpdateServices(client AppListClient, ch chan []*targetgroup.Group) error {
|
|
|
|
md, err := NewDiscovery(conf, nil)
|
2016-09-29 12:57:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-04-25 13:30:11 +00:00
|
|
|
}
|
2016-09-29 12:57:28 +00:00
|
|
|
md.appsClient = client
|
2016-06-14 09:35:21 +00:00
|
|
|
return md.updateServices(context.Background(), ch)
|
2015-07-16 12:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonSDHandleError(t *testing.T) {
|
2016-06-14 09:35:21 +00:00
|
|
|
var (
|
|
|
|
errTesting = errors.New("testing failure")
|
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 20:01:34 +00:00
|
|
|
ch = make(chan []*targetgroup.Group, 1)
|
2018-04-05 08:08:18 +00:00
|
|
|
client = func(client *http.Client, url string) (*AppList, error) { return nil, errTesting }
|
2016-06-14 09:35:21 +00:00
|
|
|
)
|
|
|
|
if err := testUpdateServices(client, ch); err != errTesting {
|
2015-07-16 12:02:07 +00:00
|
|
|
t.Fatalf("Expected error: %s", err)
|
|
|
|
}
|
2016-06-14 09:35:21 +00:00
|
|
|
select {
|
|
|
|
case tg := <-ch:
|
|
|
|
t.Fatalf("Got group: %s", tg)
|
|
|
|
default:
|
|
|
|
}
|
2015-07-16 12:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonSDEmptyList(t *testing.T) {
|
2016-06-14 09:35:21 +00:00
|
|
|
var (
|
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 20:01:34 +00:00
|
|
|
ch = make(chan []*targetgroup.Group, 1)
|
2018-04-05 08:08:18 +00:00
|
|
|
client = func(client *http.Client, url string) (*AppList, error) { return &AppList{}, nil }
|
2016-06-14 09:35:21 +00:00
|
|
|
)
|
|
|
|
if err := testUpdateServices(client, ch); err != nil {
|
2015-07-16 12:02:07 +00:00
|
|
|
t.Fatalf("Got error: %s", err)
|
|
|
|
}
|
2016-06-14 09:35:21 +00:00
|
|
|
select {
|
|
|
|
case tg := <-ch:
|
|
|
|
if len(tg) > 0 {
|
|
|
|
t.Fatalf("Got group: %v", tg)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
2015-07-16 12:02:07 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 13:30:11 +00:00
|
|
|
func marathonTestAppList(labels map[string]string, runningTasks int) *AppList {
|
2016-06-14 09:35:21 +00:00
|
|
|
var (
|
|
|
|
task = Task{
|
2018-09-21 10:53:04 +00:00
|
|
|
ID: "test-task-1",
|
|
|
|
Host: "mesos-slave1",
|
2016-06-14 09:35:21 +00:00
|
|
|
}
|
2017-03-18 20:10:44 +00:00
|
|
|
docker = DockerContainer{
|
|
|
|
Image: "repo/image:tag",
|
|
|
|
}
|
2018-09-21 10:53:04 +00:00
|
|
|
portMappings = []PortMapping{
|
2018-11-26 12:39:35 +00:00
|
|
|
{Labels: labels, HostPort: 31000},
|
2018-09-21 10:53:04 +00:00
|
|
|
}
|
|
|
|
container = Container{Docker: docker, PortMappings: portMappings}
|
2016-06-14 09:35:21 +00:00
|
|
|
app = App{
|
|
|
|
ID: "test-service",
|
|
|
|
Tasks: []Task{task},
|
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
|
|
|
}
|
|
|
|
)
|
2016-04-25 13:30:11 +00:00
|
|
|
return &AppList{
|
|
|
|
Apps: []App{app},
|
2015-07-16 12:02:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonSDSendGroup(t *testing.T) {
|
2016-06-14 09:35:21 +00:00
|
|
|
var (
|
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 20:01:34 +00:00
|
|
|
ch = make(chan []*targetgroup.Group, 1)
|
2018-04-05 08:08:18 +00:00
|
|
|
client = func(client *http.Client, url string) (*AppList, error) {
|
2016-06-14 09:35:21 +00:00
|
|
|
return marathonTestAppList(marathonValidLabel, 1), nil
|
2015-07-16 12:02:07 +00:00
|
|
|
}
|
2016-06-14 09:35:21 +00:00
|
|
|
)
|
|
|
|
if err := testUpdateServices(client, ch); err != nil {
|
2015-07-16 12:02:07 +00:00
|
|
|
t.Fatalf("Got error: %s", err)
|
|
|
|
}
|
2016-06-14 09:35:21 +00:00
|
|
|
select {
|
|
|
|
case tgs := <-ch:
|
|
|
|
tg := tgs[0]
|
|
|
|
|
|
|
|
if tg.Source != "test-service" {
|
|
|
|
t.Fatalf("Wrong target group name: %s", tg.Source)
|
|
|
|
}
|
|
|
|
if len(tg.Targets) != 1 {
|
|
|
|
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
|
|
}
|
|
|
|
tgt := tg.Targets[0]
|
|
|
|
if tgt[model.AddressLabel] != "mesos-slave1:31000" {
|
|
|
|
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
2017-03-18 20:10:44 +00:00
|
|
|
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "yes" {
|
|
|
|
t.Fatalf("Wrong first portMappings label from the first port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
2016-06-14 09:35:21 +00:00
|
|
|
default:
|
|
|
|
t.Fatal("Did not get a target group.")
|
|
|
|
}
|
2015-07-16 12:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonSDRemoveApp(t *testing.T) {
|
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 20:01:34 +00:00
|
|
|
var ch = make(chan []*targetgroup.Group, 1)
|
|
|
|
md, err := NewDiscovery(conf, nil)
|
2016-09-29 12:57:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s", err)
|
|
|
|
}
|
2017-03-17 00:21:04 +00:00
|
|
|
|
2018-04-05 08:08:18 +00:00
|
|
|
md.appsClient = func(client *http.Client, url string) (*AppList, error) {
|
2016-09-29 12:57:28 +00:00
|
|
|
return marathonTestAppList(marathonValidLabel, 1), nil
|
|
|
|
}
|
|
|
|
if err := md.updateServices(context.Background(), ch); err != nil {
|
2015-07-16 12:02:07 +00:00
|
|
|
t.Fatalf("Got error on first update: %s", err)
|
|
|
|
}
|
2017-03-17 00:21:04 +00:00
|
|
|
up1 := (<-ch)[0]
|
2015-07-16 12:02:07 +00:00
|
|
|
|
2018-04-05 08:08:18 +00:00
|
|
|
md.appsClient = func(client *http.Client, url string) (*AppList, error) {
|
2015-07-16 12:02:07 +00:00
|
|
|
return marathonTestAppList(marathonValidLabel, 0), nil
|
|
|
|
}
|
2016-09-29 12:57:28 +00:00
|
|
|
if err := md.updateServices(context.Background(), ch); err != nil {
|
2015-07-16 12:02:07 +00:00
|
|
|
t.Fatalf("Got error on second update: %s", err)
|
|
|
|
}
|
2017-03-17 00:21:04 +00:00
|
|
|
up2 := (<-ch)[0]
|
|
|
|
|
|
|
|
if up2.Source != up1.Source {
|
|
|
|
t.Fatalf("Source is different: %s", up2)
|
|
|
|
if len(up2.Targets) > 0 {
|
|
|
|
t.Fatalf("Got a non-empty target set: %s", up2.Targets)
|
|
|
|
}
|
|
|
|
}
|
2015-07-16 12:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonSDRunAndStop(t *testing.T) {
|
2016-06-14 09:35:21 +00:00
|
|
|
var (
|
2016-09-29 12:57:28 +00:00
|
|
|
refreshInterval = model.Duration(time.Millisecond * 10)
|
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 20:01:34 +00:00
|
|
|
conf = SDConfig{Servers: testServers, RefreshInterval: refreshInterval}
|
|
|
|
ch = make(chan []*targetgroup.Group)
|
2017-03-17 00:21:04 +00:00
|
|
|
doneCh = make(chan error)
|
2016-06-14 09:35:21 +00: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 20:01:34 +00:00
|
|
|
md, err := NewDiscovery(conf, nil)
|
2016-09-29 12:57:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s", err)
|
|
|
|
}
|
2018-04-05 08:08:18 +00:00
|
|
|
md.appsClient = func(client *http.Client, url string) (*AppList, error) {
|
2016-09-29 12:57:28 +00:00
|
|
|
return marathonTestAppList(marathonValidLabel, 1), nil
|
|
|
|
}
|
2016-02-18 16:26:27 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2015-07-16 12:02:07 +00:00
|
|
|
|
|
|
|
go func() {
|
2017-03-17 00:21:04 +00:00
|
|
|
md.Run(ctx, ch)
|
|
|
|
close(doneCh)
|
2015-07-16 12:02:07 +00:00
|
|
|
}()
|
2016-02-18 16:26:27 +00:00
|
|
|
|
2017-03-17 00:21:04 +00:00
|
|
|
timeout := time.After(md.refreshInterval * 3)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
cancel()
|
|
|
|
case <-doneCh:
|
2018-09-21 10:53:04 +00:00
|
|
|
cancel()
|
2017-03-17 00:21:04 +00:00
|
|
|
return
|
|
|
|
case <-timeout:
|
|
|
|
t.Fatalf("Update took too long.")
|
|
|
|
}
|
|
|
|
}
|
2015-07-16 12:02:07 +00:00
|
|
|
}
|
2016-06-13 09:24:03 +00:00
|
|
|
|
2018-09-21 10:53:04 +00:00
|
|
|
func marathonTestAppListWithMultiplePorts(labels map[string]string, runningTasks int) *AppList {
|
2017-03-18 20:10:44 +00:00
|
|
|
var (
|
|
|
|
task = Task{
|
2018-09-21 10:53:04 +00:00
|
|
|
ID: "test-task-1",
|
|
|
|
Host: "mesos-slave1",
|
2017-03-18 20:10:44 +00:00
|
|
|
}
|
|
|
|
docker = DockerContainer{
|
|
|
|
Image: "repo/image:tag",
|
|
|
|
}
|
2018-09-21 10:53:04 +00:00
|
|
|
portMappings = []PortMapping{
|
2018-11-26 12:39:35 +00:00
|
|
|
{Labels: labels, HostPort: 31000},
|
|
|
|
{Labels: make(map[string]string), HostPort: 32000},
|
2018-09-21 10:53:04 +00:00
|
|
|
}
|
|
|
|
container = Container{Docker: docker, PortMappings: portMappings}
|
2017-03-18 20:10:44 +00:00
|
|
|
app = App{
|
|
|
|
ID: "test-service",
|
|
|
|
Tasks: []Task{task},
|
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return &AppList{
|
|
|
|
Apps: []App{app},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 10:53:04 +00:00
|
|
|
func TestMarathonSDSendGroupWithMultiplePort(t *testing.T) {
|
2017-03-18 20:10:44 +00:00
|
|
|
var (
|
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 20:01:34 +00:00
|
|
|
ch = make(chan []*targetgroup.Group, 1)
|
2018-04-05 08:08:18 +00:00
|
|
|
client = func(client *http.Client, url string) (*AppList, error) {
|
2018-09-21 10:53:04 +00:00
|
|
|
return marathonTestAppListWithMultiplePorts(marathonValidLabel, 1), nil
|
2017-03-18 20:10:44 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
if err := testUpdateServices(client, ch); err != nil {
|
|
|
|
t.Fatalf("Got error: %s", err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case tgs := <-ch:
|
|
|
|
tg := tgs[0]
|
|
|
|
|
|
|
|
if tg.Source != "test-service" {
|
|
|
|
t.Fatalf("Wrong target group name: %s", tg.Source)
|
|
|
|
}
|
|
|
|
if len(tg.Targets) != 2 {
|
|
|
|
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
|
|
}
|
|
|
|
tgt := tg.Targets[0]
|
|
|
|
if tgt[model.AddressLabel] != "mesos-slave1:31000" {
|
|
|
|
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "yes" {
|
|
|
|
t.Fatalf("Wrong first portMappings label from the first port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
tgt = tg.Targets[1]
|
|
|
|
if tgt[model.AddressLabel] != "mesos-slave1:32000" {
|
|
|
|
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
|
|
|
t.Fatalf("Wrong portMappings label from the second port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Did not get a target group.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 09:24:03 +00:00
|
|
|
func marathonTestZeroTaskPortAppList(labels map[string]string, runningTasks int) *AppList {
|
2016-06-14 09:35:21 +00:00
|
|
|
var (
|
|
|
|
task = Task{
|
|
|
|
ID: "test-task-2",
|
|
|
|
Host: "mesos-slave-2",
|
|
|
|
Ports: []uint32{},
|
|
|
|
}
|
|
|
|
docker = DockerContainer{Image: "repo/image:tag"}
|
|
|
|
container = Container{Docker: docker}
|
|
|
|
app = App{
|
|
|
|
ID: "test-service-zero-ports",
|
|
|
|
Tasks: []Task{task},
|
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
|
|
|
}
|
|
|
|
)
|
2016-06-13 09:24:03 +00:00
|
|
|
return &AppList{
|
|
|
|
Apps: []App{app},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonZeroTaskPorts(t *testing.T) {
|
2016-06-14 09:35:21 +00:00
|
|
|
var (
|
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 20:01:34 +00:00
|
|
|
ch = make(chan []*targetgroup.Group, 1)
|
2018-04-05 08:08:18 +00:00
|
|
|
client = func(client *http.Client, url string) (*AppList, error) {
|
2016-06-14 09:35:21 +00:00
|
|
|
return marathonTestZeroTaskPortAppList(marathonValidLabel, 1), nil
|
2016-06-13 09:24:03 +00:00
|
|
|
}
|
2016-06-14 09:35:21 +00:00
|
|
|
)
|
|
|
|
if err := testUpdateServices(client, ch); err != nil {
|
2016-06-13 09:24:03 +00:00
|
|
|
t.Fatalf("Got error: %s", err)
|
|
|
|
}
|
2016-06-14 09:35:21 +00:00
|
|
|
select {
|
|
|
|
case tgs := <-ch:
|
|
|
|
tg := tgs[0]
|
|
|
|
|
|
|
|
if tg.Source != "test-service-zero-ports" {
|
|
|
|
t.Fatalf("Wrong target group name: %s", tg.Source)
|
|
|
|
}
|
|
|
|
if len(tg.Targets) != 0 {
|
|
|
|
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Did not get a target group.")
|
|
|
|
}
|
2016-06-13 09:24:03 +00:00
|
|
|
}
|
2017-03-18 20:10:44 +00:00
|
|
|
|
2018-04-17 08:28:06 +00:00
|
|
|
func Test500ErrorHttpResponseWithValidJSONBody(t *testing.T) {
|
|
|
|
var (
|
|
|
|
ch = make(chan []*targetgroup.Group, 1)
|
|
|
|
client = fetchApps
|
|
|
|
)
|
|
|
|
// Simulate 500 error with a valid JSON response.
|
|
|
|
respHandler := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
io.WriteString(w, `{}`)
|
|
|
|
}
|
|
|
|
// Create a test server with mock HTTP handler.
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(respHandler))
|
|
|
|
defer ts.Close()
|
|
|
|
// Backup conf for future tests.
|
|
|
|
backupConf := conf
|
|
|
|
defer func() {
|
|
|
|
conf = backupConf
|
|
|
|
}()
|
|
|
|
// Setup conf for the test case.
|
|
|
|
conf = SDConfig{Servers: []string{ts.URL}}
|
2018-11-27 16:44:29 +00:00
|
|
|
// Execute test case and validate behavior.
|
2018-04-17 08:28:06 +00:00
|
|
|
if err := testUpdateServices(client, ch); err == nil {
|
|
|
|
t.Fatalf("Expected error for 5xx HTTP response from marathon server")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 10:53:04 +00:00
|
|
|
func marathonTestAppListWithPortDefinitions(labels map[string]string, runningTasks int) *AppList {
|
2017-03-18 20:10:44 +00:00
|
|
|
var (
|
|
|
|
task = Task{
|
2018-09-21 10:53:04 +00:00
|
|
|
ID: "test-task-1",
|
|
|
|
Host: "mesos-slave1",
|
2017-03-18 20:10:44 +00:00
|
|
|
}
|
|
|
|
docker = DockerContainer{
|
|
|
|
Image: "repo/image:tag",
|
|
|
|
}
|
|
|
|
container = Container{Docker: docker}
|
|
|
|
app = App{
|
|
|
|
ID: "test-service",
|
|
|
|
Tasks: []Task{task},
|
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
2018-09-21 10:53:04 +00:00
|
|
|
PortDefinitions: []PortDefinition{
|
|
|
|
{Labels: make(map[string]string), Port: 31000},
|
|
|
|
{Labels: labels, Port: 32000},
|
2017-03-18 20:10:44 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return &AppList{
|
|
|
|
Apps: []App{app},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 10:53:04 +00:00
|
|
|
func TestMarathonSDSendGroupWithPortDefinitions(t *testing.T) {
|
2017-03-18 20:10:44 +00:00
|
|
|
var (
|
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 20:01:34 +00:00
|
|
|
ch = make(chan []*targetgroup.Group, 1)
|
2018-04-05 08:08:18 +00:00
|
|
|
client = func(client *http.Client, url string) (*AppList, error) {
|
2018-09-21 10:53:04 +00:00
|
|
|
return marathonTestAppListWithPortDefinitions(marathonValidLabel, 1), nil
|
2017-03-18 20:10:44 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
if err := testUpdateServices(client, ch); err != nil {
|
|
|
|
t.Fatalf("Got error: %s", err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case tgs := <-ch:
|
|
|
|
tg := tgs[0]
|
|
|
|
|
|
|
|
if tg.Source != "test-service" {
|
|
|
|
t.Fatalf("Wrong target group name: %s", tg.Source)
|
|
|
|
}
|
|
|
|
if len(tg.Targets) != 2 {
|
|
|
|
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
|
|
}
|
|
|
|
tgt := tg.Targets[0]
|
|
|
|
if tgt[model.AddressLabel] != "mesos-slave1:31000" {
|
|
|
|
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
|
|
|
t.Fatalf("Wrong first portMappings label from the first port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
|
|
t.Fatalf("Wrong first portDefinitions label from the first port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
tgt = tg.Targets[1]
|
|
|
|
if tgt[model.AddressLabel] != "mesos-slave1:32000" {
|
|
|
|
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
|
|
|
t.Fatalf("Wrong portMappings label from the second port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "yes" {
|
|
|
|
t.Fatalf("Wrong portDefinitions label from the second port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Did not get a target group.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 10:53:04 +00:00
|
|
|
func marathonTestAppListWithPorts(labels map[string]string, runningTasks int) *AppList {
|
2017-03-18 20:10:44 +00:00
|
|
|
var (
|
|
|
|
task = Task{
|
|
|
|
ID: "test-task-1",
|
|
|
|
Host: "mesos-slave1",
|
|
|
|
Ports: []uint32{31000, 32000},
|
|
|
|
}
|
|
|
|
docker = DockerContainer{
|
|
|
|
Image: "repo/image:tag",
|
|
|
|
}
|
|
|
|
container = Container{Docker: docker}
|
|
|
|
app = App{
|
|
|
|
ID: "test-service",
|
|
|
|
Tasks: []Task{task},
|
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return &AppList{
|
|
|
|
Apps: []App{app},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 10:53:04 +00:00
|
|
|
func TestMarathonSDSendGroupWithPorts(t *testing.T) {
|
2017-03-18 20:10:44 +00:00
|
|
|
var (
|
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 20:01:34 +00:00
|
|
|
ch = make(chan []*targetgroup.Group, 1)
|
2018-04-05 08:08:18 +00:00
|
|
|
client = func(client *http.Client, url string) (*AppList, error) {
|
2018-09-21 10:53:04 +00:00
|
|
|
return marathonTestAppListWithPorts(marathonValidLabel, 1), nil
|
2017-03-18 20:10:44 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
if err := testUpdateServices(client, ch); err != nil {
|
|
|
|
t.Fatalf("Got error: %s", err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case tgs := <-ch:
|
|
|
|
tg := tgs[0]
|
|
|
|
|
|
|
|
if tg.Source != "test-service" {
|
|
|
|
t.Fatalf("Wrong target group name: %s", tg.Source)
|
|
|
|
}
|
|
|
|
if len(tg.Targets) != 2 {
|
|
|
|
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
|
|
}
|
|
|
|
tgt := tg.Targets[0]
|
2017-12-05 19:10:40 +00:00
|
|
|
if tgt[model.AddressLabel] != "mesos-slave1:31000" {
|
|
|
|
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
2018-09-21 10:53:04 +00:00
|
|
|
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
2017-12-05 19:10:40 +00:00
|
|
|
t.Fatalf("Wrong first portMappings label from the first port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
|
|
t.Fatalf("Wrong first portDefinitions label from the first port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
tgt = tg.Targets[1]
|
|
|
|
if tgt[model.AddressLabel] != "mesos-slave1:32000" {
|
|
|
|
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
|
|
|
t.Fatalf("Wrong portMappings label from the second port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
|
|
t.Fatalf("Wrong portDefinitions label from the second port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Did not get a target group.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func marathonTestAppListWithContainerPortMappings(labels map[string]string, runningTasks int) *AppList {
|
|
|
|
var (
|
|
|
|
task = Task{
|
2018-09-21 10:53:04 +00:00
|
|
|
ID: "test-task-1",
|
|
|
|
Host: "mesos-slave1",
|
2018-11-26 12:39:35 +00:00
|
|
|
Ports: []uint32{
|
|
|
|
12345, // 'Automatically-generated' port
|
|
|
|
32000,
|
|
|
|
},
|
2017-12-05 19:10:40 +00:00
|
|
|
}
|
|
|
|
docker = DockerContainer{
|
|
|
|
Image: "repo/image:tag",
|
|
|
|
}
|
|
|
|
container = Container{
|
|
|
|
Docker: docker,
|
2018-09-21 10:53:04 +00:00
|
|
|
PortMappings: []PortMapping{
|
2018-11-26 12:39:35 +00:00
|
|
|
{Labels: labels, HostPort: 0},
|
|
|
|
{Labels: make(map[string]string), HostPort: 32000},
|
2017-12-05 19:10:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
app = App{
|
|
|
|
ID: "test-service",
|
|
|
|
Tasks: []Task{task},
|
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return &AppList{
|
|
|
|
Apps: []App{app},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonSDSendGroupWithContainerPortMappings(t *testing.T) {
|
|
|
|
var (
|
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 20:01:34 +00:00
|
|
|
ch = make(chan []*targetgroup.Group, 1)
|
2018-04-05 08:08:18 +00:00
|
|
|
client = func(client *http.Client, url string) (*AppList, error) {
|
2017-12-05 19:10:40 +00:00
|
|
|
return marathonTestAppListWithContainerPortMappings(marathonValidLabel, 1), nil
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if err := testUpdateServices(client, ch); err != nil {
|
|
|
|
t.Fatalf("Got error: %s", err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case tgs := <-ch:
|
|
|
|
tg := tgs[0]
|
|
|
|
|
|
|
|
if tg.Source != "test-service" {
|
|
|
|
t.Fatalf("Wrong target group name: %s", tg.Source)
|
|
|
|
}
|
|
|
|
if len(tg.Targets) != 2 {
|
|
|
|
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
|
|
}
|
|
|
|
tgt := tg.Targets[0]
|
2018-11-26 12:39:35 +00:00
|
|
|
if tgt[model.AddressLabel] != "mesos-slave1:12345" {
|
2017-03-18 20:10:44 +00:00
|
|
|
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "yes" {
|
|
|
|
t.Fatalf("Wrong first portMappings label from the first port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
|
|
t.Fatalf("Wrong first portDefinitions label from the first port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
tgt = tg.Targets[1]
|
|
|
|
if tgt[model.AddressLabel] != "mesos-slave1:32000" {
|
|
|
|
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
|
|
|
t.Fatalf("Wrong portMappings label from the second port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
|
|
t.Fatalf("Wrong portDefinitions label from the second port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Did not get a target group.")
|
|
|
|
}
|
|
|
|
}
|
2018-09-21 10:53:04 +00:00
|
|
|
|
|
|
|
func marathonTestAppListWithDockerContainerPortMappings(labels map[string]string, runningTasks int) *AppList {
|
|
|
|
var (
|
|
|
|
task = Task{
|
|
|
|
ID: "test-task-1",
|
|
|
|
Host: "mesos-slave1",
|
2018-11-26 12:39:35 +00:00
|
|
|
Ports: []uint32{
|
|
|
|
31000,
|
|
|
|
12345, // 'Automatically-generated' port
|
|
|
|
},
|
2018-09-21 10:53:04 +00:00
|
|
|
}
|
|
|
|
docker = DockerContainer{
|
|
|
|
Image: "repo/image:tag",
|
|
|
|
PortMappings: []PortMapping{
|
2018-11-26 12:39:35 +00:00
|
|
|
{Labels: labels, HostPort: 31000},
|
|
|
|
{Labels: make(map[string]string), HostPort: 0},
|
2018-09-21 10:53:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
container = Container{
|
|
|
|
Docker: docker,
|
|
|
|
}
|
|
|
|
app = App{
|
|
|
|
ID: "test-service",
|
|
|
|
Tasks: []Task{task},
|
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return &AppList{
|
|
|
|
Apps: []App{app},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonSDSendGroupWithDockerContainerPortMappings(t *testing.T) {
|
|
|
|
var (
|
|
|
|
ch = make(chan []*targetgroup.Group, 1)
|
|
|
|
client = func(client *http.Client, url string) (*AppList, error) {
|
|
|
|
return marathonTestAppListWithDockerContainerPortMappings(marathonValidLabel, 1), nil
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if err := testUpdateServices(client, ch); err != nil {
|
|
|
|
t.Fatalf("Got error: %s", err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case tgs := <-ch:
|
|
|
|
tg := tgs[0]
|
|
|
|
|
|
|
|
if tg.Source != "test-service" {
|
|
|
|
t.Fatalf("Wrong target group name: %s", tg.Source)
|
|
|
|
}
|
|
|
|
if len(tg.Targets) != 2 {
|
|
|
|
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
|
|
}
|
|
|
|
tgt := tg.Targets[0]
|
|
|
|
if tgt[model.AddressLabel] != "mesos-slave1:31000" {
|
|
|
|
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "yes" {
|
|
|
|
t.Fatalf("Wrong first portMappings label from the first port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
|
|
t.Fatalf("Wrong first portDefinitions label from the first port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
tgt = tg.Targets[1]
|
2018-11-26 12:39:35 +00:00
|
|
|
if tgt[model.AddressLabel] != "mesos-slave1:12345" {
|
2018-09-21 10:53:04 +00:00
|
|
|
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
|
|
|
t.Fatalf("Wrong portMappings label from the second port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
|
|
t.Fatalf("Wrong portDefinitions label from the second port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Did not get a target group.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func marathonTestAppListWithContainerNetworkAndPortMappings(labels map[string]string, runningTasks int) *AppList {
|
|
|
|
var (
|
|
|
|
task = Task{
|
|
|
|
ID: "test-task-1",
|
|
|
|
Host: "mesos-slave1",
|
|
|
|
IPAddresses: []IPAddress{
|
|
|
|
{Address: "1.2.3.4"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
docker = DockerContainer{
|
|
|
|
Image: "repo/image:tag",
|
|
|
|
}
|
|
|
|
portMappings = []PortMapping{
|
2018-11-26 12:39:35 +00:00
|
|
|
{Labels: labels, ContainerPort: 8080, HostPort: 31000},
|
|
|
|
{Labels: make(map[string]string), ContainerPort: 1234, HostPort: 32000},
|
2018-09-21 10:53:04 +00:00
|
|
|
}
|
|
|
|
container = Container{
|
|
|
|
Docker: docker,
|
|
|
|
PortMappings: portMappings,
|
|
|
|
}
|
|
|
|
networks = []Network{
|
|
|
|
{Mode: "container", Name: "test-network"},
|
|
|
|
}
|
|
|
|
app = App{
|
|
|
|
ID: "test-service",
|
|
|
|
Tasks: []Task{task},
|
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
|
|
|
Networks: networks,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return &AppList{
|
|
|
|
Apps: []App{app},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonSDSendGroupWithContainerNetworkAndPortMapping(t *testing.T) {
|
|
|
|
var (
|
|
|
|
ch = make(chan []*targetgroup.Group, 1)
|
|
|
|
client = func(client *http.Client, url string) (*AppList, error) {
|
|
|
|
return marathonTestAppListWithContainerNetworkAndPortMappings(marathonValidLabel, 1), nil
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if err := testUpdateServices(client, ch); err != nil {
|
|
|
|
t.Fatalf("Got error: %s", err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case tgs := <-ch:
|
|
|
|
tg := tgs[0]
|
|
|
|
|
|
|
|
if tg.Source != "test-service" {
|
|
|
|
t.Fatalf("Wrong target group name: %s", tg.Source)
|
|
|
|
}
|
|
|
|
if len(tg.Targets) != 2 {
|
|
|
|
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
|
|
}
|
|
|
|
tgt := tg.Targets[0]
|
|
|
|
if tgt[model.AddressLabel] != "1.2.3.4:8080" {
|
|
|
|
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "yes" {
|
|
|
|
t.Fatalf("Wrong first portMappings label from the first port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
|
|
t.Fatalf("Wrong first portDefinitions label from the first port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
tgt = tg.Targets[1]
|
|
|
|
if tgt[model.AddressLabel] != "1.2.3.4:1234" {
|
|
|
|
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
|
|
|
t.Fatalf("Wrong portMappings label from the second port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
|
|
t.Fatalf("Wrong portDefinitions label from the second port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Did not get a target group.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func marathonTestAppListWithContainerNetworkAndPortDefinition(labels map[string]string, runningTasks int) *AppList {
|
|
|
|
var (
|
|
|
|
task = Task{
|
|
|
|
ID: "test-task-1",
|
|
|
|
Host: "mesos-slave1",
|
|
|
|
IPAddresses: []IPAddress{
|
|
|
|
{Address: "1.2.3.4"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
docker = DockerContainer{
|
|
|
|
Image: "repo/image:tag",
|
|
|
|
}
|
|
|
|
portDefinitions = []PortDefinition{
|
|
|
|
{Labels: labels, Port: 8080},
|
|
|
|
{Labels: make(map[string]string), Port: 1234},
|
|
|
|
}
|
|
|
|
container = Container{
|
|
|
|
Docker: docker,
|
|
|
|
}
|
|
|
|
networks = []Network{
|
|
|
|
{Mode: "container", Name: "test-network"},
|
|
|
|
}
|
|
|
|
app = App{
|
|
|
|
ID: "test-service",
|
|
|
|
Tasks: []Task{task},
|
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
|
|
|
Networks: networks,
|
|
|
|
PortDefinitions: portDefinitions,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return &AppList{
|
|
|
|
Apps: []App{app},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonSDSendGroupWithContainerNetworkAndPortDefinition(t *testing.T) {
|
|
|
|
var (
|
|
|
|
ch = make(chan []*targetgroup.Group, 1)
|
|
|
|
client = func(client *http.Client, url string) (*AppList, error) {
|
|
|
|
return marathonTestAppListWithContainerNetworkAndPortDefinition(marathonValidLabel, 1), nil
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if err := testUpdateServices(client, ch); err != nil {
|
|
|
|
t.Fatalf("Got error: %s", err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case tgs := <-ch:
|
|
|
|
tg := tgs[0]
|
|
|
|
|
|
|
|
if tg.Source != "test-service" {
|
|
|
|
t.Fatalf("Wrong target group name: %s", tg.Source)
|
|
|
|
}
|
|
|
|
if len(tg.Targets) != 2 {
|
|
|
|
t.Fatalf("Wrong number of targets: %v", tg.Targets)
|
|
|
|
}
|
|
|
|
tgt := tg.Targets[0]
|
|
|
|
if tgt[model.AddressLabel] != "1.2.3.4:8080" {
|
|
|
|
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
|
|
|
t.Fatalf("Wrong first portMappings label from the first port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "yes" {
|
|
|
|
t.Fatalf("Wrong first portDefinitions label from the first port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
tgt = tg.Targets[1]
|
|
|
|
if tgt[model.AddressLabel] != "1.2.3.4:1234" {
|
|
|
|
t.Fatalf("Wrong target address: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portMappingLabelPrefix+"prometheus")] != "" {
|
|
|
|
t.Fatalf("Wrong portMappings label from the second port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
if tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")] != "" {
|
|
|
|
t.Fatalf("Wrong portDefinitions label from the second port: %s", tgt[model.AddressLabel])
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
t.Fatal("Did not get a target group.")
|
|
|
|
}
|
|
|
|
}
|