|
| 1 | +/* |
| 2 | +Copyright 2022. |
| 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 v1beta1 |
| 18 | + |
| 19 | +import ( |
| 20 | + condition "github.com/openstack-k8s-operators/lib-common/modules/common/condition" |
| 21 | + "github.com/openstack-k8s-operators/lib-common/modules/common/service" |
| 22 | + "github.com/openstack-k8s-operators/lib-common/modules/common/tls" |
| 23 | + "github.com/openstack-k8s-operators/lib-common/modules/common/util" |
| 24 | + topologyv1 "github.com/openstack-k8s-operators/infra-operator/apis/topology/v1beta1" |
| 25 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 26 | + corev1 "k8s.io/api/core/v1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + // DbSyncHash hash |
| 32 | + DbSyncHash = "dbsync" |
| 33 | + |
| 34 | + // DeploymentHash hash used to detect changes |
| 35 | + DeploymentHash = "deployment" |
| 36 | + |
| 37 | + // Container image fall-back defaults |
| 38 | + |
| 39 | + // PlacementAPIContainerImage is the fall-back container image for PlacementAPI |
| 40 | + PlacementAPIContainerImage = "quay.io/podified-antelope-centos9/openstack-placement-api:current-podified" |
| 41 | +) |
| 42 | + |
| 43 | +// PlacementAPISpec defines the desired state of PlacementAPI |
| 44 | +type PlacementAPISpec struct { |
| 45 | + PlacementAPISpecCore `json:",inline"` |
| 46 | + |
| 47 | + // +kubebuilder:validation:Required |
| 48 | + // PlacementAPI Container Image URL (will be set to environmental default if empty) |
| 49 | + ContainerImage string `json:"containerImage"` |
| 50 | +} |
| 51 | + |
| 52 | +// PlacementAPISpecCore - |
| 53 | +type PlacementAPISpecCore struct { |
| 54 | + // +kubebuilder:validation:Optional |
| 55 | + // +kubebuilder:default=60 |
| 56 | + // +kubebuilder:validation:Minimum=10 |
| 57 | + // APITimeout for HAProxy, Apache |
| 58 | + APITimeout int `json:"apiTimeout"` |
| 59 | + |
| 60 | + // +kubebuilder:validation:Optional |
| 61 | + // +kubebuilder:default=placement |
| 62 | + // ServiceUser - optional username used for this service to register in keystone |
| 63 | + ServiceUser string `json:"serviceUser"` |
| 64 | + |
| 65 | + // +kubebuilder:validation:Required |
| 66 | + // MariaDB instance name |
| 67 | + // Right now required by the maridb-operator to get the credentials from the instance to create the DB |
| 68 | + // Might not be required in future |
| 69 | + DatabaseInstance string `json:"databaseInstance"` |
| 70 | + |
| 71 | + // +kubebuilder:validation:Optional |
| 72 | + // +kubebuilder:default=placement |
| 73 | + // DatabaseAccount - name of MariaDBAccount which will be used to connect. |
| 74 | + DatabaseAccount string `json:"databaseAccount"` |
| 75 | + |
| 76 | + // +kubebuilder:validation:Optional |
| 77 | + // +kubebuilder:default=1 |
| 78 | + // +kubebuilder:validation:Maximum=32 |
| 79 | + // +kubebuilder:validation:Minimum=0 |
| 80 | + // Replicas of placement API to run |
| 81 | + Replicas *int32 `json:"replicas"` |
| 82 | + |
| 83 | + // +kubebuilder:validation:Required |
| 84 | + // Secret containing OpenStack password information for placement PlacementPassword |
| 85 | + Secret string `json:"secret"` |
| 86 | + |
| 87 | + // +kubebuilder:validation:Optional |
| 88 | + // +kubebuilder:default={service: PlacementPassword} |
| 89 | + // PasswordSelectors - Selectors to identify the DB and ServiceUser password from the Secret |
| 90 | + PasswordSelectors PasswordSelector `json:"passwordSelectors"` |
| 91 | + |
| 92 | + // +kubebuilder:validation:Optional |
| 93 | + // NodeSelector to target subset of worker nodes running this service |
| 94 | + NodeSelector *map[string]string `json:"nodeSelector,omitempty"` |
| 95 | + |
| 96 | + // +kubebuilder:validation:Optional |
| 97 | + // +kubebuilder:default=false |
| 98 | + // PreserveJobs - do not delete jobs after they finished e.g. to check logs |
| 99 | + PreserveJobs bool `json:"preserveJobs"` |
| 100 | + |
| 101 | + // +kubebuilder:validation:Optional |
| 102 | + // CustomServiceConfig - customize the service config using this parameter to change service defaults, |
| 103 | + // or overwrite rendered information using raw OpenStack config format. The content gets added to |
| 104 | + // to /etc/<service>/<service>.conf.d directory as custom.conf file. |
| 105 | + CustomServiceConfig string `json:"customServiceConfig"` |
| 106 | + |
| 107 | + // +kubebuilder:validation:Optional |
| 108 | + // DefaultConfigOverwrite - interface to overwrite default config files like policy.yaml. |
| 109 | + DefaultConfigOverwrite map[string]string `json:"defaultConfigOverwrite,omitempty"` |
| 110 | + |
| 111 | + // +kubebuilder:validation:Optional |
| 112 | + // Resources - Compute Resources required by this service (Limits/Requests). |
| 113 | + // https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/ |
| 114 | + Resources corev1.ResourceRequirements `json:"resources,omitempty"` |
| 115 | + |
| 116 | + // +kubebuilder:validation:Optional |
| 117 | + // NetworkAttachments is a list of NetworkAttachment resource names to expose the services to the given network |
| 118 | + NetworkAttachments []string `json:"networkAttachments,omitempty"` |
| 119 | + |
| 120 | + // +kubebuilder:validation:Optional |
| 121 | + // Override, provides the ability to override the generated manifest of several child resources. |
| 122 | + Override APIOverrideSpec `json:"override,omitempty"` |
| 123 | + |
| 124 | + // +kubebuilder:validation:Optional |
| 125 | + // +operator-sdk:csv:customresourcedefinitions:type=spec |
| 126 | + // TLS - Parameters related to the TLS |
| 127 | + TLS tls.API `json:"tls,omitempty"` |
| 128 | + |
| 129 | + // +kubebuilder:validation:Optional |
| 130 | + // TopologyRef to apply the Topology defined by the associated CR referenced |
| 131 | + // by name |
| 132 | + TopologyRef *topologyv1.TopoRef `json:"topologyRef,omitempty"` |
| 133 | +} |
| 134 | + |
| 135 | +// APIOverrideSpec to override the generated manifest of several child resources. |
| 136 | +type APIOverrideSpec struct { |
| 137 | + // Override configuration for the Service created to serve traffic to the cluster. |
| 138 | + // The key must be the endpoint type (public, internal) |
| 139 | + Service map[service.Endpoint]service.RoutedOverrideSpec `json:"service,omitempty"` |
| 140 | +} |
| 141 | + |
| 142 | +// PasswordSelector to identify the DB and AdminUser password from the Secret |
| 143 | +type PasswordSelector struct { |
| 144 | + // +kubebuilder:validation:Optional |
| 145 | + // +kubebuilder:default="PlacementPassword" |
| 146 | + // Service - Selector to get the service user password from the Secret |
| 147 | + Service string `json:"service"` |
| 148 | +} |
| 149 | + |
| 150 | +// PlacementAPIStatus defines the observed state of PlacementAPI |
| 151 | +type PlacementAPIStatus struct { |
| 152 | + // ReadyCount of placement API instances |
| 153 | + ReadyCount int32 `json:"readyCount,omitempty"` |
| 154 | + |
| 155 | + // Map of hashes to track e.g. job status |
| 156 | + Hash map[string]string `json:"hash,omitempty"` |
| 157 | + |
| 158 | + // Conditions |
| 159 | + Conditions condition.Conditions `json:"conditions,omitempty" optional:"true"` |
| 160 | + |
| 161 | + // Placement Database Hostname |
| 162 | + DatabaseHostname string `json:"databaseHostname,omitempty"` |
| 163 | + |
| 164 | + // NetworkAttachments status of the deployment pods |
| 165 | + NetworkAttachments map[string][]string `json:"networkAttachments,omitempty"` |
| 166 | + |
| 167 | + //ObservedGeneration - the most recent generation observed for this service. If the observed generation is less than the spec generation, then the controller has not processed the latest changes. |
| 168 | + ObservedGeneration int64 `json:"observedGeneration,omitempty"` |
| 169 | + |
| 170 | + // LastAppliedTopology - the last applied Topology |
| 171 | + LastAppliedTopology *topologyv1.TopoRef `json:"lastAppliedTopology,omitempty"` |
| 172 | +} |
| 173 | + |
| 174 | +// PlacementAPI is the Schema for the placementapis API |
| 175 | +// +kubebuilder:object:root=true |
| 176 | +// +kubebuilder:subresource:status |
| 177 | +// +kubebuilder:printcolumn:name="NetworkAttachments",type="string",JSONPath=".spec.networkAttachments",description="NetworkAttachments" |
| 178 | +// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.conditions[0].status",description="Status" |
| 179 | +// +kubebuilder:printcolumn:name="Message",type="string",JSONPath=".status.conditions[0].message",description="Message" |
| 180 | +type PlacementAPI struct { |
| 181 | + metav1.TypeMeta `json:",inline"` |
| 182 | + metav1.ObjectMeta `json:"metadata,omitempty"` |
| 183 | + |
| 184 | + Spec PlacementAPISpec `json:"spec,omitempty"` |
| 185 | + Status PlacementAPIStatus `json:"status,omitempty"` |
| 186 | +} |
| 187 | + |
| 188 | +// PlacementAPIList contains a list of PlacementAPI |
| 189 | +// +kubebuilder:object:root=true |
| 190 | +type PlacementAPIList struct { |
| 191 | + metav1.TypeMeta `json:",inline"` |
| 192 | + metav1.ListMeta `json:"metadata,omitempty"` |
| 193 | + Items []PlacementAPI `json:"items"` |
| 194 | +} |
| 195 | + |
| 196 | +func init() { |
| 197 | + SchemeBuilder.Register(&PlacementAPI{}, &PlacementAPIList{}) |
| 198 | +} |
| 199 | + |
| 200 | +// IsReady - returns true if PlacementAPI is reconciled successfully |
| 201 | +func (instance PlacementAPI) IsReady() bool { |
| 202 | + return instance.Status.Conditions.IsTrue(condition.ReadyCondition) |
| 203 | +} |
| 204 | + |
| 205 | +// RbacConditionsSet - set the conditions for the rbac object |
| 206 | +func (instance PlacementAPI) RbacConditionsSet(c *condition.Condition) { |
| 207 | + instance.Status.Conditions.Set(c) |
| 208 | +} |
| 209 | + |
| 210 | +// RbacNamespace - return the namespace |
| 211 | +func (instance PlacementAPI) RbacNamespace() string { |
| 212 | + return instance.Namespace |
| 213 | +} |
| 214 | + |
| 215 | +// RbacResourceName - return the name to be used for rbac objects (serviceaccount, role, rolebinding) |
| 216 | +func (instance PlacementAPI) RbacResourceName() string { |
| 217 | + return "placement-" + instance.Name |
| 218 | +} |
| 219 | + |
| 220 | +// SetupDefaults - initializes any CRD field defaults based on environment variables (the defaulting mechanism itself is implemented via webhooks) |
| 221 | +func SetupDefaults() { |
| 222 | + // Acquire environmental defaults and initialize Placement defaults with them |
| 223 | + placementDefaults := PlacementAPIDefaults{ |
| 224 | + ContainerImageURL: util.GetEnvVar("RELATED_IMAGE_PLACEMENT_API_IMAGE_URL_DEFAULT", PlacementAPIContainerImage), |
| 225 | + APITimeout: 60, |
| 226 | + } |
| 227 | + |
| 228 | + SetupPlacementAPIDefaults(placementDefaults) |
| 229 | +} |
| 230 | + |
| 231 | +// GetSecret returns the value of the Nova.Spec.Secret |
| 232 | +func (instance PlacementAPI) GetSecret() string { |
| 233 | + return instance.Spec.Secret |
| 234 | +} |
| 235 | + |
| 236 | +// ValidateTopology - |
| 237 | +func (instance *PlacementAPISpecCore) ValidateTopology( |
| 238 | + basePath *field.Path, |
| 239 | + namespace string, |
| 240 | +) field.ErrorList { |
| 241 | + var allErrs field.ErrorList |
| 242 | + allErrs = append(allErrs, topologyv1.ValidateTopologyRef( |
| 243 | + instance.TopologyRef, |
| 244 | + *basePath.Child("topologyRef"), namespace)...) |
| 245 | + return allErrs |
| 246 | +} |
0 commit comments