2015-08-17 10:49:10 -07:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-04-25 06:30:11 -07:00
|
|
|
package marathon
|
2015-07-16 05:02:07 -07:00
|
|
|
|
|
|
|
import (
|
2017-10-24 21:21:42 -07:00
|
|
|
"context"
|
2015-07-16 05:02:07 -07:00
|
|
|
"errors"
|
2018-04-17 01:28:06 -07:00
|
|
|
"io"
|
2016-09-29 05:57:28 -07:00
|
|
|
"net/http"
|
2018-04-17 01:28:06 -07:00
|
|
|
"net/http/httptest"
|
2015-07-16 05:02:07 -07:00
|
|
|
"testing"
|
|
|
|
|
2023-10-23 06:55:36 -07:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2015-08-20 08:18:46 -07:00
|
|
|
"github.com/prometheus/common/model"
|
2024-01-23 07:53:55 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-22 02:00:08 -07:00
|
|
|
|
2024-01-23 07:53:55 -08:00
|
|
|
"github.com/prometheus/prometheus/discovery"
|
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"
|
2015-07-16 05:02:07 -07:00
|
|
|
)
|
|
|
|
|
2016-06-14 02:35:21 -07:00
|
|
|
var (
|
|
|
|
marathonValidLabel = map[string]string{"prometheus": "yes"}
|
|
|
|
testServers = []string{"http://localhost:8080"}
|
|
|
|
)
|
2015-07-16 05:02:07 -07:00
|
|
|
|
2021-10-25 00:19:18 -07:00
|
|
|
func testConfig() SDConfig {
|
|
|
|
return SDConfig{Servers: testServers}
|
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
func testUpdateServices(client appListClient) ([]*targetgroup.Group, error) {
|
2024-01-23 07:53:55 -08:00
|
|
|
cfg := testConfig()
|
|
|
|
|
|
|
|
reg := prometheus.NewRegistry()
|
|
|
|
refreshMetrics := discovery.NewRefreshMetrics(reg)
|
|
|
|
metrics := cfg.NewDiscovererMetrics(reg, refreshMetrics)
|
|
|
|
err := metrics.Register()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer metrics.Unregister()
|
|
|
|
defer refreshMetrics.Unregister()
|
|
|
|
|
|
|
|
md, err := NewDiscovery(cfg, nil, metrics)
|
2016-09-29 05:57:28 -07:00
|
|
|
if err != nil {
|
2019-03-25 03:54:22 -07:00
|
|
|
return nil, err
|
2016-04-25 06:30:11 -07:00
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
if client != nil {
|
|
|
|
md.appsClient = client
|
|
|
|
}
|
|
|
|
return md.refresh(context.Background())
|
2015-07-16 05:02:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonSDHandleError(t *testing.T) {
|
2016-06-14 02:35:21 -07:00
|
|
|
var (
|
|
|
|
errTesting = errors.New("testing failure")
|
2019-03-25 03:54:22 -07:00
|
|
|
client = func(_ context.Context, _ *http.Client, _ string) (*appList, error) {
|
|
|
|
return nil, errTesting
|
|
|
|
}
|
2016-06-14 02:35:21 -07:00
|
|
|
)
|
2019-03-25 03:54:22 -07:00
|
|
|
tgs, err := testUpdateServices(client)
|
2021-09-03 06:14:25 -07:00
|
|
|
require.ErrorIs(t, err, errTesting)
|
|
|
|
require.Empty(t, tgs, "Expected no target groups.")
|
2015-07-16 05:02:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonSDEmptyList(t *testing.T) {
|
2021-10-22 01:06:44 -07:00
|
|
|
client := func(_ context.Context, _ *http.Client, _ string) (*appList, error) { return &appList{}, nil }
|
2019-03-25 03:54:22 -07:00
|
|
|
tgs, err := testUpdateServices(client)
|
2021-09-03 06:14:25 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, tgs, "Expected no target groups.")
|
2015-07-16 05:02:07 -07:00
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
func marathonTestAppList(labels map[string]string, runningTasks int) *appList {
|
2016-06-14 02:35:21 -07:00
|
|
|
var (
|
2019-03-25 03:54:22 -07:00
|
|
|
t = task{
|
2018-09-21 03:53:04 -07:00
|
|
|
ID: "test-task-1",
|
|
|
|
Host: "mesos-slave1",
|
2016-06-14 02:35:21 -07:00
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
docker = dockerContainer{
|
2017-03-18 13:10:44 -07:00
|
|
|
Image: "repo/image:tag",
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
portMappings = []portMapping{
|
2018-11-26 04:39:35 -08:00
|
|
|
{Labels: labels, HostPort: 31000},
|
2018-09-21 03:53:04 -07:00
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
container = container{Docker: docker, PortMappings: portMappings}
|
|
|
|
a = app{
|
2016-06-14 02:35:21 -07:00
|
|
|
ID: "test-service",
|
2019-03-25 03:54:22 -07:00
|
|
|
Tasks: []task{t},
|
2016-06-14 02:35:21 -07:00
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
|
|
|
}
|
|
|
|
)
|
2019-03-25 03:54:22 -07:00
|
|
|
return &appList{
|
|
|
|
Apps: []app{a},
|
2015-07-16 05:02:07 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonSDSendGroup(t *testing.T) {
|
2021-10-22 01:06:44 -07:00
|
|
|
client := func(_ context.Context, _ *http.Client, _ string) (*appList, error) {
|
|
|
|
return marathonTestAppList(marathonValidLabel, 1), nil
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
tgs, err := testUpdateServices(client)
|
2021-09-03 06:14:25 -07:00
|
|
|
require.NoError(t, err)
|
2024-02-01 06:57:11 -08:00
|
|
|
require.Len(t, tgs, 1, "Expected 1 target group.")
|
2016-06-14 02:35:21 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tg := tgs[0]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "test-service", tg.Source, "Wrong target group name.")
|
2024-02-01 06:57:11 -08:00
|
|
|
require.Len(t, tg.Targets, 1, "Expected 1 target.")
|
2019-03-25 03:54:22 -07:00
|
|
|
|
|
|
|
tgt := tg.Targets[0]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "mesos-slave1:31000", string(tgt[model.AddressLabel]), "Wrong target address.")
|
|
|
|
require.Equal(t, "yes", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the first port.")
|
2015-07-16 05:02:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonSDRemoveApp(t *testing.T) {
|
2024-01-23 07:53:55 -08:00
|
|
|
cfg := testConfig()
|
|
|
|
reg := prometheus.NewRegistry()
|
|
|
|
refreshMetrics := discovery.NewRefreshMetrics(reg)
|
|
|
|
metrics := cfg.NewDiscovererMetrics(reg, refreshMetrics)
|
|
|
|
require.NoError(t, metrics.Register())
|
|
|
|
defer metrics.Unregister()
|
|
|
|
defer refreshMetrics.Unregister()
|
|
|
|
|
|
|
|
md, err := NewDiscovery(cfg, nil, metrics)
|
2021-09-03 06:14:25 -07:00
|
|
|
require.NoError(t, err)
|
2017-03-16 17:21:04 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
md.appsClient = func(_ context.Context, _ *http.Client, _ string) (*appList, error) {
|
2016-09-29 05:57:28 -07:00
|
|
|
return marathonTestAppList(marathonValidLabel, 1), nil
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
tgs, err := md.refresh(context.Background())
|
2021-09-03 06:14:25 -07:00
|
|
|
require.NoError(t, err, "Got error on first update.")
|
2024-02-01 06:57:11 -08:00
|
|
|
require.Len(t, tgs, 1, "Expected 1 targetgroup.")
|
2019-03-25 03:54:22 -07:00
|
|
|
tg1 := tgs[0]
|
2017-03-16 17:21:04 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
md.appsClient = func(_ context.Context, _ *http.Client, _ string) (*appList, error) {
|
|
|
|
return marathonTestAppList(marathonValidLabel, 0), nil
|
2017-03-16 17:21:04 -07:00
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
tgs, err = md.refresh(context.Background())
|
2021-09-03 06:14:25 -07:00
|
|
|
require.NoError(t, err, "Got error on second update.")
|
2024-02-01 06:57:11 -08:00
|
|
|
require.Len(t, tgs, 1, "Expected 1 targetgroup.")
|
2021-09-03 06:14:25 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tg2 := tgs[0]
|
2015-07-16 05:02:07 -07:00
|
|
|
|
2021-09-03 06:14:25 -07:00
|
|
|
require.NotEmpty(t, tg2.Targets, "Got a non-empty target set.")
|
2024-02-04 06:52:50 -08:00
|
|
|
require.Equal(t, tg1.Source, tg2.Source, "Source is different.")
|
2015-07-16 05:02:07 -07:00
|
|
|
}
|
2016-06-13 02:24:03 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
func marathonTestAppListWithMultiplePorts(labels map[string]string, runningTasks int) *appList {
|
2017-03-18 13:10:44 -07:00
|
|
|
var (
|
2019-03-25 03:54:22 -07:00
|
|
|
t = task{
|
2018-09-21 03:53:04 -07:00
|
|
|
ID: "test-task-1",
|
|
|
|
Host: "mesos-slave1",
|
2017-03-18 13:10:44 -07:00
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
docker = dockerContainer{
|
2017-03-18 13:10:44 -07:00
|
|
|
Image: "repo/image:tag",
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
portMappings = []portMapping{
|
2018-11-26 04:39:35 -08:00
|
|
|
{Labels: labels, HostPort: 31000},
|
|
|
|
{Labels: make(map[string]string), HostPort: 32000},
|
2018-09-21 03:53:04 -07:00
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
container = container{Docker: docker, PortMappings: portMappings}
|
|
|
|
a = app{
|
2017-03-18 13:10:44 -07:00
|
|
|
ID: "test-service",
|
2019-03-25 03:54:22 -07:00
|
|
|
Tasks: []task{t},
|
2017-03-18 13:10:44 -07:00
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
|
|
|
}
|
|
|
|
)
|
2019-03-25 03:54:22 -07:00
|
|
|
return &appList{
|
|
|
|
Apps: []app{a},
|
2017-03-18 13:10:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 03:53:04 -07:00
|
|
|
func TestMarathonSDSendGroupWithMultiplePort(t *testing.T) {
|
2021-10-22 01:06:44 -07:00
|
|
|
client := func(_ context.Context, _ *http.Client, _ string) (*appList, error) {
|
|
|
|
return marathonTestAppListWithMultiplePorts(marathonValidLabel, 1), nil
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
tgs, err := testUpdateServices(client)
|
2021-09-03 06:14:25 -07:00
|
|
|
require.NoError(t, err)
|
2024-02-01 06:57:11 -08:00
|
|
|
require.Len(t, tgs, 1, "Expected 1 target group.")
|
2021-09-03 06:14:25 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tg := tgs[0]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "test-service", tg.Source, "Wrong target group name.")
|
2024-02-01 06:57:11 -08:00
|
|
|
require.Len(t, tg.Targets, 2, "Wrong number of targets.")
|
2017-03-18 13:10:44 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tgt := tg.Targets[0]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "mesos-slave1:31000", string(tgt[model.AddressLabel]), "Wrong target address.")
|
|
|
|
require.Equal(t, "yes", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]),
|
|
|
|
"Wrong portMappings label from the first port: %s", tgt[model.AddressLabel])
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tgt = tg.Targets[1]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "mesos-slave1:32000", string(tgt[model.AddressLabel]), "Wrong target address.")
|
|
|
|
require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]),
|
|
|
|
"Wrong portMappings label from the second port: %s", tgt[model.AddressLabel])
|
2017-03-18 13:10:44 -07:00
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
func marathonTestZeroTaskPortAppList(labels map[string]string, runningTasks int) *appList {
|
2016-06-14 02:35:21 -07:00
|
|
|
var (
|
2019-03-25 03:54:22 -07:00
|
|
|
t = task{
|
2016-06-14 02:35:21 -07:00
|
|
|
ID: "test-task-2",
|
|
|
|
Host: "mesos-slave-2",
|
|
|
|
Ports: []uint32{},
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
docker = dockerContainer{Image: "repo/image:tag"}
|
|
|
|
container = container{Docker: docker}
|
|
|
|
a = app{
|
2016-06-14 02:35:21 -07:00
|
|
|
ID: "test-service-zero-ports",
|
2019-03-25 03:54:22 -07:00
|
|
|
Tasks: []task{t},
|
2016-06-14 02:35:21 -07:00
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
|
|
|
}
|
|
|
|
)
|
2019-03-25 03:54:22 -07:00
|
|
|
return &appList{
|
|
|
|
Apps: []app{a},
|
2016-06-13 02:24:03 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarathonZeroTaskPorts(t *testing.T) {
|
2021-10-22 01:06:44 -07:00
|
|
|
client := func(_ context.Context, _ *http.Client, _ string) (*appList, error) {
|
|
|
|
return marathonTestZeroTaskPortAppList(marathonValidLabel, 1), nil
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
tgs, err := testUpdateServices(client)
|
2021-09-03 06:14:25 -07:00
|
|
|
require.NoError(t, err)
|
2024-02-01 06:57:11 -08:00
|
|
|
require.Len(t, tgs, 1, "Expected 1 target group.")
|
2016-06-14 02:35:21 -07:00
|
|
|
|
2021-09-03 06:14:25 -07:00
|
|
|
tg := tgs[0]
|
|
|
|
require.Equal(t, "test-service-zero-ports", tg.Source, "Wrong target group name.")
|
|
|
|
require.Empty(t, tg.Targets, "Wrong number of targets.")
|
2016-06-13 02:24:03 -07:00
|
|
|
}
|
2017-03-18 13:10:44 -07:00
|
|
|
|
2018-04-17 01:28:06 -07:00
|
|
|
func Test500ErrorHttpResponseWithValidJSONBody(t *testing.T) {
|
|
|
|
// 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()
|
2018-11-27 08:44:29 -08:00
|
|
|
// Execute test case and validate behavior.
|
2019-03-25 03:54:22 -07:00
|
|
|
_, err := testUpdateServices(nil)
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Error(t, err, "Expected error for 5xx HTTP response from marathon server.")
|
2018-04-17 01:28:06 -07:00
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
func marathonTestAppListWithPortDefinitions(labels map[string]string, runningTasks int) *appList {
|
2017-03-18 13:10:44 -07:00
|
|
|
var (
|
2019-03-25 03:54:22 -07:00
|
|
|
t = task{
|
2018-09-21 03:53:04 -07:00
|
|
|
ID: "test-task-1",
|
|
|
|
Host: "mesos-slave1",
|
2019-01-14 09:20:22 -08:00
|
|
|
// Auto-generated ports when requirePorts is false
|
|
|
|
Ports: []uint32{1234, 5678},
|
2017-03-18 13:10:44 -07:00
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
docker = dockerContainer{
|
2017-03-18 13:10:44 -07:00
|
|
|
Image: "repo/image:tag",
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
container = container{Docker: docker}
|
|
|
|
a = app{
|
2017-03-18 13:10:44 -07:00
|
|
|
ID: "test-service",
|
2019-03-25 03:54:22 -07:00
|
|
|
Tasks: []task{t},
|
2017-03-18 13:10:44 -07:00
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
2019-03-25 03:54:22 -07:00
|
|
|
PortDefinitions: []portDefinition{
|
2018-09-21 03:53:04 -07:00
|
|
|
{Labels: make(map[string]string), Port: 31000},
|
|
|
|
{Labels: labels, Port: 32000},
|
2017-03-18 13:10:44 -07:00
|
|
|
},
|
2019-01-14 09:20:22 -08:00
|
|
|
RequirePorts: false, // default
|
2017-03-18 13:10:44 -07:00
|
|
|
}
|
|
|
|
)
|
2019-03-25 03:54:22 -07:00
|
|
|
return &appList{
|
|
|
|
Apps: []app{a},
|
2017-03-18 13:10:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 03:53:04 -07:00
|
|
|
func TestMarathonSDSendGroupWithPortDefinitions(t *testing.T) {
|
2021-10-22 01:06:44 -07:00
|
|
|
client := func(_ context.Context, _ *http.Client, _ string) (*appList, error) {
|
|
|
|
return marathonTestAppListWithPortDefinitions(marathonValidLabel, 1), nil
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
tgs, err := testUpdateServices(client)
|
2021-09-03 06:14:25 -07:00
|
|
|
require.NoError(t, err)
|
2024-02-01 06:57:11 -08:00
|
|
|
require.Len(t, tgs, 1, "Expected 1 target group.")
|
2021-09-03 06:14:25 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tg := tgs[0]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "test-service", tg.Source, "Wrong target group name.")
|
2024-02-01 06:57:11 -08:00
|
|
|
require.Len(t, tg.Targets, 2, "Wrong number of targets.")
|
2017-03-18 13:10:44 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tgt := tg.Targets[0]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "mesos-slave1:1234", string(tgt[model.AddressLabel]), "Wrong target address.")
|
|
|
|
require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]),
|
|
|
|
"Wrong portMappings label from the first port.")
|
|
|
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]),
|
|
|
|
"Wrong portDefinitions label from the first port.")
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tgt = tg.Targets[1]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "mesos-slave1:5678", string(tgt[model.AddressLabel]), "Wrong target address.")
|
2024-02-04 06:52:50 -08:00
|
|
|
require.Empty(t, tgt[model.LabelName(portMappingLabelPrefix+"prometheus")], "Wrong portMappings label from the second port.")
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "yes", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the second port.")
|
2017-03-18 13:10:44 -07:00
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
func marathonTestAppListWithPortDefinitionsRequirePorts(labels map[string]string, runningTasks int) *appList {
|
2017-03-18 13:10:44 -07:00
|
|
|
var (
|
2019-03-25 03:54:22 -07:00
|
|
|
t = task{
|
2017-03-18 13:10:44 -07:00
|
|
|
ID: "test-task-1",
|
|
|
|
Host: "mesos-slave1",
|
|
|
|
Ports: []uint32{31000, 32000},
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
docker = dockerContainer{
|
2017-03-18 13:10:44 -07:00
|
|
|
Image: "repo/image:tag",
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
container = container{Docker: docker}
|
|
|
|
a = app{
|
2017-03-18 13:10:44 -07:00
|
|
|
ID: "test-service",
|
2019-03-25 03:54:22 -07:00
|
|
|
Tasks: []task{t},
|
2017-03-18 13:10:44 -07:00
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
2019-03-25 03:54:22 -07:00
|
|
|
PortDefinitions: []portDefinition{
|
2019-01-14 09:20:22 -08:00
|
|
|
{Labels: make(map[string]string), Port: 31000},
|
|
|
|
{Labels: labels, Port: 32000},
|
|
|
|
},
|
|
|
|
RequirePorts: true,
|
2017-03-18 13:10:44 -07:00
|
|
|
}
|
|
|
|
)
|
2019-03-25 03:54:22 -07:00
|
|
|
return &appList{
|
|
|
|
Apps: []app{a},
|
2017-03-18 13:10:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 09:20:22 -08:00
|
|
|
func TestMarathonSDSendGroupWithPortDefinitionsRequirePorts(t *testing.T) {
|
2021-10-22 01:06:44 -07:00
|
|
|
client := func(_ context.Context, _ *http.Client, _ string) (*appList, error) {
|
|
|
|
return marathonTestAppListWithPortDefinitionsRequirePorts(marathonValidLabel, 1), nil
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
tgs, err := testUpdateServices(client)
|
2021-09-03 06:14:25 -07:00
|
|
|
require.NoError(t, err)
|
2024-02-01 06:57:11 -08:00
|
|
|
require.Len(t, tgs, 1, "Expected 1 target group.")
|
2021-09-03 06:14:25 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tg := tgs[0]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "test-service", tg.Source, "Wrong target group name.")
|
2024-02-01 06:57:11 -08:00
|
|
|
require.Len(t, tg.Targets, 2, "Wrong number of targets.")
|
2017-03-18 13:10:44 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tgt := tg.Targets[0]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "mesos-slave1:31000", string(tgt[model.AddressLabel]), "Wrong target address.")
|
|
|
|
require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the first port.")
|
|
|
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the first port.")
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tgt = tg.Targets[1]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "mesos-slave1:32000", string(tgt[model.AddressLabel]), "Wrong target address.")
|
|
|
|
require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the second port.")
|
|
|
|
require.Equal(t, "yes", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the second port.")
|
2017-12-05 11:10:40 -08:00
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
func marathonTestAppListWithPorts(labels map[string]string, runningTasks int) *appList {
|
2017-12-05 11:10:40 -08:00
|
|
|
var (
|
2019-03-25 03:54:22 -07:00
|
|
|
t = task{
|
2019-01-14 09:20:22 -08:00
|
|
|
ID: "test-task-1",
|
|
|
|
Host: "mesos-slave1",
|
|
|
|
Ports: []uint32{31000, 32000},
|
2017-12-05 11:10:40 -08:00
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
docker = dockerContainer{
|
2017-12-05 11:10:40 -08:00
|
|
|
Image: "repo/image:tag",
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
container = container{Docker: docker}
|
|
|
|
a = app{
|
2017-12-05 11:10:40 -08:00
|
|
|
ID: "test-service",
|
2019-03-25 03:54:22 -07:00
|
|
|
Tasks: []task{t},
|
2017-12-05 11:10:40 -08:00
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
|
|
|
}
|
|
|
|
)
|
2019-03-25 03:54:22 -07:00
|
|
|
return &appList{
|
|
|
|
Apps: []app{a},
|
2017-12-05 11:10:40 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 09:20:22 -08:00
|
|
|
func TestMarathonSDSendGroupWithPorts(t *testing.T) {
|
2021-10-22 01:06:44 -07:00
|
|
|
client := func(_ context.Context, _ *http.Client, _ string) (*appList, error) {
|
|
|
|
return marathonTestAppListWithPorts(marathonValidLabel, 1), nil
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
tgs, err := testUpdateServices(client)
|
2021-09-03 06:14:25 -07:00
|
|
|
require.NoError(t, err)
|
2024-02-01 06:57:11 -08:00
|
|
|
require.Len(t, tgs, 1, "Expected 1 target group.")
|
2021-09-03 06:14:25 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tg := tgs[0]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "test-service", tg.Source, "Wrong target group name.")
|
2024-02-01 06:57:11 -08:00
|
|
|
require.Len(t, tg.Targets, 2, "Wrong number of targets.")
|
2017-12-05 11:10:40 -08:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tgt := tg.Targets[0]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "mesos-slave1:31000", string(tgt[model.AddressLabel]), "Wrong target address.")
|
|
|
|
require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the first port.")
|
|
|
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the first port.")
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tgt = tg.Targets[1]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "mesos-slave1:32000", string(tgt[model.AddressLabel]), "Wrong target address.")
|
|
|
|
require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the second port.")
|
|
|
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the second port.")
|
2017-03-18 13:10:44 -07:00
|
|
|
}
|
2018-09-21 03:53:04 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
func marathonTestAppListWithContainerPortMappings(labels map[string]string, runningTasks int) *appList {
|
2018-09-21 03:53:04 -07:00
|
|
|
var (
|
2019-03-25 03:54:22 -07:00
|
|
|
t = task{
|
2018-09-21 03:53:04 -07:00
|
|
|
ID: "test-task-1",
|
|
|
|
Host: "mesos-slave1",
|
2018-11-26 04:39:35 -08:00
|
|
|
Ports: []uint32{
|
|
|
|
12345, // 'Automatically-generated' port
|
2019-01-14 09:20:22 -08:00
|
|
|
32000,
|
2018-11-26 04:39:35 -08:00
|
|
|
},
|
2018-09-21 03:53:04 -07:00
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
docker = dockerContainer{
|
2018-09-21 03:53:04 -07:00
|
|
|
Image: "repo/image:tag",
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
container = container{
|
2018-09-21 03:53:04 -07:00
|
|
|
Docker: docker,
|
2019-03-25 03:54:22 -07:00
|
|
|
PortMappings: []portMapping{
|
2019-01-14 09:20:22 -08:00
|
|
|
{Labels: labels, HostPort: 0},
|
|
|
|
{Labels: make(map[string]string), HostPort: 32000},
|
|
|
|
},
|
2018-09-21 03:53:04 -07:00
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
a = app{
|
2018-09-21 03:53:04 -07:00
|
|
|
ID: "test-service",
|
2019-03-25 03:54:22 -07:00
|
|
|
Tasks: []task{t},
|
2018-09-21 03:53:04 -07:00
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
|
|
|
}
|
|
|
|
)
|
2019-03-25 03:54:22 -07:00
|
|
|
return &appList{
|
|
|
|
Apps: []app{a},
|
2018-09-21 03:53:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 09:20:22 -08:00
|
|
|
func TestMarathonSDSendGroupWithContainerPortMappings(t *testing.T) {
|
2021-10-22 01:06:44 -07:00
|
|
|
client := func(_ context.Context, _ *http.Client, _ string) (*appList, error) {
|
|
|
|
return marathonTestAppListWithContainerPortMappings(marathonValidLabel, 1), nil
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
tgs, err := testUpdateServices(client)
|
2021-09-03 06:14:25 -07:00
|
|
|
require.NoError(t, err)
|
2024-02-01 06:57:11 -08:00
|
|
|
require.Len(t, tgs, 1, "Expected 1 target group.")
|
2021-09-03 06:14:25 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tg := tgs[0]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "test-service", tg.Source, "Wrong target group name.")
|
2024-02-01 06:57:11 -08:00
|
|
|
require.Len(t, tg.Targets, 2, "Wrong number of targets.")
|
2018-09-21 03:53:04 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tgt := tg.Targets[0]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "mesos-slave1:12345", string(tgt[model.AddressLabel]), "Wrong target address.")
|
|
|
|
require.Equal(t, "yes", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the first port.")
|
|
|
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the first port.")
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tgt = tg.Targets[1]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "mesos-slave1:32000", string(tgt[model.AddressLabel]), "Wrong target address.")
|
|
|
|
require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the second port.")
|
|
|
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the second port.")
|
2018-09-21 03:53:04 -07:00
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
func marathonTestAppListWithDockerContainerPortMappings(labels map[string]string, runningTasks int) *appList {
|
2018-09-21 03:53:04 -07:00
|
|
|
var (
|
2019-03-25 03:54:22 -07:00
|
|
|
t = task{
|
2018-09-21 03:53:04 -07:00
|
|
|
ID: "test-task-1",
|
|
|
|
Host: "mesos-slave1",
|
2019-01-14 09:20:22 -08:00
|
|
|
Ports: []uint32{
|
|
|
|
31000,
|
|
|
|
12345, // 'Automatically-generated' port
|
2018-09-21 03:53:04 -07:00
|
|
|
},
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
docker = dockerContainer{
|
2018-09-21 03:53:04 -07:00
|
|
|
Image: "repo/image:tag",
|
2019-03-25 03:54:22 -07:00
|
|
|
PortMappings: []portMapping{
|
2019-01-14 09:20:22 -08:00
|
|
|
{Labels: labels, HostPort: 31000},
|
|
|
|
{Labels: make(map[string]string), HostPort: 0},
|
|
|
|
},
|
2018-09-21 03:53:04 -07:00
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
container = container{
|
2019-01-14 09:20:22 -08:00
|
|
|
Docker: docker,
|
2018-09-21 03:53:04 -07:00
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
a = app{
|
2018-09-21 03:53:04 -07:00
|
|
|
ID: "test-service",
|
2019-03-25 03:54:22 -07:00
|
|
|
Tasks: []task{t},
|
2018-09-21 03:53:04 -07:00
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
|
|
|
}
|
|
|
|
)
|
2019-03-25 03:54:22 -07:00
|
|
|
return &appList{
|
|
|
|
Apps: []app{a},
|
2018-09-21 03:53:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 09:20:22 -08:00
|
|
|
func TestMarathonSDSendGroupWithDockerContainerPortMappings(t *testing.T) {
|
2021-10-22 01:06:44 -07:00
|
|
|
client := func(_ context.Context, _ *http.Client, _ string) (*appList, error) {
|
|
|
|
return marathonTestAppListWithDockerContainerPortMappings(marathonValidLabel, 1), nil
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
tgs, err := testUpdateServices(client)
|
2021-09-03 06:14:25 -07:00
|
|
|
require.NoError(t, err)
|
2024-02-01 06:57:11 -08:00
|
|
|
require.Len(t, tgs, 1, "Expected 1 target group.")
|
2021-09-03 06:14:25 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tg := tgs[0]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "test-service", tg.Source, "Wrong target group name.")
|
2024-02-01 06:57:11 -08:00
|
|
|
require.Len(t, tg.Targets, 2, "Wrong number of targets.")
|
2018-09-21 03:53:04 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tgt := tg.Targets[0]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "mesos-slave1:31000", string(tgt[model.AddressLabel]), "Wrong target address.")
|
|
|
|
require.Equal(t, "yes", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the first port.")
|
|
|
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the first port.")
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tgt = tg.Targets[1]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "mesos-slave1:12345", string(tgt[model.AddressLabel]), "Wrong target address.")
|
|
|
|
require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the second port.")
|
|
|
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the second port.")
|
2018-09-21 03:53:04 -07:00
|
|
|
}
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
func marathonTestAppListWithContainerNetworkAndPortMappings(labels map[string]string, runningTasks int) *appList {
|
2018-09-21 03:53:04 -07:00
|
|
|
var (
|
2019-03-25 03:54:22 -07:00
|
|
|
t = task{
|
2018-09-21 03:53:04 -07:00
|
|
|
ID: "test-task-1",
|
|
|
|
Host: "mesos-slave1",
|
2019-03-25 03:54:22 -07:00
|
|
|
IPAddresses: []ipAddress{
|
2018-09-21 03:53:04 -07:00
|
|
|
{Address: "1.2.3.4"},
|
|
|
|
},
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
docker = dockerContainer{
|
2018-09-21 03:53:04 -07:00
|
|
|
Image: "repo/image:tag",
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
portMappings = []portMapping{
|
2019-01-14 09:20:22 -08:00
|
|
|
{Labels: labels, ContainerPort: 8080, HostPort: 31000},
|
|
|
|
{Labels: make(map[string]string), ContainerPort: 1234, HostPort: 32000},
|
2018-09-21 03:53:04 -07:00
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
container = container{
|
2019-01-14 09:20:22 -08:00
|
|
|
Docker: docker,
|
|
|
|
PortMappings: portMappings,
|
2018-09-21 03:53:04 -07:00
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
networks = []network{
|
2018-09-21 03:53:04 -07:00
|
|
|
{Mode: "container", Name: "test-network"},
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
a = app{
|
2019-01-14 09:20:22 -08:00
|
|
|
ID: "test-service",
|
2019-03-25 03:54:22 -07:00
|
|
|
Tasks: []task{t},
|
2019-01-14 09:20:22 -08:00
|
|
|
RunningTasks: runningTasks,
|
|
|
|
Labels: labels,
|
|
|
|
Container: container,
|
|
|
|
Networks: networks,
|
2018-09-21 03:53:04 -07:00
|
|
|
}
|
|
|
|
)
|
2019-03-25 03:54:22 -07:00
|
|
|
return &appList{
|
|
|
|
Apps: []app{a},
|
2018-09-21 03:53:04 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 09:20:22 -08:00
|
|
|
func TestMarathonSDSendGroupWithContainerNetworkAndPortMapping(t *testing.T) {
|
2021-10-22 01:06:44 -07:00
|
|
|
client := func(_ context.Context, _ *http.Client, _ string) (*appList, error) {
|
|
|
|
return marathonTestAppListWithContainerNetworkAndPortMappings(marathonValidLabel, 1), nil
|
|
|
|
}
|
2019-03-25 03:54:22 -07:00
|
|
|
tgs, err := testUpdateServices(client)
|
2021-09-03 06:14:25 -07:00
|
|
|
require.NoError(t, err)
|
2024-02-01 06:57:11 -08:00
|
|
|
require.Len(t, tgs, 1, "Expected 1 target group.")
|
2021-09-03 06:14:25 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tg := tgs[0]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "test-service", tg.Source, "Wrong target group name.")
|
2024-02-01 06:57:11 -08:00
|
|
|
require.Len(t, tg.Targets, 2, "Wrong number of targets.")
|
2018-09-21 03:53:04 -07:00
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tgt := tg.Targets[0]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "1.2.3.4:8080", string(tgt[model.AddressLabel]), "Wrong target address.")
|
|
|
|
require.Equal(t, "yes", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the first port.")
|
|
|
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the first port.")
|
|
|
|
|
2019-03-25 03:54:22 -07:00
|
|
|
tgt = tg.Targets[1]
|
2021-09-03 06:14:25 -07:00
|
|
|
require.Equal(t, "1.2.3.4:1234", string(tgt[model.AddressLabel]), "Wrong target address.")
|
|
|
|
require.Equal(t, "", string(tgt[model.LabelName(portMappingLabelPrefix+"prometheus")]), "Wrong portMappings label from the second port.")
|
|
|
|
require.Equal(t, "", string(tgt[model.LabelName(portDefinitionLabelPrefix+"prometheus")]), "Wrong portDefinitions label from the second port.")
|
2018-09-21 03:53:04 -07:00
|
|
|
}
|