-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathwatcher.go
More file actions
278 lines (246 loc) · 12 KB
/
Copy pathwatcher.go
File metadata and controls
278 lines (246 loc) · 12 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
package openstack
import (
"context"
"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"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
corev1beta1 "github.com/openstack-k8s-operators/openstack-operator/api/core/v1beta1"
watcherv1 "github.com/openstack-k8s-operators/watcher-operator/api/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"
)
// ReconcileWatcher -
func ReconcileWatcher(ctx context.Context, instance *corev1beta1.OpenStackControlPlane, version *corev1beta1.OpenStackVersion, helper *helper.Helper) (ctrl.Result, error) {
watcher := &watcherv1.Watcher{
ObjectMeta: metav1.ObjectMeta{
Name: "watcher",
Namespace: instance.Namespace,
},
}
if !instance.Spec.Watcher.Enabled {
if res, err := EnsureDeleted(ctx, helper, watcher); err != nil {
return res, err
}
instance.Status.Conditions.Remove(corev1beta1.OpenStackControlPlaneWatcherReadyCondition)
instance.Status.Conditions.Remove(corev1beta1.OpenStackControlPlaneExposeWatcherReadyCondition)
instance.Status.ContainerImages.WatcherAPIImage = nil
instance.Status.ContainerImages.WatcherApplierImage = nil
instance.Status.ContainerImages.WatcherDecisionEngineImage = nil
// Clean up AC CRs when service is disabled
if err := CleanupApplicationCredentialForService(ctx, helper, instance, watcher.Name); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
if instance.Spec.Watcher.Template == nil {
instance.Spec.Watcher.Template = &watcherv1.WatcherSpecCore{}
}
// 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.Watcher.Template.APIServiceTemplate.Override.Service == nil {
instance.Spec.Watcher.Template.APIServiceTemplate.Override.Service = map[service.Endpoint]service.RoutedOverrideSpec{}
}
instance.Spec.Watcher.Template.APIServiceTemplate.Override.Service[endpointType] = AddServiceOpenStackOperatorLabel(
instance.Spec.Watcher.Template.APIServiceTemplate.Override.Service[endpointType],
watcher.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: "watcher", Namespace: instance.Namespace}, watcher); err != nil {
if !k8s_errors.IsNotFound(err) {
return ctrl.Result{}, err
}
}
// Application Credential Management (Day-2 operation)
// Watcher uses pointer fields, safely extract values
watcherReady := watcher.Status.Conditions != nil && watcher.Status.ObservedGeneration == watcher.Generation && watcher.IsReady()
// Helper to get Watcher values (which are pointers) with fallback logic
getWatcherSecret := func() string {
if instance.Spec.Watcher.Template.Secret != nil && *instance.Spec.Watcher.Template.Secret != "" {
return *instance.Spec.Watcher.Template.Secret
}
// Apply same fallback as in CreateOrPatch
return instance.Spec.Secret
}
getWatcherServiceUser := func() string {
if instance.Spec.Watcher.Template.ServiceUser != nil {
return *instance.Spec.Watcher.Template.ServiceUser
}
return ""
}
getWatcherPasswordSelector := func() string {
if instance.Spec.Watcher.Template.PasswordSelectors.Service != nil {
return *instance.Spec.Watcher.Template.PasswordSelectors.Service
}
return ""
}
// Always reconcile AC - EnsureApplicationCredentialForService checks cluster state and handles the full AC lifecycle.
if instance.Spec.Watcher.ApplicationCredential != nil ||
instance.Spec.Watcher.Template.Auth.ApplicationCredentialSecret != "" {
acSecretName, acResult, err := EnsureApplicationCredentialForService(
ctx,
helper,
instance,
watcher.Name,
watcherReady,
getWatcherSecret(),
getWatcherPasswordSelector(),
getWatcherServiceUser(),
instance.Spec.Watcher.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.Watcher.Template.Auth.ApplicationCredentialSecret = acSecretName
}
// preserve any previously set TLS certs, set CA cert
if instance.Spec.TLS.PodLevel.Enabled {
instance.Spec.Watcher.Template.APIServiceTemplate.TLS = watcher.Spec.APIServiceTemplate.TLS
}
instance.Spec.Watcher.Template.APIServiceTemplate.TLS.CaBundleSecretName = instance.Status.TLS.CaBundleSecretName
svcs, err := service.GetServicesListWithLabel(
ctx,
helper,
instance.Namespace,
GetServiceOpenStackOperatorLabel(watcher.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.Watcher.Template.APIServiceTemplate.Override.Service) {
endpointDetails, ctrlResult, err := EnsureEndpointConfig(
ctx,
instance,
helper,
watcher,
svcs,
instance.Spec.Watcher.Template.APIServiceTemplate.Override.Service,
instance.Spec.Watcher.APIOverride,
corev1beta1.OpenStackControlPlaneExposeWatcherReadyCondition,
false, // TODO: (mschuppert) could be removed when all integrated service support TLS
instance.Spec.Watcher.Template.APIServiceTemplate.TLS,
)
if err != nil {
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}
// set service overrides
instance.Spec.Watcher.Template.APIServiceTemplate.Override.Service = endpointDetails.GetEndpointServiceOverrides()
// update TLS settings with cert secret
instance.Spec.Watcher.Template.APIServiceTemplate.TLS.API.Public.SecretName = endpointDetails.GetEndptCertSecret(service.EndpointPublic)
instance.Spec.Watcher.Template.APIServiceTemplate.TLS.API.Internal.SecretName = endpointDetails.GetEndptCertSecret(service.EndpointInternal)
}
if instance.Spec.Watcher.Template.NodeSelector == nil && len(instance.Spec.NodeSelector) > 0 {
instance.Spec.Watcher.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.Watcher.Template.TopologyRef == nil {
instance.Spec.Watcher.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.Watcher.Template.MessagingBus.Cluster == "" {
instance.Spec.Watcher.Template.MessagingBus = *instance.Spec.MessagingBus
}
}
// Propagate NotificationsBus from top-level to template if not set
// Template-level takes precedence over top-level
if instance.Spec.NotificationsBus != nil {
if instance.Spec.Watcher.Template.NotificationsBus == nil {
instance.Spec.Watcher.Template.NotificationsBus = instance.Spec.NotificationsBus
}
}
helper.GetLogger().Info("Reconciling Watcher", "Watcher.Namespace", instance.Namespace, "Watcher.Name", "watcher")
op, err := controllerutil.CreateOrPatch(ctx, helper.GetClient(), watcher, func() error {
instance.Spec.Watcher.Template.DeepCopyInto(&watcher.Spec.WatcherSpecCore)
// Explicitly propagate NotificationsBus only if non-nil to allow webhook defaulting from rabbitMqClusterName
if instance.Spec.Watcher.Template.NotificationsBus != nil {
watcher.Spec.NotificationsBus = instance.Spec.Watcher.Template.NotificationsBus
}
if version.Status.ContainerImages.WatcherAPIImage == nil ||
version.Status.ContainerImages.WatcherApplierImage == nil ||
version.Status.ContainerImages.WatcherDecisionEngineImage == nil {
return fmt.Errorf("no Watcher images found in the OpenStackVersion")
}
watcher.Spec.APIContainerImageURL = *version.Status.ContainerImages.WatcherAPIImage
watcher.Spec.ApplierContainerImageURL = *version.Status.ContainerImages.WatcherApplierImage
watcher.Spec.DecisionEngineContainerImageURL = *version.Status.ContainerImages.WatcherDecisionEngineImage
if *watcher.Spec.Secret == "" {
*watcher.Spec.Secret = instance.Spec.Secret
}
err := controllerutil.SetControllerReference(helper.GetBeforeObject(), watcher, helper.GetScheme())
if err != nil {
return err
}
return nil
})
if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
corev1beta1.OpenStackControlPlaneWatcherReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
corev1beta1.OpenStackControlPlaneWatcherReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
helper.GetLogger().Info(fmt.Sprintf("watcher %s - %s", watcher.Name, op))
}
if watcher.Status.ObservedGeneration == watcher.Generation && watcher.IsReady() {
helper.GetLogger().Info("Watcher ready condition is true")
instance.Status.ContainerImages.WatcherAPIImage = version.Status.ContainerImages.WatcherAPIImage
instance.Status.ContainerImages.WatcherApplierImage = version.Status.ContainerImages.WatcherApplierImage
instance.Status.ContainerImages.WatcherDecisionEngineImage = version.Status.ContainerImages.WatcherDecisionEngineImage
instance.Status.Conditions.MarkTrue(corev1beta1.OpenStackControlPlaneWatcherReadyCondition, corev1beta1.OpenStackControlPlaneWatcherReadyMessage)
} else {
// We want to mirror the condition of the highest priority from the Watcher resource into the instance
// under the condition of type OpenStackControlPlaneWatcherReadyCondition, 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(watcher.Status.Conditions) > 0 {
MirrorSubResourceCondition(watcher.Status.Conditions, corev1beta1.OpenStackControlPlaneWatcherReadyCondition, instance, watcher.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.OpenStackControlPlaneWatcherReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
corev1beta1.OpenStackControlPlaneWatcherReadyRunningMessage))
}
}
return ctrl.Result{}, nil
}
// WatcherImageMatch - return true if the Watcher images match on the ControlPlane and Version, or if Watcher is not enabled
func WatcherImageMatch(ctx context.Context, controlPlane *corev1beta1.OpenStackControlPlane, version *corev1beta1.OpenStackVersion) bool {
Log := GetLogger(ctx)
if controlPlane.Spec.Watcher.Enabled {
if !stringPointersEqual(controlPlane.Status.ContainerImages.WatcherAPIImage, version.Status.ContainerImages.WatcherAPIImage) ||
!stringPointersEqual(controlPlane.Status.ContainerImages.WatcherApplierImage, version.Status.ContainerImages.WatcherApplierImage) ||
!stringPointersEqual(controlPlane.Status.ContainerImages.WatcherDecisionEngineImage, version.Status.ContainerImages.WatcherDecisionEngineImage) {
Log.Info("Watcher images do not match")
return false
}
}
return true
}