Skip to content

Commit 6ea74bd

Browse files
author
Valeriy Khorunzhin
committed
final
Signed-off-by: Valeriy Khorunzhin <valeriy.khorunzhin@flant.com>
1 parent 1d0a122 commit 6ea74bd

6 files changed

Lines changed: 19 additions & 55 deletions

File tree

api/core/v1alpha2/vmcondition/condition.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ const (
169169
ReasonVirtualMachineRunning RunningReason = "Running"
170170
ReasonInternalVirtualMachineError RunningReason = "InternalVirtualMachineError"
171171
ReasonPodNotStarted RunningReason = "PodNotStarted"
172-
ReasonPodContainerCreating RunningReason = "PodContainerCreating"
173172
ReasonPodVolumeErrors RunningReason = "PodVolumeErrors"
174173
ReasonPodTerminating RunningReason = "PodTerminating"
175174
ReasonPodNotFound RunningReason = "PodNotFound"

images/virtualization-artifact/pkg/controller/vm/internal/lifecycle.go

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ type LifeCycleHandler struct {
5555
recorder eventrecord.EventRecorderLogger
5656
}
5757

58+
type podVolumeErrorEvent struct {
59+
Reason string
60+
Message string
61+
}
62+
5863
func (h *LifeCycleHandler) Handle(ctx context.Context, s state.VirtualMachineState) (reconcile.Result, error) {
5964
if s.VirtualMachine().IsEmpty() {
6065
return reconcile.Result{}, nil
@@ -111,19 +116,7 @@ func (h *LifeCycleHandler) Handle(ctx context.Context, s state.VirtualMachineSta
111116
if volumeErr := h.checkPodVolumeErrors(ctx, changed, log); volumeErr != nil {
112117
cb.Status(metav1.ConditionFalse).
113118
Reason(vmcondition.ReasonPodVolumeErrors).
114-
Message(fmt.Sprintf("Volume errors detected on Pod: %s", volumeErr.Error()))
115-
conditions.SetCondition(cb, &changed.Status.Conditions)
116-
return reconcile.Result{}, nil
117-
}
118-
119-
isVMInContainerCreating, err := h.isVMInContainerCreatingState(ctx, changed, log)
120-
if err != nil {
121-
return reconcile.Result{}, err
122-
}
123-
if isVMInContainerCreating {
124-
cb.Status(metav1.ConditionFalse).
125-
Reason(vmcondition.ReasonPodContainerCreating).
126-
Message("Pod is in ContainerCreating phase. Check the pod for more details.")
119+
Message(fmt.Sprintf("Error attaching block devices to virtual machine: %s: %s", volumeErr.Reason, volumeErr.Message))
127120
conditions.SetCondition(cb, &changed.Status.Conditions)
128121
return reconcile.Result{}, nil
129122
}
@@ -150,8 +143,8 @@ func (h *LifeCycleHandler) syncRunning(ctx context.Context, vm *v1alpha2.Virtual
150143

151144
if volumeError := h.checkPodVolumeErrors(ctx, vm, log); volumeError != nil {
152145
cb.Status(metav1.ConditionFalse).
153-
Reason(vmcondition.ReasonPodNotStarted).
154-
Message(volumeError.Error())
146+
Reason(vmcondition.ReasonPodVolumeErrors).
147+
Message(fmt.Sprintf("Error attaching block devices to virtual machine: %s: %s", volumeError.Reason, volumeError.Message))
155148
conditions.SetCondition(cb, &vm.Status.Conditions)
156149
return
157150
}
@@ -163,7 +156,7 @@ func (h *LifeCycleHandler) syncRunning(ctx context.Context, vm *v1alpha2.Virtual
163156
if podScheduled.Message != "" {
164157
cb.Status(metav1.ConditionFalse).
165158
Reason(vmcondition.ReasonPodNotStarted).
166-
Message(fmt.Sprintf("%s: %s", podScheduled.Reason, podScheduled.Message))
159+
Message(fmt.Sprintf("Could not schedule the virtual machine: %s: %s", podScheduled.Reason, podScheduled.Message))
167160
conditions.SetCondition(cb, &vm.Status.Conditions)
168161
}
169162

@@ -240,27 +233,7 @@ func (h *LifeCycleHandler) syncRunning(ctx context.Context, vm *v1alpha2.Virtual
240233
conditions.SetCondition(cb, &vm.Status.Conditions)
241234
}
242235

243-
func (h *LifeCycleHandler) isVMInContainerCreatingState(ctx context.Context, vm *v1alpha2.VirtualMachine, log *slog.Logger) (bool, error) {
244-
var podList corev1.PodList
245-
err := h.client.List(ctx, &podList, &client.ListOptions{
246-
Namespace: vm.Namespace,
247-
LabelSelector: labels.SelectorFromSet(map[string]string{
248-
virtv1.VirtualMachineNameLabel: vm.Name,
249-
}),
250-
})
251-
if err != nil {
252-
log.Error("Failed to list pods", "error", err)
253-
return false, err
254-
}
255-
256-
if len(podList.Items) == 1 {
257-
return isContainerCreating(&podList.Items[0]), nil
258-
}
259-
260-
return false, nil
261-
}
262-
263-
func (h *LifeCycleHandler) checkPodVolumeErrors(ctx context.Context, vm *v1alpha2.VirtualMachine, log *slog.Logger) error {
236+
func (h *LifeCycleHandler) checkPodVolumeErrors(ctx context.Context, vm *v1alpha2.VirtualMachine, log *slog.Logger) *podVolumeErrorEvent {
264237
var podList corev1.PodList
265238
err := h.client.List(ctx, &podList, &client.ListOptions{
266239
Namespace: vm.Namespace,
@@ -297,7 +270,7 @@ func isContainerCreating(pod *corev1.Pod) bool {
297270
return false
298271
}
299272

300-
func (h *LifeCycleHandler) getPodVolumeError(ctx context.Context, pod *corev1.Pod, log *slog.Logger) error {
273+
func (h *LifeCycleHandler) getPodVolumeError(ctx context.Context, pod *corev1.Pod, log *slog.Logger) *podVolumeErrorEvent {
301274
if !isContainerCreating(pod) {
302275
return nil
303276
}
@@ -323,7 +296,10 @@ func (h *LifeCycleHandler) getPodVolumeError(ctx context.Context, pod *corev1.Po
323296
return a.LastTimestamp.Compare(b.LastTimestamp.Time)
324297
})
325298
if last.Reason == watcher.ReasonFailedAttachVolume || last.Reason == watcher.ReasonFailedMount {
326-
return fmt.Errorf("%s: %s", last.Reason, last.Message)
299+
return &podVolumeErrorEvent{
300+
Reason: last.Reason,
301+
Message: last.Message,
302+
}
327303
}
328304

329305
return nil

images/virtualization-artifact/pkg/controller/vm/internal/watcher/pod_watcher.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package watcher
1919
import (
2020
"context"
2121
"fmt"
22-
"reflect"
2322

2423
corev1 "k8s.io/api/core/v1"
2524
"k8s.io/apimachinery/pkg/types"
@@ -67,8 +66,7 @@ func (w *PodWatcher) Watch(mgr manager.Manager, ctr controller.Controller) error
6766
DeleteFunc: func(e event.TypedDeleteEvent[*corev1.Pod]) bool { return true },
6867
UpdateFunc: func(e event.TypedUpdateEvent[*corev1.Pod]) bool {
6968
return e.ObjectOld.Status.Phase != e.ObjectNew.Status.Phase ||
70-
e.ObjectOld.Annotations[annotations.AnnNetworksStatus] != e.ObjectNew.Annotations[annotations.AnnNetworksStatus] ||
71-
!reflect.DeepEqual(e.ObjectOld.Status.ContainerStatuses, e.ObjectNew.Status.ContainerStatuses)
69+
e.ObjectOld.Annotations[annotations.AnnNetworksStatus] != e.ObjectNew.Annotations[annotations.AnnNetworksStatus]
7270
},
7371
},
7472
),

images/virtualization-artifact/pkg/controller/vmbda/internal/life_cycle.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ func (h LifeCycleHandler) handleHotPlugPodIssues(
329329
cb.
330330
Status(metav1.ConditionFalse).
331331
Reason(vmbdacondition.HotPlugPodNotScheduled).
332-
Message(fmt.Sprintf("Hot plug pod not scheduled: %s: %s", c.Reason, c.Message))
332+
Message(fmt.Sprintf("Error attaching block device to virtual machine: %s: %s", c.Reason, c.Message))
333333
return true, nil
334334
}
335335
}
@@ -343,14 +343,9 @@ func (h LifeCycleHandler) handleHotPlugPodIssues(
343343
cb.
344344
Status(metav1.ConditionFalse).
345345
Reason(vmbdacondition.FailedAttachVolume).
346-
Message(fmt.Sprintf("Hot plug pod failed to attach volume: %s: %s", lastEvent.Reason, lastEvent.Message))
346+
Message(fmt.Sprintf("Error attaching block device to virtual machine: %s: %s", lastEvent.Reason, lastEvent.Message))
347347
return true, nil
348348
}
349-
350-
cb.Status(metav1.ConditionFalse).
351-
Reason(vmbdacondition.AttachmentRequestSent).
352-
Message(fmt.Sprintf("Pod %q is in ContainerCreating phase. Check the pod for more details.", hotPlugPod.Name))
353-
return true, nil
354349
}
355350

356351
return false, nil

images/virtualization-artifact/pkg/controller/vmbda/internal/watcher/hotplug_pod_watcher.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package watcher
1919
import (
2020
"context"
2121
"fmt"
22-
"reflect"
2322

2423
corev1 "k8s.io/api/core/v1"
2524
"k8s.io/apimachinery/pkg/types"
@@ -57,9 +56,7 @@ func (w *HotPlugPodWatcher) Watch(mgr manager.Manager, ctr controller.Controller
5756
CreateFunc: func(e event.TypedCreateEvent[*corev1.Pod]) bool { return true },
5857
DeleteFunc: func(e event.TypedDeleteEvent[*corev1.Pod]) bool { return true },
5958
UpdateFunc: func(e event.TypedUpdateEvent[*corev1.Pod]) bool {
60-
return e.ObjectOld.Status.Phase != e.ObjectNew.Status.Phase ||
61-
!reflect.DeepEqual(e.ObjectOld.Status.Conditions, e.ObjectNew.Status.Conditions) ||
62-
!reflect.DeepEqual(e.ObjectOld.Status.ContainerStatuses, e.ObjectNew.Status.ContainerStatuses)
59+
return e.ObjectOld.Status.Phase != e.ObjectNew.Status.Phase
6360
},
6461
},
6562
),

images/virtualization-artifact/pkg/controller/vmbda/vmbda_reconciler.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ func (r *Reconciler) SetupController(_ context.Context, mgr manager.Manager, ctr
8585
watcher.NewVirtualImageWatcherr(mgr.GetClient()),
8686
watcher.NewKVVMIWatcher(mgr.GetClient()),
8787
watcher.NewVolumeEventWatcher(mgr.GetClient()),
88-
watcher.NewHotPlugPodWatcher(mgr.GetClient()),
8988
} {
9089
err := w.Watch(mgr, ctr)
9190
if err != nil {

0 commit comments

Comments
 (0)