/
filters.go
221 lines (198 loc) · 8.1 KB
/
filters.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (C) 2022-2024 Red Hat, Inc.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
package provider
import (
"fmt"
"strings"
"github.com/test-network-function/cnf-certification-test/internal/clientsholder"
"github.com/test-network-function/cnf-certification-test/internal/log"
)
// GetGuaranteedPodsWithExclusiveCPUs returns a slice of Pod objects that are guaranteed to have exclusive CPUs.
// It iterates over the Pods in the TestEnvironment and filters out the Pods that do not have exclusive CPUs.
// The filtered Pods are then returned as a slice.
func (env *TestEnvironment) GetGuaranteedPodsWithExclusiveCPUs() []*Pod {
var filteredPods []*Pod
for _, p := range env.Pods {
if p.IsPodGuaranteedWithExclusiveCPUs() {
filteredPods = append(filteredPods, p)
}
}
return filteredPods
}
// GetGuaranteedPodsWithIsolatedCPUs returns a list of pods from the TestEnvironment
// that are guaranteed to have isolated CPUs and are CPU isolation compliant.
func (env *TestEnvironment) GetGuaranteedPodsWithIsolatedCPUs() []*Pod {
var filteredPods []*Pod
for _, p := range env.Pods {
if p.IsPodGuaranteedWithExclusiveCPUs() && p.IsCPUIsolationCompliant() {
filteredPods = append(filteredPods, p)
}
}
return filteredPods
}
// GetGuaranteedPods returns a slice of guaranteed pods in the test environment.
// A guaranteed pod is a pod that meets certain criteria specified by the IsPodGuaranteed method.
// The method iterates over all pods in the environment and filters out the guaranteed ones.
// It returns the filtered pods as a slice.
func (env *TestEnvironment) GetGuaranteedPods() []*Pod {
var filteredPods []*Pod
for _, p := range env.Pods {
if p.IsPodGuaranteed() {
filteredPods = append(filteredPods, p)
}
}
return filteredPods
}
// GetNonGuaranteedPods returns a slice of non-guaranteed pods in the test environment.
func (env *TestEnvironment) GetNonGuaranteedPods() []*Pod {
var filteredPods []*Pod
for _, p := range env.Pods {
if !p.IsPodGuaranteed() {
filteredPods = append(filteredPods, p)
}
}
return filteredPods
}
// GetPodsWithoutAffinityRequiredLabel returns a slice of Pod objects that do not have the affinity required label.
// It iterates over the Pods in the TestEnvironment and filters out the ones that do not have the affinity required label.
// The filtered Pods are returned as a slice.
func (env *TestEnvironment) GetPodsWithoutAffinityRequiredLabel() []*Pod {
var filteredPods []*Pod
for _, p := range env.Pods {
if !p.AffinityRequired() {
filteredPods = append(filteredPods, p)
}
}
return filteredPods
}
// GetAffinityRequiredPods returns a slice of Pod objects that have affinity required.
// It iterates over the Pods in the TestEnvironment and filters out the Pods that have affinity required.
// The filtered Pods are returned as a slice.
func (env *TestEnvironment) GetAffinityRequiredPods() []*Pod {
var filteredPods []*Pod
for _, p := range env.Pods {
if p.AffinityRequired() {
filteredPods = append(filteredPods, p)
}
}
return filteredPods
}
// GetHugepagesPods returns a slice of Pod objects that have hugepages enabled.
// It iterates over the Pods in the TestEnvironment and filters out the ones that do not have hugepages.
// The filtered Pods are returned as a []*Pod.
func (env *TestEnvironment) GetHugepagesPods() []*Pod {
var filteredPods []*Pod
for _, p := range env.Pods {
if p.HasHugepages() {
filteredPods = append(filteredPods, p)
}
}
return filteredPods
}
// GetCPUPinningPodsWithDpdk returns a slice of Pods that have CPU pinning enabled with DPDK.
func (env *TestEnvironment) GetCPUPinningPodsWithDpdk() []*Pod {
return filterDPDKRunningPods(env.GetGuaranteedPodsWithExclusiveCPUs())
}
func filterPodsWithoutHostPID(pods []*Pod) []*Pod {
var withoutHostPIDPods []*Pod
for _, pod := range pods {
if pod.Spec.HostPID {
continue
}
withoutHostPIDPods = append(withoutHostPIDPods, pod)
}
return withoutHostPIDPods
}
func filterDPDKRunningPods(pods []*Pod) []*Pod {
var filteredPods []*Pod
const (
dpdkDriver = "vfio-pci"
findDeviceSubCommand = "find /sys -name"
)
o := clientsholder.GetClientsHolder()
for _, pod := range pods {
if len(pod.MultusPCIs) == 0 {
continue
}
ctx := clientsholder.NewContext(pod.Namespace, pod.Name, pod.Spec.Containers[0].Name)
findCommand := fmt.Sprintf("%s '%s'", findDeviceSubCommand, pod.MultusPCIs[0])
outStr, errStr, err := o.ExecCommandContainer(ctx, findCommand)
if err != nil || errStr != "" {
log.Error("Failed to execute command %s in debug %s, errStr: %s, err: %v", findCommand, pod.String(), errStr, err)
continue
}
if strings.Contains(outStr, dpdkDriver) {
filteredPods = append(filteredPods, pod)
}
}
return filteredPods
}
// GetShareProcessNamespacePods returns a slice of Pod objects that have the ShareProcessNamespace flag set to true.
// It iterates over the Pods in the TestEnvironment and filters out the ones that do not have the ShareProcessNamespace flag set.
// The filtered Pods are then returned as a slice.
func (env *TestEnvironment) GetShareProcessNamespacePods() []*Pod {
var filteredPods []*Pod
for _, p := range env.Pods {
if p.IsShareProcessNamespace() {
filteredPods = append(filteredPods, p)
}
}
return filteredPods
}
// GetPodsUsingSRIOV returns a list of pods that are using SR-IOV.
// It iterates through the pods in the TestEnvironment and checks if each pod is using SR-IOV.
// If an error occurs while checking the SR-IOV usage for a pod, it returns an error.
// The filtered pods that are using SR-IOV are returned along with a nil error.
func (env *TestEnvironment) GetPodsUsingSRIOV() ([]*Pod, error) {
var filteredPods []*Pod
for _, p := range env.Pods {
usesSRIOV, err := p.IsUsingSRIOV()
if err != nil {
return nil, fmt.Errorf("failed to check sriov usage for pod %s: %v", p, err)
}
if usesSRIOV {
filteredPods = append(filteredPods, p)
}
}
return filteredPods, nil
}
func getContainers(pods []*Pod) []*Container {
var containers []*Container
for _, pod := range pods {
containers = append(containers, pod.Containers...)
}
return containers
}
// GetGuaranteedPodContainersWithExclusiveCPUs returns a slice of Container objects representing the containers
// that have exclusive CPUs in the TestEnvironment.
func (env *TestEnvironment) GetGuaranteedPodContainersWithExclusiveCPUs() []*Container {
return getContainers(env.GetGuaranteedPodsWithExclusiveCPUs())
}
// GetNonGuaranteedPodContainersWithoutHostPID returns a slice of containers from the test environment
// that belong to non-guaranteed pods without the HostPID setting enabled.
func (env *TestEnvironment) GetNonGuaranteedPodContainersWithoutHostPID() []*Container {
return getContainers(filterPodsWithoutHostPID(env.GetNonGuaranteedPods()))
}
// GetGuaranteedPodContainersWithExclusiveCPUsWithoutHostPID returns a slice of containers from the test environment
// that belong to pods with exclusive CPUs and do not have the host PID enabled.
func (env *TestEnvironment) GetGuaranteedPodContainersWithExclusiveCPUsWithoutHostPID() []*Container {
return getContainers(filterPodsWithoutHostPID(env.GetGuaranteedPodsWithExclusiveCPUs()))
}
// GetGuaranteedPodContainersWithIsolatedCPUsWithoutHostPID returns a slice of containers from the TestEnvironment
// that have guaranteed pods with isolated CPUs and without the HostPID flag set.
func (env *TestEnvironment) GetGuaranteedPodContainersWithIsolatedCPUsWithoutHostPID() []*Container {
return getContainers(filterPodsWithoutHostPID(env.GetGuaranteedPodsWithIsolatedCPUs()))
}