From fce9192caafb818e67c725c005937b40a64c4648 Mon Sep 17 00:00:00 2001 From: Haydn Evans Date: Mon, 30 Dec 2024 14:36:02 +0100 Subject: [PATCH] code quality suggestions --- pkg/scalers/postgresql_scaler.go | 7 ++++--- pkg/scalers/postgresql_scaler_test.go | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/pkg/scalers/postgresql_scaler.go b/pkg/scalers/postgresql_scaler.go index 04ce1af2c27..2404ee4f4ad 100644 --- a/pkg/scalers/postgresql_scaler.go +++ b/pkg/scalers/postgresql_scaler.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "fmt" + "net" "regexp" "strings" "time" @@ -14,11 +15,11 @@ import ( "github.com/aws/aws-sdk-go-v2/feature/rds/auth" "github.com/go-logr/logr" _ "github.com/jackc/pgx/v5/stdlib" // PostreSQL drive required for this scaler - awsutils "github.com/kedacore/keda/v2/pkg/scalers/aws" v2 "k8s.io/api/autoscaling/v2" "k8s.io/metrics/pkg/apis/external_metrics" kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1" + awsutils "github.com/kedacore/keda/v2/pkg/scalers/aws" "github.com/kedacore/keda/v2/pkg/scalers/azure" "github.com/kedacore/keda/v2/pkg/scalers/scalersconfig" kedautil "github.com/kedacore/keda/v2/pkg/util" @@ -49,7 +50,7 @@ type postgreSQLMetadata struct { Query string `keda:"name=query, order=triggerMetadata"` triggerIndex int azureAuthContext azureAuthContext - AwsRegion string `keda:"name=awsRegion, order=triggerMetadata;authParams"` + AwsRegion string `keda:"name=awsRegion, order=triggerMetadata;authParams, optional"` awsAuthorization awsutils.AuthorizationMetadata awsAuthContext awsAuthContext @@ -202,7 +203,7 @@ func getConnection(ctx context.Context, meta *postgreSQLMetadata, podIdentity ke if err != nil { return nil, err } - DBendpoint := fmt.Sprintf("%s:%s", meta.Host, meta.Port) + DBendpoint := net.JoinHostPort(meta.Host, meta.Port) password, err := auth.BuildAuthToken(ctx, DBendpoint, meta.AwsRegion, meta.UserName, cfg.Credentials) if err != nil { return nil, err diff --git a/pkg/scalers/postgresql_scaler_test.go b/pkg/scalers/postgresql_scaler_test.go index 83fdaf354d9..fff69a10af3 100644 --- a/pkg/scalers/postgresql_scaler_test.go +++ b/pkg/scalers/postgresql_scaler_test.go @@ -98,7 +98,7 @@ var testPodIdentityAzureWorkloadPostgreSQLConnectionstring = []postgreSQLConnect var testPodIdentityAwsWorkloadPostgresSQLConnectionstring = []postgreSQLConnectionStringTestData{ // from meta - {metadata: map[string]string{"query": "test_query", "targetQueryValue": "5", "host": "localhost", "port": "1234", "dbName": "testDb", "userName": "user", "sslmode": "required"}, connectionString: "host=localhost port=1234 user=user dbname=testDb sslmode=required %PASSWORD%"}, + {metadata: map[string]string{"query": "test_query", "targetQueryValue": "5", "host": "localhost", "port": "1234", "dbName": "testDb", "userName": "user", "sslmode": "required", "awsRegion": "eu-central-1"}, connectionString: "host=localhost port=1234 user=user dbname=testDb sslmode=required %PASSWORD%"}, } func TestPodIdentityAzureWorkloadPosgresSQLConnectionStringGeneration(t *testing.T) { @@ -118,7 +118,7 @@ func TestPodIdentityAzureWorkloadPosgresSQLConnectionStringGeneration(t *testing func TestPodIdentityAWSWorkloadPosgresSQLConnectionStringGeneration(t *testing.T) { identityID := "IDENTITY_ID_CORRESPONDING_TO_USERNAME_FIELD" for _, testData := range testPodIdentityAwsWorkloadPostgresSQLConnectionstring { - meta, _, err := parsePostgreSQLMetadata(logr.Discard(), &scalersconfig.ScalerConfig{ResolvedEnv: testData.resolvedEnv, TriggerMetadata: testData.metadata, PodIdentity: kedav1alpha1.AuthPodIdentity{Provider: kedav1alpha1.PodIdentityProviderAWSWorkload, IdentityID: &identityID}, AuthParams: testData.authParam, TriggerIndex: 0}) + meta, _, err := parsePostgreSQLMetadata(logr.Discard(), &scalersconfig.ScalerConfig{ResolvedEnv: testData.resolvedEnv, TriggerMetadata: testData.metadata, PodIdentity: kedav1alpha1.AuthPodIdentity{Provider: kedav1alpha1.PodIdentityProviderAws, IdentityID: &identityID}, AuthParams: testData.authParam, TriggerIndex: 0}) if err != nil { t.Fatal("Could not parse metadata:", err) }