Skip to content

Commit

Permalink
feat: Add support for PostgreSQL Publication
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyno-zeta committed Nov 29, 2024
1 parent 2022f21 commit 055273d
Show file tree
Hide file tree
Showing 28 changed files with 5,768 additions and 21 deletions.
9 changes: 9 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,13 @@ resources:
kind: PostgresqlUserRole
path: github.com/easymile/postgresql-operator/apis/postgresql/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: easymile.com
group: postgresql
kind: PostgresqlPublication
path: github.com/easymile/postgresql-operator/api/postgresql/v1alpha1
version: v1alpha1
version: "3"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Moreover, a single User can only have rights to one Database.
| [PostgresqlEngineConfiguration](docs/crds/PostgresqlEngineConfiguration.md) | Represents a PostgreSQL Engine Configuration with all necessary data to connect it |
| [PostgresqlDatabase](docs/crds/PostgresqlDatabase.md) | Represents a PostgreSQL Database |
| [PostgresqlUserRole](docs/crds/PostgresqlUserRole.md) | Represents a PostgreSQL User Role |
| [PostgresqlPublication](docs/crds/PostgresqlPublication.md) | Represents a PostgreSQL Publication |

## How to deploy ?

Expand Down
133 changes: 133 additions & 0 deletions api/postgresql/v1alpha1/postgresqlpublication_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
Copyright 2022.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
"github.com/easymile/postgresql-operator/api/postgresql/common"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// PostgresqlPublicationSpec defines the desired state of PostgresqlPublication.
type PostgresqlPublicationSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Postgresql Database
// +required
// +kubebuilder:validation:Required
Database *common.CRLink `json:"database"`
// Postgresql Publication name
// +required
// +kubebuilder:validation:Required
Name string `json:"name"`
// Should drop database on Custom Resource deletion ?
// +optional
DropOnDelete bool `json:"dropOnDelete,omitempty"`
// Publication for all tables
// Note: This is mutually exclusive with "tablesInSchema" & "tables"
// +optional
AllTables bool `json:"allTables,omitempty"`
// Publication for tables in schema
// Note: This is a list of schema
// +optional
TablesInSchema []string `json:"tablesInSchema,omitempty"`
// Publication for selected tables
// +optional
Tables []*PostgresqlPublicationTable `json:"tables,omitempty"`
// Publication with parameters
// +optional
WithParameters *PostgresqlPublicationWith `json:"withParameters,omitempty"`
}

type PostgresqlPublicationTable struct {
// Table name to use for publication
TableName string `json:"tableName"`
// Columns to export
Columns *[]string `json:"columns,omitempty"`
// Additional WHERE for table
AdditionalWhere *string `json:"additionalWhere,omitempty"`
}

type PostgresqlPublicationWith struct {
// Publish param
// See here: https://www.postgresql.org/docs/current/sql-createpublication.html#SQL-CREATEPUBLICATION-PARAMS-WITH-PUBLISH
Publish string `json:"publish"`
// Publish via partition root param
// See here: https://www.postgresql.org/docs/current/sql-createpublication.html#SQL-CREATEPUBLICATION-PARAMS-WITH-PUBLISH
PublishViaPartitionRoot *bool `json:"publishViaPartitionRoot,omitempty"`
}

type PublicationStatusPhase string

const PublicationNoPhase PublicationStatusPhase = ""
const PublicationFailedPhase PublicationStatusPhase = "Failed"
const PublicationCreatedPhase PublicationStatusPhase = "Created"

// PostgresqlPublicationStatus defines the observed state of PostgresqlPublication.
type PostgresqlPublicationStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file

// Current phase of the operator
Phase PublicationStatusPhase `json:"phase"`
// Human-readable message indicating details about current operator phase or error.
// +optional
Message string `json:"message"`
// True if all resources are in a ready state and all work is done.
// +optional
Ready bool `json:"ready"`
// Created publication name
// +optional
Name string `json:"name,omitempty"`
// Marker for save
// +optional
AllTables *bool `json:"allTables,omitempty"`
// Resource Spec hash
// +optional
Hash string `json:"hash,omitempty"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:resource:path=postgresqlpublications,scope=Namespaced,shortName=pgpublication;pgpub
//+kubebuilder:printcolumn:name="Publication",type=string,description="Publication",JSONPath=".status.name"
//+kubebuilder:printcolumn:name="Phase",type=string,description="Status phase",JSONPath=".status.phase"

// PostgresqlPublication is the Schema for the postgresqlpublications API.
type PostgresqlPublication struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec PostgresqlPublicationSpec `json:"spec,omitempty"`
Status PostgresqlPublicationStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// PostgresqlPublicationList contains a list of PostgresqlPublication.
type PostgresqlPublicationList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []PostgresqlPublication `json:"items"`
}

func init() {
SchemeBuilder.Register(&PostgresqlPublication{}, &PostgresqlPublicationList{})
}
169 changes: 169 additions & 0 deletions api/postgresql/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/metrics"

"github.com/prometheus/client_golang/prometheus"

postgresqlv1alpha1 "github.com/easymile/postgresql-operator/api/postgresql/v1alpha1"
postgresqlcontrollers "github.com/easymile/postgresql-operator/internal/controller/postgresql"
"github.com/prometheus/client_golang/prometheus"
//+kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -173,6 +174,25 @@ func main() {
setupLog.Error(err, "unable to create controller", "controller", "PostgresqlUserRole")
os.Exit(1)
}

if err = (&postgresqlcontrollers.PostgresqlPublicationReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("postgresqlpublication-controller"),
Log: ctrl.Log.WithValues(
"controller",
"postgresqlpublication",
"controllerKind",
"PostgresqlPublication",
"controllerGroup",
"postgresql.easymile.com",
),
ControllerRuntimeDetailedErrorTotal: controllerRuntimeDetailedErrorTotal,
ControllerName: "postgresqlpublication",
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "PostgresqlPublication")
os.Exit(1)
}
//+kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Expand Down
Loading

0 comments on commit 055273d

Please sign in to comment.