-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathcinder.go
More file actions
328 lines (290 loc) · 14 KB
/
cinder.go
File metadata and controls
328 lines (290 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package openstack
import (
"context"
"errors"
"fmt"
"github.com/openstack-k8s-operators/lib-common/modules/common/condition"
"github.com/openstack-k8s-operators/lib-common/modules/common/helper"
"github.com/openstack-k8s-operators/lib-common/modules/common/service"
"github.com/openstack-k8s-operators/lib-common/modules/common/webhook"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
cinderv1 "github.com/openstack-k8s-operators/cinder-operator/api/v1beta1"
corev1beta1 "github.com/openstack-k8s-operators/openstack-operator/api/core/v1beta1"
k8s_errors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
)
// ReconcileCinder -
func ReconcileCinder(ctx context.Context, instance *corev1beta1.OpenStackControlPlane, version *corev1beta1.OpenStackVersion, helper *helper.Helper) (ctrl.Result, error) {
Log := GetLogger(ctx)
// Trigger webhook to cache service name if UniquePodNames is enabled and not yet cached
// This handles operator upgrade scenario where existing CRs don't have ServiceName set
if instance.Spec.Cinder.UniquePodNames && instance.Spec.Cinder.ServiceName == "" {
return webhook.EnsureWebhookTrigger(ctx, instance, corev1beta1.ReconcileTriggerAnnotation, "Cinder service name caching", Log, 0)
}
cinderName, altCinderName := instance.GetServiceNameCached(corev1beta1.CinderName, instance.Spec.Cinder.UniquePodNames, instance.Spec.Cinder.ServiceName)
// Ensure the alternate cinder CR doesn't exist, as the randomPodNames flag may have been toggled
cinder := &cinderv1.Cinder{
ObjectMeta: metav1.ObjectMeta{
Name: altCinderName,
Namespace: instance.Namespace,
},
}
if res, err := EnsureDeleted(ctx, helper, cinder); err != nil {
return res, err
}
cinder = &cinderv1.Cinder{
ObjectMeta: metav1.ObjectMeta{
Name: cinderName,
Namespace: instance.Namespace,
},
}
if !instance.Spec.Cinder.Enabled {
if res, err := EnsureDeleted(ctx, helper, cinder); err != nil {
return res, err
}
instance.Status.Conditions.Remove(corev1beta1.OpenStackControlPlaneCinderReadyCondition)
instance.Status.Conditions.Remove(corev1beta1.OpenStackControlPlaneExposeCinderReadyCondition)
instance.Status.ContainerImages.CinderAPIImage = nil
instance.Status.ContainerImages.CinderSchedulerImage = nil
instance.Status.ContainerImages.CinderBackupImage = nil
instance.Status.ContainerImages.CinderVolumeImages = make(map[string]*string)
// Clean up AC CRs when service is disabled
if err := CleanupApplicationCredentialForService(ctx, helper, instance, cinder.Name); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
if instance.Spec.Cinder.Template == nil {
instance.Spec.Cinder.Template = &cinderv1.CinderSpecCore{}
}
// Note: Migration from rabbitMqClusterName to messagingBus.cluster is handled by the webhook
// via annotation-based triggers. No direct spec mutation here to avoid GitOps conflicts.
// add selector to service overrides
for _, endpointType := range []service.Endpoint{service.EndpointPublic, service.EndpointInternal} {
if instance.Spec.Cinder.Template.CinderAPI.Override.Service == nil {
instance.Spec.Cinder.Template.CinderAPI.Override.Service = map[service.Endpoint]service.RoutedOverrideSpec{}
}
instance.Spec.Cinder.Template.CinderAPI.Override.Service[endpointType] =
AddServiceOpenStackOperatorLabel(
instance.Spec.Cinder.Template.CinderAPI.Override.Service[endpointType],
cinder.Name)
}
// When component services got created check if there is the need to create a route
if err := helper.GetClient().Get(ctx, types.NamespacedName{Name: cinderName, Namespace: instance.Namespace}, cinder); err != nil {
if !k8s_errors.IsNotFound(err) {
return ctrl.Result{}, err
}
}
// Application Credential Management (Day-2 operation)
cinderReady := cinder.Status.Conditions != nil && cinder.Status.ObservedGeneration == cinder.Generation && cinder.IsReady()
// Apply same fallback logic as in CreateOrPatch to avoid passing empty values to AC
cinderSecret := instance.Spec.Cinder.Template.Secret
if cinderSecret == "" {
cinderSecret = instance.Spec.Secret
}
// Always reconcile AC - EnsureApplicationCredentialForService checks cluster state and handles the full AC lifecycle.
if instance.Spec.Cinder.ApplicationCredential != nil ||
instance.Spec.Cinder.Template.Auth.ApplicationCredentialSecret != "" {
acSecretName, acResult, err := EnsureApplicationCredentialForService(
ctx,
helper,
instance,
cinder.Name,
cinderReady,
cinderSecret,
instance.Spec.Cinder.Template.PasswordSelectors.Service,
instance.Spec.Cinder.Template.ServiceUser,
instance.Spec.Cinder.ApplicationCredential,
)
if err != nil {
return ctrl.Result{}, err
}
// If AC is not ready, return immediately without updating the service CR
if (acResult != ctrl.Result{}) {
return acResult, nil
}
// Set ApplicationCredentialSecret based on what the helper returned:
// - If AC disabled: returns ""
// - If AC enabled and ready: returns the AC secret name
instance.Spec.Cinder.Template.Auth.ApplicationCredentialSecret = acSecretName
}
// preserve any previously set TLS certs,set CA cert
if instance.Spec.TLS.PodLevel.Enabled {
instance.Spec.Cinder.Template.CinderAPI.TLS = cinder.Spec.CinderAPI.TLS
}
instance.Spec.Cinder.Template.CinderAPI.TLS.CaBundleSecretName = instance.Status.TLS.CaBundleSecretName
svcs, err := service.GetServicesListWithLabel(
ctx,
helper,
instance.Namespace,
GetServiceOpenStackOperatorLabel(cinder.Name),
)
if err != nil {
return ctrl.Result{}, err
}
// make sure to get to EndpointConfig when all service got created
if len(svcs.Items) == len(instance.Spec.Cinder.Template.CinderAPI.Override.Service) {
endpointDetails, ctrlResult, err := EnsureEndpointConfig(
ctx,
instance,
helper,
cinder,
svcs,
instance.Spec.Cinder.Template.CinderAPI.Override.Service,
instance.Spec.Cinder.APIOverride,
corev1beta1.OpenStackControlPlaneExposeCinderReadyCondition,
false, // TODO (mschuppert) could be removed when all integrated service support TLS
instance.Spec.Cinder.Template.CinderAPI.TLS,
)
if err != nil {
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}
// set service overrides
instance.Spec.Cinder.Template.CinderAPI.Override.Service = endpointDetails.GetEndpointServiceOverrides()
// update TLS settings with cert secret
instance.Spec.Cinder.Template.CinderAPI.TLS.API.Public.SecretName = endpointDetails.GetEndptCertSecret(service.EndpointPublic)
instance.Spec.Cinder.Template.CinderAPI.TLS.API.Internal.SecretName = endpointDetails.GetEndptCertSecret(service.EndpointInternal)
}
if instance.Spec.Cinder.Template.NodeSelector == nil && len(instance.Spec.NodeSelector) > 0 {
instance.Spec.Cinder.Template.NodeSelector = &instance.Spec.NodeSelector
}
// When there's no Topology referenced in the Service Template, inject the
// top-level one
// NOTE: This does not check the Service subCRs: by default the generated
// subCRs inherit the top-level TopologyRef unless an override is present
if instance.Spec.Cinder.Template.TopologyRef == nil {
instance.Spec.Cinder.Template.TopologyRef = instance.Spec.TopologyRef
}
// Propagate MessagingBus from top-level to template if not set
// Template-level takes precedence over top-level
if instance.Spec.MessagingBus != nil && instance.Spec.MessagingBus.Cluster != "" {
if instance.Spec.Cinder.Template.MessagingBus.Cluster == "" {
instance.Spec.Cinder.Template.MessagingBus = *instance.Spec.MessagingBus
}
}
// When no NotificationsBus is referenced in the subCR (override)
// try to inject the top-level one if defined
if instance.Spec.NotificationsBus != nil {
if instance.Spec.Cinder.Template.NotificationsBus == nil {
instance.Spec.Cinder.Template.NotificationsBus = instance.Spec.NotificationsBus
}
}
Log.Info("Reconciling Cinder", "Cinder.Namespace", instance.Namespace, "Cinder.Name", cinderName)
op, err := controllerutil.CreateOrPatch(ctx, helper.GetClient(), cinder, func() error {
instance.Spec.Cinder.Template.CinderSpecBase.DeepCopyInto(&cinder.Spec.CinderSpecBase)
instance.Spec.Cinder.Template.CinderAPI.DeepCopyInto(&cinder.Spec.CinderAPI.CinderAPITemplateCore)
instance.Spec.Cinder.Template.CinderScheduler.DeepCopyInto(&cinder.Spec.CinderScheduler.CinderSchedulerTemplateCore)
instance.Spec.Cinder.Template.CinderBackup.DeepCopyInto(&cinder.Spec.CinderBackup.CinderBackupTemplateCore)
cinder.Spec.CinderAPI.ContainerImage = *version.Status.ContainerImages.CinderAPIImage
cinder.Spec.CinderScheduler.ContainerImage = *version.Status.ContainerImages.CinderSchedulerImage
cinder.Spec.CinderBackup.ContainerImage = *version.Status.ContainerImages.CinderBackupImage
defaultVolumeImg := version.Status.ContainerImages.CinderVolumeImages["default"]
if defaultVolumeImg == nil {
return errors.New("default Cinder Volume images is unset")
}
// Discard old list of volume services and rebuild it
cinder.Spec.CinderVolumes = make(map[string]cinderv1.CinderVolumeTemplate)
for name, volume := range instance.Spec.Cinder.Template.CinderVolumes {
cinderCore := cinderv1.CinderVolumeTemplate{}
volume.DeepCopyInto(&cinderCore.CinderVolumeTemplateCore)
if volVal, ok := version.Status.ContainerImages.CinderVolumeImages[name]; ok {
cinderCore.ContainerImage = *volVal
} else {
cinderCore.ContainerImage = *defaultVolumeImg
}
cinder.Spec.CinderVolumes[name] = cinderCore
}
if instance.Spec.Cinder.Template.CinderBackups != nil {
bkp := make(map[string]cinderv1.CinderBackupTemplate)
cinder.Spec.CinderBackups = &bkp
for name, backup := range *instance.Spec.Cinder.Template.CinderBackups {
cinderBkpCore := cinderv1.CinderBackupTemplate{}
backup.DeepCopyInto(&cinderBkpCore.CinderBackupTemplateCore)
cinderBkpCore.ContainerImage = *version.Status.ContainerImages.CinderBackupImage
(*cinder.Spec.CinderBackups)[name] = cinderBkpCore
}
}
if cinder.Spec.Secret == "" {
cinder.Spec.Secret = instance.Spec.Secret
}
if cinder.Spec.DatabaseInstance == "" {
//cinder.Spec.DatabaseInstance = instance.Name // name of MariaDB we create here
cinder.Spec.DatabaseInstance = "openstack" //FIXME: see above
}
// Append globally defined extraMounts to the service's own list.
for _, ev := range instance.Spec.ExtraMounts {
cinder.Spec.ExtraMounts = append(cinder.Spec.ExtraMounts, cinderv1.CinderExtraVolMounts{
Name: ev.Name,
Region: ev.Region,
VolMounts: ev.VolMounts,
})
}
err := controllerutil.SetControllerReference(helper.GetBeforeObject(), cinder, helper.GetScheme())
if err != nil {
return err
}
return nil
})
if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
corev1beta1.OpenStackControlPlaneCinderReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
corev1beta1.OpenStackControlPlaneCinderReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
Log.Info(fmt.Sprintf("Cinder %s - %s", cinder.Name, op))
}
if cinder.Status.ObservedGeneration == cinder.Generation && cinder.IsReady() {
Log.Info("Cinder ready condition is true")
instance.Status.ContainerImages.CinderAPIImage = version.Status.ContainerImages.CinderAPIImage
instance.Status.ContainerImages.CinderSchedulerImage = version.Status.ContainerImages.CinderSchedulerImage
instance.Status.ContainerImages.CinderBackupImage = version.Status.ContainerImages.CinderBackupImage
instance.Status.ContainerImages.CinderVolumeImages = version.Status.ContainerImages.DeepCopy().CinderVolumeImages
instance.Status.Conditions.MarkTrue(corev1beta1.OpenStackControlPlaneCinderReadyCondition, corev1beta1.OpenStackControlPlaneCinderReadyMessage)
} else {
// We want to mirror the condition of the highest priority from the Cinder resource into the instance
// under the condition of type OpenStackControlPlaneCinderReadyCondition, but only if the sub-resource
// currently has any conditions (which won't be true for the initial creation of the sub-resource, since
// it has not gone through a reconcile loop yet to have any conditions). If this condition ends up being
// the highest priority condition in the OpenStackControlPlane, it will appear in the OpenStackControlPlane's
// "Ready" condition at the end of the reconciliation loop, clearly surfacing the condition to the user in
// the "oc get oscontrolplane -n <namespace>" output.
if len(cinder.Status.Conditions) > 0 {
MirrorSubResourceCondition(cinder.Status.Conditions, corev1beta1.OpenStackControlPlaneCinderReadyCondition, instance, cinder.Kind)
} else {
// Default to the associated "running" condition message for the sub-resource if it currently lacks any conditions for mirroring
instance.Status.Conditions.Set(condition.FalseCondition(
corev1beta1.OpenStackControlPlaneCinderReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
corev1beta1.OpenStackControlPlaneCinderReadyRunningMessage))
}
}
return ctrl.Result{}, nil
}
// CinderImageMatch - return true if the Cinder images match on the ControlPlane and Version, or if Cinder is not enabled
func CinderImageMatch(ctx context.Context, controlPlane *corev1beta1.OpenStackControlPlane, version *corev1beta1.OpenStackVersion) bool {
Log := GetLogger(ctx)
if controlPlane.Spec.Cinder.Enabled {
if !stringPointersEqual(controlPlane.Status.ContainerImages.CinderAPIImage, version.Status.ContainerImages.CinderAPIImage) ||
!stringPointersEqual(controlPlane.Status.ContainerImages.CinderSchedulerImage, version.Status.ContainerImages.CinderSchedulerImage) ||
!stringPointersEqual(controlPlane.Status.ContainerImages.CinderBackupImage, version.Status.ContainerImages.CinderBackupImage) {
Log.Info("Cinder images do not match")
return false
}
for name, img := range version.Status.ContainerImages.CinderVolumeImages {
if !stringPointersEqual(controlPlane.Status.ContainerImages.CinderVolumeImages[name], img) {
Log.Info("Cinder Volume images do not match")
return false
}
}
}
return true
}