Skip to content

Commit

Permalink
Add request-evict-only annotation to virt-launcher pods
Browse files Browse the repository at this point in the history
Add the `descheduler.alpha.kubernetes.io/request-evict-only` annotation
to virt-launcher pods.
This annotation should be added to already running pods too.
ref: kubevirt/community#258

Signed-off-by: fossedihelm <[email protected]>
  • Loading branch information
fossedihelm committed Jun 11, 2024
1 parent a7556cd commit a70333b
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 17 deletions.
1 change: 1 addition & 0 deletions pkg/virt-controller/services/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ go_library(
"//pkg/util/net/dns:go_default_library",
"//pkg/virt-config:go_default_library",
"//pkg/virt-controller/network:go_default_library",
"//pkg/virt-controller/watch/descheduler:go_default_library",
"//pkg/virt-controller/watch/topology:go_default_library",
"//pkg/virt-launcher/virtwrap/api:go_default_library",
"//pkg/virt-operator/util:go_default_library",
Expand Down
3 changes: 3 additions & 0 deletions pkg/virt-controller/services/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import (
"kubevirt.io/kubevirt/pkg/util/net/dns"
virtconfig "kubevirt.io/kubevirt/pkg/virt-config"
"kubevirt.io/kubevirt/pkg/virt-controller/network"
"kubevirt.io/kubevirt/pkg/virt-controller/watch/descheduler"
"kubevirt.io/kubevirt/pkg/virt-controller/watch/topology"
"kubevirt.io/kubevirt/pkg/virt-launcher/virtwrap/api"
operatorutil "kubevirt.io/kubevirt/pkg/virt-operator/util"
Expand Down Expand Up @@ -1365,6 +1366,8 @@ func generatePodAnnotations(vmi *v1.VirtualMachineInstance, config *virtconfig.C
// Set this annotation now to indicate that the newly created virt-launchers will use
// unix sockets as a transport for migration
annotationsSet[v1.MigrationTransportUnixAnnotation] = "true"
annotationsSet[descheduler.EvictOnlyAnnotation] = ""

return annotationsSet, nil
}

Expand Down
19 changes: 10 additions & 9 deletions pkg/virt-controller/services/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,15 +418,16 @@ var _ = Describe("Template", func() {
v1.VirtualMachineNameLabel: "testvmi",
}))
Expect(pod.ObjectMeta.Annotations).To(Equal(map[string]string{
v1.DomainAnnotation: "testvmi",
"test": "shouldBeInPod",
hooks.HookSidecarListAnnotationName: `[{"image": "some-image:v1", "imagePullPolicy": "IfNotPresent"}]`,
"pre.hook.backup.velero.io/container": "compute",
"pre.hook.backup.velero.io/command": "[\"/usr/bin/virt-freezer\", \"--freeze\", \"--name\", \"testvmi\", \"--namespace\", \"testns\"]",
"post.hook.backup.velero.io/container": "compute",
"post.hook.backup.velero.io/command": "[\"/usr/bin/virt-freezer\", \"--unfreeze\", \"--name\", \"testvmi\", \"--namespace\", \"testns\"]",
"kubevirt.io/migrationTransportUnix": "true",
"kubectl.kubernetes.io/default-container": "compute",
v1.DomainAnnotation: "testvmi",
"test": "shouldBeInPod",
hooks.HookSidecarListAnnotationName: `[{"image": "some-image:v1", "imagePullPolicy": "IfNotPresent"}]`,
"pre.hook.backup.velero.io/container": "compute",
"pre.hook.backup.velero.io/command": "[\"/usr/bin/virt-freezer\", \"--freeze\", \"--name\", \"testvmi\", \"--namespace\", \"testns\"]",
"post.hook.backup.velero.io/container": "compute",
"post.hook.backup.velero.io/command": "[\"/usr/bin/virt-freezer\", \"--unfreeze\", \"--name\", \"testvmi\", \"--namespace\", \"testns\"]",
"kubevirt.io/migrationTransportUnix": "true",
"kubectl.kubernetes.io/default-container": "compute",
"descheduler.alpha.kubernetes.io/request-evict-only": "",
}))
Expect(pod.ObjectMeta.OwnerReferences).To(Equal([]metav1.OwnerReference{{
APIVersion: v1.VirtualMachineInstanceGroupVersionKind.GroupVersion().String(),
Expand Down
2 changes: 2 additions & 0 deletions pkg/virt-controller/watch/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ go_library(
"//pkg/virt-controller/network:go_default_library",
"//pkg/virt-controller/services:go_default_library",
"//pkg/virt-controller/watch/clone:go_default_library",
"//pkg/virt-controller/watch/descheduler:go_default_library",
"//pkg/virt-controller/watch/drain/disruptionbudget:go_default_library",
"//pkg/virt-controller/watch/drain/evacuation:go_default_library",
"//pkg/virt-controller/watch/migration:go_default_library",
Expand Down Expand Up @@ -130,6 +131,7 @@ go_test(
"//pkg/virt-controller/network:go_default_library",
"//pkg/virt-controller/services:go_default_library",
"//pkg/virt-controller/watch/clone:go_default_library",
"//pkg/virt-controller/watch/descheduler:go_default_library",
"//pkg/virt-controller/watch/drain/disruptionbudget:go_default_library",
"//pkg/virt-controller/watch/drain/evacuation:go_default_library",
"//pkg/virt-controller/watch/migration:go_default_library",
Expand Down
8 changes: 8 additions & 0 deletions pkg/virt-controller/watch/descheduler/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["descheduler.go"],
importpath = "kubevirt.io/kubevirt/pkg/virt-controller/watch/descheduler",
visibility = ["//visibility:public"],
)
25 changes: 25 additions & 0 deletions pkg/virt-controller/watch/descheduler/descheduler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* This file is part of the KubeVirt project
*
* 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.
*
* Copyright The KubeVirt Authors
*
*/

package descheduler

// EvictOnlyAnnotation indicates pods whose eviction is not expected to be completed right away.
// Instead, an eviction request is expected to be intercepted by an external component which will initiate the
// eviction process for the pod.
const EvictOnlyAnnotation = "descheduler.alpha.kubernetes.io/request-evict-only"
16 changes: 9 additions & 7 deletions pkg/virt-controller/watch/vmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"encoding/json"
"errors"
"fmt"
"maps"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -63,6 +64,7 @@ import (
traceUtils "kubevirt.io/kubevirt/pkg/util/trace"
virtconfig "kubevirt.io/kubevirt/pkg/virt-config"
"kubevirt.io/kubevirt/pkg/virt-controller/services"
"kubevirt.io/kubevirt/pkg/virt-controller/watch/descheduler"
)

const (
Expand Down Expand Up @@ -1134,14 +1136,14 @@ func (c *VMIController) sync(vmi *virtv1.VirtualMachineInstance, pod *k8sv1.Pod,
}

if !isTempPod(pod) && controller.IsPodReady(pod) {
newAnnotations := network.GeneratePodAnnotations(vmi.Spec.Networks, vmi.Spec.Domain.Devices.Interfaces, pod.Annotations[networkv1.NetworkStatusAnnot], c.clusterConfig.GetNetworkBindings())
if len(newAnnotations) != 0 {
patchedPod, err := c.syncPodAnnotations(pod, newAnnotations)
if err != nil {
return &syncErrorImpl{err, controller.FailedPodPatchReason}
}
*pod = *patchedPod
newAnnotations := map[string]string{descheduler.EvictOnlyAnnotation: ""}
maps.Copy(newAnnotations, network.GeneratePodAnnotations(vmi.Spec.Networks, vmi.Spec.Domain.Devices.Interfaces, pod.Annotations[networkv1.NetworkStatusAnnot], c.clusterConfig.GetNetworkBindings()))

patchedPod, err := c.syncPodAnnotations(pod, newAnnotations)
if err != nil {
return &syncErrorImpl{err, controller.FailedPodPatchReason}
}
*pod = *patchedPod

hotplugVolumes := controller.GetHotplugVolumes(vmi, pod)
hotplugAttachmentPods, err := controller.AttachmentPods(pod, c.podIndexer)
Expand Down
4 changes: 3 additions & 1 deletion pkg/virt-controller/watch/vmi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import (
virtconfig "kubevirt.io/kubevirt/pkg/virt-config"
"kubevirt.io/kubevirt/pkg/virt-controller/network"
"kubevirt.io/kubevirt/pkg/virt-controller/services"
"kubevirt.io/kubevirt/pkg/virt-controller/watch/descheduler"
"kubevirt.io/kubevirt/pkg/virt-controller/watch/topology"
)

Expand Down Expand Up @@ -3942,7 +3943,8 @@ func setDataVolumeCondition(dv *cdiv1.DataVolume, cond cdiv1.DataVolumeCondition

func NewPodForVirtualMachine(vmi *virtv1.VirtualMachineInstance, phase k8sv1.PodPhase) *k8sv1.Pod {
podAnnotations := map[string]string{
virtv1.DomainAnnotation: vmi.Name,
virtv1.DomainAnnotation: vmi.Name,
descheduler.EvictOnlyAnnotation: "",
}
return &k8sv1.Pod{
ObjectMeta: metav1.ObjectMeta{
Expand Down

0 comments on commit a70333b

Please sign in to comment.