Skip to content

Commit 2ba795f

Browse files
committed
feat: add support of PostgreSQL schemas
1 parent 93e4664 commit 2ba795f

16 files changed

+1495
-0
lines changed

PROJECT

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,12 @@ resources:
2424
kind: PostgresRole
2525
path: github.com/hoppscale/managed-postgres-operator/api/v1alpha1
2626
version: v1alpha1
27+
- api:
28+
crdVersion: v1
29+
namespaced: true
30+
controller: true
31+
domain: managed-postgres-operator.hoppscale.com
32+
kind: PostgresSchema
33+
path: github.com/hoppscale/managed-postgres-operator/api/v1alpha1
34+
version: v1alpha1
2735
version: "3"
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
// PostgresSchemaPrivilegesSpec defines the desired schema privileges to grant to roles
24+
type PostgresSchemaPrivilegesSpec struct {
25+
Create bool `json:"create,omitempty"`
26+
Usage bool `json:"usage,omitempty"`
27+
}
28+
29+
// PostgresSchemaSpec defines the desired state of a PostgreSQL schema
30+
type PostgresSchemaSpec struct {
31+
// Database is the PostgreSQL database's name in which the schema exists
32+
// +kubebuilder:validation:Required
33+
// +kubebuilder:validation:XValidation:message="database is immutable",rule="self == oldSelf"
34+
Database string `json:"database"`
35+
36+
// Name is the PostgreSQL schema's name
37+
// +kubebuilder:validation:Required
38+
// +kubebuilder:validation:XValidation:message="name is immutable",rule="self == oldSelf"
39+
Name string `json:"name"`
40+
41+
// Owner is the PostgreSQL schema's owner. It must be a valid existing role.
42+
Owner string `json:"owner,omitempty"`
43+
44+
// PrivilegesByRole will grant privileges to roles on this schema
45+
PrivilegesByRole map[string]PostgresSchemaPrivilegesSpec `json:"privilegesByRole,omitempty"`
46+
}
47+
48+
// PostgresSchemaStatus defines the observed state of PostgresSchema.
49+
type PostgresSchemaStatus struct {
50+
Succeeded bool `json:"succeeded"`
51+
}
52+
53+
// +kubebuilder:object:root=true
54+
// +kubebuilder:subresource:status
55+
56+
// PostgresSchema is the Schema for the postgresschemas API.
57+
type PostgresSchema struct {
58+
metav1.TypeMeta `json:",inline"`
59+
metav1.ObjectMeta `json:"metadata,omitempty"`
60+
61+
Spec PostgresSchemaSpec `json:"spec,omitempty"`
62+
Status PostgresSchemaStatus `json:"status,omitempty"`
63+
}
64+
65+
// +kubebuilder:object:root=true
66+
67+
// PostgresSchemaList contains a list of PostgresSchema.
68+
type PostgresSchemaList struct {
69+
metav1.TypeMeta `json:",inline"`
70+
metav1.ListMeta `json:"metadata,omitempty"`
71+
Items []PostgresSchema `json:"items"`
72+
}
73+
74+
func init() {
75+
SchemeBuilder.Register(&PostgresSchema{}, &PostgresSchemaList{})
76+
}

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 111 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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,15 @@ func main() {
228228
setupLog.Error(err, "unable to create controller", "controller", "PostgresRole")
229229
os.Exit(1)
230230
}
231+
if err = (&controller.PostgresSchemaReconciler{
232+
Client: mgr.GetClient(),
233+
Scheme: mgr.GetScheme(),
234+
PGPools: pgpools,
235+
OperatorInstanceName: operatorInstanceName,
236+
}).SetupWithManager(mgr); err != nil {
237+
setupLog.Error(err, "unable to create controller", "controller", "PostgresSchema")
238+
os.Exit(1)
239+
}
231240
// +kubebuilder:scaffold:builder
232241

233242
if metricsCertWatcher != nil {
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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: postgresschemas.managed-postgres-operator.hoppscale.com
8+
spec:
9+
group: managed-postgres-operator.hoppscale.com
10+
names:
11+
kind: PostgresSchema
12+
listKind: PostgresSchemaList
13+
plural: postgresschemas
14+
singular: postgresschema
15+
scope: Namespaced
16+
versions:
17+
- name: v1alpha1
18+
schema:
19+
openAPIV3Schema:
20+
description: PostgresSchema is the Schema for the postgresschemas 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: PostgresSchemaSpec defines the desired state of a PostgreSQL
41+
schema
42+
properties:
43+
database:
44+
description: Database is the PostgreSQL database's name in which the
45+
schema exists
46+
type: string
47+
x-kubernetes-validations:
48+
- message: database is immutable
49+
rule: self == oldSelf
50+
name:
51+
description: Name is the PostgreSQL schema's name
52+
type: string
53+
x-kubernetes-validations:
54+
- message: name is immutable
55+
rule: self == oldSelf
56+
owner:
57+
description: Owner is the PostgreSQL schema's owner. It must be a
58+
valid existing role.
59+
type: string
60+
privilegesByRole:
61+
additionalProperties:
62+
description: PostgresSchemaPrivilegesSpec defines the desired schema
63+
privileges to grant to roles
64+
properties:
65+
create:
66+
type: boolean
67+
usage:
68+
type: boolean
69+
type: object
70+
description: PrivilegesByRole will grant privileges to roles on this
71+
schema
72+
type: object
73+
required:
74+
- database
75+
- name
76+
type: object
77+
status:
78+
description: PostgresSchemaStatus defines the observed state of PostgresSchema.
79+
properties:
80+
succeeded:
81+
type: boolean
82+
required:
83+
- succeeded
84+
type: object
85+
type: object
86+
served: true
87+
storage: true
88+
subresources:
89+
status: {}

config/crd/kustomization.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
resources:
55
- bases/managed-postgres-operator.hoppscale.com_postgresdatabases.yaml
66
- bases/managed-postgres-operator.hoppscale.com_postgresroles.yaml
7+
- bases/managed-postgres-operator.hoppscale.com_postgresschemas.yaml
78
# +kubebuilder:scaffold:crdkustomizeresource
89

910
patches:

config/rbac/kustomization.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ resources:
2121
# default, aiding admins in cluster management. Those roles are
2222
# not used by the {{ .ProjectName }} itself. You can comment the following lines
2323
# if you do not want those helpers be installed with your Project.
24+
- postgresschema_admin_role.yaml
25+
- postgresschema_editor_role.yaml
26+
- postgresschema_viewer_role.yaml
2427
- postgresrole_admin_role.yaml
2528
- postgresrole_editor_role.yaml
2629
- postgresrole_viewer_role.yaml
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# This rule is not used by the project managed-postgres-operator itself.
2+
# It is provided to allow the cluster admin to help manage permissions for users.
3+
#
4+
# Grants full permissions ('*') over managed-postgres-operator.hoppscale.com.
5+
# This role is intended for users authorized to modify roles and bindings within the cluster,
6+
# enabling them to delegate specific permissions to other users or groups as needed.
7+
8+
apiVersion: rbac.authorization.k8s.io/v1
9+
kind: ClusterRole
10+
metadata:
11+
labels:
12+
app.kubernetes.io/name: managed-postgres-operator
13+
app.kubernetes.io/managed-by: kustomize
14+
name: postgresschema-admin-role
15+
rules:
16+
- apiGroups:
17+
- managed-postgres-operator.hoppscale.com
18+
resources:
19+
- postgresschemas
20+
verbs:
21+
- '*'
22+
- apiGroups:
23+
- managed-postgres-operator.hoppscale.com
24+
resources:
25+
- postgresschemas/status
26+
verbs:
27+
- get
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# This rule is not used by the project managed-postgres-operator itself.
2+
# It is provided to allow the cluster admin to help manage permissions for users.
3+
#
4+
# Grants permissions to create, update, and delete resources within the managed-postgres-operator.hoppscale.com.
5+
# This role is intended for users who need to manage these resources
6+
# but should not control RBAC or manage permissions for others.
7+
8+
apiVersion: rbac.authorization.k8s.io/v1
9+
kind: ClusterRole
10+
metadata:
11+
labels:
12+
app.kubernetes.io/name: managed-postgres-operator
13+
app.kubernetes.io/managed-by: kustomize
14+
name: postgresschema-editor-role
15+
rules:
16+
- apiGroups:
17+
- managed-postgres-operator.hoppscale.com
18+
resources:
19+
- postgresschemas
20+
verbs:
21+
- create
22+
- delete
23+
- get
24+
- list
25+
- patch
26+
- update
27+
- watch
28+
- apiGroups:
29+
- managed-postgres-operator.hoppscale.com
30+
resources:
31+
- postgresschemas/status
32+
verbs:
33+
- get

0 commit comments

Comments
 (0)