2016-12-28 02:16:47 +00:00
|
|
|
// Copyright 2016 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package triton
|
|
|
|
|
|
|
|
import (
|
2017-10-25 04:21:42 +00:00
|
|
|
"context"
|
2016-12-28 02:16:47 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
|
2023-10-23 13:55:36 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2018-01-11 15:10:25 +00:00
|
|
|
"github.com/prometheus/common/config"
|
2016-12-28 02:16:47 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2020-10-29 09:43:23 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2024-01-23 15:53:55 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/prometheus/discovery"
|
2016-12-28 02:16:47 +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
|
|
|
conf = SDConfig{
|
2016-12-28 02:16:47 +00:00
|
|
|
Account: "testAccount",
|
2020-05-22 15:19:21 +00:00
|
|
|
Role: "container",
|
2016-12-28 02:16:47 +00:00
|
|
|
DNSSuffix: "triton.example.com",
|
|
|
|
Endpoint: "127.0.0.1",
|
|
|
|
Port: 443,
|
|
|
|
Version: 1,
|
|
|
|
RefreshInterval: 1,
|
|
|
|
TLSConfig: config.TLSConfig{InsecureSkipVerify: true},
|
|
|
|
}
|
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
|
|
|
badconf = SDConfig{
|
2016-12-28 02:16:47 +00:00
|
|
|
Account: "badTestAccount",
|
2020-05-22 15:19:21 +00:00
|
|
|
Role: "container",
|
2016-12-28 02:16:47 +00:00
|
|
|
DNSSuffix: "bad.triton.example.com",
|
|
|
|
Endpoint: "127.0.0.1",
|
|
|
|
Port: 443,
|
|
|
|
Version: 1,
|
|
|
|
RefreshInterval: 1,
|
|
|
|
TLSConfig: config.TLSConfig{
|
|
|
|
InsecureSkipVerify: false,
|
|
|
|
KeyFile: "shouldnotexist.key",
|
|
|
|
CAFile: "shouldnotexist.ca",
|
|
|
|
CertFile: "shouldnotexist.cert",
|
|
|
|
},
|
|
|
|
}
|
2018-10-10 09:03:34 +00:00
|
|
|
groupsconf = SDConfig{
|
|
|
|
Account: "testAccount",
|
2020-05-22 15:19:21 +00:00
|
|
|
Role: "container",
|
2018-10-10 09:03:34 +00:00
|
|
|
DNSSuffix: "triton.example.com",
|
|
|
|
Endpoint: "127.0.0.1",
|
|
|
|
Groups: []string{"foo", "bar"},
|
|
|
|
Port: 443,
|
|
|
|
Version: 1,
|
|
|
|
RefreshInterval: 1,
|
|
|
|
TLSConfig: config.TLSConfig{InsecureSkipVerify: true},
|
|
|
|
}
|
2020-05-22 15:19:21 +00:00
|
|
|
cnconf = SDConfig{
|
|
|
|
Account: "testAccount",
|
|
|
|
Role: "cn",
|
|
|
|
DNSSuffix: "triton.example.com",
|
|
|
|
Endpoint: "127.0.0.1",
|
|
|
|
Port: 443,
|
|
|
|
Version: 1,
|
|
|
|
RefreshInterval: 1,
|
|
|
|
TLSConfig: config.TLSConfig{InsecureSkipVerify: true},
|
|
|
|
}
|
2016-12-28 02:16:47 +00:00
|
|
|
)
|
|
|
|
|
2024-01-23 15:53:55 +00:00
|
|
|
func newTritonDiscovery(c SDConfig) (*Discovery, discovery.DiscovererMetrics, error) {
|
|
|
|
reg := prometheus.NewRegistry()
|
|
|
|
refreshMetrics := discovery.NewRefreshMetrics(reg)
|
|
|
|
// TODO(ptodev): Add the ability to unregister refresh metrics.
|
|
|
|
metrics := c.NewDiscovererMetrics(reg, refreshMetrics)
|
|
|
|
err := metrics.Register()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := New(nil, &c, metrics)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d, metrics, nil
|
2019-02-26 13:47:04 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 02:16:47 +00:00
|
|
|
func TestTritonSDNew(t *testing.T) {
|
2024-01-23 15:53:55 +00:00
|
|
|
td, m, err := newTritonDiscovery(conf)
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, td)
|
|
|
|
require.NotNil(t, td.client)
|
|
|
|
require.NotZero(t, td.interval)
|
|
|
|
require.NotNil(t, td.sdConfig)
|
|
|
|
require.Equal(t, conf.Account, td.sdConfig.Account)
|
|
|
|
require.Equal(t, conf.DNSSuffix, td.sdConfig.DNSSuffix)
|
|
|
|
require.Equal(t, conf.Endpoint, td.sdConfig.Endpoint)
|
|
|
|
require.Equal(t, conf.Port, td.sdConfig.Port)
|
2024-01-23 15:53:55 +00:00
|
|
|
m.Unregister()
|
2016-12-28 02:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTritonSDNewBadConfig(t *testing.T) {
|
2024-01-23 15:53:55 +00:00
|
|
|
td, _, err := newTritonDiscovery(badconf)
|
2020-10-29 09:43:23 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Nil(t, td)
|
2016-12-28 02:16:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 09:03:34 +00:00
|
|
|
func TestTritonSDNewGroupsConfig(t *testing.T) {
|
2024-01-23 15:53:55 +00:00
|
|
|
td, m, err := newTritonDiscovery(groupsconf)
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, td)
|
|
|
|
require.NotNil(t, td.client)
|
|
|
|
require.NotZero(t, td.interval)
|
|
|
|
require.NotNil(t, td.sdConfig)
|
|
|
|
require.Equal(t, groupsconf.Account, td.sdConfig.Account)
|
|
|
|
require.Equal(t, groupsconf.DNSSuffix, td.sdConfig.DNSSuffix)
|
|
|
|
require.Equal(t, groupsconf.Endpoint, td.sdConfig.Endpoint)
|
|
|
|
require.Equal(t, groupsconf.Groups, td.sdConfig.Groups)
|
|
|
|
require.Equal(t, groupsconf.Port, td.sdConfig.Port)
|
2024-01-23 15:53:55 +00:00
|
|
|
m.Unregister()
|
2018-10-10 09:03:34 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 15:19:21 +00:00
|
|
|
func TestTritonSDNewCNConfig(t *testing.T) {
|
2024-01-23 15:53:55 +00:00
|
|
|
td, m, err := newTritonDiscovery(cnconf)
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, td)
|
|
|
|
require.NotNil(t, td.client)
|
|
|
|
require.NotZero(t, td.interval)
|
|
|
|
require.NotZero(t, td.sdConfig)
|
|
|
|
require.Equal(t, cnconf.Role, td.sdConfig.Role)
|
|
|
|
require.Equal(t, cnconf.Account, td.sdConfig.Account)
|
|
|
|
require.Equal(t, cnconf.DNSSuffix, td.sdConfig.DNSSuffix)
|
|
|
|
require.Equal(t, cnconf.Endpoint, td.sdConfig.Endpoint)
|
|
|
|
require.Equal(t, cnconf.Port, td.sdConfig.Port)
|
2024-01-23 15:53:55 +00:00
|
|
|
m.Unregister()
|
2020-05-22 15:19:21 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 02:16:47 +00:00
|
|
|
func TestTritonSDRefreshNoTargets(t *testing.T) {
|
2020-05-22 15:19:21 +00:00
|
|
|
tgts := testTritonSDRefresh(t, conf, "{\"containers\":[]}")
|
2020-10-29 09:43:23 +00:00
|
|
|
require.Nil(t, tgts)
|
2016-12-28 02:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTritonSDRefreshMultipleTargets(t *testing.T) {
|
2021-10-22 08:06:44 +00:00
|
|
|
dstr := `{"containers":[
|
2016-12-28 02:16:47 +00:00
|
|
|
{
|
2018-10-10 09:03:34 +00:00
|
|
|
"groups":["foo","bar","baz"],
|
2016-12-28 02:16:47 +00:00
|
|
|
"server_uuid":"44454c4c-5000-104d-8037-b7c04f5a5131",
|
|
|
|
"vm_alias":"server01",
|
2017-04-06 21:09:23 +00:00
|
|
|
"vm_brand":"lx",
|
2016-12-28 02:16:47 +00:00
|
|
|
"vm_image_uuid":"7b27a514-89d7-11e6-bee6-3f96f367bee7",
|
|
|
|
"vm_uuid":"ad466fbf-46a2-4027-9b64-8d3cdb7e9072"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"server_uuid":"a5894692-bd32-4ca1-908a-e2dda3c3a5e6",
|
|
|
|
"vm_alias":"server02",
|
2017-04-06 21:09:23 +00:00
|
|
|
"vm_brand":"kvm",
|
2016-12-28 02:16:47 +00:00
|
|
|
"vm_image_uuid":"a5894692-bd32-4ca1-908a-e2dda3c3a5e6",
|
|
|
|
"vm_uuid":"7b27a514-89d7-11e6-bee6-3f96f367bee7"
|
|
|
|
}]
|
|
|
|
}`
|
|
|
|
|
2020-05-22 15:19:21 +00:00
|
|
|
tgts := testTritonSDRefresh(t, conf, dstr)
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NotNil(t, tgts)
|
2023-12-07 11:35:01 +00:00
|
|
|
require.Len(t, tgts, 2)
|
2016-12-28 02:16:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTritonSDRefreshNoServer(t *testing.T) {
|
2024-01-23 15:53:55 +00:00
|
|
|
td, m, _ := newTritonDiscovery(conf)
|
2016-12-28 02:16:47 +00:00
|
|
|
|
2019-02-26 13:47:04 +00:00
|
|
|
_, err := td.refresh(context.Background())
|
2024-10-06 16:35:29 +00:00
|
|
|
require.ErrorContains(t, err, "an error occurred when requesting targets from the discovery endpoint")
|
2024-01-23 15:53:55 +00:00
|
|
|
m.Unregister()
|
2019-02-26 13:47:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTritonSDRefreshCancelled(t *testing.T) {
|
2024-01-23 15:53:55 +00:00
|
|
|
td, m, _ := newTritonDiscovery(conf)
|
2019-02-26 13:47:04 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
|
|
|
_, err := td.refresh(ctx)
|
2024-10-06 16:35:29 +00:00
|
|
|
require.ErrorContains(t, err, context.Canceled.Error())
|
2024-01-23 15:53:55 +00:00
|
|
|
m.Unregister()
|
2016-12-28 02:16:47 +00:00
|
|
|
}
|
|
|
|
|
2020-05-22 15:19:21 +00:00
|
|
|
func TestTritonSDRefreshCNsUUIDOnly(t *testing.T) {
|
2021-10-22 08:06:44 +00:00
|
|
|
dstr := `{"cns":[
|
2020-05-22 15:19:21 +00:00
|
|
|
{
|
|
|
|
"server_uuid":"44454c4c-5000-104d-8037-b7c04f5a5131"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"server_uuid":"a5894692-bd32-4ca1-908a-e2dda3c3a5e6"
|
|
|
|
}]
|
|
|
|
}`
|
|
|
|
|
|
|
|
tgts := testTritonSDRefresh(t, cnconf, dstr)
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NotNil(t, tgts)
|
2023-12-07 11:35:01 +00:00
|
|
|
require.Len(t, tgts, 2)
|
2020-05-22 15:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTritonSDRefreshCNsWithHostname(t *testing.T) {
|
2021-10-22 08:06:44 +00:00
|
|
|
dstr := `{"cns":[
|
2020-05-22 15:19:21 +00:00
|
|
|
{
|
|
|
|
"server_uuid":"44454c4c-5000-104d-8037-b7c04f5a5131",
|
|
|
|
"server_hostname": "server01"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"server_uuid":"a5894692-bd32-4ca1-908a-e2dda3c3a5e6",
|
|
|
|
"server_hostname": "server02"
|
|
|
|
}]
|
|
|
|
}`
|
|
|
|
|
|
|
|
tgts := testTritonSDRefresh(t, cnconf, dstr)
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NotNil(t, tgts)
|
2023-12-07 11:35:01 +00:00
|
|
|
require.Len(t, tgts, 2)
|
2020-05-22 15:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testTritonSDRefresh(t *testing.T, c SDConfig, dstr string) []model.LabelSet {
|
|
|
|
var (
|
2024-01-23 15:53:55 +00:00
|
|
|
td, m, _ = newTritonDiscovery(c)
|
|
|
|
s = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2016-12-28 02:16:47 +00:00
|
|
|
fmt.Fprintln(w, dstr)
|
|
|
|
}))
|
|
|
|
)
|
|
|
|
|
|
|
|
defer s.Close()
|
|
|
|
|
2019-02-26 13:47:04 +00:00
|
|
|
u, err := url.Parse(s.URL)
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, u)
|
2016-12-28 02:16:47 +00:00
|
|
|
|
2019-02-26 13:47:04 +00:00
|
|
|
host, strport, err := net.SplitHostPort(u.Host)
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, host)
|
|
|
|
require.NotEmpty(t, strport)
|
2016-12-28 02:16:47 +00:00
|
|
|
|
2019-02-26 13:47:04 +00:00
|
|
|
port, err := strconv.Atoi(strport)
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotZero(t, port)
|
2016-12-28 02:16:47 +00:00
|
|
|
|
|
|
|
td.sdConfig.Port = port
|
|
|
|
|
2019-03-25 10:54:22 +00:00
|
|
|
tgs, err := td.refresh(context.Background())
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-07 11:35:01 +00:00
|
|
|
require.Len(t, tgs, 1)
|
2019-03-25 10:54:22 +00:00
|
|
|
tg := tgs[0]
|
2020-10-29 09:43:23 +00:00
|
|
|
require.NotNil(t, tg)
|
2016-12-28 02:16:47 +00:00
|
|
|
|
2024-01-23 15:53:55 +00:00
|
|
|
m.Unregister()
|
|
|
|
|
2016-12-28 02:16:47 +00:00
|
|
|
return tg.Targets
|
|
|
|
}
|