Skip to content

Commit b3eb847

Browse files
committed
postgrestable: New controller for creating tables.
Offers a way to create tables declaratively. Currently, it only supports creation of tables, it will not run ALTER TABLE commands to bring the table to the target state.
1 parent a995561 commit b3eb847

17 files changed

+766
-9
lines changed

PROJECT

+9
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,13 @@ resources:
3737
webhooks:
3838
validation: true
3939
webhookVersion: v1
40+
- api:
41+
crdVersion: v1
42+
namespaced: true
43+
controller: true
44+
domain: glints.com
45+
group: postgres
46+
kind: PostgresTable
47+
path: github.com/glints-dev/postgres-config-operator/api/v1alpha1
48+
version: v1alpha1
4049
version: "3"

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This operator is written using the [Operator SDK](https://sdk.operatorframework.
88
## Operator Features
99

1010
- Creating and maintaining [publications](https://www.postgresql.org/docs/current/sql-createpublication.html)
11+
- Creating [tables](https://www.postgresql.org/docs/current/sql-createtable.html)
1112
- (Coming soon) Creating and maintaining [roles](https://www.postgresql.org/docs/13/sql-createrole.html)
1213

1314
## Getting Started
@@ -35,3 +36,7 @@ Testing can be done locally by simply running:
3536
```
3637
make test
3738
```
39+
40+
## Examples
41+
42+
Example manifests can be found under config/samples/.

api/v1alpha1/postgrestable_types.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Copyright 2021.
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+
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
24+
25+
// PostgresTableSpec defines the desired state of PostgresTable
26+
type PostgresTableSpec struct {
27+
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
28+
// Important: Run "make" to regenerate code after modifying this file
29+
30+
// PostgresRef is a reference to the PostgreSQL server to configure
31+
PostgresRef PostgresRef `json:"postgresRef"`
32+
33+
// Columns is the list of columns to be created
34+
Columns []PostgresColumn `json:"columns"`
35+
36+
// Inherit fields from PostgresIdentifier
37+
PostgresIdentifier `json:""`
38+
}
39+
40+
// PostgresColumn represents a column
41+
type PostgresColumn struct {
42+
// Name of the column
43+
Name string `json:"name"`
44+
45+
// DataType is the data type of the column
46+
DataType string `json:"dataType"`
47+
48+
// Nullable determines whether the created column can be NULL
49+
Nullable bool `json:"nullable"`
50+
51+
// PrimaryKey defines whether the column is part of the table's primary key
52+
PrimaryKey bool `json:"primaryKey"`
53+
}
54+
55+
// PostgresTableStatus defines the observed state of PostgresTable
56+
type PostgresTableStatus struct {
57+
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
58+
// Important: Run "make" to regenerate code after modifying this file
59+
}
60+
61+
//+kubebuilder:object:root=true
62+
//+kubebuilder:subresource:status
63+
64+
// PostgresTable is the Schema for the postgrestables API
65+
type PostgresTable struct {
66+
metav1.TypeMeta `json:",inline"`
67+
metav1.ObjectMeta `json:"metadata,omitempty"`
68+
69+
Spec PostgresTableSpec `json:"spec,omitempty"`
70+
Status PostgresTableStatus `json:"status,omitempty"`
71+
}
72+
73+
//+kubebuilder:object:root=true
74+
75+
// PostgresTableList contains a list of PostgresTable
76+
type PostgresTableList struct {
77+
metav1.TypeMeta `json:",inline"`
78+
metav1.ListMeta `json:"metadata,omitempty"`
79+
Items []PostgresTable `json:"items"`
80+
}
81+
82+
func init() {
83+
SchemeBuilder.Register(&PostgresTable{}, &PostgresTableList{})
84+
}

api/v1alpha1/zz_generated.deepcopy.go

+112
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
2+
---
3+
apiVersion: apiextensions.k8s.io/v1
4+
kind: CustomResourceDefinition
5+
metadata:
6+
annotations:
7+
controller-gen.kubebuilder.io/version: v0.4.1
8+
creationTimestamp: null
9+
name: postgrestables.postgres.glints.com
10+
spec:
11+
group: postgres.glints.com
12+
names:
13+
kind: PostgresTable
14+
listKind: PostgresTableList
15+
plural: postgrestables
16+
singular: postgrestable
17+
scope: Namespaced
18+
versions:
19+
- name: v1alpha1
20+
schema:
21+
openAPIV3Schema:
22+
description: PostgresTable is the Schema for the postgrestables API
23+
properties:
24+
apiVersion:
25+
description: 'APIVersion defines the versioned schema of this representation
26+
of an object. Servers should convert recognized schemas to the latest
27+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
28+
type: string
29+
kind:
30+
description: 'Kind is a string value representing the REST resource this
31+
object represents. Servers may infer this from the endpoint the client
32+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
33+
type: string
34+
metadata:
35+
type: object
36+
spec:
37+
description: PostgresTableSpec defines the desired state of PostgresTable
38+
properties:
39+
columns:
40+
description: Columns is the list of columns to be created
41+
items:
42+
description: PostgresColumn represents a column
43+
properties:
44+
dataType:
45+
description: DataType is the data type of the column
46+
type: string
47+
name:
48+
description: Name of the column
49+
type: string
50+
nullable:
51+
description: Nullable determines whether the created column
52+
can be NULL
53+
type: boolean
54+
primaryKey:
55+
description: PrimaryKey defines whether the column is part of
56+
the table's primary key
57+
type: boolean
58+
required:
59+
- dataType
60+
- name
61+
- nullable
62+
- primaryKey
63+
type: object
64+
type: array
65+
name:
66+
description: Name is the name of the table
67+
type: string
68+
postgresRef:
69+
description: PostgresRef is a reference to the PostgreSQL server to
70+
configure
71+
properties:
72+
database:
73+
description: Database is the name of the database to configure
74+
type: string
75+
host:
76+
description: Host is the host of the PostgreSQL server to configure
77+
type: string
78+
port:
79+
description: Port is the port of the PostgreSQL server to configure
80+
type: integer
81+
secretRef:
82+
description: SecretRef is a reference to a secret in the same
83+
namespace that contains credentials to authenticate against
84+
the PostgreSQL server
85+
properties:
86+
secretName:
87+
description: SecretName is the name of the secret.
88+
type: string
89+
type: object
90+
variant:
91+
description: 'Variant is for specific database-as-a-service providers.
92+
Valid values are: aiven, standard. The default value is "standard"'
93+
type: string
94+
required:
95+
- secretRef
96+
type: object
97+
schema:
98+
description: Schema is the name of the schema the table resides in
99+
type: string
100+
required:
101+
- columns
102+
- name
103+
- postgresRef
104+
- schema
105+
type: object
106+
status:
107+
description: PostgresTableStatus defines the observed state of PostgresTable
108+
type: object
109+
type: object
110+
served: true
111+
storage: true
112+
subresources:
113+
status: {}
114+
status:
115+
acceptedNames:
116+
kind: ""
117+
plural: ""
118+
conditions: []
119+
storedVersions: []

config/crd/kustomization.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ resources:
55
- bases/postgres.glints.com_postgresconfigs.yaml
66
- bases/postgres.glints.com_postgrespublications.yaml
77
- bases/postgres.glints.com_postgresgrants.yaml
8+
- bases/postgres.glints.com_postgrestables.yaml
89
#+kubebuilder:scaffold:crdkustomizeresource
910

1011
patchesStrategicMerge:
@@ -13,13 +14,15 @@ patchesStrategicMerge:
1314
#- patches/webhook_in_postgresconfigs.yaml
1415
#- patches/webhook_in_postgrespublications.yaml
1516
- patches/webhook_in_postgresgrants.yaml
17+
#- patches/webhook_in_postgrestables.yaml
1618
#+kubebuilder:scaffold:crdkustomizewebhookpatch
1719

1820
# [CERTMANAGER] To enable webhook, uncomment all the sections with [CERTMANAGER] prefix.
1921
# patches here are for enabling the CA injection for each CRD
2022
#- patches/cainjection_in_postgresconfigs.yaml
2123
#- patches/cainjection_in_postgrespublications.yaml
2224
- patches/cainjection_in_postgresgrants.yaml
25+
#- patches/cainjection_in_postgrestables.yaml
2326
#+kubebuilder:scaffold:crdkustomizecainjectionpatch
2427

2528
# the following config is for teaching kustomize how to do kustomization for CRDs.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# The following patch adds a directive for certmanager to inject CA into the CRD
2+
apiVersion: apiextensions.k8s.io/v1
3+
kind: CustomResourceDefinition
4+
metadata:
5+
annotations:
6+
cert-manager.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME)
7+
name: postgrestables.postgres.glints.com

0 commit comments

Comments
 (0)