Skip to content

Commit

Permalink
Move ParsePciAddress function to hardware utils
Browse files Browse the repository at this point in the history
Signed-off-by: Vasiliy Ulyanov <vulyanov@suse.de>
  • Loading branch information
vasiliy-ul committed Jan 8, 2021
1 parent 21d4617 commit d033255
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 50 deletions.
1 change: 0 additions & 1 deletion pkg/util/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = [
"hw_helper.go",
"os_helper.go",
"util.go",
],
Expand Down
21 changes: 20 additions & 1 deletion pkg/util/hardware/hw_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@
package hardware

import (
"fmt"
"regexp"
"strconv"
"strings"

v1 "kubevirt.io/client-go/api/v1"
)

const CPUSET_PATH = "/sys/fs/cgroup/cpuset/cpuset.cpus"
const (
CPUSET_PATH = "/sys/fs/cgroup/cpuset/cpuset.cpus"

PCI_ADDRESS_PATTERN = `^([\da-fA-F]{4}):([\da-fA-F]{2}):([\da-fA-F]{2}).([0-7]{1})$`
)

// Parse linux cpuset into an array of ints
// See: http://man7.org/linux/man-pages/man7/cpuset.7.html#FORMATS
Expand Down Expand Up @@ -79,3 +85,16 @@ func GetNumberOfVCPUs(cpuSpec *v1.CPU) int64 {
}
return int64(vCPUs)
}

// ParsePciAddress returns an array of PCI DBSF fields (domain, bus, slot, function)
func ParsePciAddress(pciAddress string) ([]string, error) {
pciAddrRegx, err := regexp.Compile(PCI_ADDRESS_PATTERN)
if err != nil {
return nil, fmt.Errorf("failed to compile pci address pattern, %v", err)
}
res := pciAddrRegx.FindStringSubmatch(pciAddress)
if len(res) == 0 {
return nil, fmt.Errorf("failed to parse pci address %s", pciAddress)
}
return res[1:], nil
}
40 changes: 0 additions & 40 deletions pkg/util/hw_helper.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ go_library(
deps = [
"//pkg/controller:go_default_library",
"//pkg/hooks:go_default_library",
"//pkg/util:go_default_library",
"//pkg/util/hardware:go_default_library",
"//pkg/util/webhooks:go_default_library",
"//pkg/util/webhooks/validating-webhooks:go_default_library",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import (

v1 "kubevirt.io/client-go/api/v1"
"kubevirt.io/kubevirt/pkg/hooks"
"kubevirt.io/kubevirt/pkg/util"
"kubevirt.io/kubevirt/pkg/util/hardware"
hwutil "kubevirt.io/kubevirt/pkg/util/hardware"
webhookutils "kubevirt.io/kubevirt/pkg/util/webhooks"
"kubevirt.io/kubevirt/pkg/virt-api/webhooks"
virtconfig "kubevirt.io/kubevirt/pkg/virt-config"
Expand Down Expand Up @@ -754,7 +754,7 @@ func ValidateVirtualMachineInstanceSpec(field *k8sfield.Path, spec *v1.VirtualMa
}
// verify that the specified pci address is valid
if iface.PciAddress != "" {
_, err := util.ParsePciAddress(iface.PciAddress)
_, err := hwutil.ParsePciAddress(iface.PciAddress)
if err != nil {
causes = append(causes, metav1.StatusCause{
Type: metav1.CauseTypeFieldValueInvalid,
Expand Down Expand Up @@ -1712,7 +1712,7 @@ func validateDisks(field *k8sfield.Path, disks []v1.Disk) []metav1.StatusCause {
})
}

_, err := util.ParsePciAddress(disk.Disk.PciAddress)
_, err := hwutil.ParsePciAddress(disk.Disk.PciAddress)
if err != nil {
causes = append(causes, metav1.StatusCause{
Type: metav1.CauseTypeFieldValueInvalid,
Expand Down
1 change: 1 addition & 0 deletions pkg/virt-launcher/virtwrap/converter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ go_library(
"//pkg/host-disk:go_default_library",
"//pkg/ignition:go_default_library",
"//pkg/util:go_default_library",
"//pkg/util/hardware:go_default_library",
"//pkg/util/net/dns:go_default_library",
"//pkg/virt-launcher/virtwrap/api:go_default_library",
"//staging/src/kubevirt.io/client-go/api/v1:go_default_library",
Expand Down
3 changes: 2 additions & 1 deletion pkg/virt-launcher/virtwrap/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
hostdisk "kubevirt.io/kubevirt/pkg/host-disk"
"kubevirt.io/kubevirt/pkg/ignition"
"kubevirt.io/kubevirt/pkg/util"
hwutil "kubevirt.io/kubevirt/pkg/util/hardware"
"kubevirt.io/kubevirt/pkg/util/net/dns"
)

Expand Down Expand Up @@ -1964,7 +1965,7 @@ func GetResolvConfDetailsFromPod() ([][]byte, []string, error) {
}

func decoratePciAddressField(addressField string) (*api.Address, error) {
dbsfFields, err := util.ParsePciAddress(addressField)
dbsfFields, err := hwutil.ParsePciAddress(addressField)
if err != nil {
return nil, err
}
Expand Down
1 change: 0 additions & 1 deletion tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ go_test(
"//pkg/hooks/v1alpha2:go_default_library",
"//pkg/host-disk:go_default_library",
"//pkg/testutils:go_default_library",
"//pkg/util:go_default_library",
"//pkg/util/cluster:go_default_library",
"//pkg/util/hardware:go_default_library",
"//pkg/util/migrations:go_default_library",
Expand Down
4 changes: 2 additions & 2 deletions tests/vmi_gpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
v1 "kubevirt.io/client-go/api/v1"
"kubevirt.io/client-go/kubecli"
"kubevirt.io/client-go/log"
"kubevirt.io/kubevirt/pkg/util"
hwutil "kubevirt.io/kubevirt/pkg/util/hardware"
"kubevirt.io/kubevirt/pkg/virt-launcher/virtwrap/api"

"kubevirt.io/kubevirt/tests"
Expand Down Expand Up @@ -129,7 +129,7 @@ var _ = Describe("[Serial]GPU", func() {
Expect(domSpec.Devices.HostDevices[n].Type).To(Equal("pci"))
Expect(domSpec.Devices.HostDevices[n].Managed).To(Equal("yes"))
Expect(domSpec.Devices.HostDevices[n].Mode).To(Equal("subsystem"))
dbsfFields, err := util.ParsePciAddress(addr)
dbsfFields, err := hwutil.ParsePciAddress(addr)
Expect(err).ToNot(HaveOccurred())
Expect(domSpec.Devices.HostDevices[n].Source.Address.Domain).To(Equal("0x" + dbsfFields[0]))
Expect(domSpec.Devices.HostDevices[n].Source.Address.Bus).To(Equal("0x" + dbsfFields[1]))
Expand Down

0 comments on commit d033255

Please sign in to comment.