Skip to content

Commit 79f80fc

Browse files
authored
Merge pull request #5 from arnarg/split-crd
Splitting apart db and role creation
2 parents 7ce2255 + d1ede66 commit 79f80fc

15 files changed

+801
-156
lines changed

README.md

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,51 @@
11
# External PostgreSQL server operator for Kubernetes
22

33
## Features
4-
* creates a database
5-
* creates a role with random username and password
6-
* assigns role to the database
7-
* if the database exist, it will only create a role
8-
* drops role when removing CR, assigns all objects to user `postgres`
9-
* creates a Kubernetes secret with postgres_uri in the same namespace as CR
10-
11-
CR example
4+
* Creates a database from a CR
5+
* Creates a role with random username and password from a CR
6+
* If the database exist, it will only create a role
7+
* Multiple user roles can own one database
8+
* Creates Kubernetes secret with postgres_uri in the same namespace as CR
9+
10+
## CRs
1211
```yaml
1312
apiVersion: db.movetokube.com/v1alpha1
1413
kind: Postgres
1514
metadata:
1615
name: my-db
1716
namespace: app
1817
spec:
19-
# Add fields here
20-
database: test-db
18+
database: test-db # Name of database created in PostgreSQL
19+
```
20+
21+
This creates a database called `test-db` and a role `test-db-group` that is set as the owner of the database.
22+
23+
```yaml
24+
apiVersion: db.movetokube.com/v1alpha1
25+
kind: PostgresUser
26+
metadata:
27+
name: my-db-user
28+
namespace: app
29+
spec:
30+
role: username
31+
database: my-db # This references the Postgres CR
2132
secretName: my-secret
2233
```
2334

35+
This creates a user role `username-<hash>` and grants role `test-db-group` to it. Its credentials are put in secret `my-secret-my-db-user`.
36+
37+
`PostgresUser` needs to reference a `Postgres` in the same namespace.
38+
39+
Two `Postgres` referencing the same database can exist in more than one namespace. The last CR referencing a database will drop the group role and transfer database ownership to the role used by the operator.
40+
2441
## Installation
2542

2643
1. Configure Postgres credentials for the operator in `deploy/operator.yaml`
2744
2. `kubectl apply -f deploy/crds/db_v1alpha1_postgres_crd.yaml`
28-
3. `kubectl apply -f deploy/namespace.yaml`
29-
4. `kubectl apply -f role.yaml`
30-
5. `kubectl apply -f role_binding.yaml`
31-
6. `kubectl apply -f service_account.yaml`
32-
7. `kubectl apply -f operator.yaml`
45+
3. `kubectl apply -f deploy/crds/db_v1alpha1_postgresuser_crd.yaml`
46+
4. `kubectl apply -f deploy/namespace.yaml`
47+
5. `kubectl apply -f role.yaml`
48+
6. `kubectl apply -f role_binding.yaml`
49+
7. `kubectl apply -f service_account.yaml`
50+
8. `kubectl apply -f operator.yaml`
3351

deploy/crds/db_v1alpha1_postgres_cr.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@ metadata:
44
name: my-db
55
namespace: app
66
spec:
7-
# Add fields here
87
database: test-db
9-
secretName: my-secret

deploy/crds/db_v1alpha1_postgres_crd.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@ spec:
2929
properties:
3030
database:
3131
type: string
32-
secretName:
33-
type: string
3432
required:
3533
- database
36-
- secretName
3734
type: object
3835
status:
3936
properties:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: db.movetokube.com/v1alpha1
2+
kind: PostgresUser
3+
metadata:
4+
name: my-db-user
5+
namespace: app
6+
spec:
7+
role: username
8+
database: my-db
9+
secretName: my-credential-secret
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
apiVersion: apiextensions.k8s.io/v1beta1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: postgresusers.db.movetokube.com
5+
spec:
6+
group: db.movetokube.com
7+
names:
8+
kind: PostgresUser
9+
listKind: PostgresUserList
10+
plural: postgresusers
11+
singular: postgresuser
12+
scope: Namespaced
13+
validation:
14+
openAPIV3Schema:
15+
properties:
16+
apiVersion:
17+
description: 'APIVersion defines the versioned schema of this representation
18+
of an object. Servers should convert recognized schemas to the latest
19+
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources'
20+
type: string
21+
kind:
22+
description: 'Kind is a string value representing the REST resource this
23+
object represents. Servers may infer this from the endpoint the client
24+
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds'
25+
type: string
26+
metadata:
27+
type: object
28+
spec:
29+
properties:
30+
database:
31+
type: string
32+
role:
33+
type: string
34+
secretName:
35+
type: string
36+
required:
37+
- role
38+
- database
39+
- secretName
40+
type: object
41+
status:
42+
properties:
43+
databaseName:
44+
type: string
45+
postgresGroup:
46+
type: string
47+
postgresRole:
48+
type: string
49+
succeeded:
50+
type: boolean
51+
required:
52+
- succeeded
53+
- postgresRole
54+
- postgresGroup
55+
- databaseName
56+
type: object
57+
version: v1alpha1
58+
versions:
59+
- name: v1alpha1
60+
served: true
61+
storage: true

pkg/apis/db/v1alpha1/postgres_types.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import (
1010
// PostgresSpec defines the desired state of Postgres
1111
// +k8s:openapi-gen=true
1212
type PostgresSpec struct {
13-
Database string `json:"database"`
14-
SecretName string `json:"secretName"`
13+
Database string `json:"database"`
1514
}
1615

1716
// PostgresStatus defines the observed state of Postgres
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package v1alpha1
2+
3+
import (
4+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
5+
)
6+
7+
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
8+
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
9+
10+
// PostgresUserSpec defines the desired state of PostgresUser
11+
// +k8s:openapi-gen=true
12+
type PostgresUserSpec struct {
13+
Role string `json:"role"`
14+
Database string `json:"database"`
15+
SecretName string `json:"secretName"`
16+
}
17+
18+
// PostgresUserStatus defines the observed state of PostgresUser
19+
// +k8s:openapi-gen=true
20+
type PostgresUserStatus struct {
21+
Succeeded bool `json:"succeeded"`
22+
PostgresRole string `json:"postgresRole"`
23+
PostgresGroup string `json:"postgresGroup"`
24+
DatabaseName string `json:"databaseName"`
25+
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
26+
// Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file
27+
// Add custom validation using kubebuilder tags: https://book.kubebuilder.io/beyond_basics/generating_crd.html
28+
}
29+
30+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
31+
32+
// PostgresUser is the Schema for the postgresusers API
33+
// +k8s:openapi-gen=true
34+
// +kubebuilder:subresource:status
35+
type PostgresUser struct {
36+
metav1.TypeMeta `json:",inline"`
37+
metav1.ObjectMeta `json:"metadata,omitempty"`
38+
39+
Spec PostgresUserSpec `json:"spec,omitempty"`
40+
Status PostgresUserStatus `json:"status,omitempty"`
41+
}
42+
43+
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
44+
45+
// PostgresUserList contains a list of PostgresUser
46+
type PostgresUserList struct {
47+
metav1.TypeMeta `json:",inline"`
48+
metav1.ListMeta `json:"metadata,omitempty"`
49+
Items []PostgresUser `json:"items"`
50+
}
51+
52+
func init() {
53+
SchemeBuilder.Register(&PostgresUser{}, &PostgresUserList{})
54+
}

pkg/apis/db/v1alpha1/zz_generated.deepcopy.go

Lines changed: 94 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)