Skip to content

Commit 7644666

Browse files
authored
Merge branch 'master' into master
2 parents aba386d + 3c2516d commit 7644666

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,7 @@ Please consider sponsoring my work
2828
<a class="github-button" href="https://github.com/sponsors/hitman99" data-icon="octicon-heart" data-size="large" aria-label="Sponsor @hitman99 on GitHub">Sponsor</a>
2929

3030
### Current Sponsors
31-
32-
<p align="center">
33-
<a href="https://github.com/ElementAnalytics">
34-
<img src="https://github.com/ElementAnalytics.png" width="50px" alt="ElementAnalytics" />
35-
</a>
36-
</p>
31+
None
3732

3833
## Features
3934

@@ -76,9 +71,17 @@ These environment variables are embedded in [deploy/operator.yaml](deploy/operat
7671
* `WATCH_NAMESPACE` - which namespace to watch. Defaults to empty string for all namespaces
7772
* `OPERATOR_NAME` - name of the operator, defaults to `ext-postgres-operator`
7873
* `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)
7975

8076
`POSTGRES_INSTANCE` is only available since version 1.2.0
8177

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+
8285
## Installation
8386

8487
This operator requires a Kubernetes Secret to be created in the same namespace as operator itself.
@@ -172,7 +175,7 @@ spec:
172175
foo: "bar"
173176
```
174177

175-
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).
176179

177180
`PostgresUser` needs to reference a `Postgres` in the same namespace.
178181

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,

pkg/postgres/database.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,18 @@ func (c *pg) CreateSchema(db, role, schema string, logger logr.Logger) error {
5858
}
5959

6060
func (c *pg) DropDatabase(database string, logger logr.Logger) error {
61-
_, err := c.db.Exec(fmt.Sprintf(DROP_DATABASE, database))
61+
_, err := c.db.Exec(fmt.Sprintf(REVOKE_CONNECT, database))
62+
// Error code 3D000 is returned if database doesn't exist
63+
if err != nil && err.(*pq.Error).Code != "3D000" {
64+
return err
65+
}
66+
67+
_, err = c.db.Exec(fmt.Sprintf(TERMINATE_BACKEND, database))
68+
// Error code 3D000 is returned if database doesn't exist
69+
if err != nil && err.(*pq.Error).Code != "3D000" {
70+
return err
71+
}
72+
_, err = c.db.Exec(fmt.Sprintf(DROP_DATABASE, database))
6273
// Error code 3D000 is returned if database doesn't exist
6374
if err != nil && err.(*pq.Error).Code != "3D000" {
6475
return err

0 commit comments

Comments
 (0)