|
| 1 | +/* |
| 2 | +Copyright The CloudNativePG Contributors |
| 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 v1 |
| 18 | + |
| 19 | +import ( |
| 20 | + corev1 "k8s.io/api/core/v1" |
| 21 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 22 | +) |
| 23 | + |
| 24 | +// PublicationReclaimPolicy defines a policy for end-of-life maintenance of Publications. |
| 25 | +// +enum |
| 26 | +type PublicationReclaimPolicy string |
| 27 | + |
| 28 | +const ( |
| 29 | + // PublicationReclaimDelete means the publication will be deleted from Kubernetes on release |
| 30 | + // from its claim. |
| 31 | + PublicationReclaimDelete PublicationReclaimPolicy = "delete" |
| 32 | + |
| 33 | + // PublicationReclaimRetain means the publication will be left in its current phase for manual |
| 34 | + // reclamation by the administrator. The default policy is Retain. |
| 35 | + PublicationReclaimRetain PublicationReclaimPolicy = "retain" |
| 36 | +) |
| 37 | + |
| 38 | +// PublicationSpec defines the desired state of Publication |
| 39 | +type PublicationSpec struct { |
| 40 | + // The name of the PostgreSQL cluster that identifies the "publisher" |
| 41 | + ClusterRef corev1.LocalObjectReference `json:"cluster"` |
| 42 | + |
| 43 | + // The name of the publication inside PostgreSQL |
| 44 | + // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="name is immutable" |
| 45 | + Name string `json:"name"` |
| 46 | + |
| 47 | + // The name of the database where the publication will be installed in |
| 48 | + // the "publisher" cluster |
| 49 | + // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="dbname is immutable" |
| 50 | + DBName string `json:"dbname"` |
| 51 | + |
| 52 | + // Publication parameters part of the `WITH` clause as expected by |
| 53 | + // PostgreSQL `CREATE PUBLICATION` command |
| 54 | + // +optional |
| 55 | + Parameters map[string]string `json:"parameters,omitempty"` |
| 56 | + |
| 57 | + // Target of the publication as expected by PostgreSQL `CREATE PUBLICATION` command |
| 58 | + Target PublicationTarget `json:"target"` |
| 59 | + |
| 60 | + // The policy for end-of-life maintenance of this publication |
| 61 | + // +kubebuilder:validation:Enum=delete;retain |
| 62 | + // +kubebuilder:default:=retain |
| 63 | + // +optional |
| 64 | + ReclaimPolicy PublicationReclaimPolicy `json:"publicationReclaimPolicy,omitempty"` |
| 65 | +} |
| 66 | + |
| 67 | +// PublicationTarget is what this publication should publish |
| 68 | +// +kubebuilder:validation:XValidation:rule="(has(self.allTables) && !has(self.objects)) || (!has(self.allTables) && has(self.objects))",message="allTables and objects are mutually exclusive" |
| 69 | +type PublicationTarget struct { |
| 70 | + // Marks the publication as one that replicates changes for all tables |
| 71 | + // in the database, including tables created in the future. |
| 72 | + // Corresponding to `FOR ALL TABLES` in PostgreSQL. |
| 73 | + // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="allTables is immutable" |
| 74 | + // +optional |
| 75 | + AllTables bool `json:"allTables,omitempty"` |
| 76 | + |
| 77 | + // Just the following schema objects |
| 78 | + // +kubebuilder:validation:XValidation:rule="!(self.exists(o, has(o.table) && has(o.table.columns)) && self.exists(o, has(o.tablesInSchema)))",message="specifying a column list when the publication also publishes tablesInSchema is not supported" |
| 79 | + // +kubebuilder:validation:MaxItems=100000 |
| 80 | + // +optional |
| 81 | + Objects []PublicationTargetObject `json:"objects,omitempty"` |
| 82 | +} |
| 83 | + |
| 84 | +// PublicationTargetObject is an object to publish |
| 85 | +// +kubebuilder:validation:XValidation:rule="(has(self.tablesInSchema) && !has(self.table)) || (!has(self.tablesInSchema) && has(self.table))",message="tablesInSchema and table are mutually exclusive" |
| 86 | +type PublicationTargetObject struct { |
| 87 | + // Marks the publication as one that replicates changes for all tables |
| 88 | + // in the specified list of schemas, including tables created in the |
| 89 | + // future. Corresponding to `FOR TABLES IN SCHEMA` in PostgreSQL. |
| 90 | + // +optional |
| 91 | + TablesInSchema string `json:"tablesInSchema,omitempty"` |
| 92 | + |
| 93 | + // Specifies a list of tables to add to the publication. Corresponding |
| 94 | + // to `FOR TABLE` in PostgreSQL. |
| 95 | + // +optional |
| 96 | + Table *PublicationTargetTable `json:"table,omitempty"` |
| 97 | +} |
| 98 | + |
| 99 | +// PublicationTargetTable is a table to publish |
| 100 | +type PublicationTargetTable struct { |
| 101 | + // Whether to limit to the table only or include all its descendants |
| 102 | + // +optional |
| 103 | + Only bool `json:"only,omitempty"` |
| 104 | + |
| 105 | + // The table name |
| 106 | + Name string `json:"name"` |
| 107 | + |
| 108 | + // The schema name |
| 109 | + // +optional |
| 110 | + Schema string `json:"schema,omitempty"` |
| 111 | + |
| 112 | + // The columns to publish |
| 113 | + // +optional |
| 114 | + Columns []string `json:"columns,omitempty"` |
| 115 | +} |
| 116 | + |
| 117 | +// PublicationStatus defines the observed state of Publication |
| 118 | +type PublicationStatus struct { |
| 119 | + // A sequence number representing the latest |
| 120 | + // desired state that was synchronized |
| 121 | + // +optional |
| 122 | + ObservedGeneration int64 `json:"observedGeneration,omitempty"` |
| 123 | + |
| 124 | + // Applied is true if the publication was reconciled correctly |
| 125 | + // +optional |
| 126 | + Applied *bool `json:"applied,omitempty"` |
| 127 | + |
| 128 | + // Message is the reconciliation output message |
| 129 | + // +optional |
| 130 | + Message string `json:"message,omitempty"` |
| 131 | +} |
| 132 | + |
| 133 | +// +genclient |
| 134 | +// +kubebuilder:object:root=true |
| 135 | +// +kubebuilder:subresource:status |
| 136 | +// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp" |
| 137 | +// +kubebuilder:printcolumn:name="Cluster",type="string",JSONPath=".spec.cluster.name" |
| 138 | +// +kubebuilder:printcolumn:name="PG Name",type="string",JSONPath=".spec.name" |
| 139 | +// +kubebuilder:printcolumn:name="Applied",type="boolean",JSONPath=".status.applied" |
| 140 | +// +kubebuilder:printcolumn:name="Message",type="string",JSONPath=".status.message",description="Latest reconciliation message" |
| 141 | + |
| 142 | +// Publication is the Schema for the publications API |
| 143 | +type Publication struct { |
| 144 | + metav1.TypeMeta `json:",inline"` |
| 145 | + metav1.ObjectMeta `json:"metadata"` |
| 146 | + |
| 147 | + Spec PublicationSpec `json:"spec"` |
| 148 | + Status PublicationStatus `json:"status,omitempty"` |
| 149 | +} |
| 150 | + |
| 151 | +// +kubebuilder:object:root=true |
| 152 | + |
| 153 | +// PublicationList contains a list of Publication |
| 154 | +type PublicationList struct { |
| 155 | + metav1.TypeMeta `json:",inline"` |
| 156 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 157 | + Items []Publication `json:"items"` |
| 158 | +} |
| 159 | + |
| 160 | +func init() { |
| 161 | + SchemeBuilder.Register(&Publication{}, &PublicationList{}) |
| 162 | +} |
0 commit comments