forked from openstack-k8s-operators/openstack-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplacement.go
More file actions
228 lines (200 loc) · 9.55 KB
/
Copy pathplacement.go
File metadata and controls
228 lines (200 loc) · 9.55 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
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"
placementv1 "github.com/openstack-k8s-operators/placement-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"
)
// ReconcilePlacementAPI -
func ReconcilePlacementAPI(ctx context.Context, instance *corev1beta1.OpenStackControlPlane, version *corev1beta1.OpenStackVersion, helper *helper.Helper) (ctrl.Result, error) {
placementAPI := &placementv1.PlacementAPI{
ObjectMeta: metav1.ObjectMeta{
Name: "placement",
Namespace: instance.Namespace,
},
}
Log := GetLogger(ctx)
if !instance.Spec.Placement.Enabled {
if res, err := EnsureDeleted(ctx, helper, placementAPI); err != nil {
return res, err
}
instance.Status.Conditions.Remove(corev1beta1.OpenStackControlPlanePlacementAPIReadyCondition)
instance.Status.Conditions.Remove(corev1beta1.OpenStackControlPlaneExposePlacementAPIReadyCondition)
// Clean up AC CRs when service is disabled
if err := CleanupApplicationCredentialForService(ctx, helper, instance, placementAPI.Name); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
if instance.Spec.Placement.Template == nil {
instance.Spec.Placement.Template = &placementv1.PlacementAPISpecCore{}
}
if instance.Spec.Placement.Template.NodeSelector == nil {
instance.Spec.Placement.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.Placement.Template.TopologyRef == nil {
instance.Spec.Placement.Template.TopologyRef = instance.Spec.TopologyRef
}
// add selector to service overrides
for _, endpointType := range []service.Endpoint{service.EndpointPublic, service.EndpointInternal} {
if instance.Spec.Placement.Template.Override.Service == nil {
instance.Spec.Placement.Template.Override.Service = map[service.Endpoint]service.RoutedOverrideSpec{}
}
instance.Spec.Placement.Template.Override.Service[endpointType] = AddServiceOpenStackOperatorLabel(
instance.Spec.Placement.Template.Override.Service[endpointType],
placementAPI.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: "placement", Namespace: instance.Namespace}, placementAPI); err != nil {
if !k8s_errors.IsNotFound(err) {
return ctrl.Result{}, err
}
}
// Application Credential Management (Day-2 operation)
placementReady := placementAPI.Status.Conditions != nil && placementAPI.Status.ObservedGeneration == placementAPI.Generation && placementAPI.IsReady()
// Apply same fallback logic as in CreateOrPatch to avoid passing empty values to AC
placementSecret := instance.Spec.Placement.Template.Secret
if placementSecret == "" {
placementSecret = instance.Spec.Secret
}
// Always reconcile AC - EnsureApplicationCredentialForService checks cluster state and handles the full AC lifecycle.
if instance.Spec.Placement.ApplicationCredential != nil ||
instance.Spec.Placement.Template.Auth.ApplicationCredentialSecret != "" {
acSecretName, acResult, err := EnsureApplicationCredentialForService(
ctx,
helper,
instance,
placementAPI.Name,
placementReady,
placementSecret,
instance.Spec.Placement.Template.PasswordSelectors.Service,
instance.Spec.Placement.Template.ServiceUser,
instance.Spec.Placement.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.Placement.Template.Auth.ApplicationCredentialSecret = acSecretName
}
// set CA cert and preserve any previously set TLS certs
if instance.Spec.TLS.PodLevel.Enabled {
instance.Spec.Placement.Template.TLS = placementAPI.Spec.TLS
}
instance.Spec.Placement.Template.TLS.CaBundleSecretName = instance.Status.TLS.CaBundleSecretName
svcs, err := service.GetServicesListWithLabel(
ctx,
helper,
instance.Namespace,
GetServiceOpenStackOperatorLabel(placementAPI.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.Placement.Template.Override.Service) {
endpointDetails, ctrlResult, err := EnsureEndpointConfig(
ctx,
instance,
helper,
placementAPI,
svcs,
instance.Spec.Placement.Template.Override.Service,
instance.Spec.Placement.APIOverride,
corev1beta1.OpenStackControlPlaneExposePlacementAPIReadyCondition,
false, // TODO (mschuppert) could be removed when all integrated service support TLS
instance.Spec.Placement.Template.TLS,
)
if err != nil {
return ctrlResult, err
} else if (ctrlResult != ctrl.Result{}) {
return ctrlResult, nil
}
// set service overrides
instance.Spec.Placement.Template.Override.Service = endpointDetails.GetEndpointServiceOverrides()
// update TLS settings with cert secret
instance.Spec.Placement.Template.TLS.API.Public.SecretName = endpointDetails.GetEndptCertSecret(service.EndpointPublic)
instance.Spec.Placement.Template.TLS.API.Internal.SecretName = endpointDetails.GetEndptCertSecret(service.EndpointInternal)
}
Log.Info("Reconciling PlacementAPI", "PlacementAPI.Namespace", instance.Namespace, "PlacementAPI.Name", "placement")
op, err := controllerutil.CreateOrPatch(ctx, helper.GetClient(), placementAPI, func() error {
instance.Spec.Placement.Template.DeepCopyInto(&placementAPI.Spec.PlacementAPISpecCore)
placementAPI.Spec.ContainerImage = *version.Status.ContainerImages.PlacementAPIImage
if placementAPI.Spec.Secret == "" {
placementAPI.Spec.Secret = instance.Spec.Secret
}
if placementAPI.Spec.DatabaseInstance == "" {
placementAPI.Spec.DatabaseInstance = "openstack"
}
err := controllerutil.SetControllerReference(helper.GetBeforeObject(), placementAPI, helper.GetScheme())
if err != nil {
return err
}
return nil
})
if err != nil {
instance.Status.Conditions.Set(condition.FalseCondition(
corev1beta1.OpenStackControlPlanePlacementAPIReadyCondition,
condition.ErrorReason,
condition.SeverityWarning,
corev1beta1.OpenStackControlPlanePlacementAPIReadyErrorMessage,
err.Error()))
return ctrl.Result{}, err
}
if op != controllerutil.OperationResultNone {
Log.Info(fmt.Sprintf("placementAPI %s - %s", placementAPI.Name, op))
}
if placementAPI.Status.ObservedGeneration == placementAPI.Generation && placementAPI.IsReady() {
Log.Info("PlacementAPI ready condition is true")
instance.Status.ContainerImages.PlacementAPIImage = version.Status.ContainerImages.PlacementAPIImage
instance.Status.Conditions.MarkTrue(corev1beta1.OpenStackControlPlanePlacementAPIReadyCondition, corev1beta1.OpenStackControlPlanePlacementAPIReadyMessage)
} else {
// We want to mirror the condition of the highest priority from the PlacementAPI resource into the instance
// under the condition of type OpenStackControlPlanePlacementAPIReadyCondition, 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(placementAPI.Status.Conditions) > 0 {
MirrorSubResourceCondition(placementAPI.Status.Conditions, corev1beta1.OpenStackControlPlanePlacementAPIReadyCondition, instance, placementAPI.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.OpenStackControlPlanePlacementAPIReadyCondition,
condition.RequestedReason,
condition.SeverityInfo,
corev1beta1.OpenStackControlPlanePlacementAPIReadyRunningMessage))
}
}
return ctrl.Result{}, nil
}
// PlacementImageMatch - return true if the placement images match on the ControlPlane and Version, or if Placement is not enabled
func PlacementImageMatch(ctx context.Context, controlPlane *corev1beta1.OpenStackControlPlane, version *corev1beta1.OpenStackVersion) bool {
Log := GetLogger(ctx)
if controlPlane.Spec.Placement.Enabled {
if !stringPointersEqual(controlPlane.Status.ContainerImages.PlacementAPIImage, version.Status.ContainerImages.PlacementAPIImage) {
Log.Info("Placement API image mismatch", "controlPlane.Status.ContainerImages.PlacementAPIImage", controlPlane.Status.ContainerImages.PlacementAPIImage, "version.Status.ContainerImages.PlacementAPIImage", version.Status.ContainerImages.PlacementAPIImage)
return false
}
}
return true
}