Skip to content

Commit 368e03c

Browse files
authored
Merge pull request #5969 from crazy-max/fix-cdi-class
cdi: fix device request by class annotation
2 parents e338604 + 8755d60 commit 368e03c

File tree

4 files changed

+36
-42
lines changed

4 files changed

+36
-42
lines changed

client/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11884,7 +11884,7 @@ devices:
1188411884
st = busybox.Run(append(ro, llb.Shlex(cmd), llb.Dir("/wd"))...).AddMount("/wd", st)
1188511885
}
1188611886

11887-
run(`sh -c 'env|sort | tee class.env'`, llb.AddCDIDevice(llb.CDIDeviceName("vendor1.com/device=class1")))
11887+
run(`sh -c 'env|sort | tee class.env'`, llb.AddCDIDevice(llb.CDIDeviceName("class1")))
1188811888

1188911889
def, err := st.Marshal(sb.Context())
1189011890
require.NoError(t, err)

solver/llbsolver/cdidevices/fixtures/vendor1-devicemulti.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ devices:
1010
env:
1111
- BAR=injected
1212
- name: baz
13+
annotations:
14+
org.mobyproject.buildkit.device.class: class1
1315
containerEdits:
1416
env:
1517
- BAZ=injected

solver/llbsolver/cdidevices/manager.go

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -157,49 +157,41 @@ func (m *Manager) parseDevice(dev *pb.CDIDevice, all []string) ([]string, error)
157157

158158
kind, name, _ := strings.Cut(dev.Name, "=")
159159

160-
// validate kind
161-
if vendor, class := parser.ParseQualifier(kind); vendor == "" {
162-
return nil, errors.Errorf("invalid device %q", dev.Name)
163-
} else if err := parser.ValidateVendorName(vendor); err != nil {
164-
return nil, errors.Wrapf(err, "invalid device %q", dev.Name)
165-
} else if err := parser.ValidateClassName(class); err != nil {
166-
return nil, errors.Wrapf(err, "invalid device %q", dev.Name)
167-
}
168-
169-
switch name {
170-
case "":
171-
// first device of kind if no name is specified
172-
for _, d := range all {
173-
if strings.HasPrefix(d, kind+"=") {
174-
out = append(out, d)
175-
break
176-
}
177-
}
178-
case "*":
179-
// all devices of kind if the name is a wildcard
180-
for _, d := range all {
181-
if strings.HasPrefix(d, kind+"=") {
182-
out = append(out, d)
160+
vendor, _ := parser.ParseQualifier(kind)
161+
if vendor != "" {
162+
switch name {
163+
case "":
164+
// first device of kind if no name is specified
165+
for _, d := range all {
166+
if strings.HasPrefix(d, kind+"=") {
167+
out = append(out, d)
168+
break
169+
}
183170
}
184-
}
185-
default:
186-
// the specified device
187-
for _, d := range all {
188-
if d == dev.Name {
189-
out = append(out, d)
190-
break
171+
case "*":
172+
// all devices of kind if the name is a wildcard
173+
for _, d := range all {
174+
if strings.HasPrefix(d, kind+"=") {
175+
out = append(out, d)
176+
}
191177
}
192-
}
193-
if len(out) == 0 {
194-
// check class annotation if name unknown
178+
default:
179+
// the specified device
195180
for _, d := range all {
196-
if !strings.HasPrefix(d, kind+"=") {
197-
continue
181+
if d == dev.Name {
182+
out = append(out, d)
183+
break
198184
}
199-
if a := deviceAnnotations(m.cache.GetDevice(d)); a != nil {
200-
if class, ok := a[deviceAnnotationClass]; ok && class == name {
201-
out = append(out, d)
202-
}
185+
}
186+
}
187+
}
188+
189+
// check class annotation if device qualifier invalid or no device found
190+
if vendor == "" || len(out) == 0 {
191+
for _, d := range all {
192+
if a := deviceAnnotations(m.cache.GetDevice(d)); a != nil {
193+
if class, ok := a[deviceAnnotationClass]; ok && class == dev.Name {
194+
out = append(out, d)
203195
}
204196
}
205197
}

solver/llbsolver/cdidevices/manager_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ func TestFindDevices(t *testing.T) {
3939
{
4040
name: "Find devices by class",
4141
devices: []*pb.CDIDevice{
42-
{Name: "vendor1.com/deviceclass=class1"},
42+
{Name: "class1"},
4343
},
44-
expected: []string{"vendor1.com/deviceclass=foo", "vendor1.com/deviceclass=bar"},
44+
expected: []string{"vendor1.com/deviceclass=foo", "vendor1.com/deviceclass=bar", "vendor1.com/devicemulti=baz"},
4545
},
4646
{
4747
name: "Device not found",

0 commit comments

Comments
 (0)