Skip to content

Commit

Permalink
code quality suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Haydn Evans committed Dec 30, 2024
1 parent dec7e3c commit fce9192
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions pkg/scalers/postgresql_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"net"
"regexp"
"strings"
"time"
Expand All @@ -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"
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/scalers/postgresql_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Expand Down

0 comments on commit fce9192

Please sign in to comment.