-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaggregates_controller.go
More file actions
231 lines (198 loc) · 8.72 KB
/
aggregates_controller.go
File metadata and controls
231 lines (198 loc) · 8.72 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
/*
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"
"errors"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
logger "sigs.k8s.io/controller-runtime/pkg/log"
"github.com/gophercloud/gophercloud/v2"
kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/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;list;watch;create;update;patch;delete
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
}
base := hv.DeepCopy()
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
}
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
condition := metav1.Condition{
Type: kvmv1.ConditionTypeAggregatesUpdated,
Status: metav1.ConditionFalse,
Reason: kvmv1.ConditionReasonFailed,
Message: fmt.Errorf("failed to apply aggregates: %w", err).Error(),
}
if meta.SetStatusCondition(&hv.Status.Conditions, condition) {
if err2 := ac.Status().Patch(ctx, hv, k8sclient.MergeFromWithOptions(base,
k8sclient.MergeFromWithOptimisticLock{}), k8sclient.FieldOwner(AggregatesControllerName)); err2 != nil {
return ctrl.Result{}, errors.Join(err, err2)
}
}
return ctrl.Result{}, err
}
hv.Status.Aggregates = aggregates
}
// Set the condition based on the determined desired state
meta.SetStatusCondition(&hv.Status.Conditions, desiredCondition)
if equality.Semantic.DeepEqual(base, hv) {
return ctrl.Result{}, nil
}
return ctrl.Result{}, ac.Status().Patch(ctx, hv, k8sclient.MergeFromWithOptions(base,
k8sclient.MergeFromWithOptimisticLock{}), k8sclient.FieldOwner(AggregatesControllerName))
}
// 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 meta.IsStatusConditionTrue(hv.Status.Conditions, kvmv1.ConditionTypeTerminating) {
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
}
// registerWithManager registers the controller with the Manager without acquiring OpenStack clients.
// This is useful for testing where clients are injected directly.
func (ac *AggregatesController) registerWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
Named(AggregatesControllerName).
For(&kvmv1.Hypervisor{}, builder.WithPredicates(utils.LifecycleEnabledPredicate)).
Complete(ac)
}
// SetupWithManager sets up the controller with the Manager.
func (ac *AggregatesController) SetupWithManager(mgr ctrl.Manager) error {
ctx := context.Background()
_ = logger.FromContext(ctx)
var err error
if ac.computeClient, err = openstack.GetServiceClient(ctx, "compute", nil); err != nil {
return err
}
return ac.registerWithManager(mgr)
}