From d12ccf9fa29dbc2d0f40013f04dbe7adb8a1113e Mon Sep 17 00:00:00 2001 From: Marcin Skalski Date: Mon, 11 Dec 2023 16:33:42 +0100 Subject: [PATCH 1/5] kuma_sd: Extend Kuma SD configuration to allow users to specify ClientId Signed-off-by: Marcin Skalski --- config/config_test.go | 1 + config/testdata/conf.good.yml | 1 + config/testdata/roundtrip.good.yml | 1 + discovery/xds/kuma.go | 18 ++++++++++++++---- discovery/xds/kuma_test.go | 2 +- discovery/xds/xds.go | 1 + discovery/xds/xds_test.go | 1 + docs/configuration/configuration.md | 5 +++++ 8 files changed, 25 insertions(+), 5 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index 5d753a0f73..7c061fd548 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -568,6 +568,7 @@ var expectedConf = &Config{ ServiceDiscoveryConfigs: discovery.Configs{ &xds.KumaSDConfig{ Server: "http://kuma-control-plane.kuma-system.svc:5676", + ClientId: "main-prometheus", HTTPClientConfig: config.DefaultHTTPClientConfig, RefreshInterval: model.Duration(15 * time.Second), FetchTimeout: model.Duration(2 * time.Minute), diff --git a/config/testdata/conf.good.yml b/config/testdata/conf.good.yml index e034eff431..972099800c 100644 --- a/config/testdata/conf.good.yml +++ b/config/testdata/conf.good.yml @@ -221,6 +221,7 @@ scrape_configs: kuma_sd_configs: - server: http://kuma-control-plane.kuma-system.svc:5676 + clientId: main-prometheus - job_name: service-marathon marathon_sd_configs: diff --git a/config/testdata/roundtrip.good.yml b/config/testdata/roundtrip.good.yml index f2634d257a..26589ad1be 100644 --- a/config/testdata/roundtrip.good.yml +++ b/config/testdata/roundtrip.good.yml @@ -108,6 +108,7 @@ scrape_configs: kuma_sd_configs: - server: http://kuma-control-plane.kuma-system.svc:5676 + clientId: main-prometheus marathon_sd_configs: - servers: diff --git a/discovery/xds/kuma.go b/discovery/xds/kuma.go index bc88ba5540..1cec053e44 100644 --- a/discovery/xds/kuma.go +++ b/discovery/xds/kuma.go @@ -178,10 +178,11 @@ func kumaMadsV1ResourceParser(resources []*anypb.Any, typeURL string) ([]model.L func NewKumaHTTPDiscovery(conf *KumaSDConfig, logger log.Logger) (discovery.Discoverer, error) { // Default to "prometheus" if hostname is unavailable. - clientID, err := osutil.GetFQDN() - if err != nil { - level.Debug(logger).Log("msg", "error getting FQDN", "err", err) - clientID = "prometheus" + var clientID string + if conf.ClientId == "" { + clientID = defaultClientId(logger) + } else { + clientID = conf.ClientId } clientConfig := &HTTPResourceClientConfig{ @@ -215,3 +216,12 @@ func NewKumaHTTPDiscovery(conf *KumaSDConfig, logger log.Logger) (discovery.Disc return d, nil } + +func defaultClientId(logger log.Logger) string { + clientID, err := osutil.GetFQDN() + if err != nil { + level.Debug(logger).Log("msg", "error getting FQDN", "err", err) + clientID = "prometheus" + } + return clientID +} diff --git a/discovery/xds/kuma_test.go b/discovery/xds/kuma_test.go index 581be9fb11..7f2b0ce3b4 100644 --- a/discovery/xds/kuma_test.go +++ b/discovery/xds/kuma_test.go @@ -204,7 +204,7 @@ func TestNewKumaHTTPDiscovery(t *testing.T) { require.True(t, ok) require.Equal(t, kumaConf.Server, resClient.Server()) require.Equal(t, KumaMadsV1ResourceTypeURL, resClient.ResourceTypeURL()) - require.NotEmpty(t, resClient.ID()) + require.Equal(t, kumaConf.ClientId, resClient.ID()) require.Equal(t, KumaMadsV1ResourceType, resClient.config.ResourceType) } diff --git a/discovery/xds/xds.go b/discovery/xds/xds.go index 48bdbab02b..47baece788 100644 --- a/discovery/xds/xds.go +++ b/discovery/xds/xds.go @@ -55,6 +55,7 @@ type SDConfig struct { RefreshInterval model.Duration `yaml:"refresh_interval,omitempty"` FetchTimeout model.Duration `yaml:"fetch_timeout,omitempty"` Server string `yaml:"server,omitempty"` + ClientId string `yaml:"clientId,omitempty"` } // mustRegisterMessage registers the provided message type in the typeRegistry, and panics diff --git a/discovery/xds/xds_test.go b/discovery/xds/xds_test.go index 974a47342f..2e0f24e19b 100644 --- a/discovery/xds/xds_test.go +++ b/discovery/xds/xds_test.go @@ -36,6 +36,7 @@ var ( sdConf = SDConfig{ Server: "http://127.0.0.1", RefreshInterval: model.Duration(10 * time.Second), + ClientId: "test-id", } testFetchFailuresCount = prometheus.NewCounter( diff --git a/docs/configuration/configuration.md b/docs/configuration/configuration.md index f05925d2b1..d3ee459bea 100644 --- a/docs/configuration/configuration.md +++ b/docs/configuration/configuration.md @@ -2230,6 +2230,11 @@ See below for the configuration options for Kuma MonitoringAssignment discovery: # Address of the Kuma Control Plane's MADS xDS server. server: +# Client id is used by Kuma Control Plane to compute Monitoring Assignment for specific Prometheus backend. +# This is useful when migrating between multiple Prometheus backends, or having separate backend for each Mesh +# When not specified, system hostname/fqdn will be used if available, if not `prometheus` will be used. +clientId: + # The time to wait between polling update requests. [ refresh_interval: | default = 30s ] From 0af810aa718a9cb9a9466785715103ad6117e959 Mon Sep 17 00:00:00 2001 From: Marcin Skalski Date: Mon, 11 Dec 2023 17:01:52 +0100 Subject: [PATCH 2/5] fix go lint Signed-off-by: Marcin Skalski --- config/testdata/conf.good.yml | 2 +- config/testdata/roundtrip.good.yml | 2 +- discovery/xds/kuma.go | 8 ++++---- discovery/xds/kuma_test.go | 2 +- discovery/xds/xds.go | 2 +- discovery/xds/xds_test.go | 2 +- docs/configuration/configuration.md | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/config/testdata/conf.good.yml b/config/testdata/conf.good.yml index 972099800c..b584301649 100644 --- a/config/testdata/conf.good.yml +++ b/config/testdata/conf.good.yml @@ -221,7 +221,7 @@ scrape_configs: kuma_sd_configs: - server: http://kuma-control-plane.kuma-system.svc:5676 - clientId: main-prometheus + client_id: main-prometheus - job_name: service-marathon marathon_sd_configs: diff --git a/config/testdata/roundtrip.good.yml b/config/testdata/roundtrip.good.yml index 26589ad1be..24ab7d2592 100644 --- a/config/testdata/roundtrip.good.yml +++ b/config/testdata/roundtrip.good.yml @@ -108,7 +108,7 @@ scrape_configs: kuma_sd_configs: - server: http://kuma-control-plane.kuma-system.svc:5676 - clientId: main-prometheus + client_id: main-prometheus marathon_sd_configs: - servers: diff --git a/discovery/xds/kuma.go b/discovery/xds/kuma.go index 1cec053e44..e79195bb28 100644 --- a/discovery/xds/kuma.go +++ b/discovery/xds/kuma.go @@ -179,10 +179,10 @@ func kumaMadsV1ResourceParser(resources []*anypb.Any, typeURL string) ([]model.L func NewKumaHTTPDiscovery(conf *KumaSDConfig, logger log.Logger) (discovery.Discoverer, error) { // Default to "prometheus" if hostname is unavailable. var clientID string - if conf.ClientId == "" { - clientID = defaultClientId(logger) + if conf.ClientID == "" { + clientID = defaultClientID(logger) } else { - clientID = conf.ClientId + clientID = conf.ClientID } clientConfig := &HTTPResourceClientConfig{ @@ -217,7 +217,7 @@ func NewKumaHTTPDiscovery(conf *KumaSDConfig, logger log.Logger) (discovery.Disc return d, nil } -func defaultClientId(logger log.Logger) string { +func defaultClientID(logger log.Logger) string { clientID, err := osutil.GetFQDN() if err != nil { level.Debug(logger).Log("msg", "error getting FQDN", "err", err) diff --git a/discovery/xds/kuma_test.go b/discovery/xds/kuma_test.go index 7f2b0ce3b4..6b4bb8784b 100644 --- a/discovery/xds/kuma_test.go +++ b/discovery/xds/kuma_test.go @@ -204,7 +204,7 @@ func TestNewKumaHTTPDiscovery(t *testing.T) { require.True(t, ok) require.Equal(t, kumaConf.Server, resClient.Server()) require.Equal(t, KumaMadsV1ResourceTypeURL, resClient.ResourceTypeURL()) - require.Equal(t, kumaConf.ClientId, resClient.ID()) + require.Equal(t, kumaConf.ClientID, resClient.ID()) require.Equal(t, KumaMadsV1ResourceType, resClient.config.ResourceType) } diff --git a/discovery/xds/xds.go b/discovery/xds/xds.go index 47baece788..16aa3f1482 100644 --- a/discovery/xds/xds.go +++ b/discovery/xds/xds.go @@ -55,7 +55,7 @@ type SDConfig struct { RefreshInterval model.Duration `yaml:"refresh_interval,omitempty"` FetchTimeout model.Duration `yaml:"fetch_timeout,omitempty"` Server string `yaml:"server,omitempty"` - ClientId string `yaml:"clientId,omitempty"` + ClientID string `yaml:"client_id,omitempty"` } // mustRegisterMessage registers the provided message type in the typeRegistry, and panics diff --git a/discovery/xds/xds_test.go b/discovery/xds/xds_test.go index 2e0f24e19b..f57fff9968 100644 --- a/discovery/xds/xds_test.go +++ b/discovery/xds/xds_test.go @@ -36,7 +36,7 @@ var ( sdConf = SDConfig{ Server: "http://127.0.0.1", RefreshInterval: model.Duration(10 * time.Second), - ClientId: "test-id", + ClientID: "test-id", } testFetchFailuresCount = prometheus.NewCounter( diff --git a/docs/configuration/configuration.md b/docs/configuration/configuration.md index d3ee459bea..41f54dbd22 100644 --- a/docs/configuration/configuration.md +++ b/docs/configuration/configuration.md @@ -2233,7 +2233,7 @@ server: # Client id is used by Kuma Control Plane to compute Monitoring Assignment for specific Prometheus backend. # This is useful when migrating between multiple Prometheus backends, or having separate backend for each Mesh # When not specified, system hostname/fqdn will be used if available, if not `prometheus` will be used. -clientId: +client_id: # The time to wait between polling update requests. [ refresh_interval: | default = 30s ] From 48934aaef3c8e18b91d232a436c00c802c99a77a Mon Sep 17 00:00:00 2001 From: Marcin Skalski Date: Mon, 11 Dec 2023 17:04:56 +0100 Subject: [PATCH 3/5] fix go lint Signed-off-by: Marcin Skalski --- config/config_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config_test.go b/config/config_test.go index 7c061fd548..e614a44637 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -568,7 +568,7 @@ var expectedConf = &Config{ ServiceDiscoveryConfigs: discovery.Configs{ &xds.KumaSDConfig{ Server: "http://kuma-control-plane.kuma-system.svc:5676", - ClientId: "main-prometheus", + ClientID: "main-prometheus", HTTPClientConfig: config.DefaultHTTPClientConfig, RefreshInterval: model.Duration(15 * time.Second), FetchTimeout: model.Duration(2 * time.Minute), From e27232614a37edbc40f7778c659806a23a3dd61b Mon Sep 17 00:00:00 2001 From: Marcin Skalski Date: Tue, 12 Dec 2023 08:32:46 +0100 Subject: [PATCH 4/5] code review Signed-off-by: Marcin Skalski --- discovery/xds/kuma.go | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/discovery/xds/kuma.go b/discovery/xds/kuma.go index e79195bb28..567e5ab7cd 100644 --- a/discovery/xds/kuma.go +++ b/discovery/xds/kuma.go @@ -178,11 +178,14 @@ func kumaMadsV1ResourceParser(resources []*anypb.Any, typeURL string) ([]model.L func NewKumaHTTPDiscovery(conf *KumaSDConfig, logger log.Logger) (discovery.Discoverer, error) { // Default to "prometheus" if hostname is unavailable. - var clientID string - if conf.ClientID == "" { - clientID = defaultClientID(logger) - } else { - clientID = conf.ClientID + clientID := conf.ClientID + if clientID == "" { + var err error + clientID, err = osutil.GetFQDN() + if err != nil { + level.Debug(logger).Log("msg", "error getting FQDN", "err", err) + clientID = "prometheus" + } } clientConfig := &HTTPResourceClientConfig{ @@ -216,12 +219,3 @@ func NewKumaHTTPDiscovery(conf *KumaSDConfig, logger log.Logger) (discovery.Disc return d, nil } - -func defaultClientID(logger log.Logger) string { - clientID, err := osutil.GetFQDN() - if err != nil { - level.Debug(logger).Log("msg", "error getting FQDN", "err", err) - clientID = "prometheus" - } - return clientID -} From 19709f75d05deb8f24c69788fa7b5a4939394734 Mon Sep 17 00:00:00 2001 From: Marcin Skalski Date: Tue, 12 Dec 2023 14:49:43 +0100 Subject: [PATCH 5/5] fix kuma_sd docs Signed-off-by: Marcin Skalski --- docs/configuration/configuration.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/configuration/configuration.md b/docs/configuration/configuration.md index 41f54dbd22..5e2f31c1c6 100644 --- a/docs/configuration/configuration.md +++ b/docs/configuration/configuration.md @@ -2231,9 +2231,9 @@ See below for the configuration options for Kuma MonitoringAssignment discovery: server: # Client id is used by Kuma Control Plane to compute Monitoring Assignment for specific Prometheus backend. -# This is useful when migrating between multiple Prometheus backends, or having separate backend for each Mesh +# This is useful when migrating between multiple Prometheus backends, or having separate backend for each Mesh. # When not specified, system hostname/fqdn will be used if available, if not `prometheus` will be used. -client_id: +[ client_id: ] # The time to wait between polling update requests. [ refresh_interval: | default = 30s ]