Skip to content

Commit 39b12a7

Browse files
authored
Merge pull request #179 from elezar/add-additional-guids
Add AdditionalGIDs field to ContainerEdits
2 parents 7021321 + 9ba82ac commit 39b12a7

File tree

7 files changed

+145
-7
lines changed

7 files changed

+145
-7
lines changed

SPEC.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Released versions of the spec are available as Git tags.
2929
| v0.6.0 | | Add `Annotations` field to `Spec` and `Device` specifications |
3030
| | | Allow dots (`.`) in name segment of `Kind` field. |
3131
| v0.7.0 | | Add `IntelRdt`field. |
32+
| v0.7.0 | | Add `AdditionalGIDs` to `ContainerEdits` |
3233

3334
*Note*: The initial release of a **spec** with version `v0.x.0` will be tagged as
3435
`v0.x.0` with subsequent changes to the API applicable to this version tagged as `v0.x.y`.
@@ -150,6 +151,11 @@ The keywords "must", "must not", "required", "shall", "shall not", "should", "sh
150151
"env": [ "<envName>=<envValue>"], (optional)
151152
"timeout": <int> (optional)
152153
}
154+
],
155+
// Additional GIDs to add to the container process.
156+
// Note that a value of 0 is ignored.
157+
additionalGIDs: [ (optional)
158+
<uint32>
153159
]
154160
"intelRdt": { (optional)
155161
"closID": "<name>", (optional)
@@ -234,6 +240,7 @@ The `containerEdits` field has the following definition:
234240
* `memBwSchema` (string, OPTIONAL) memory bandwidth allocation schema for the `CLOS`.
235241
* `enableCMT` (boolean, OPTIONAL) whether to enable cache monitoring
236242
* `enableMBM` (boolean, OPTIONAL) whether to enable memory bandwidth monitoring
243+
* `additionalGids` (array of uint32s, OPTIONAL) A list of additional group IDs to add with the container process. These values are added to the `user.additionalGids` field in the OCI runtime specification. Values of 0 are ignored.
237244

238245
## Error Handling
239246
* Kind requested is not present in any CDI file.

pkg/cdi/container-edits.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ func (e *ContainerEdits) Apply(spec *oci.Spec) error {
151151
spec.Linux.IntelRdt = e.IntelRdt.ToOCI()
152152
}
153153

154+
for _, additionalGID := range e.AdditionalGIDs {
155+
if additionalGID == 0 {
156+
continue
157+
}
158+
specgen.AddProcessAdditionalGid(additionalGID)
159+
}
160+
154161
return nil
155162
}
156163

@@ -207,6 +214,7 @@ func (e *ContainerEdits) Append(o *ContainerEdits) *ContainerEdits {
207214
if o.IntelRdt != nil {
208215
e.IntelRdt = o.IntelRdt
209216
}
217+
e.AdditionalGIDs = append(e.AdditionalGIDs, o.AdditionalGIDs...)
210218

211219
return e
212220
}
@@ -217,7 +225,25 @@ func (e *ContainerEdits) isEmpty() bool {
217225
if e == nil {
218226
return false
219227
}
220-
return len(e.Env)+len(e.DeviceNodes)+len(e.Hooks)+len(e.Mounts) == 0 && e.IntelRdt == nil
228+
if len(e.Env) > 0 {
229+
return false
230+
}
231+
if len(e.DeviceNodes) > 0 {
232+
return false
233+
}
234+
if len(e.Hooks) > 0 {
235+
return false
236+
}
237+
if len(e.Mounts) > 0 {
238+
return false
239+
}
240+
if len(e.AdditionalGIDs) > 0 {
241+
return false
242+
}
243+
if e.IntelRdt != nil {
244+
return false
245+
}
246+
return true
221247
}
222248

223249
// ValidateEnv validates the given environment variables.

pkg/cdi/container-edits_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,42 @@ func TestApplyContainerEdits(t *testing.T) {
563563
},
564564
},
565565
},
566+
{
567+
name: "additional GIDs are applied",
568+
spec: &oci.Spec{},
569+
edits: &cdi.ContainerEdits{
570+
AdditionalGIDs: []uint32{4, 5, 6},
571+
},
572+
result: &oci.Spec{
573+
Process: &oci.Process{
574+
User: oci.User{
575+
AdditionalGids: []uint32{4, 5, 6},
576+
},
577+
},
578+
},
579+
},
580+
{
581+
name: "duplicate GIDs are ignored",
582+
spec: &oci.Spec{},
583+
edits: &cdi.ContainerEdits{
584+
AdditionalGIDs: []uint32{4, 5, 6, 5, 6, 4},
585+
},
586+
result: &oci.Spec{
587+
Process: &oci.Process{
588+
User: oci.User{
589+
AdditionalGids: []uint32{4, 5, 6},
590+
},
591+
},
592+
},
593+
},
594+
{
595+
name: "additional GID 0 is skipped",
596+
spec: &oci.Spec{},
597+
edits: &cdi.ContainerEdits{
598+
AdditionalGIDs: []uint32{0},
599+
},
600+
result: &oci.Spec{},
601+
},
566602
} {
567603
t.Run(tc.name, func(t *testing.T) {
568604
edits := ContainerEdits{tc.edits}
@@ -718,6 +754,36 @@ func TestAppend(t *testing.T) {
718754
},
719755
},
720756
},
757+
{
758+
name: "merge additional GIDs does not deduplicate",
759+
dst: &ContainerEdits{
760+
ContainerEdits: &cdi.ContainerEdits{
761+
AdditionalGIDs: []uint32{5},
762+
},
763+
},
764+
src: []*ContainerEdits{
765+
{
766+
ContainerEdits: &cdi.ContainerEdits{
767+
AdditionalGIDs: []uint32{0},
768+
},
769+
},
770+
{
771+
ContainerEdits: &cdi.ContainerEdits{
772+
AdditionalGIDs: []uint32{5},
773+
},
774+
},
775+
{
776+
ContainerEdits: &cdi.ContainerEdits{
777+
AdditionalGIDs: []uint32{6, 7, 6},
778+
},
779+
},
780+
},
781+
result: &ContainerEdits{
782+
ContainerEdits: &cdi.ContainerEdits{
783+
AdditionalGIDs: []uint32{5, 0, 5, 6, 7, 6},
784+
},
785+
},
786+
},
721787
} {
722788
t.Run(tc.name, func(t *testing.T) {
723789
dst := tc.dst

pkg/cdi/spec_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,30 @@ func TestRequiredVersion(t *testing.T) {
695695
},
696696
expectedVersion: "0.7.0",
697697
},
698+
{
699+
description: "additionalGIDs in spec require v0.7.0",
700+
spec: &cdi.Spec{
701+
ContainerEdits: cdi.ContainerEdits{
702+
AdditionalGIDs: []uint32{5},
703+
},
704+
},
705+
expectedVersion: "0.7.0",
706+
},
707+
{
708+
709+
description: "additionalGIDs in device require v0.7.0",
710+
spec: &cdi.Spec{
711+
Devices: []cdi.Device{
712+
{
713+
Name: "device0",
714+
ContainerEdits: cdi.ContainerEdits{
715+
AdditionalGIDs: []uint32{5},
716+
},
717+
},
718+
},
719+
},
720+
expectedVersion: "0.7.0",
721+
},
698722
}
699723

700724
for _, tc := range testCases {

pkg/cdi/version.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,19 @@ func requiresV070(spec *cdi.Spec) bool {
125125
if spec.ContainerEdits.IntelRdt != nil {
126126
return true
127127
}
128+
// The v0.7.0 spec allows additional GIDs to be specified at a spec level.
129+
if len(spec.ContainerEdits.AdditionalGIDs) > 0 {
130+
return true
131+
}
128132

129133
for _, d := range spec.Devices {
130134
if d.ContainerEdits.IntelRdt != nil {
131135
return true
132136
}
137+
// The v0.7.0 spec allows additional GIDs to be specified at a device level.
138+
if len(d.ContainerEdits.AdditionalGIDs) > 0 {
139+
return true
140+
}
133141
}
134142

135143
return false

schema/defs.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@
157157
"type": "boolean"
158158
}
159159
}
160+
},
161+
"additionalGids": {
162+
"type": "array",
163+
"items": {
164+
"$ref": "#/definitions/uint32"
165+
}
160166
}
161167
}
162168
},

specs-go/config.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package specs
33
import "os"
44

55
// CurrentVersion is the current version of the Spec.
6-
const CurrentVersion = "0.6.0"
6+
const CurrentVersion = "0.7.0"
77

88
// Spec is the base configuration for CDI
99
type Spec struct {
@@ -25,11 +25,12 @@ type Device struct {
2525

2626
// ContainerEdits are edits a container runtime must make to the OCI spec to expose the device.
2727
type ContainerEdits struct {
28-
Env []string `json:"env,omitempty"`
29-
DeviceNodes []*DeviceNode `json:"deviceNodes,omitempty"`
30-
Hooks []*Hook `json:"hooks,omitempty"`
31-
Mounts []*Mount `json:"mounts,omitempty"`
32-
IntelRdt *IntelRdt `json:"intelRdt,omitempty"`
28+
Env []string `json:"env,omitempty"`
29+
DeviceNodes []*DeviceNode `json:"deviceNodes,omitempty"`
30+
Hooks []*Hook `json:"hooks,omitempty"`
31+
Mounts []*Mount `json:"mounts,omitempty"`
32+
IntelRdt *IntelRdt `json:"intelRdt,omitempty"`
33+
AdditionalGIDs []uint32 `json:"additionalGids,omitempty"`
3334
}
3435

3536
// DeviceNode represents a device node that needs to be added to the OCI spec.

0 commit comments

Comments
 (0)