cpuset: Remove 'MustParse' method

Removes exit/fatal from cpuset library.

Usage in podresources test was not necessary.

Library reference in cpu_manager_test was moved to a local function, and
converted to use e2e test framework error catching.
This commit is contained in:
Ian K. Coolidge 2022-11-08 06:13:27 +00:00
parent 824bd57ad6
commit 67a057d4f2
3 changed files with 23 additions and 32 deletions

View File

@ -19,13 +19,10 @@ package cpuset
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"os"
"reflect" "reflect"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"k8s.io/klog/v2"
) )
// Builder is a mutable builder for CPUSet. Functions that mutate instances // Builder is a mutable builder for CPUSet. Functions that mutate instances
@ -259,18 +256,6 @@ func (s CPUSet) String() string {
return strings.TrimRight(result.String(), ",") return strings.TrimRight(result.String(), ",")
} }
// MustParse CPUSet constructs a new CPU set from a Linux CPU list formatted
// string. Unlike Parse, it does not return an error but rather panics if the
// input cannot be used to construct a CPU set.
func MustParse(s string) CPUSet {
res, err := Parse(s)
if err != nil {
klog.ErrorS(err, "Failed to parse input as CPUSet", "input", s)
os.Exit(1)
}
return res
}
// Parse CPUSet constructs a new CPU set from a Linux CPU list formatted string. // Parse CPUSet constructs a new CPU set from a Linux CPU list formatted string.
// //
// See: http://man7.org/linux/man-pages/man7/cpuset.7.html#FORMATS // See: http://man7.org/linux/man-pages/man7/cpuset.7.html#FORMATS

View File

@ -280,6 +280,12 @@ func runNonGuPodTest(ctx context.Context, f *framework.Framework, cpuCap int64)
waitForContainerRemoval(ctx, pod.Spec.Containers[0].Name, pod.Name, pod.Namespace) waitForContainerRemoval(ctx, pod.Spec.Containers[0].Name, pod.Name, pod.Namespace)
} }
func mustParseCPUSet(s string) cpuset.CPUSet {
res, err := cpuset.Parse(s)
framework.ExpectNoError(err)
return res
}
func runMultipleGuNonGuPods(ctx context.Context, f *framework.Framework, cpuCap int64, cpuAlloc int64) { func runMultipleGuNonGuPods(ctx context.Context, f *framework.Framework, cpuCap int64, cpuAlloc int64) {
var cpuListString, expAllowedCPUsListRegex string var cpuListString, expAllowedCPUsListRegex string
var cpuList []int var cpuList []int
@ -312,10 +318,10 @@ func runMultipleGuNonGuPods(ctx context.Context, f *framework.Framework, cpuCap
ginkgo.By("checking if the expected cpuset was assigned") ginkgo.By("checking if the expected cpuset was assigned")
cpu1 = 1 cpu1 = 1
if isHTEnabled() { if isHTEnabled() {
cpuList = cpuset.MustParse(getCPUSiblingList(0)).ToSlice() cpuList = mustParseCPUSet(getCPUSiblingList(0)).ToSlice()
cpu1 = cpuList[1] cpu1 = cpuList[1]
} else if isMultiNUMA() { } else if isMultiNUMA() {
cpuList = cpuset.MustParse(getCoreSiblingList(0)).ToSlice() cpuList = mustParseCPUSet(getCoreSiblingList(0)).ToSlice()
if len(cpuList) > 1 { if len(cpuList) > 1 {
cpu1 = cpuList[1] cpu1 = cpuList[1]
} }
@ -327,7 +333,7 @@ func runMultipleGuNonGuPods(ctx context.Context, f *framework.Framework, cpuCap
cpuListString = "0" cpuListString = "0"
if cpuAlloc > 2 { if cpuAlloc > 2 {
cset = cpuset.MustParse(fmt.Sprintf("0-%d", cpuCap-1)) cset = mustParseCPUSet(fmt.Sprintf("0-%d", cpuCap-1))
cpuListString = fmt.Sprintf("%s", cset.Difference(cpuset.NewCPUSet(cpu1))) cpuListString = fmt.Sprintf("%s", cset.Difference(cpuset.NewCPUSet(cpu1)))
} }
expAllowedCPUsListRegex = fmt.Sprintf("^%s\n$", cpuListString) expAllowedCPUsListRegex = fmt.Sprintf("^%s\n$", cpuListString)
@ -361,19 +367,19 @@ func runMultipleCPUGuPod(ctx context.Context, f *framework.Framework) {
ginkgo.By("checking if the expected cpuset was assigned") ginkgo.By("checking if the expected cpuset was assigned")
cpuListString = "1-2" cpuListString = "1-2"
if isMultiNUMA() { if isMultiNUMA() {
cpuList = cpuset.MustParse(getCoreSiblingList(0)).ToSlice() cpuList = mustParseCPUSet(getCoreSiblingList(0)).ToSlice()
if len(cpuList) > 1 { if len(cpuList) > 1 {
cset = cpuset.MustParse(getCPUSiblingList(int64(cpuList[1]))) cset = mustParseCPUSet(getCPUSiblingList(int64(cpuList[1])))
if !isHTEnabled() && len(cpuList) > 2 { if !isHTEnabled() && len(cpuList) > 2 {
cset = cpuset.MustParse(fmt.Sprintf("%d-%d", cpuList[1], cpuList[2])) cset = mustParseCPUSet(fmt.Sprintf("%d-%d", cpuList[1], cpuList[2]))
} }
cpuListString = fmt.Sprintf("%s", cset) cpuListString = fmt.Sprintf("%s", cset)
} }
} else if isHTEnabled() { } else if isHTEnabled() {
cpuListString = "2-3" cpuListString = "2-3"
cpuList = cpuset.MustParse(getCPUSiblingList(0)).ToSlice() cpuList = mustParseCPUSet(getCPUSiblingList(0)).ToSlice()
if cpuList[1] != 1 { if cpuList[1] != 1 {
cset = cpuset.MustParse(getCPUSiblingList(1)) cset = mustParseCPUSet(getCPUSiblingList(1))
cpuListString = fmt.Sprintf("%s", cset) cpuListString = fmt.Sprintf("%s", cset)
} }
} }
@ -412,18 +418,18 @@ func runMultipleCPUContainersGuPod(ctx context.Context, f *framework.Framework)
ginkgo.By("checking if the expected cpuset was assigned") ginkgo.By("checking if the expected cpuset was assigned")
cpu1, cpu2 = 1, 2 cpu1, cpu2 = 1, 2
if isHTEnabled() { if isHTEnabled() {
cpuList = cpuset.MustParse(getCPUSiblingList(0)).ToSlice() cpuList = mustParseCPUSet(getCPUSiblingList(0)).ToSlice()
if cpuList[1] != 1 { if cpuList[1] != 1 {
cpu1, cpu2 = cpuList[1], 1 cpu1, cpu2 = cpuList[1], 1
} }
if isMultiNUMA() { if isMultiNUMA() {
cpuList = cpuset.MustParse(getCoreSiblingList(0)).ToSlice() cpuList = mustParseCPUSet(getCoreSiblingList(0)).ToSlice()
if len(cpuList) > 1 { if len(cpuList) > 1 {
cpu2 = cpuList[1] cpu2 = cpuList[1]
} }
} }
} else if isMultiNUMA() { } else if isMultiNUMA() {
cpuList = cpuset.MustParse(getCoreSiblingList(0)).ToSlice() cpuList = mustParseCPUSet(getCoreSiblingList(0)).ToSlice()
if len(cpuList) > 2 { if len(cpuList) > 2 {
cpu1, cpu2 = cpuList[1], cpuList[2] cpu1, cpu2 = cpuList[1], cpuList[2]
} }
@ -474,18 +480,18 @@ func runMultipleGuPods(ctx context.Context, f *framework.Framework) {
ginkgo.By("checking if the expected cpuset was assigned") ginkgo.By("checking if the expected cpuset was assigned")
cpu1, cpu2 = 1, 2 cpu1, cpu2 = 1, 2
if isHTEnabled() { if isHTEnabled() {
cpuList = cpuset.MustParse(getCPUSiblingList(0)).ToSlice() cpuList = mustParseCPUSet(getCPUSiblingList(0)).ToSlice()
if cpuList[1] != 1 { if cpuList[1] != 1 {
cpu1, cpu2 = cpuList[1], 1 cpu1, cpu2 = cpuList[1], 1
} }
if isMultiNUMA() { if isMultiNUMA() {
cpuList = cpuset.MustParse(getCoreSiblingList(0)).ToSlice() cpuList = mustParseCPUSet(getCoreSiblingList(0)).ToSlice()
if len(cpuList) > 1 { if len(cpuList) > 1 {
cpu2 = cpuList[1] cpu2 = cpuList[1]
} }
} }
} else if isMultiNUMA() { } else if isMultiNUMA() {
cpuList = cpuset.MustParse(getCoreSiblingList(0)).ToSlice() cpuList = mustParseCPUSet(getCoreSiblingList(0)).ToSlice()
if len(cpuList) > 2 { if len(cpuList) > 2 {
cpu1, cpu2 = cpuList[1], cpuList[2] cpu1, cpu2 = cpuList[1], cpuList[2]
} }
@ -582,10 +588,10 @@ func runCPUManagerTests(f *framework.Framework) {
ginkgo.By("checking if the expected cpuset was assigned") ginkgo.By("checking if the expected cpuset was assigned")
cpu1 = 1 cpu1 = 1
if isHTEnabled() { if isHTEnabled() {
cpuList = cpuset.MustParse(getCPUSiblingList(0)).ToSlice() cpuList = mustParseCPUSet(getCPUSiblingList(0)).ToSlice()
cpu1 = cpuList[1] cpu1 = cpuList[1]
} else if isMultiNUMA() { } else if isMultiNUMA() {
cpuList = cpuset.MustParse(getCoreSiblingList(0)).ToSlice() cpuList = mustParseCPUSet(getCoreSiblingList(0)).ToSlice()
if len(cpuList) > 1 { if len(cpuList) > 1 {
cpu1 = cpuList[1] cpu1 = cpuList[1]
} }

View File

@ -557,7 +557,7 @@ var _ = SIGDescribe("POD Resources [Serial] [Feature:PodResources][NodeFeature:P
f := framework.NewDefaultFramework("podresources-test") f := framework.NewDefaultFramework("podresources-test")
f.NamespacePodSecurityEnforceLevel = admissionapi.LevelPrivileged f.NamespacePodSecurityEnforceLevel = admissionapi.LevelPrivileged
reservedSystemCPUs := cpuset.MustParse("1") reservedSystemCPUs := cpuset.NewCPUSet(1)
ginkgo.Context("with SRIOV devices in the system", func() { ginkgo.Context("with SRIOV devices in the system", func() {
ginkgo.BeforeEach(func() { ginkgo.BeforeEach(func() {