-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaggregates_controller.go
More file actions
287 lines (252 loc) · 10.6 KB
/
aggregates_controller.go
File metadata and controls
287 lines (252 loc) · 10.6 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
/*
SPDX-FileCopyrightText: Copyright 2024 SAP SE or an SAP affiliate company and cobaltcore-dev contributors
SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"context"
"encoding/json"
"errors"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
k8sacmetav1 "k8s.io/client-go/applyconfigurations/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
"github.com/gophercloud/gophercloud/v2"
kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
apiv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/applyconfigurations/api/v1"
"github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/openstack"
"github.com/cobaltcore-dev/openstack-hypervisor-operator/internal/utils"
)
const (
AggregatesControllerName = "aggregates"
)
type AggregatesController struct {
k8sclient.Client
Scheme *runtime.Scheme
computeClient *gophercloud.ServiceClient
}
// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=hypervisors,verbs=get;list;watch
// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=hypervisors/status,verbs=get;update;patch
func (ac *AggregatesController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
hv := &kvmv1.Hypervisor{}
if err := ac.Get(ctx, req.NamespacedName, hv); err != nil {
return ctrl.Result{}, k8sclient.IgnoreNotFound(err)
}
// Wait for onboarding controller to populate HypervisorID and ServiceID
// before attempting to modify aggregates
if hv.Status.HypervisorID == "" || hv.Status.ServiceID == "" {
return ctrl.Result{}, nil
}
desiredAggregateNames, desiredCondition := ac.determineDesiredState(hv)
// Extract current aggregate names for comparison
currentAggregateNames := make([]string, len(hv.Status.Aggregates))
for i, agg := range hv.Status.Aggregates {
currentAggregateNames[i] = agg.Name
}
var newAggregates []kvmv1.Aggregate
aggregatesChanged := false
if !slicesEqualUnordered(desiredAggregateNames, currentAggregateNames) {
// Apply aggregates to OpenStack and update status
aggregates, err := openstack.ApplyAggregates(ctx, ac.computeClient, hv.Name, desiredAggregateNames)
if err != nil {
// Set error condition, preserving current aggregate ownership
statusCfg := apiv1.HypervisorStatus()
statusCfg.Conditions = utils.ConditionsFromStatus(hv.Status.Conditions)
utils.SetApplyConfigurationStatusCondition(&statusCfg.Conditions,
*k8sacmetav1.Condition().
WithType(kvmv1.ConditionTypeAggregatesUpdated).
WithStatus(metav1.ConditionFalse).
WithReason(kvmv1.ConditionReasonFailed).
WithMessage(fmt.Errorf("failed to apply aggregates: %w", err).Error()))
if len(hv.Status.Aggregates) > 0 {
aggCfgs := make([]apiv1.AggregateApplyConfiguration, len(hv.Status.Aggregates))
for i, agg := range hv.Status.Aggregates {
a := apiv1.Aggregate().WithName(agg.Name).WithUUID(agg.UUID)
if len(agg.Metadata) > 0 {
a.WithMetadata(agg.Metadata)
}
aggCfgs[i] = *a
}
statusCfg.Aggregates = aggCfgs
}
if err2 := ac.Status().Apply(ctx,
apiv1.Hypervisor(hv.Name, "").WithStatus(statusCfg),
k8sclient.ForceOwnership, k8sclient.FieldOwner(AggregatesControllerName)); err2 != nil {
return ctrl.Result{}, errors.Join(err, err2)
}
return ctrl.Result{}, err
}
newAggregates = aggregates
aggregatesChanged = true
}
// Skip the round-trip when nothing would change
existing := meta.FindStatusCondition(hv.Status.Conditions, desiredCondition.Type)
conditionUnchanged := existing != nil &&
existing.Status == desiredCondition.Status &&
existing.Reason == desiredCondition.Reason &&
existing.Message == desiredCondition.Message
if !aggregatesChanged && conditionUnchanged {
return ctrl.Result{}, nil
}
statusCfg := apiv1.HypervisorStatus()
statusCfg.Conditions = utils.ConditionsFromStatus(hv.Status.Conditions)
utils.SetApplyConfigurationStatusCondition(&statusCfg.Conditions,
*k8sacmetav1.Condition().
WithType(desiredCondition.Type).
WithStatus(desiredCondition.Status).
WithReason(desiredCondition.Reason).
WithMessage(desiredCondition.Message))
if aggregatesChanged && len(newAggregates) > 0 {
aggCfgs := make([]apiv1.AggregateApplyConfiguration, len(newAggregates))
for i, agg := range newAggregates {
a := apiv1.Aggregate().WithName(agg.Name).WithUUID(agg.UUID)
if len(agg.Metadata) > 0 {
a.WithMetadata(agg.Metadata)
}
aggCfgs[i] = *a
}
statusCfg.Aggregates = aggCfgs
}
if err := ac.Status().Apply(ctx,
apiv1.Hypervisor(hv.Name, "").WithStatus(statusCfg),
k8sclient.ForceOwnership, k8sclient.FieldOwner(AggregatesControllerName)); err != nil {
return ctrl.Result{}, err
}
// The Aggregates field uses omitempty in the generated apply config, so an
// empty slice cannot be sent via SSA. Use a targeted merge patch to clear it.
if aggregatesChanged && len(newAggregates) == 0 {
patch, err := json.Marshal(map[string]any{
"status": map[string]any{
"aggregates": []kvmv1.Aggregate{},
},
})
if err != nil {
return ctrl.Result{}, err
}
fresh := &kvmv1.Hypervisor{}
if err := ac.Get(ctx, k8sclient.ObjectKey{Name: hv.Name}, fresh); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{}, ac.Status().Patch(ctx, fresh, k8sclient.RawPatch(types.MergePatchType, patch))
}
return ctrl.Result{}, nil
}
// determineDesiredState returns the desired aggregates and the corresponding condition
// based on the hypervisor's current state. The condition status is True only when
// spec aggregates are being applied. Otherwise, it's False with a reason explaining
// why different aggregates are applied.
func (ac *AggregatesController) determineDesiredState(hv *kvmv1.Hypervisor) ([]string, metav1.Condition) {
// If terminating AND evicted, remove from all aggregates
// We must wait for eviction to complete before removing aggregates
if hv.Spec.Maintenance == kvmv1.MaintenanceTermination {
evictingCondition := meta.FindStatusCondition(hv.Status.Conditions, kvmv1.ConditionTypeEvicting)
// Only remove aggregates if eviction is complete (Evicting=False)
// If Evicting condition is not set or still True, keep current aggregates
if evictingCondition != nil && evictingCondition.Status == metav1.ConditionFalse {
return []string{}, metav1.Condition{
Type: kvmv1.ConditionTypeAggregatesUpdated,
Status: metav1.ConditionFalse,
Reason: kvmv1.ConditionReasonTerminating,
Message: "Aggregates cleared due to termination after eviction",
}
}
// Still evicting or eviction not started - keep current aggregate names
currentAggregateNames := make([]string, len(hv.Status.Aggregates))
for i, agg := range hv.Status.Aggregates {
currentAggregateNames[i] = agg.Name
}
return currentAggregateNames, metav1.Condition{
Type: kvmv1.ConditionTypeAggregatesUpdated,
Status: metav1.ConditionFalse,
Reason: kvmv1.ConditionReasonEvictionInProgress,
Message: "Aggregates unchanged while terminating and eviction in progress",
}
}
// If onboarding is in progress (Initial or Testing), add test aggregate
onboardingCondition := meta.FindStatusCondition(hv.Status.Conditions, kvmv1.ConditionTypeOnboarding)
if onboardingCondition != nil && onboardingCondition.Status == metav1.ConditionTrue {
if onboardingCondition.Reason == kvmv1.ConditionReasonInitial || onboardingCondition.Reason == kvmv1.ConditionReasonTesting {
zone := hv.Labels[corev1.LabelTopologyZone]
return []string{zone, testAggregateName}, metav1.Condition{
Type: kvmv1.ConditionTypeAggregatesUpdated,
Status: metav1.ConditionFalse,
Reason: kvmv1.ConditionReasonTestAggregates,
Message: "Test aggregate applied during onboarding instead of spec aggregates",
}
}
// If the onboarding is almost complete, it will wait (among other things) for this controller to switch to Spec.Aggregates.
// We wait for traits to be applied first to ensure sequential ordering: Traits → Aggregates.
if onboardingCondition.Reason == kvmv1.ConditionReasonHandover {
if !meta.IsStatusConditionTrue(hv.Status.Conditions, kvmv1.ConditionTypeTraitsUpdated) {
// Traits not yet applied — keep test aggregates and signal we're waiting
zone := hv.Labels[corev1.LabelTopologyZone]
return []string{zone, testAggregateName}, metav1.Condition{
Type: kvmv1.ConditionTypeAggregatesUpdated,
Status: metav1.ConditionFalse,
Reason: kvmv1.ConditionReasonWaitingForTraits,
Message: "Waiting for traits to be applied before switching to spec aggregates",
}
}
return hv.Spec.Aggregates, metav1.Condition{
Type: kvmv1.ConditionTypeAggregatesUpdated,
Status: metav1.ConditionTrue,
Reason: kvmv1.ConditionReasonSucceeded,
Message: "Aggregates from spec applied successfully",
}
}
}
// Normal operations or onboarding complete: use Spec.Aggregates
return hv.Spec.Aggregates, metav1.Condition{
Type: kvmv1.ConditionTypeAggregatesUpdated,
Status: metav1.ConditionTrue,
Reason: kvmv1.ConditionReasonSucceeded,
Message: "Aggregates from spec applied successfully",
}
}
// slicesEqualUnordered compares two string slices without considering order.
// Returns true if both slices contain the same elements, regardless of order.
func slicesEqualUnordered(a, b []string) bool {
if len(a) != len(b) {
return false
}
// Create a map to count occurrences in slice a
counts := make(map[string]int)
for _, s := range a {
counts[s]++
}
// Verify all elements in b exist in a with correct counts
for _, s := range b {
counts[s]--
if counts[s] < 0 {
return false
}
}
return true
}
// SetupWithManager sets up the controller with the Manager.
func (ac *AggregatesController) SetupWithManager(mgr ctrl.Manager) error {
ctx := context.Background()
var err error
if ac.computeClient, err = openstack.GetServiceClient(ctx, "compute", nil); err != nil {
return err
}
return ctrl.NewControllerManagedBy(mgr).
Named(AggregatesControllerName).
For(&kvmv1.Hypervisor{}, builder.WithPredicates(utils.LifecycleEnabledPredicate)).
Complete(ac)
}