forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubevirtresource.go
205 lines (170 loc) · 6.74 KB
/
kubevirtresource.go
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
/*
* 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 2022 Red Hat, Inc.
*
*/
package testsuite
import (
"context"
"encoding/json"
"fmt"
"time"
"kubevirt.io/kubevirt/tests/libstorage"
"kubevirt.io/kubevirt/tests/framework/kubevirt"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/utils/pointer"
v1 "kubevirt.io/api/core/v1"
"kubevirt.io/client-go/kubecli"
virtconfig "kubevirt.io/kubevirt/pkg/virt-config"
"kubevirt.io/kubevirt/tests/flags"
"kubevirt.io/kubevirt/tests/framework/checks"
"kubevirt.io/kubevirt/tests/util"
)
var (
KubeVirtDefaultConfig v1.KubeVirtConfiguration
originalKV *v1.KubeVirt
)
func AdjustKubeVirtResource() {
virtClient := kubevirt.Client()
kv := util.GetCurrentKv(virtClient)
originalKV = kv.DeepCopy()
KubeVirtDefaultConfig = originalKV.Spec.Configuration
if !flags.ApplyDefaulte2eConfiguration {
return
}
// Rotate very often during the tests to ensure that things are working
kv.Spec.CertificateRotationStrategy = v1.KubeVirtCertificateRotateStrategy{SelfSigned: &v1.KubeVirtSelfSignConfiguration{
CA: &v1.CertConfig{
Duration: &metav1.Duration{Duration: 20 * time.Minute},
RenewBefore: &metav1.Duration{Duration: 12 * time.Minute},
},
Server: &v1.CertConfig{
Duration: &metav1.Duration{Duration: 14 * time.Minute},
RenewBefore: &metav1.Duration{Duration: 10 * time.Minute},
},
}}
// match default kubevirt-config testing resource
if kv.Spec.Configuration.DeveloperConfiguration == nil {
kv.Spec.Configuration.DeveloperConfiguration = &v1.DeveloperConfiguration{}
}
if kv.Spec.Configuration.DeveloperConfiguration.FeatureGates == nil {
kv.Spec.Configuration.DeveloperConfiguration.FeatureGates = []string{}
}
kv.Spec.Configuration.SeccompConfiguration = &v1.SeccompConfiguration{
VirtualMachineInstanceProfile: &v1.VirtualMachineInstanceProfile{
CustomProfile: &v1.CustomProfile{
LocalhostProfile: pointer.String("kubevirt/kubevirt.json"),
},
},
}
kv.Spec.Configuration.DeveloperConfiguration.FeatureGates = append(kv.Spec.Configuration.DeveloperConfiguration.FeatureGates,
virtconfig.CPUManager,
virtconfig.IgnitionGate,
virtconfig.SidecarGate,
virtconfig.SnapshotGate,
virtconfig.HostDiskGate,
virtconfig.VirtIOFSGate,
virtconfig.HotplugVolumesGate,
virtconfig.DownwardMetricsFeatureGate,
virtconfig.NUMAFeatureGate,
virtconfig.ExpandDisksGate,
virtconfig.WorkloadEncryptionSEV,
virtconfig.VMExportGate,
virtconfig.KubevirtSeccompProfile,
virtconfig.HotplugNetworkIfacesGate,
virtconfig.VMPersistentState,
virtconfig.VMLiveUpdateFeaturesGate,
virtconfig.AutoResourceLimitsGate,
)
if flags.DisableCustomSELinuxPolicy {
kv.Spec.Configuration.DeveloperConfiguration.FeatureGates = append(kv.Spec.Configuration.DeveloperConfiguration.FeatureGates,
virtconfig.DisableCustomSELinuxPolicy,
)
}
if kv.Spec.Configuration.NetworkConfiguration == nil {
testDefaultPermitSlirpInterface := true
kv.Spec.Configuration.NetworkConfiguration = &v1.NetworkConfiguration{
PermitSlirpInterface: &testDefaultPermitSlirpInterface,
}
}
storageClass, exists := libstorage.GetRWXFileSystemStorageClass()
if exists {
kv.Spec.Configuration.VMStateStorageClass = storageClass
}
data, err := json.Marshal(kv.Spec)
Expect(err).ToNot(HaveOccurred())
patchData := fmt.Sprintf(`[{ "op": "replace", "path": "/spec", "value": %s }]`, string(data))
adjustedKV, err := virtClient.KubeVirt(kv.Namespace).Patch(kv.Name, types.JSONPatchType, []byte(patchData), &metav1.PatchOptions{})
util.PanicOnError(err)
KubeVirtDefaultConfig = adjustedKV.Spec.Configuration
if checks.HasFeature(virtconfig.CPUManager) {
// CPUManager is not enabled in the control-plane node(s)
nodes, err := virtClient.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{LabelSelector: "!node-role.kubernetes.io/control-plane"})
Expect(err).NotTo(HaveOccurred())
waitForSchedulableNodesWithCPUManager(len(nodes.Items))
}
}
func waitForSchedulableNodesWithCPUManager(n int) {
virtClient := kubevirt.Client()
Eventually(func() bool {
nodes, err := virtClient.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{LabelSelector: v1.NodeSchedulable + "=" + "true," + v1.CPUManager + "=true"})
Expect(err).ToNot(HaveOccurred(), "Should list compute nodes")
return len(nodes.Items) == n
}, 360, 1*time.Second).Should(BeTrue())
}
func RestoreKubeVirtResource() {
if originalKV != nil {
virtClient := kubevirt.Client()
data, err := json.Marshal(originalKV.Spec)
Expect(err).ToNot(HaveOccurred())
patchData := fmt.Sprintf(`[{ "op": "replace", "path": "/spec", "value": %s }]`, string(data))
_, err = virtClient.KubeVirt(originalKV.Namespace).Patch(originalKV.Name, types.JSONPatchType, []byte(patchData), &metav1.PatchOptions{})
util.PanicOnError(err)
}
}
func ShouldAllowEmulation(virtClient kubecli.KubevirtClient) bool {
allowEmulation := false
kv := util.GetCurrentKv(virtClient)
if kv.Spec.Configuration.DeveloperConfiguration != nil {
allowEmulation = kv.Spec.Configuration.DeveloperConfiguration.UseEmulation
}
return allowEmulation
}
// UpdateKubeVirtConfigValue updates the given configuration in the kubevirt custom resource
func UpdateKubeVirtConfigValue(kvConfig v1.KubeVirtConfiguration) *v1.KubeVirt {
virtClient := kubevirt.Client()
kv := util.GetCurrentKv(virtClient)
old, err := json.Marshal(kv)
Expect(err).ToNot(HaveOccurred())
if equality.Semantic.DeepEqual(kv.Spec.Configuration, kvConfig) {
return kv
}
Expect(CurrentSpecReport().IsSerial).To(BeTrue(), "Tests which alter the global kubevirt configuration must not be executed in parallel, see https://onsi.github.io/ginkgo/#serial-specs")
updatedKV := kv.DeepCopy()
updatedKV.Spec.Configuration = kvConfig
newJson, err := json.Marshal(updatedKV)
Expect(err).ToNot(HaveOccurred())
patch, err := strategicpatch.CreateTwoWayMergePatch(old, newJson, kv)
Expect(err).ToNot(HaveOccurred())
kv, err = virtClient.KubeVirt(kv.Namespace).Patch(kv.GetName(), types.MergePatchType, patch, &metav1.PatchOptions{})
Expect(err).ToNot(HaveOccurred())
return kv
}