From 85f48864dc0a7e04c1dcc83339f457ee75e25cb2 Mon Sep 17 00:00:00 2001 From: Bharadwajshivam28 Date: Mon, 14 Oct 2024 15:07:04 +0530 Subject: [PATCH] modification in the logic and alignment of test according to the current method Signed-off-by: Bharadwajshivam28 --- .../common/providers/prometheus/common.go | 15 ++++----------- .../common/providers/prometheus/common_test.go | 12 ++++++++++-- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/metrics-operator/controllers/common/providers/prometheus/common.go b/metrics-operator/controllers/common/providers/prometheus/common.go index 566959c5572..c68472dc3c4 100644 --- a/metrics-operator/controllers/common/providers/prometheus/common.go +++ b/metrics-operator/controllers/common/providers/prometheus/common.go @@ -41,19 +41,12 @@ func (r RoundTripperRetriever) GetRoundTripper(ctx context.Context, provider met return nil, err } - baseTransport := promapi.DefaultRoundTripper - - if provider.Spec.InsecureSkipTlsVerify { - if httpTransport, ok := baseTransport.(*http.Transport); ok { - newTransport := httpTransport.Clone() - newTransport.TLSClientConfig = &tls.Config{ - InsecureSkipVerify: true, - } - baseTransport = newTransport - } + transport := promapi.DefaultRoundTripper.(*http.Transport).Clone() + transport.TLSClientConfig = &tls.Config{ + InsecureSkipVerify: provider.Spec.InsecureSkipTlsVerify, } - return config.NewBasicAuthRoundTripper(secret.User, secret.Password, "", "", baseTransport), nil + return config.NewBasicAuthRoundTripper(secret.User, secret.Password, "", "", transport), nil } func getPrometheusSecret(ctx context.Context, provider metricsapi.KeptnMetricsProvider, k8sClient client.Client) (*SecretData, error) { diff --git a/metrics-operator/controllers/common/providers/prometheus/common_test.go b/metrics-operator/controllers/common/providers/prometheus/common_test.go index 046a99fc059..e65a9edc3c4 100644 --- a/metrics-operator/controllers/common/providers/prometheus/common_test.go +++ b/metrics-operator/controllers/common/providers/prometheus/common_test.go @@ -2,6 +2,7 @@ package prometheus import ( "context" + "crypto/tls" "net/http" "net/http/httptest" "reflect" @@ -137,11 +138,18 @@ func Test_GetRoundtripper(t *testing.T) { Key: "", Optional: nil, }, + InsecureSkipTlsVerify: true, }, }, k8sClient: fake.NewClient(goodsecret), - want: config.NewBasicAuthRoundTripper("myuser", "mytoken", "", "", promapi.DefaultRoundTripper), - wantErr: false, + want: func() http.RoundTripper { + transport := promapi.DefaultRoundTripper.(*http.Transport).Clone() + transport.TLSClientConfig = &tls.Config{ + InsecureSkipVerify: true, + } + return config.NewBasicAuthRoundTripper("myuser", "mytoken", "", "", transport) + }(), + wantErr: false, }, { name: "TestSecretNotDefined",