Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ docker-build: test
docker-push:
docker push ${IMG}

docker-load-kind:
kind load docker-image ${IMG}
kind-load-image:
kind load docker-image ${IMG} -v 1

# find or download controller-gen
# download controller-gen if necessary
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
# postgres-controller
A small controller which act as bridge between zalando-postgres-operator

## Run an example on kind-cluster
```
# Intall zalando dependencies.
k apply -k github.com/zalando/postgres-operator/manifests

# Install our dependencies.
make generate && make manifests && make install

make docker-build

# Check the image is ready.
docker image ls

# Load the image to the kind-cluster.
make kind-load-image

make deploy

# Apply an example of our CRD Postgres
k apply -f config/samples/database_v1_postgres.yaml
```
19 changes: 14 additions & 5 deletions api/v1/postgres_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// +kubebuilder:object:root=true

// Postgres is the Schema for the postgres API
// +kubebuilder:printcolumn:name="Name",type=string,JSONPath=`.spec.name`
// +kubebuilder:printcolumn:name="Version",type=string,JSONPath=`.spec.version`

// Postgres is the Schema for the postgres API
type Postgres struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Expand All @@ -41,12 +41,16 @@ type PostgresSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Name the name of this postgres database
Name string `json:"name,omitempty"`
NumberOfInstances int32 `json:"numberOfInstances"`
PostgresConfig
TeamID string `json:"teamID"`

// Version is the postgres version
Version string `json:"version,omitempty"`
}

type PostgresConfig struct {
Version string `json:"version"`
}
// PostgresStatus defines the observed state of Postgres
type PostgresStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
Expand All @@ -62,6 +66,11 @@ type PostgresList struct {
Items []Postgres `json:"items"`
}

// IsBeingDeleted returns true if the deletion-timestamp is set
func (p *Postgres) IsBeingDeleted() bool {
return !p.ObjectMeta.DeletionTimestamp.IsZero()
}

func init() {
SchemeBuilder.Register(&Postgres{}, &PostgresList{})
}
9 changes: 7 additions & 2 deletions config/crd/bases/database.fits.cloud_postgres.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,17 @@ spec:
spec:
description: PostgresSpec defines the desired state of Postgres
properties:
name:
description: Name the name of this postgres database
numberOfInstances:
format: int32
type: integer
teamId:
type: string
version:
description: Version is the postgres version
type: string
required:
- numberOfInstances
- teamId
type: object
status:
description: PostgresStatus defines the observed state of Postgres
Expand Down
6 changes: 6 additions & 0 deletions config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
resources:
- manager.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
images:
- name: controller
newName: postgres-controller
newTag: latest
2 changes: 1 addition & 1 deletion config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ spec:
- /manager
args:
- --enable-leader-election
image: postgres-controller:latest
image: controller:latest
imagePullPolicy: IfNotPresent
name: manager
resources:
Expand Down
20 changes: 20 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ metadata:
creationTimestamp: null
name: manager-role
rules:
- apiGroups:
- acid.zalan.do
resources:
- postgresqls
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- acid.zalan.do
resources:
- postgresqls/status
verbs:
- get
- patch
- update
- apiGroups:
- database.fits.cloud
resources:
Expand Down
6 changes: 3 additions & 3 deletions config/rbac/role_binding.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ roleRef:
kind: ClusterRole
name: manager-role
subjects:
- kind: ServiceAccount
name: default
namespace: system
- kind: ServiceAccount
name: default
namespace: system
16 changes: 12 additions & 4 deletions config/samples/database_v1_postgres.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
apiVersion: database.fits.cloud/v1
kind: Postgres
metadata:
name: postgres-sample
name: acid-minimal-cluster
namespace: default
spec:
# Add fields here
name: my-db
version: "11.2"
numberOfInstances: 2
postgresConfig:
version: "12"
teamId: "acid"
users:
zalando:
- createdb
- superuser
volume:
size: 1Gi
79 changes: 74 additions & 5 deletions controllers/postgres_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ package controllers
import (
"context"
"fmt"
"time"
"log"

"github.com/go-logr/logr"
zalando "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
"k8s.io/apimachinery/pkg/api/errors"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -38,20 +41,86 @@ type PostgresReconciler struct {

// +kubebuilder:rbac:groups=database.fits.cloud,resources=postgres,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=database.fits.cloud,resources=postgres/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=acid.zalan.do,resources=postgresqls,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=acid.zalan.do,resources=postgresqls/status,verbs=get;update;patch

func (r *PostgresReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
_ = context.Background()
_ = r.Log.WithValues("postgres", req.NamespacedName)

// your logic here
// FIXME remove this is a stupid example
if 1 < 0 {
return ctrl.Result{RequeueAfter: 10 * time.Second, Requeue: true}, fmt.Errorf("one unequal 0")
instance := &databasev1.Postgres{}
if err := r.Get(context.Background(), req.NamespacedName, instance); err != nil {
if errors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}

if instance.IsBeingDeleted() {
// Delete the instance.
}

// Evaluate the status of the instance.
// One circumstance: Create
newZInstance, err := addDefaultValue(&zalando.Postgresql{
ObjectMeta: meta.ObjectMeta{
Name: instance.Spec.TeamID + "-" + instance.Name,
Namespace: instance.Namespace,
},
Spec: zalando.PostgresSpec{
Clone: zalando.CloneDescription{
ClusterName: "cluster-name-example",
},
NumberOfInstances: instance.Spec.NumberOfInstances,
Resources: zalando.Resources{
ResourceLimits: zalando.ResourceDescription{
CPU: "1",
Memory: "2Gi",
},
ResourceRequests: zalando.ResourceDescription{
CPU: "1",
Memory: "2Gi",
},
},
Patroni: zalando.Patroni{
InitDB: map[string]string{},
PgHba: []string{},
Slots: map[string]map[string]string{},
},
PodAnnotations: map[string]string{},
PostgresqlParam: zalando.PostgresqlParam{
Parameters: map[string]string{},
PgVersion: instance.Spec.Version,
},
ServiceAnnotations: map[string]string{},
StandbyCluster: &zalando.StandbyDescription{
S3WalPath: "",
},
TeamID: instance.Spec.TeamID,
TLS: &zalando.TLSDescription{
SecretName: "",
},
Users: map[string]zalando.UserFlags{},
Volume: zalando.Volume{
Size: "1Gi",
},
},
})
log.Print(newZInstance)
if err != nil {
return ctrl.Result{}, fmt.Errorf("error while creating the local postgresql in GO-code: %v", err)
}
if err := r.Create(context.Background(), newZInstance); err != nil {
return ctrl.Result{}, fmt.Errorf("error while creating CRD postgresql: %v", err)
}
return ctrl.Result{}, nil
}

// todo: Modify the postgresql object.
func addDefaultValue(before *zalando.Postgresql) (*zalando.Postgresql, error) {
// after := &zalando.Postgresql{}
return before, nil
}
func (r *PostgresReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&databasev1.Postgres{}).
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ go 1.15

require (
github.com/go-logr/logr v0.3.0
github.com/go-logr/zapr v0.3.0
github.com/go-logr/zapr v0.3.0 // indirect
github.com/onsi/ginkgo v1.14.2
github.com/onsi/gomega v1.10.3
github.com/zalando/postgres-operator v1.5.0
k8s.io/apimachinery v0.19.3
k8s.io/client-go v0.19.3
k8s.io/client-go v11.0.0+incompatible
sigs.k8s.io/controller-runtime v0.6.3
)

replace k8s.io/client-go => k8s.io/client-go v0.19.3
Loading