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