Skip to content

Commit 3c2516d

Browse files
authored
Merge pull request #137 from reddec/fix/keep-secret-name
allow keeping secret name as defined in user spec
2 parents 2a872dc + d4a559b commit 3c2516d

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,17 @@ These environment variables are embedded in [deploy/operator.yaml](deploy/operat
7171
* `WATCH_NAMESPACE` - which namespace to watch. Defaults to empty string for all namespaces
7272
* `OPERATOR_NAME` - name of the operator, defaults to `ext-postgres-operator`
7373
* `POSTGRES_INSTANCE` - identity of operator, this matched with `postgres.db.movetokube.com/instance` in CRs. Default is empty
74+
* `KEEP_SECRET_NAME` - use secret name as provided by user (disabled by default)
7475

7576
`POSTGRES_INSTANCE` is only available since version 1.2.0
7677

78+
> While using `KEEP_SECRET_NAME` could be a convenient way to define secrets with predictable and explicit names,
79+
> the default logic reduces risk of operator from entering the endless reconcile loop as secret is very unlikely to exist.
80+
>
81+
> The administrator should ensure that the `SecretName` does not collide with other secrets in the same namespace.
82+
> If the secret already exists, the operator will never stop reconciling the CR until either offending secret is deleted
83+
> or CR is deleted or updated with another SecretName
84+
7785
## Installation
7886

7987
This operator requires a Kubernetes Secret to be created in the same namespace as operator itself.
@@ -167,7 +175,7 @@ spec:
167175
foo: "bar"
168176
```
169177

170-
This creates a user role `username-<hash>` and grants role `test-db-group`, `test-db-writer` or `test-db-reader` depending on `privileges` property. Its credentials are put in secret `my-secret-my-db-user`.
178+
This creates a user role `username-<hash>` and grants role `test-db-group`, `test-db-writer` or `test-db-reader` depending on `privileges` property. Its credentials are put in secret `my-secret-my-db-user` (unless `KEEP_SECRET_NAME` is enabled).
171179

172180
`PostgresUser` needs to reference a `Postgres` in the same namespace.
173181

deploy/operator.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ spec:
2525
env:
2626
- name: WATCH_NAMESPACE
2727
value: ""
28+
- name: KEEP_SECRET_NAME
29+
value: "false"
2830
- name: POD_NAME
2931
valueFrom:
3032
fieldRef:

pkg/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"net/url"
5+
"strconv"
56
"sync"
67

78
"github.com/movetokube/postgres-operator/pkg/utils"
@@ -15,6 +16,7 @@ type cfg struct {
1516
PostgresDefaultDb string
1617
CloudProvider string
1718
AnnotationFilter string
19+
KeepSecretName bool
1820
}
1921

2022
var doOnce sync.Once
@@ -30,6 +32,9 @@ func Get() *cfg {
3032
config.PostgresDefaultDb = utils.GetEnv("POSTGRES_DEFAULT_DATABASE")
3133
config.CloudProvider = utils.GetEnv("POSTGRES_CLOUD_PROVIDER")
3234
config.AnnotationFilter = utils.GetEnv("POSTGRES_INSTANCE")
35+
if value, err := strconv.ParseBool(utils.GetEnv("KEEP_SECRET_NAME")); err == nil {
36+
config.KeepSecretName = value
37+
}
3338
})
3439
return config
3540
}

pkg/controller/postgresuser/postgresuser_controller.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func newReconciler(mgr manager.Manager) reconcile.Reconciler {
5454
pg: pg,
5555
pgHost: c.PostgresHost,
5656
instanceFilter: c.AnnotationFilter,
57+
keepSecretName: c.KeepSecretName,
5758
}
5859
}
5960

@@ -98,6 +99,7 @@ type ReconcilePostgresUser struct {
9899
pg postgres.PG
99100
pgHost string
100101
instanceFilter string
102+
keepSecretName bool // use secret name as defined in PostgresUserSpec
101103
}
102104

103105
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
@@ -276,10 +278,14 @@ func (r *ReconcilePostgresUser) newSecretForCR(cr *dbv1alpha1.PostgresUser, role
276278
"app": cr.Name,
277279
}
278280
annotations := cr.Spec.Annotations
281+
name := fmt.Sprintf("%s-%s", cr.Spec.SecretName, cr.Name)
282+
if r.keepSecretName {
283+
name = cr.Spec.SecretName
284+
}
279285

280286
return &corev1.Secret{
281287
ObjectMeta: metav1.ObjectMeta{
282-
Name: fmt.Sprintf("%s-%s", cr.Spec.SecretName, cr.Name),
288+
Name: name,
283289
Namespace: cr.Namespace,
284290
Labels: labels,
285291
Annotations: annotations,

0 commit comments

Comments
 (0)