From 266ca35b5ef2d3bcb2e255521bf9eadb01cb580b Mon Sep 17 00:00:00 2001 From: Nikita Vaniasin Date: Fri, 29 Sep 2023 11:27:21 +0200 Subject: [PATCH] Allow tcp:// and ssl:// protocols in endpoints for members (#1423) --- CHANGELOG.md | 1 + pkg/exporter/monitor.go | 13 ++++--- .../endpoint.go => exporter/monitor_test.go} | 36 ++++++++++--------- 3 files changed, 29 insertions(+), 21 deletions(-) rename pkg/{util/arangod/endpoint.go => exporter/monitor_test.go} (50%) diff --git a/CHANGELOG.md b/CHANGELOG.md index adbf9eacb..bc3c2fdce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A) - (Bugfix) Fix make manifests-crd-file command +- (Improvement) Allow tcp:// and ssl:// protocols in endpoints for members ## [1.2.33](https://github.com/arangodb/kube-arangodb/tree/1.2.33) (2023-09-27) - (Maintenance) Bump golang.org/x/net to v0.13.0 diff --git a/pkg/exporter/monitor.go b/pkg/exporter/monitor.go index 6373c16f5..773628443 100644 --- a/pkg/exporter/monitor.go +++ b/pkg/exporter/monitor.go @@ -34,6 +34,7 @@ import ( "time" "github.com/arangodb/go-driver" + "github.com/arangodb/go-driver/util" "github.com/arangodb/kube-arangodb/pkg/apis/shared" "github.com/arangodb/kube-arangodb/pkg/logging" @@ -51,7 +52,7 @@ var logger = logging.Global().RegisterAndGetLogger("monitor", logging.Info) var currentMembersStatus atomic.Value func NewMonitor(arangodbEndpoint string, auth Authentication, sslVerify bool, timeout time.Duration) *monitor { - uri, err := setPath(arangodbEndpoint, shared.ArangoExporterClusterHealthEndpoint) + uri, err := prepareEndpointURL(arangodbEndpoint, shared.ArangoExporterClusterHealthEndpoint) if err != nil { logger.Err(err).Error("Fatal") os.Exit(1) @@ -75,14 +76,14 @@ func (m monitor) UpdateMonitorStatus(ctx context.Context) { health, err := m.GetClusterHealth() if err != nil { - logger.Err(err).Error("GetClusterHealth error") + logger.Err(err).Info("GetClusterHealth error") sleep = failRefreshInterval } else { var output strings.Builder for key, value := range health.Health { entry, err := m.GetMemberStatus(key, value) if err != nil { - logger.Err(err).Error("GetMemberStatus error") + logger.Err(err).Info("GetMemberStatus error") sleep = failRefreshInterval } output.WriteString(entry) @@ -134,7 +135,7 @@ func (m monitor) GetMemberStatus(id driver.ServerID, member driver.ServerHealth) return result, err } - req.URL, err = setPath(member.Endpoint, shared.ArangoExporterStatusEndpoint) + req.URL, err = prepareEndpointURL(member.Endpoint, shared.ArangoExporterStatusEndpoint) if err != nil { return result, err } @@ -155,7 +156,9 @@ func (m monitor) GetMemberStatus(id driver.ServerID, member driver.ServerHealth) return fmt.Sprintf(monitorMetricTemplate, member.Role, id, 1), nil } -func setPath(uri, uriPath string) (*url.URL, error) { +func prepareEndpointURL(uri, uriPath string) (*url.URL, error) { + uri = util.FixupEndpointURLScheme(uri) + u, err := url.Parse(uri) if err != nil { return u, err diff --git a/pkg/util/arangod/endpoint.go b/pkg/exporter/monitor_test.go similarity index 50% rename from pkg/util/arangod/endpoint.go rename to pkg/exporter/monitor_test.go index 41af9a752..c919a6bac 100644 --- a/pkg/util/arangod/endpoint.go +++ b/pkg/exporter/monitor_test.go @@ -1,7 +1,7 @@ // // DISCLAIMER // -// Copyright 2016-2022 ArangoDB GmbH, Cologne, Germany +// Copyright 2023 ArangoDB GmbH, Cologne, Germany // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -18,23 +18,27 @@ // Copyright holder is ArangoDB GmbH, Cologne, Germany // -package arangod +package exporter -import "net/url" +import ( + "testing" -// IsSameEndpoint returns true when the 2 given endpoints -// refer to the same server. -func IsSameEndpoint_(a, b string) bool { - if a == b { - return true + "github.com/stretchr/testify/require" +) + +func Test_prepareEndpointURL(t *testing.T) { + tcs := []struct { + url, path, expected string + }{ + {"http://some-host", "health", "http://some-host/health"}, + {"https://some-host", "health", "https://some-host/health"}, + {"tcp://some-host", "health", "http://some-host/health"}, + {"ssl://some-host", "health", "https://some-host/health"}, } - ua, err := url.Parse(a) - if err != nil { - return false + + for i, tc := range tcs { + u, err := prepareEndpointURL(tc.url, tc.path) + require.NoErrorf(t, err, "case %d", i) + require.Equalf(t, tc.expected, u.String(), "case %d", i) } - ub, err := url.Parse(b) - if err != nil { - return false - } - return ua.Hostname() == ub.Hostname() }