Skip to content

Commit 94efb8b

Browse files
authored
Merge pull request #1 from hoppscale/add-support-role
feat: add support of role
2 parents b988240 + 4d193e7 commit 94efb8b

File tree

10 files changed

+1399
-0
lines changed

10 files changed

+1399
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,22 @@ spec:
1919
keepDatabaseOnDelete: true # Should the database be kept if the Kubernetes resource is deleted?
2020
preserveConnectionsOnDelete: false # Should the operator wait until the open connections are closed before deleting the database?
2121
```
22+
23+
### PostgresRole
24+
25+
```yaml
26+
apiVersion: managed-postgres-operator.hoppscale.com/v1alpha1
27+
kind: PostgresRole
28+
metadata:
29+
name: myrole
30+
spec:
31+
name: myrole # Role's name
32+
superUser: false # Should the role be a superuser?
33+
createDB: false # Should the role be able to create databases?
34+
createRole: false # Should the role be able to create roles?
35+
inherit: false # Should the role inherit the permissions of the role of which it is a member?
36+
login: false # Should the role be able to log in?
37+
replication: false # Is the role used for replication?
38+
bypassRLS: false # Should the role bypass the defined row-level security (RLS) policies?
39+
passwordSecretName: "my-secret" # Name of the secret from where the role's password should be retrieved under the key `password`
40+
```

api/v1alpha1/postgresrole_types.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Copyright 2025.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
)
22+
23+
// PostgresRoleSpec defines the desired state of PostgresRole.
24+
type PostgresRoleSpec struct {
25+
// PostgreSQL role name
26+
// +kubebuilder:validation:Required
27+
// +kubebuilder:validation:XValidation:message="name is immutable",rule="self == oldSelf"
28+
Name string `json:"name,omitempty"`
29+
30+
SuperUser bool `json:"superUser,omitempty"`
31+
CreateDB bool `json:"createDB,omitempty"`
32+
CreateRole bool `json:"createRole,omitempty"`
33+
Inherit bool `json:"inherit,omitempty"`
34+
Login bool `json:"login,omitempty"`
35+
Replication bool `json:"replication,omitempty"`
36+
BypassRLS bool `json:"bypassRLS,omitempty"`
37+
38+
PasswordSecretName string `json:"passwordSecretName,omitempty"`
39+
}
40+
41+
// PostgresRoleStatus defines the observed state of PostgresRole.
42+
type PostgresRoleStatus struct {
43+
Succeeded bool `json:"succeeded"`
44+
}
45+
46+
// +kubebuilder:object:root=true
47+
// +kubebuilder:subresource:status
48+
49+
// PostgresRole is the Schema for the postgresroles API.
50+
type PostgresRole struct {
51+
metav1.TypeMeta `json:",inline"`
52+
metav1.ObjectMeta `json:"metadata,omitempty"`
53+
54+
Spec PostgresRoleSpec `json:"spec,omitempty"`
55+
Status PostgresRoleStatus `json:"status,omitempty"`
56+
}
57+
58+
// +kubebuilder:object:root=true
59+
60+
// PostgresRoleList contains a list of PostgresRole.
61+
type PostgresRoleList struct {
62+
metav1.TypeMeta `json:",inline"`
63+
metav1.ListMeta `json:"metadata,omitempty"`
64+
Items []PostgresRole `json:"items"`
65+
}
66+
67+
func init() {
68+
SchemeBuilder.Register(&PostgresRole{}, &PostgresRoleList{})
69+
}

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 89 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ func main() {
106106
},
107107
}
108108

109+
cacheRolePasswords := make(map[string]string)
110+
109111
// Create watchers for metrics and webhooks certificates
110112
var metricsCertWatcher, webhookCertWatcher *certwatcher.CertWatcher
111113

@@ -213,6 +215,15 @@ func main() {
213215

214216
os.Exit(1)
215217
}
218+
if err = (&controller.PostgresRoleReconciler{
219+
Client: mgr.GetClient(),
220+
Scheme: mgr.GetScheme(),
221+
PGPools: pgpools,
222+
CacheRolePasswords: cacheRolePasswords,
223+
}).SetupWithManager(mgr); err != nil {
224+
setupLog.Error(err, "unable to create controller", "controller", "PostgresRole")
225+
os.Exit(1)
226+
}
216227
// +kubebuilder:scaffold:builder
217228

218229
if metricsCertWatcher != nil {
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
apiVersion: apiextensions.k8s.io/v1
3+
kind: CustomResourceDefinition
4+
metadata:
5+
annotations:
6+
controller-gen.kubebuilder.io/version: v0.17.2
7+
name: postgresroles.managed-postgres-operator.hoppscale.com
8+
spec:
9+
group: managed-postgres-operator.hoppscale.com
10+
names:
11+
kind: PostgresRole
12+
listKind: PostgresRoleList
13+
plural: postgresroles
14+
singular: postgresrole
15+
scope: Namespaced
16+
versions:
17+
- name: v1alpha1
18+
schema:
19+
openAPIV3Schema:
20+
description: PostgresRole is the Schema for the postgresroles API.
21+
properties:
22+
apiVersion:
23+
description: |-
24+
APIVersion defines the versioned schema of this representation of an object.
25+
Servers should convert recognized schemas to the latest internal value, and
26+
may reject unrecognized values.
27+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
28+
type: string
29+
kind:
30+
description: |-
31+
Kind is a string value representing the REST resource this object represents.
32+
Servers may infer this from the endpoint the client submits requests to.
33+
Cannot be updated.
34+
In CamelCase.
35+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
36+
type: string
37+
metadata:
38+
type: object
39+
spec:
40+
description: PostgresRoleSpec defines the desired state of PostgresRole.
41+
properties:
42+
bypassRLS:
43+
type: boolean
44+
createDB:
45+
type: boolean
46+
createRole:
47+
type: boolean
48+
inherit:
49+
type: boolean
50+
login:
51+
type: boolean
52+
name:
53+
description: PostgreSQL role name
54+
type: string
55+
x-kubernetes-validations:
56+
- message: name is immutable
57+
rule: self == oldSelf
58+
passwordSecretName:
59+
type: string
60+
replication:
61+
type: boolean
62+
superUser:
63+
type: boolean
64+
required:
65+
- name
66+
type: object
67+
status:
68+
description: PostgresRoleStatus defines the observed state of PostgresRole.
69+
properties:
70+
succeeded:
71+
type: boolean
72+
required:
73+
- succeeded
74+
type: object
75+
type: object
76+
served: true
77+
storage: true
78+
subresources:
79+
status: {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: managed-postgres-operator.hoppscale.com/v1alpha1
2+
kind: PostgresRole
3+
metadata:
4+
labels:
5+
app.kubernetes.io/name: managed-postgres-operator
6+
app.kubernetes.io/managed-by: kustomize
7+
name: postgresrole-sample
8+
spec:
9+
# TODO(user): Add fields here

0 commit comments

Comments
 (0)