-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhypervisor_controller.go
More file actions
375 lines (328 loc) · 13.4 KB
/
Copy pathhypervisor_controller.go
File metadata and controls
375 lines (328 loc) · 13.4 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
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"
"strings"
"time"
kvmv1 "github.com/cobaltcore-dev/openstack-hypervisor-operator/api/v1"
golibvirt "github.com/digitalocean/go-libvirt"
"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/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
logger "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/source"
"github.com/cobaltcore-dev/kvm-node-agent/internal/certificates"
"github.com/cobaltcore-dev/kvm-node-agent/internal/evacuation"
"github.com/cobaltcore-dev/kvm-node-agent/internal/libvirt"
"github.com/cobaltcore-dev/kvm-node-agent/internal/sys"
"github.com/cobaltcore-dev/kvm-node-agent/internal/systemd"
)
// HypervisorReconciler reconciles a Hypervisor object
type HypervisorReconciler struct {
client.Client
Scheme *runtime.Scheme
Systemd systemd.Interface
Libvirt libvirt.Interface
osDescriptor *systemd.Descriptor
evacuateOnReboot bool
// Channel that can be used to trigger reconcile events.
reconcileCh chan event.GenericEvent
}
const (
OSUpdateType = "OperatingSystemUpdate"
LibVirtType = "LibVirtConnection"
)
// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=hypervisors,verbs=get;list;watch;update;patch;delete
// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=hypervisors/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=hypervisors/finalizers,verbs=update
// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=evictions,verbs=get;create
// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=migrations,verbs=get;list;watch;create
// +kubebuilder:rbac:groups=kvm.cloud.sap,resources=migrations/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=cert-manager.io,resources=certificates,verbs=get;list;watch;create;update;patch
func (r *HypervisorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := logger.FromContext(ctx, "controller", "hypervisor")
// only reconcile the node I am running on
if req.Name != sys.Hostname {
panic(fmt.Sprintf("reconciling hypervisor %s, but I am running on %s", req.Name, sys.Hostname))
}
log.Info("Reconcile", "name", req.Name, "namespace", req.Namespace)
var hypervisor kvmv1.Hypervisor
if err := r.Get(ctx, req.NamespacedName, &hypervisor); err != nil {
// ignore not found errors, could be deleted
return ctrl.Result{}, client.IgnoreNotFound(err)
}
base := hypervisor.DeepCopy()
// ====================================================================================================
// Systemd
// ====================================================================================================
if r.Systemd.IsConnected() {
var unitNames = []string{"libvirtd.service", "openvswitch-switch.service"}
units, err := r.Systemd.ListUnitsByNames(ctx, unitNames)
if err != nil {
log.Error(err, "unable to list units")
return ctrl.Result{}, err
}
var unitReasonsMap = map[string]string{
"active": "Running",
"inactive": "Stopped",
"failed": "Failed",
}
var unitStatusesMap = map[string]metav1.ConditionStatus{
"active": metav1.ConditionTrue,
"inactive": metav1.ConditionFalse,
"failed": metav1.ConditionFalse,
}
for _, unit := range units {
reason := unitReasonsMap[unit.ActiveState]
status := unitStatusesMap[unit.ActiveState]
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
Type: unit.Name,
Status: status,
Reason: reason,
Message: fmt.Sprintf("%s: %s, %s", unit.Name, unit.ActiveState, unit.LoadState),
})
}
if r.osDescriptor != nil && hypervisor.Status.OperatingSystem.Version == "" {
for _, line := range r.osDescriptor.OperatingSystemReleaseData {
// split line in two strings at the first =
splitLine := strings.SplitN(line, "=", 2)
switch splitLine[0] {
case "PRETTY_NAME":
hypervisor.Status.OperatingSystem.PrettyVersion = splitLine[1]
case "GARDENLINUX_VERSION":
hypervisor.Status.OperatingSystem.Version = splitLine[1]
case "GARDENLINUX_COMMIT_ID_LONG":
hypervisor.Status.OperatingSystem.GardenLinuxCommitID = splitLine[1]
case "GARDENLINUX_FEATURES":
hypervisor.Status.OperatingSystem.GardenLinuxFeatures = strings.Split(splitLine[1], ",")
case "VARIANT_ID":
hypervisor.Status.OperatingSystem.VariantID = splitLine[1]
}
}
hypervisor.Status.OperatingSystem.KernelVersion = r.osDescriptor.KernelVersion
hypervisor.Status.OperatingSystem.KernelRelease = r.osDescriptor.KernelRelease
hypervisor.Status.OperatingSystem.KernelName = r.osDescriptor.KernelName
hypervisor.Status.OperatingSystem.HardwareVendor = r.osDescriptor.HardwareVendor
hypervisor.Status.OperatingSystem.HardwareModel = r.osDescriptor.HardwareModel
hypervisor.Status.OperatingSystem.HardwareSerial = r.osDescriptor.HardwareSerial
hypervisor.Status.OperatingSystem.FirmwareVersion = r.osDescriptor.FirmwareVersion
hypervisor.Status.OperatingSystem.FirmwareVendor = r.osDescriptor.FirmwareVendor
hypervisor.Status.OperatingSystem.FirmwareDate = metav1.NewTime(time.UnixMicro(r.osDescriptor.FirmwareDate))
}
if hypervisor.Spec.EvacuateOnReboot != r.evacuateOnReboot {
if hypervisor.Spec.EvacuateOnReboot {
e := &evacuation.EvictionController{Client: r.Client}
if err := r.Systemd.EnableShutdownInhibit(ctx, e.EvictCurrentHost); err != nil {
return ctrl.Result{}, err
}
} else {
if err := r.Systemd.DisableShutdownInhibit(); err != nil {
return ctrl.Result{}, err
}
}
r.evacuateOnReboot = hypervisor.Spec.EvacuateOnReboot
}
}
// ====================================================================================================
// Libvirt
// ====================================================================================================
// Try (re)connect to Libvirt, update status
if meta.IsStatusConditionFalse(hypervisor.Status.Conditions, "libvirtd.service") {
// libvirtd service is not running, skip libvirt connection, systemd socket activation could
// be blocking the libvirt connection. Could reconnect with next reconcile loop.
log.Info("libvirtd.service is not running, skipping libvirt connection")
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
Type: LibVirtType,
Status: metav1.ConditionFalse,
Message: "libvirtd service is not running",
Reason: "LibVirtServiceNotRunning",
})
} else if err := r.Libvirt.Connect(); err != nil {
log.Error(err, "unable to connect to Libvirt system bus")
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
Type: LibVirtType,
Status: metav1.ConditionFalse,
Message: fmt.Sprintf("unable to connect to libvirtd: %v", err),
Reason: "ConnectFailed",
})
} else {
// We're connected.
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
Type: LibVirtType,
Status: metav1.ConditionTrue,
Reason: "Connected",
})
var err error
hypervisor, err = r.Libvirt.Process(hypervisor)
if err != nil {
log.Error(err, "unable to process hypervisor via libvirt")
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
Type: LibVirtType,
Status: metav1.ConditionFalse,
Message: fmt.Sprintf("unable to process hypervisor via libvirt: %v", err),
Reason: "ProcessFailed",
})
return ctrl.Result{}, err
}
}
// Reconcile operating system update
if hypervisor.Spec.OperatingSystemVersion != "" &&
// only update if the version is different to current running version
hypervisor.Spec.OperatingSystemVersion != hypervisor.Status.OperatingSystem.Version &&
// only update if the version is different to the installed version
hypervisor.Spec.OperatingSystemVersion != hypervisor.Status.Update.Installed {
if hypervisor.Status.Update.Retry == 0 {
// we reached the retry limit, unset the version to stop the update
// failed message of past retries is still available in the conditions
// reset retry count
hypervisor.Status.Update.Retry = 3
if err := r.Status().Patch(ctx, &hypervisor, client.MergeFromWithOptions(base, client.MergeFromWithOptimisticLock{})); err != nil {
log.Error(err, "unable to update hypervisor status spec")
return ctrl.Result{}, err
}
hypervisor.Spec.OperatingSystemVersion = ""
if err := r.Status().Patch(ctx, &hypervisor, client.MergeFromWithOptions(base, client.MergeFromWithOptimisticLock{})); err != nil {
log.Error(err, "unable to update hypervisor spec")
return ctrl.Result{}, err
}
// Todo: include some timeout?
return ctrl.Result{}, nil
}
// Reconcile operating system update
running, err := r.Systemd.ReconcileSysUpdate(ctx, &hypervisor)
// failed
if err != nil {
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
Type: OSUpdateType,
Status: metav1.ConditionFalse,
Reason: "Stopped",
Message: err.Error(),
})
if !errors.Is(err, systemd.ErrFailed) {
log.Error(err, "error while reconcile operating system update")
}
// decrease retry count
hypervisor.Status.Update.Retry--
}
// started
if !hypervisor.Status.Update.InProgress && running {
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
Type: OSUpdateType,
Status: metav1.ConditionTrue,
Reason: "Running",
Message: fmt.Sprintf("Operating system update to %s is running",
hypervisor.Spec.OperatingSystemVersion),
})
}
// finished
if !running && err == nil {
meta.SetStatusCondition(&hypervisor.Status.Conditions, metav1.Condition{
Type: OSUpdateType,
Status: metav1.ConditionTrue,
Reason: "Completed",
Message: fmt.Sprintf("Operating system update %s is installed",
hypervisor.Spec.OperatingSystemVersion),
})
hypervisor.Status.Update.Installed = hypervisor.Spec.OperatingSystemVersion
}
hypervisor.Status.Update.InProgress = running
}
if hypervisor.Spec.CreateCertManagerCertificate {
if err := certificates.EnsureCertificate(ctx, r.Client, sys.Hostname); err != nil {
return ctrl.Result{}, err
}
}
if err := r.Status().Patch(ctx, &hypervisor, client.MergeFromWithOptions(base, client.MergeFromWithOptimisticLock{})); err != nil {
log.Error(err, "unable to update hypervisor status")
return ctrl.Result{}, err
}
return ctrl.Result{RequeueAfter: 1 * time.Minute}, nil
}
// Trigger a reconcile event for the managed hypervisor through the
// event channel which is watched by the controller manager.
func (r *HypervisorReconciler) triggerReconcile() {
r.reconcileCh <- event.GenericEvent{
Object: &kvmv1.Hypervisor{
TypeMeta: metav1.TypeMeta{
Kind: "Hypervisor",
APIVersion: "kvm.cloud.sap/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: sys.Hostname,
Namespace: sys.Namespace,
},
},
}
}
// Start is called when the manager starts. It starts the libvirt
// event subscription to receive events when the hypervisor needs to be
// reconciled.
func (r *HypervisorReconciler) Start(ctx context.Context) error {
log := logger.FromContext(ctx, "controller", "hypervisor")
log.Info("starting libvirt event subscription")
// Ensure we're connected to libvirt.
if err := r.Libvirt.Connect(); err != nil {
log.Error(err, "unable to connect to libvirt")
return err
}
// Run a ticker which reconciles the hypervisor resource every minute.
// This ensures that we periodically reconcile the hypervisor even
// if no events are received from libvirt.
go func() {
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
for {
select {
case <-ticker.C:
r.triggerReconcile()
case <-ctx.Done():
return
}
}
}()
// Domain lifecycle events impact the list of active/inactive domains,
// as well as the allocation of resources on the hypervisor.
r.Libvirt.WatchDomainChanges(
golibvirt.DomainEventIDLifecycle,
"reconcile-on-domain-lifecycle",
func(_ context.Context, _ any) { r.triggerReconcile() },
)
return nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *HypervisorReconciler) SetupWithManager(mgr ctrl.Manager) error {
ctx := context.Background()
var err error
if r.osDescriptor, err = r.Systemd.Describe(ctx); err != nil {
return fmt.Errorf("unable to get Systemd hostname describe(): %w", err)
}
// Prepare an event channel that will trigger a reconcile event.
r.reconcileCh = make(chan event.GenericEvent)
src := source.Channel(r.reconcileCh, &handler.EnqueueRequestForObject{})
// Run the Start(ctx context.Context) method when the manager starts.
if err := mgr.Add(r); err != nil {
return err
}
return ctrl.NewControllerManagedBy(mgr).
For(&kvmv1.Hypervisor{}).
WatchesRawSource(src).
Complete(r)
}