From d0332554ff28419cd00eb0ff353af5c6eca49e99 Mon Sep 17 00:00:00 2001 From: Vasiliy Ulyanov Date: Fri, 8 Jan 2021 10:10:24 +0100 Subject: [PATCH] Move ParsePciAddress function to hardware utils Signed-off-by: Vasiliy Ulyanov --- pkg/util/BUILD.bazel | 1 - pkg/util/hardware/hw_utils.go | 21 +++++++++- pkg/util/hw_helper.go | 40 ------------------- .../validating-webhook/admitters/BUILD.bazel | 1 - .../admitters/vmi-create-admitter.go | 6 +-- .../virtwrap/converter/BUILD.bazel | 1 + .../virtwrap/converter/converter.go | 3 +- tests/BUILD.bazel | 1 - tests/vmi_gpu_test.go | 4 +- 9 files changed, 28 insertions(+), 50 deletions(-) delete mode 100644 pkg/util/hw_helper.go diff --git a/pkg/util/BUILD.bazel b/pkg/util/BUILD.bazel index d912f8df7768..e508564230ae 100644 --- a/pkg/util/BUILD.bazel +++ b/pkg/util/BUILD.bazel @@ -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", ], diff --git a/pkg/util/hardware/hw_utils.go b/pkg/util/hardware/hw_utils.go index 23dd075a3fb2..2d2d8ed61589 100644 --- a/pkg/util/hardware/hw_utils.go +++ b/pkg/util/hardware/hw_utils.go @@ -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 @@ -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 +} diff --git a/pkg/util/hw_helper.go b/pkg/util/hw_helper.go deleted file mode 100644 index 1109171d1cfe..000000000000 --- a/pkg/util/hw_helper.go +++ /dev/null @@ -1,40 +0,0 @@ -/* - * 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 2018 Red Hat, Inc. - * - */ - -package util - -import ( - "fmt" - "regexp" -) - -const PCI_ADDRESS_PATTERN = `^([\da-fA-F]{4}):([\da-fA-F]{2}):([\da-fA-F]{2}).([0-7]{1})$` - -// 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 -} diff --git a/pkg/virt-api/webhooks/validating-webhook/admitters/BUILD.bazel b/pkg/virt-api/webhooks/validating-webhook/admitters/BUILD.bazel index dc14713b7f9d..6cf32492e9ba 100644 --- a/pkg/virt-api/webhooks/validating-webhook/admitters/BUILD.bazel +++ b/pkg/virt-api/webhooks/validating-webhook/admitters/BUILD.bazel @@ -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", diff --git a/pkg/virt-api/webhooks/validating-webhook/admitters/vmi-create-admitter.go b/pkg/virt-api/webhooks/validating-webhook/admitters/vmi-create-admitter.go index 18383ebf2ce5..6efcfcfb66bf 100644 --- a/pkg/virt-api/webhooks/validating-webhook/admitters/vmi-create-admitter.go +++ b/pkg/virt-api/webhooks/validating-webhook/admitters/vmi-create-admitter.go @@ -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" @@ -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, @@ -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, diff --git a/pkg/virt-launcher/virtwrap/converter/BUILD.bazel b/pkg/virt-launcher/virtwrap/converter/BUILD.bazel index 52abaa597152..0a1e0ee53cf9 100644 --- a/pkg/virt-launcher/virtwrap/converter/BUILD.bazel +++ b/pkg/virt-launcher/virtwrap/converter/BUILD.bazel @@ -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", diff --git a/pkg/virt-launcher/virtwrap/converter/converter.go b/pkg/virt-launcher/virtwrap/converter/converter.go index b288b7423b8f..ef6983bcb8a9 100644 --- a/pkg/virt-launcher/virtwrap/converter/converter.go +++ b/pkg/virt-launcher/virtwrap/converter/converter.go @@ -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" ) @@ -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 } diff --git a/tests/BUILD.bazel b/tests/BUILD.bazel index 8c5da750a53b..20bf70331e0b 100644 --- a/tests/BUILD.bazel +++ b/tests/BUILD.bazel @@ -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", diff --git a/tests/vmi_gpu_test.go b/tests/vmi_gpu_test.go index 8d958db8dd5c..b1a24352b46a 100644 --- a/tests/vmi_gpu_test.go +++ b/tests/vmi_gpu_test.go @@ -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" @@ -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]))