Skip to content

Commit daf475f

Browse files
fix(vm): choose correct active pod (#967)
Сhoose correct active pod --------- Signed-off-by: Yaroslav Borbat <yaroslav.borbat@flant.com>
1 parent b6edfc9 commit daf475f

6 files changed

Lines changed: 259 additions & 241 deletions

File tree

images/virtualization-artifact/pkg/builder/vm/option.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ limitations under the License.
1717
package vm
1818

1919
import (
20+
"k8s.io/apimachinery/pkg/api/resource"
21+
2022
"github.com/deckhouse/virtualization-controller/pkg/builder/meta"
2123
"github.com/deckhouse/virtualization/api/core/v1alpha2"
2224
)
2325

24-
type Option func(vmop *v1alpha2.VirtualMachine)
26+
type Option func(vm *v1alpha2.VirtualMachine)
2527

2628
var (
2729
WithName = meta.WithName[*v1alpha2.VirtualMachine]
@@ -32,3 +34,18 @@ var (
3234
WithAnnotation = meta.WithAnnotation[*v1alpha2.VirtualMachine]
3335
WithAnnotations = meta.WithAnnotations[*v1alpha2.VirtualMachine]
3436
)
37+
38+
func WithCPU(cores int, coreFraction *string) Option {
39+
return func(vm *v1alpha2.VirtualMachine) {
40+
vm.Spec.CPU.Cores = cores
41+
if coreFraction != nil {
42+
vm.Spec.CPU.CoreFraction = *coreFraction
43+
}
44+
}
45+
}
46+
47+
func WithMemory(size resource.Quantity) Option {
48+
return func(vm *v1alpha2.VirtualMachine) {
49+
vm.Spec.Memory.Size = size
50+
}
51+
}

images/virtualization-artifact/pkg/common/kvvm/kvvm.go

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ limitations under the License.
1717
package kvvm
1818

1919
import (
20+
"cmp"
2021
"context"
2122
"fmt"
23+
"slices"
2224

2325
corev1 "k8s.io/api/core/v1"
2426
"k8s.io/apimachinery/pkg/labels"
@@ -63,29 +65,38 @@ func FindPodByKVVMI(ctx context.Context, cli client.Client, kvvmi *virtv1.Virtua
6365
}
6466

6567
func GetVMPod(kvvmi *virtv1.VirtualMachineInstance, podList *corev1.PodList) *corev1.Pod {
66-
if len(podList.Items) == 0 {
68+
if kvvmi == nil {
6769
return nil
6870
}
69-
if len(podList.Items) == 1 {
70-
return &podList.Items[0]
71-
}
7271

73-
// If migration is completed - return the target pod.
74-
if kvvmi != nil && kvvmi.Status.MigrationState != nil && kvvmi.Status.MigrationState.Completed {
75-
for _, pod := range podList.Items {
76-
if pod.Name == kvvmi.Status.MigrationState.TargetPod {
77-
return &pod
78-
}
72+
var pods []corev1.Pod
73+
for _, pod := range podList.Items {
74+
if pod.Spec.NodeName != kvvmi.Status.NodeName {
75+
continue
7976
}
77+
if _, exists := kvvmi.Status.ActivePods[pod.GetUID()]; !exists {
78+
continue
79+
}
80+
pods = append(pods, pod)
8081
}
8182

82-
// Return the first Running Pod or just a first Pod.
83-
for _, pod := range podList.Items {
83+
switch len(pods) {
84+
case 0:
85+
return nil
86+
case 1:
87+
return &pods[0]
88+
}
89+
90+
slices.SortFunc(pods, func(a, b corev1.Pod) int {
91+
return cmp.Compare(a.GetCreationTimestamp().UnixNano(), b.GetCreationTimestamp().UnixNano())
92+
})
93+
94+
for _, pod := range pods {
8495
if pod.Status.Phase == corev1.PodRunning {
8596
return &pod
8697
}
8798
}
88-
return &podList.Items[0]
99+
return &pods[0]
89100
}
90101

91102
// DeletePodByKVVMI deletes pod by kvvmi.

images/virtualization-artifact/pkg/controller/powerstate/operations_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ func TestRestartVM(t *testing.T) {
3535
const (
3636
vmName = "test-vm"
3737
vmNamespace = "test-namespace"
38-
uid types.UID = "test-uid"
38+
nodeName = "worker-01"
39+
kvvmiUID types.UID = "test-kvvmi-uid"
40+
podUID types.UID = "test-pod-uid"
3941
)
4042
key := types.NamespacedName{
4143
Name: vmName,
@@ -57,21 +59,31 @@ func TestRestartVM(t *testing.T) {
5759
ObjectMeta: metav1.ObjectMeta{
5860
Name: vmName,
5961
Namespace: vmNamespace,
60-
UID: uid,
62+
UID: kvvmiUID,
6163
},
6264
TypeMeta: metav1.TypeMeta{
6365
Kind: "VirtualMachineInstance",
6466
APIVersion: virtv1.SchemeGroupVersion.String(),
6567
},
68+
Status: virtv1.VirtualMachineInstanceStatus{
69+
NodeName: nodeName,
70+
ActivePods: map[types.UID]string{
71+
podUID: vmName,
72+
},
73+
},
6674
}
6775
pod := &corev1.Pod{
6876
ObjectMeta: metav1.ObjectMeta{
6977
Name: vmName,
7078
Namespace: vmNamespace,
7179
Labels: map[string]string{
7280
virtv1.AppLabel: "virt-launcher",
73-
virtv1.CreatedByLabel: string(uid),
81+
virtv1.CreatedByLabel: string(kvvmiUID),
7482
},
83+
UID: podUID,
84+
},
85+
Spec: corev1.PodSpec{
86+
NodeName: nodeName,
7587
},
7688
TypeMeta: metav1.TypeMeta{
7789
Kind: "Pod",
@@ -118,7 +130,7 @@ func TestRestartVM(t *testing.T) {
118130
require.Len(t, newKVVM.Status.StateChangeRequests, 2)
119131
require.Equal(t, virtv1.StopRequest, newKVVM.Status.StateChangeRequests[0].Action)
120132
require.NotNil(t, newKVVM.Status.StateChangeRequests[0].UID)
121-
require.Equal(t, uid, *newKVVM.Status.StateChangeRequests[0].UID)
133+
require.Equal(t, kvvmiUID, *newKVVM.Status.StateChangeRequests[0].UID)
122134
require.Equal(t, virtv1.StartRequest, newKVVM.Status.StateChangeRequests[1].Action)
123135

124136
pod := &corev1.Pod{}

0 commit comments

Comments
 (0)