|
| 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 v4 |
| 18 | + |
| 19 | +import ( |
| 20 | + corev1 "k8s.io/api/core/v1" |
| 21 | + "k8s.io/apimachinery/pkg/api/resource" |
| 22 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | +) |
| 24 | + |
| 25 | +// ClusterClassSpec defines the desired state of ClusterClass. |
| 26 | +// ClusterClass is immutable after creation - it serves as a template for Cluster CRs. |
| 27 | +type ClusterClassSpec struct { |
| 28 | + // Provisioner identifies which database provisioner to use. |
| 29 | + // Currently supported: "postgresql.cnpg.io" (CloudNativePG) |
| 30 | + // +kubebuilder:validation:Required |
| 31 | + // +kubebuilder:validation:Enum=postgresql.cnpg.io |
| 32 | + Provisioner string `json:"provisioner"` |
| 33 | + |
| 34 | + // ClusterConfig contains cluster-level configuration. |
| 35 | + // These settings apply to database cluster infrastructure. |
| 36 | + // Can be overridden in Cluster CR. |
| 37 | + // +kubebuilder:default={} |
| 38 | + // +optional |
| 39 | + ClusterConfig ClusterConfig `json:"clusterConfig,omitempty"` |
| 40 | + |
| 41 | + // CNPG contains CloudNativePG-specific configuration and policies. |
| 42 | + // Only used when Provisioner is "postgresql.cnpg.io" |
| 43 | + // These settings CANNOT be overridden in Cluster CR (platform policy). |
| 44 | + // +optional |
| 45 | + CNPG *CNPGConfig `json:"cnpg,omitempty"` |
| 46 | +} |
| 47 | + |
| 48 | +// ClusterConfig contains provider-agnostic cluster configuration. |
| 49 | +// These fields define database cluster infrastructure and can be overridden in Cluster CR. |
| 50 | +type ClusterConfig struct { |
| 51 | + // Instances is the number of database instances (1 primary + N replicas). |
| 52 | + // Single instance (1) is suitable for development. |
| 53 | + // High availability requires at least 3 instances (1 primary + 2 replicas). |
| 54 | + // +kubebuilder:validation:Minimum=1 |
| 55 | + // +kubebuilder:validation:Maximum=10 |
| 56 | + // +kubebuilder:default=1 |
| 57 | + // +optional |
| 58 | + Instances *int32 `json:"instances,omitempty"` |
| 59 | + |
| 60 | + // Storage is the size of persistent volume for each instance. |
| 61 | + // Cannot be decreased after cluster creation (PostgreSQL limitation). |
| 62 | + // Recommended minimum: 10Gi for production viability. |
| 63 | + // Example: "50Gi", "100Gi", "1Ti" |
| 64 | + // +kubebuilder:default="1Gi" |
| 65 | + // +optional |
| 66 | + Storage *resource.Quantity `json:"storage,omitempty"` |
| 67 | + |
| 68 | + // PostgresVersion is the PostgreSQL version (major or major.minor). |
| 69 | + // Examples: "18" (latest 18.x), "18.1" (specific minor), "17", "16" |
| 70 | + // +kubebuilder:validation:Pattern=`^[0-9]+(\.[0-9]+)?$` |
| 71 | + // +kubebuilder:default="15" |
| 72 | + // +optional |
| 73 | + PostgresVersion *string `json:"postgresVersion,omitempty"` |
| 74 | + |
| 75 | + // Resources defines CPU and memory requests/limits per instance. |
| 76 | + // All instances in the cluster have the same resources. |
| 77 | + // +optional |
| 78 | + Resources *corev1.ResourceRequirements `json:"resources,omitempty"` |
| 79 | + |
| 80 | + // PostgreSQLConfig contains PostgreSQL engine configuration parameters. |
| 81 | + // Maps to postgresql.conf settings (cluster-wide). |
| 82 | + // Example: {"max_connections": "200", "shared_buffers": "2GB"} |
| 83 | + // +optional |
| 84 | + PostgreSQLConfig map[string]string `json:"postgresqlConfig,omitempty"` |
| 85 | + |
| 86 | + // PgHBA contains pg_hba.conf host-based authentication rules. |
| 87 | + // Defines client authentication and connection security (cluster-wide). |
| 88 | + // Example: ["hostssl all all 0.0.0.0/0 scram-sha-256"] |
| 89 | + // +optional |
| 90 | + PgHBA []string `json:"pgHBA,omitempty"` |
| 91 | +} |
| 92 | + |
| 93 | +// CNPGConfig contains CloudNativePG-specific configuration. |
| 94 | +// These fields control CNPG operator behavior and enforce platform policies. |
| 95 | +// Cannot be overridden in Cluster CR. |
| 96 | +type CNPGConfig struct { |
| 97 | + // PrimaryUpdateMethod determines how the primary instance is updated. |
| 98 | + // "restart" - tolerate brief downtime (suitable for development) |
| 99 | + // "switchover" - minimal downtime via automated failover (production-grade) |
| 100 | + // |
| 101 | + // NOTE: When using "switchover", ensure clusterConfig.instances > 1. |
| 102 | + // Switchover requires at least one replica to fail over to. |
| 103 | + // +kubebuilder:validation:Enum=restart;switchover |
| 104 | + // +kubebuilder:default=switchover |
| 105 | + // +optional |
| 106 | + PrimaryUpdateMethod string `json:"primaryUpdateMethod,omitempty"` |
| 107 | +} |
| 108 | + |
| 109 | +// ClusterClassStatus defines the observed state of ClusterClass. |
| 110 | +type ClusterClassStatus struct { |
| 111 | + // Conditions represent the latest available observations of the ClusterClass state. |
| 112 | + // +optional |
| 113 | + Conditions []metav1.Condition `json:"conditions,omitempty"` |
| 114 | + |
| 115 | + // Phase represents the current phase of the ClusterClass. |
| 116 | + // Valid phases: "Ready", "Invalid" |
| 117 | + // +optional |
| 118 | + Phase string `json:"phase,omitempty"` |
| 119 | +} |
| 120 | + |
| 121 | +// +kubebuilder:object:root=true |
| 122 | +// +kubebuilder:subresource:status |
| 123 | +// +kubebuilder:resource:scope=Cluster |
| 124 | +// +kubebuilder:printcolumn:name="Provisioner",type=string,JSONPath=`.spec.provisioner` |
| 125 | +// +kubebuilder:printcolumn:name="Instances",type=integer,JSONPath=`.spec.clusterConfig.instances` |
| 126 | +// +kubebuilder:printcolumn:name="Storage",type=string,JSONPath=`.spec.clusterConfig.storage` |
| 127 | +// +kubebuilder:printcolumn:name="Version",type=string,JSONPath=`.spec.clusterConfig.postgresVersion` |
| 128 | +// +kubebuilder:printcolumn:name="Phase",type=string,JSONPath=`.status.phase` |
| 129 | +// +kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp` |
| 130 | + |
| 131 | +// ClusterClass is the Schema for the clusterclasses API. |
| 132 | +// ClusterClass defines a reusable template and policy for database cluster provisioning. |
| 133 | +type ClusterClass struct { |
| 134 | + metav1.TypeMeta `json:",inline"` |
| 135 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 136 | + |
| 137 | + Spec ClusterClassSpec `json:"spec,omitempty"` |
| 138 | + Status ClusterClassStatus `json:"status,omitempty"` |
| 139 | +} |
| 140 | + |
| 141 | +// +kubebuilder:object:root=true |
| 142 | + |
| 143 | +// ClusterClassList contains a list of ClusterClass. |
| 144 | +type ClusterClassList struct { |
| 145 | + metav1.TypeMeta `json:",inline"` |
| 146 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 147 | + Items []ClusterClass `json:"items"` |
| 148 | +} |
| 149 | + |
| 150 | +func init() { |
| 151 | + SchemeBuilder.Register(&ClusterClass{}, &ClusterClassList{}) |
| 152 | +} |
0 commit comments