Skip to content

Commit 89dfc5c

Browse files
committed
Controller adds GPU PCI addresses to domain XML
1 parent 75e0fff commit 89dfc5c

3 files changed

Lines changed: 147 additions & 0 deletions

File tree

internal/controllers/machine_controller.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,8 @@ func (r *MachineReconciler) domainFor(
601601
serialTargetType = "pci-serial"
602602
}
603603

604+
hostDevs := clamedGPUsToHostDevs(machine)
605+
604606
domainDesc := &libvirtxml.Domain{
605607
Name: machine.GetID(),
606608
UUID: machine.GetID(),
@@ -681,6 +683,7 @@ func (r *MachineReconciler) domainFor(
681683
},
682684
},
683685
},
686+
Hostdevs: hostDevs,
684687
},
685688
}
686689

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package controllers
5+
6+
import (
7+
"fmt"
8+
9+
"github.com/ironcore-dev/libvirt-provider/api"
10+
"libvirt.org/go/libvirtxml"
11+
)
12+
13+
func clamedGPUsToHostDevs(machine *api.Machine) []libvirtxml.DomainHostdev {
14+
hostDevs := make([]libvirtxml.DomainHostdev, len(machine.Spec.Gpu))
15+
16+
for i, gpuAddr := range machine.Spec.Gpu {
17+
domain := gpuAddr.Domain
18+
bus := gpuAddr.Bus
19+
slot := gpuAddr.Slot
20+
function := gpuAddr.Function
21+
22+
hostDevs[i] = libvirtxml.DomainHostdev{
23+
Alias: &libvirtxml.DomainAlias{
24+
Name: fmt.Sprintf("gpu%d", i),
25+
},
26+
Managed: "yes",
27+
SubsysPCI: &libvirtxml.DomainHostdevSubsysPCI{
28+
Source: &libvirtxml.DomainHostdevSubsysPCISource{
29+
Address: &libvirtxml.DomainAddressPCI{
30+
Domain: &domain,
31+
Bus: &bus,
32+
Slot: &slot,
33+
Function: &function,
34+
},
35+
},
36+
},
37+
Address: &libvirtxml.DomainAddress{
38+
PCI: &libvirtxml.DomainAddressPCI{},
39+
},
40+
}
41+
}
42+
43+
return hostDevs
44+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package controllers
5+
6+
import (
7+
"github.com/ironcore-dev/libvirt-provider/api"
8+
"github.com/ironcore-dev/provider-utils/claimutils/pci"
9+
. "github.com/onsi/ginkgo/v2"
10+
. "github.com/onsi/gomega"
11+
"libvirt.org/go/libvirtxml"
12+
)
13+
14+
var _ = Describe("MachineController with GPUs", func() {
15+
It("should convert claimed GPUs to libvirt host devices", func(ctx SpecContext) {
16+
By("creating a machine with claimed GPUs")
17+
machine := &api.Machine{
18+
Spec: api.MachineSpec{
19+
Gpu: []pci.Address{
20+
{Domain: 0, Bus: 1, Slot: 0, Function: 0},
21+
{Domain: 0, Bus: 2, Slot: 0, Function: 1},
22+
},
23+
},
24+
}
25+
26+
By("converting claimed GPUs to host devices")
27+
hostDevs := clamedGPUsToHostDevs(machine)
28+
29+
By("ensuring the correct host devices are returned")
30+
Expect(hostDevs).To(HaveLen(2))
31+
Expect(hostDevs).To(ContainElements(
32+
libvirtxml.DomainHostdev{
33+
Alias: &libvirtxml.DomainAlias{
34+
Name: "gpu0",
35+
},
36+
Managed: "yes",
37+
SubsysPCI: &libvirtxml.DomainHostdevSubsysPCI{
38+
Source: &libvirtxml.DomainHostdevSubsysPCISource{
39+
Address: &libvirtxml.DomainAddressPCI{
40+
Domain: &machine.Spec.Gpu[0].Domain,
41+
Bus: &machine.Spec.Gpu[0].Bus,
42+
Slot: &machine.Spec.Gpu[0].Slot,
43+
Function: &machine.Spec.Gpu[0].Function,
44+
},
45+
},
46+
},
47+
Address: &libvirtxml.DomainAddress{
48+
PCI: &libvirtxml.DomainAddressPCI{},
49+
},
50+
},
51+
libvirtxml.DomainHostdev{
52+
Alias: &libvirtxml.DomainAlias{
53+
Name: "gpu1",
54+
},
55+
Managed: "yes",
56+
SubsysPCI: &libvirtxml.DomainHostdevSubsysPCI{
57+
Source: &libvirtxml.DomainHostdevSubsysPCISource{
58+
Address: &libvirtxml.DomainAddressPCI{
59+
Domain: &machine.Spec.Gpu[1].Domain,
60+
Bus: &machine.Spec.Gpu[1].Bus,
61+
Slot: &machine.Spec.Gpu[1].Slot,
62+
Function: &machine.Spec.Gpu[1].Function,
63+
},
64+
},
65+
},
66+
Address: &libvirtxml.DomainAddress{
67+
PCI: &libvirtxml.DomainAddressPCI{},
68+
},
69+
},
70+
))
71+
})
72+
73+
It("should convert claimed GPUs to libvirt host devices", func(ctx SpecContext) {
74+
By("creating a machine with 0 claimed GPUs")
75+
machine := &api.Machine{
76+
Spec: api.MachineSpec{
77+
Gpu: []pci.Address{},
78+
},
79+
}
80+
81+
By("converting claimed GPUs to host devices")
82+
hostDevs := clamedGPUsToHostDevs(machine)
83+
84+
By("ensuring no host devices are returned")
85+
Expect(hostDevs).To(HaveLen(0))
86+
})
87+
88+
It("should convert claimed GPUs to libvirt host devices", func(ctx SpecContext) {
89+
By("creating a machine with nil Gu field")
90+
machine := &api.Machine{
91+
Spec: api.MachineSpec{},
92+
}
93+
94+
By("converting claimed GPUs to host devices")
95+
hostDevs := clamedGPUsToHostDevs(machine)
96+
97+
By("ensuring no host devices are returned")
98+
Expect(hostDevs).To(HaveLen(0))
99+
})
100+
})

0 commit comments

Comments
 (0)