From 3322eec9f801de36fd0d3f0802b2284e5f058691 Mon Sep 17 00:00:00 2001 From: Lukas Frank Date: Thu, 21 Dec 2023 15:55:11 +0100 Subject: [PATCH] Removed unused code (#86) --- pkg/libvirt/networkbridges/networkbridges.go | 121 ------------- pkg/os/gpuutils/gpuutils.go | 147 --------------- pkg/os/gpuutils/gpuutils_suite_test.go | 28 --- pkg/os/gpuutils/gpuutils_test.go | 177 ------------------- 4 files changed, 473 deletions(-) delete mode 100644 pkg/libvirt/networkbridges/networkbridges.go delete mode 100644 pkg/os/gpuutils/gpuutils.go delete mode 100644 pkg/os/gpuutils/gpuutils_suite_test.go delete mode 100644 pkg/os/gpuutils/gpuutils_test.go diff --git a/pkg/libvirt/networkbridges/networkbridges.go b/pkg/libvirt/networkbridges/networkbridges.go deleted file mode 100644 index 940a2a6d..00000000 --- a/pkg/libvirt/networkbridges/networkbridges.go +++ /dev/null @@ -1,121 +0,0 @@ -// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors -// SPDX-License-Identifier: Apache-2.0 - -package networkbridges - -import ( - "fmt" - "strconv" - "strings" - "sync" - - "github.com/digitalocean/go-libvirt" - "k8s.io/apimachinery/pkg/types" - "k8s.io/apimachinery/pkg/util/sets" - "libvirt.org/go/libvirtxml" -) - -type Manager interface { - Allocate(networkUID types.UID) (string, error) - Free(networkUID types.UID) -} - -type manager struct { - mu sync.RWMutex - - libvirt *libvirt.Libvirt - - allocatedByUID map[types.UID]int - allocated sets.Set[int] - - prefix string - maxAllocated int -} - -func (m *manager) Allocate(networkUID types.UID) (string, error) { - m.mu.Lock() - defer m.mu.Unlock() - - if _, ok := m.allocatedByUID[networkUID]; ok { - return "", fmt.Errorf("network %s already is allocated", networkUID) - } - - for i := 0; i < m.maxAllocated; i++ { - if !m.allocated.Has(i) { - name := fmt.Sprintf("%s%d", m.prefix, i) - m.allocatedByUID[networkUID] = i - m.allocated.Insert(i) - return name, nil - } - } - return "", fmt.Errorf("no bridge available") -} - -func (m *manager) Free(networkUID types.UID) { - m.mu.Lock() - defer m.mu.Unlock() - - allocated, ok := m.allocatedByUID[networkUID] - if !ok { - return - } - - delete(m.allocatedByUID, networkUID) - m.allocated.Delete(allocated) -} - -type Options struct { - Prefix string - MaxAllocated int -} - -func setOptionsDefaults(o *Options) { - if o.Prefix == "" { - o.Prefix = "bridge" - } - if o.MaxAllocated <= 0 { - o.MaxAllocated = 100 - } -} - -func NewManager(lv *libvirt.Libvirt, opts Options) (Manager, error) { - setOptionsDefaults(&opts) - nets, _, err := lv.ConnectListAllNetworks(1000, 0) - if err != nil { - return nil, err - } - - mgr := &manager{ - libvirt: lv, - allocatedByUID: make(map[types.UID]int), - allocated: sets.New[int](), - prefix: opts.Prefix, - maxAllocated: opts.MaxAllocated, - } - - for _, net := range nets { - data, err := lv.NetworkGetXMLDesc(net, 0) - if err != nil { - return nil, err - } - - networkDesc := &libvirtxml.Network{} - if err := networkDesc.Unmarshal(data); err != nil { - return nil, err - } - - bridge := networkDesc.Bridge - if bridge == nil || !strings.HasPrefix(bridge.Name, opts.Prefix) { - continue - } - - id, err := strconv.Atoi(strings.TrimPrefix(bridge.Name, opts.Prefix)) - if err != nil { - continue - } - - mgr.allocated.Insert(id) - mgr.allocatedByUID[types.UID(net.UUID[:])] = id - } - return mgr, nil -} diff --git a/pkg/os/gpuutils/gpuutils.go b/pkg/os/gpuutils/gpuutils.go deleted file mode 100644 index cfa00a2f..00000000 --- a/pkg/os/gpuutils/gpuutils.go +++ /dev/null @@ -1,147 +0,0 @@ -// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors -// SPDX-License-Identifier: Apache-2.0 - -package gpuutils - -import ( - "bufio" - "errors" - "fmt" - "os" - "path/filepath" - "strconv" - "strings" - - "github.com/go-logr/logr" - "libvirt.org/go/libvirtxml" -) - -const ( - nvidiaVendorID = "0x10de" - controllerClassIdPrefix = "0x03" - classAttribute = "class" - vendorAttribute = "vendor" -) - -var ( - errNoNVIDIAGPUController = errors.New("no NVIDIA GPU controller found on host") -) - -func GetGPUAddress(log logr.Logger, pciDevicePath string) (*libvirtxml.DomainAddressPCI, error) { - dirEntries, err := os.ReadDir(pciDevicePath) - if err != nil { - return nil, fmt.Errorf("error reading the provided pciDevicePath: %w", err) - } - - for _, entry := range dirEntries { - file := filepath.Join(pciDevicePath, entry.Name()) - - domainAddressPci, err := processPCIDevice(log, file) - if err != nil { - log.Error(err, "error processing PCI device", "Device", entry.Name()) - continue - } - - if domainAddressPci != nil { - return domainAddressPci, nil - } - } - - return nil, errNoNVIDIAGPUController -} - -func processPCIDevice(log logr.Logger, file string) (*libvirtxml.DomainAddressPCI, error) { - vendorID, err := readPCIAttributeWithBufio(log, file, vendorAttribute) - if err != nil { - return nil, err - } - - if vendorID != nvidiaVendorID { - return nil, nil - } - - classID, err := readPCIAttributeWithBufio(log, file, classAttribute) - if err != nil { - return nil, err - } - - if !strings.HasPrefix(classID, controllerClassIdPrefix) { - return nil, nil - } - - domain, bus, slot, function, err := parsePCIAddress(filepath.Base(file)) - if err != nil { - return nil, err - } - - return gpuAddressToDomainAddressPCI(domain, bus, slot, function) -} - -func readPCIAttributeWithBufio(log logr.Logger, devicePath, attributeName string) (string, error) { - attributePath := filepath.Join(devicePath, attributeName) - file, err := os.Open(attributePath) - if err != nil { - return "", err - } - defer func() { - defErr := file.Close() - if defErr != nil { - log.Error(defErr, "error closing file", "Path", file) - } - }() - - scanner := bufio.NewScanner(file) - scanner.Scan() - if err := scanner.Err(); err != nil { - return "", err - } - - return strings.TrimSpace(scanner.Text()), nil -} - -func parsePCIAddress(address string) (domain, bus, slot, function string, err error) { - _, err = fmt.Sscanf(address, "%4s:%2s:%2s.%1s", &domain, &bus, &slot, &function) - if err != nil { - return "", "", "", "", fmt.Errorf("error parsing PCI address: %w", err) - } - - return -} - -func gpuAddressToDomainAddressPCI(domainStr, busStr, slotStr, functionStr string) (*libvirtxml.DomainAddressPCI, error) { - domain, err := parseHexStringToUint(domainStr) - if err != nil { - return nil, fmt.Errorf("error parsing domain to uint: %w", err) - } - - bus, err := parseHexStringToUint(busStr) - if err != nil { - return nil, fmt.Errorf("error parsing bus to uint: %w", err) - } - - slot, err := parseHexStringToUint(slotStr) - if err != nil { - return nil, fmt.Errorf("error parsing slot to uint: %w", err) - } - - function, err := parseHexStringToUint(functionStr) - if err != nil { - return nil, fmt.Errorf("error parsing function to uint: %w", err) - } - - return &libvirtxml.DomainAddressPCI{ - Domain: domain, - Bus: bus, - Slot: slot, - Function: function, - }, nil -} - -func parseHexStringToUint(hexStr string) (*uint, error) { - hexValue, err := strconv.ParseUint(hexStr, 16, 32) // Assuming 32-bit uint - if err != nil { - return nil, err - } - uintValue := uint(hexValue) - return &uintValue, nil -} diff --git a/pkg/os/gpuutils/gpuutils_suite_test.go b/pkg/os/gpuutils/gpuutils_suite_test.go deleted file mode 100644 index 6fced5d5..00000000 --- a/pkg/os/gpuutils/gpuutils_suite_test.go +++ /dev/null @@ -1,28 +0,0 @@ -// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors -// SPDX-License-Identifier: Apache-2.0 - -package gpuutils - -import ( - "os" - "testing" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -func TestGPUUtils(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "GPUUtils Suite") -} - -var _ = BeforeSuite(func() { - var err error - providerDir, err = os.MkdirTemp("", "libvirt-provider-GPUUtils-test-") - Expect(err).ToNot(HaveOccurred(), "error creating temporary directory for gpu test") -}) - -var _ = AfterSuite(func() { - err := os.RemoveAll(providerDir) - Expect(err).ToNot(HaveOccurred(), "error cleanup test folder") -}) diff --git a/pkg/os/gpuutils/gpuutils_test.go b/pkg/os/gpuutils/gpuutils_test.go deleted file mode 100644 index 99695931..00000000 --- a/pkg/os/gpuutils/gpuutils_test.go +++ /dev/null @@ -1,177 +0,0 @@ -// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors -// SPDX-License-Identifier: Apache-2.0 - -package gpuutils - -import ( - "errors" - "fmt" - "os" - "path/filepath" - - "github.com/go-logr/logr" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - "libvirt.org/go/libvirtxml" -) - -const ( - testVendorID = "0x0000" - testClassID = "0x000000" - filePerm = 0644 - testAttribute = "test" -) - -var ( - log logr.Logger - providerDir string -) - -var _ = Describe("GetGPUAddress", Ordered, func() { - BeforeAll(func() { - for i := 0; i < 10; i++ { - devicePath := filepath.Join(providerDir, fmt.Sprintf("0000:00:00.%d", i)) - err := os.MkdirAll(devicePath, os.ModePerm) - Expect(err).ToNot(HaveOccurred(), "error creating test folder") - - vendorFile := filepath.Join(devicePath, vendorAttribute) - err = os.WriteFile(vendorFile, []byte(testVendorID), filePerm) - Expect(err).ToNot(HaveOccurred(), "error writing to vendor file") - - classFile := filepath.Join(devicePath, classAttribute) - err = os.WriteFile(classFile, []byte(testClassID), filePerm) - Expect(err).ToNot(HaveOccurred(), "error writing to vendor file") - } - }) - When("The PCI devices path does not exist", func() { - It("Should give error", func() { - _, err := GetGPUAddress(log, "/testPath") - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("error reading the provided pciDevicePath")) - }) - }) - When("The PCI devices path exists", func() { - It("Should return error if there is no controller with NVIDIA vendor ID", func() { - _, err := GetGPUAddress(log, providerDir) - Expect(err).To(HaveOccurred()) - Expect(err).Should(MatchError(errNoNVIDIAGPUController)) - }) - - It("Should return error if the controller with NVIDIA vendor ID exists but its class ID does not match", func() { - devicePath := filepath.Join(providerDir, "0000:vi:00.0") - err := os.MkdirAll(devicePath, os.ModePerm) - Expect(err).ToNot(HaveOccurred(), "error creating test folder") - - vendorFile := filepath.Join(devicePath, vendorAttribute) - err = os.WriteFile(vendorFile, []byte(nvidiaVendorID), filePerm) - Expect(err).ToNot(HaveOccurred(), "error writing to vendor file") - - classFile := filepath.Join(devicePath, classAttribute) - err = os.WriteFile(classFile, []byte(testClassID), filePerm) - Expect(err).ToNot(HaveOccurred(), "error writing to class file") - - _, err = GetGPUAddress(log, providerDir) - Expect(err).To(HaveOccurred()) - Expect(err).To(Equal(errNoNVIDIAGPUController)) - }) - - It("Should return error if the controller with NVIDIA class ID exists but its vendor ID does not match", func() { - devicePath := filepath.Join(providerDir, "0000:ci:00.0") - err := os.MkdirAll(devicePath, os.ModePerm) - Expect(err).ToNot(HaveOccurred(), "error creating test folder") - - vendorFile := filepath.Join(devicePath, vendorAttribute) - err = os.WriteFile(vendorFile, []byte(testVendorID), filePerm) - Expect(err).ToNot(HaveOccurred(), "error writing to vendor file") - - classFile := filepath.Join(devicePath, classAttribute) - err = os.WriteFile(classFile, []byte(fmt.Sprintf("%s0000", controllerClassIdPrefix)), filePerm) - Expect(err).ToNot(HaveOccurred(), "error writing to class file") - - _, err = GetGPUAddress(log, providerDir) - Expect(err).To(HaveOccurred()) - Expect(err).To(Equal(errNoNVIDIAGPUController)) - }) - - It("Should return the desired DomainAddressPCI if the controller with NVIDIA vendor ID and class ID exists", func() { - devicePath := filepath.Join(providerDir, "0000:ca:00.0") - err := os.MkdirAll(devicePath, os.ModePerm) - Expect(err).ToNot(HaveOccurred(), "error creating test folder") - - vendorFile := filepath.Join(devicePath, vendorAttribute) - err = os.WriteFile(vendorFile, []byte(nvidiaVendorID), filePerm) - Expect(err).ToNot(HaveOccurred(), "error writing to vendor file") - - classFile := filepath.Join(devicePath, classAttribute) - err = os.WriteFile(classFile, []byte(fmt.Sprintf("%s0000", controllerClassIdPrefix)), filePerm) - Expect(err).ToNot(HaveOccurred(), "error writing to vendor file") - - domainAddressPCI, err := GetGPUAddress(log, providerDir) - Expect(err).NotTo(HaveOccurred()) - - expectedDomain := uint(0) - expectedBus := uint(202) // 0xca - exptectedSlot := uint(0) - exptedFunction := uint(0) - Expect(domainAddressPCI).To(Equal(&libvirtxml.DomainAddressPCI{ - Domain: &expectedDomain, - Bus: &expectedBus, - Slot: &exptectedSlot, - Function: &exptedFunction, - })) - }) - }) -}) - -var _ = DescribeTable("parseHexStringToUint", - func(input string, expectedValue uint, expectError bool) { - result, err := parseHexStringToUint(input) - - if expectError { - Expect(err).To(HaveOccurred()) - Expect(result).To(BeNil()) - } else { - Expect(err).ToNot(HaveOccurred()) - Expect(result).ToNot(BeNil()) - Expect(*result).To(Equal(expectedValue)) - } - }, - Entry("with a valid hexadecimal string", "1A", uint(26), false), - Entry("with an invalid hexadecimal string", "invalid_hex", uint(0), true), - Entry("with a very large hexadecimal string", "FFFFFFFFF", uint(0), true), -) - -var _ = DescribeTable("gpuAddressToDomainAddressPCI", - func(domain, bus, slot, function string, expectedErrorMsg string) { - result, err := gpuAddressToDomainAddressPCI(domain, bus, slot, function) - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring(expectedErrorMsg)) - Expect(result).To(BeNil()) - }, - Entry("should return an error for invalid domain", "invalid", "00", "00", "0", "error parsing domain to uint"), - Entry("should return an error for invalid bus", "0000", "invalid", "00", "0", "error parsing bus to uint"), - Entry("should return an error for invalid slot", "0000", "00", "invalid", "0", "error parsing slot to uint"), - Entry("should return an error for invalid function", "0000", "00", "00", "invalid", "error parsing function to uint"), -) - -var _ = DescribeTable("parsePCIAddress", - func(input, expectedErrorMsg string) { - domain, bus, slot, function, err := parsePCIAddress(input) - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring(expectedErrorMsg)) - Expect(domain).To(BeEmpty()) - Expect(bus).To(BeEmpty()) - Expect(slot).To(BeEmpty()) - Expect(function).To(BeEmpty()) - }, - Entry("should return an error", "invalid_address", "error parsing PCI address"), -) - -var _ = DescribeTable("readPCIAttributeWithBufio", - func(log logr.Logger, providerDir, testAttribute string) { - _, err := readPCIAttributeWithBufio(log, providerDir, testAttribute) - Expect(err).To(HaveOccurred()) - Expect(errors.Is(err, os.ErrNotExist)).To(BeTrue()) - }, - Entry("should return an error", log, providerDir, testAttribute), -)