Skip to content

Commit b4d4124

Browse files
committed
postgresconfig: Add support for custom secret keys.
1 parent e33775a commit b4d4124

7 files changed

+53
-3
lines changed

api/v1alpha1/postgresconfig_types.go

+8
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ type PostgresConfigStatus struct {
7878
type SecretRef struct {
7979
// SecretName is the name of the secret.
8080
SecretName string `json:"secretName,omitempty"`
81+
82+
// UsernameKey is the name of the key in the secret storing the username.
83+
// The default value is POSTGRES_USER.
84+
UsernameKey string `json:"usernameKey,omitempty"`
85+
86+
// PasswordKey is the name of the key in the secret storing the password.
87+
// The default value is POSTGRES_PASSWORD.
88+
PasswordKey string `json:"passwordKey,omitempty"`
8189
}
8290

8391
//+kubebuilder:object:root=true

config/crd/bases/postgres.glints.com_postgresconfigs.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,17 @@ spec:
5353
namespace that contains credentials to authenticate against
5454
the PostgreSQL server
5555
properties:
56+
passwordKey:
57+
description: PasswordKey is the name of the key in the secret
58+
storing the password. The default value is POSTGRES_PASSWORD.
59+
type: string
5660
secretName:
5761
description: SecretName is the name of the secret.
5862
type: string
63+
usernameKey:
64+
description: UsernameKey is the name of the key in the secret
65+
storing the username. The default value is POSTGRES_USER.
66+
type: string
5967
type: object
6068
variant:
6169
description: 'Variant is for specific database-as-a-service providers.

config/crd/bases/postgres.glints.com_postgresgrants.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,17 @@ spec:
9090
namespace that contains credentials to authenticate against
9191
the PostgreSQL server
9292
properties:
93+
passwordKey:
94+
description: PasswordKey is the name of the key in the secret
95+
storing the password. The default value is POSTGRES_PASSWORD.
96+
type: string
9397
secretName:
9498
description: SecretName is the name of the secret.
9599
type: string
100+
usernameKey:
101+
description: UsernameKey is the name of the key in the secret
102+
storing the username. The default value is POSTGRES_USER.
103+
type: string
96104
type: object
97105
variant:
98106
description: 'Variant is for specific database-as-a-service providers.

config/crd/bases/postgres.glints.com_postgrespublications.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,17 @@ spec:
6565
namespace that contains credentials to authenticate against
6666
the PostgreSQL server
6767
properties:
68+
passwordKey:
69+
description: PasswordKey is the name of the key in the secret
70+
storing the password. The default value is POSTGRES_PASSWORD.
71+
type: string
6872
secretName:
6973
description: SecretName is the name of the secret.
7074
type: string
75+
usernameKey:
76+
description: UsernameKey is the name of the key in the secret
77+
storing the username. The default value is POSTGRES_USER.
78+
type: string
7179
type: object
7280
variant:
7381
description: 'Variant is for specific database-as-a-service providers.

config/crd/bases/postgres.glints.com_postgrestables.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,17 @@ spec:
8282
namespace that contains credentials to authenticate against
8383
the PostgreSQL server
8484
properties:
85+
passwordKey:
86+
description: PasswordKey is the name of the key in the secret
87+
storing the password. The default value is POSTGRES_PASSWORD.
88+
type: string
8589
secretName:
8690
description: SecretName is the name of the secret.
8791
type: string
92+
usernameKey:
93+
description: UsernameKey is the name of the key in the secret
94+
storing the username. The default value is POSTGRES_USER.
95+
type: string
8896
type: object
8997
variant:
9098
description: 'Variant is for specific database-as-a-service providers.

controllers/suite_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func SetupPostgresContainer(ctx context.Context) {
139139
BeforeEach(func() {
140140
const port = "5432/tcp"
141141
req := testcontainers.ContainerRequest{
142-
Image: "postgres:12-alpine",
142+
Image: "postgres:14-alpine",
143143
Env: map[string]string{
144144
"POSTGRES_USER": postgresUser,
145145
"POSTGRES_PASSWORD": postgresPassword,

controllers/utils/utils.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,21 @@ func SetupPostgresConnection(
4040
return nil, &ErrGetSecret{Err: err}
4141
}
4242

43+
usernameKey := "POSTGRES_USER"
44+
if len(ref.SecretRef.UsernameKey) > 0 {
45+
usernameKey = ref.SecretRef.UsernameKey
46+
}
47+
48+
passwordKey := "POSTGRES_PASSWORD"
49+
if len(ref.SecretRef.PasswordKey) > 0 {
50+
passwordKey = ref.SecretRef.PasswordKey
51+
}
52+
4353
connURL := url.URL{
4454
Scheme: "postgres",
4555
User: url.UserPassword(
46-
string(secretRef.Data["POSTGRES_USER"]),
47-
string(secretRef.Data["POSTGRES_PASSWORD"]),
56+
string(secretRef.Data[usernameKey]),
57+
string(secretRef.Data[passwordKey]),
4858
),
4959
Host: fmt.Sprintf("%s:%d", ref.Host, ref.Port),
5060
Path: fmt.Sprintf("/%s", ref.Database),

0 commit comments

Comments
 (0)