Skip to content

Commit 526bc40

Browse files
ayushr2gvisor-bot
authored andcommitted
Correct updated return value in UpdateVolumeAnnotations.
`UpdateVolumeAnnotations` was returning `updated = true` even when a mount type was already correct. This avoids unnecessarily overwriting the spec when there are no changes. Also simplify tests. PiperOrigin-RevId: 766710817
1 parent 4510814 commit 526bc40

File tree

4 files changed

+33
-90
lines changed

4 files changed

+33
-90
lines changed

pkg/shim/v1/utils/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@ go_test(
2727
size = "small",
2828
srcs = ["volumes_test.go"],
2929
library = ":utils",
30-
deps = ["@com_github_opencontainers_runtime_spec//specs-go:go_default_library"],
30+
deps = [
31+
"@com_github_mohae_deepcopy//:go_default_library",
32+
"@com_github_opencontainers_runtime_spec//specs-go:go_default_library",
33+
],
3134
)

pkg/shim/v1/utils/volumes.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,9 @@ func UpdateVolumeAnnotations(s *specs.Spec) (bool, error) {
195195
}
196196
// Container mount type must match the mount type specified by
197197
// admission controller. See note about EmptyDir.
198-
specutils.ChangeMountType(&s.Mounts[i], v)
199-
updated = true
198+
if specutils.ChangeMountType(&s.Mounts[i], v) {
199+
updated = true
200+
}
200201
}
201202
}
202203
}
@@ -275,14 +276,17 @@ func configureShm(s *specs.Spec) (bool, error) {
275276
// inside the sandbox anyways) and apply options to subcontainers as
276277
// they bind mount individually.
277278
s.Annotations[volumeKeyPrefix+devshmName+".options"] = "rw"
279+
updated = true
278280
}
279281

280-
specutils.ChangeMountType(m, devshmType)
281-
updated = true
282+
if specutils.ChangeMountType(m, devshmType) {
283+
updated = true
284+
}
282285

283286
// Remove the duplicate entry now that we found the shared /dev/shm mount.
284287
if duplicate >= 0 {
285288
s.Mounts = append(s.Mounts[:duplicate], s.Mounts[duplicate+1:]...)
289+
updated = true
286290
}
287291
break
288292
}

pkg/shim/v1/utils/volumes_test.go

Lines changed: 11 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"reflect"
2121
"testing"
2222

23+
"github.com/mohae/deepcopy"
2324
specs "github.com/opencontainers/runtime-spec/specs-go"
2425
)
2526

@@ -52,11 +53,10 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
5253
}
5354

5455
for _, test := range []struct {
55-
name string
56-
spec *specs.Spec
57-
expected *specs.Spec
58-
expectErr bool
59-
expectUpdate bool
56+
name string
57+
spec *specs.Spec
58+
expected *specs.Spec // If nil, the spec is expected to be unchanged.
59+
expectErr bool
6060
}{
6161
{
6262
name: "volume annotations for sandbox",
@@ -79,7 +79,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
7979
volumeKeyPrefix + testVolumeName + ".source": testVolumePath,
8080
},
8181
},
82-
expectUpdate: true,
8382
},
8483
{
8584
name: "volume annotations for sandbox with legacy log path",
@@ -102,7 +101,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
102101
volumeKeyPrefix + testVolumeName + ".source": testVolumePath,
103102
},
104103
},
105-
expectUpdate: true,
106104
},
107105
{
108106
name: "tmpfs: volume annotations for container",
@@ -150,7 +148,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
150148
volumeKeyPrefix + testVolumeName + ".options": "ro",
151149
},
152150
},
153-
expectUpdate: true,
154151
},
155152
{
156153
name: "non-empty volume for sandbox",
@@ -173,7 +170,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
173170
volumeKeyPrefix + testNonEmptyVolumeName + ".source": testNonEmptyVolumePath,
174171
},
175172
},
176-
expectUpdate: true,
177173
},
178174
{
179175
name: "non-empty volume for container",
@@ -193,22 +189,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
193189
volumeKeyPrefix + testNonEmptyVolumeName + ".options": "ro",
194190
},
195191
},
196-
expected: &specs.Spec{
197-
Mounts: []specs.Mount{
198-
{
199-
Destination: "/test",
200-
Type: "bind",
201-
Source: testNonEmptyVolumePath,
202-
Options: []string{"ro"},
203-
},
204-
},
205-
Annotations: map[string]string{
206-
ContainerTypeAnnotation: ContainerTypeContainer,
207-
volumeKeyPrefix + testNonEmptyVolumeName + ".share": "pod",
208-
volumeKeyPrefix + testNonEmptyVolumeName + ".type": "tmpfs",
209-
volumeKeyPrefix + testNonEmptyVolumeName + ".options": "ro",
210-
},
211-
},
212192
},
213193
{
214194
name: "bind: volume annotations for sandbox",
@@ -231,7 +211,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
231211
volumeKeyPrefix + testVolumeName + ".source": testVolumePath,
232212
},
233213
},
234-
expectUpdate: true,
235214
},
236215
{
237216
name: "bind: volume annotations for container",
@@ -251,23 +230,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
251230
volumeKeyPrefix + testVolumeName + ".options": "ro",
252231
},
253232
},
254-
expected: &specs.Spec{
255-
Mounts: []specs.Mount{
256-
{
257-
Destination: "/test",
258-
Type: "bind",
259-
Source: testVolumePath,
260-
Options: []string{"ro"},
261-
},
262-
},
263-
Annotations: map[string]string{
264-
ContainerTypeAnnotation: ContainerTypeContainer,
265-
volumeKeyPrefix + testVolumeName + ".share": "container",
266-
volumeKeyPrefix + testVolumeName + ".type": "bind",
267-
volumeKeyPrefix + testVolumeName + ".options": "ro",
268-
},
269-
},
270-
expectUpdate: true,
271233
},
272234
{
273235
name: "should not return error without pod log directory",
@@ -279,14 +241,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
279241
volumeKeyPrefix + testVolumeName + ".options": "ro",
280242
},
281243
},
282-
expected: &specs.Spec{
283-
Annotations: map[string]string{
284-
ContainerTypeAnnotation: containerTypeSandbox,
285-
volumeKeyPrefix + testVolumeName + ".share": "pod",
286-
volumeKeyPrefix + testVolumeName + ".type": "tmpfs",
287-
volumeKeyPrefix + testVolumeName + ".options": "ro",
288-
},
289-
},
290244
},
291245
{
292246
name: "should return error if volume path does not exist",
@@ -309,12 +263,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
309263
ContainerTypeAnnotation: containerTypeSandbox,
310264
},
311265
},
312-
expected: &specs.Spec{
313-
Annotations: map[string]string{
314-
sandboxLogDirAnnotation: testLogDirPath,
315-
ContainerTypeAnnotation: containerTypeSandbox,
316-
},
317-
},
318266
},
319267
{
320268
name: "no volume annotations for container",
@@ -337,25 +285,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
337285
ContainerTypeAnnotation: ContainerTypeContainer,
338286
},
339287
},
340-
expected: &specs.Spec{
341-
Mounts: []specs.Mount{
342-
{
343-
Destination: "/test",
344-
Type: "bind",
345-
Source: "/test",
346-
Options: []string{"ro"},
347-
},
348-
{
349-
Destination: "/random",
350-
Type: "bind",
351-
Source: "/random",
352-
Options: []string{"ro"},
353-
},
354-
},
355-
Annotations: map[string]string{
356-
ContainerTypeAnnotation: ContainerTypeContainer,
357-
},
358-
},
359288
},
360289
{
361290
name: "bind options removed",
@@ -393,7 +322,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
393322
},
394323
},
395324
},
396-
expectUpdate: true,
397325
},
398326
{
399327
name: "shm-sandbox",
@@ -429,7 +357,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
429357
},
430358
},
431359
},
432-
expectUpdate: true,
433360
},
434361
{
435362
name: "shm-container",
@@ -459,7 +386,6 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
459386
},
460387
},
461388
},
462-
expectUpdate: true,
463389
},
464390
{
465391
name: "shm-duplicate",
@@ -505,10 +431,13 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
505431
},
506432
},
507433
},
508-
expectUpdate: true,
509434
},
510435
} {
511436
t.Run(test.name, func(t *testing.T) {
437+
expectUpdate := test.expected != nil
438+
if test.expected == nil && !test.expectErr {
439+
test.expected = deepcopy.Copy(test.spec).(*specs.Spec)
440+
}
512441
updated, err := UpdateVolumeAnnotations(test.spec)
513442
if test.expectErr {
514443
if err == nil {
@@ -519,8 +448,8 @@ func TestUpdateVolumeAnnotations(t *testing.T) {
519448
if err != nil {
520449
t.Fatalf("UpdateVolumeAnnotations(spec): %v", err)
521450
}
522-
if test.expectUpdate != updated {
523-
t.Errorf("want: %v, got: %v", test.expectUpdate, updated)
451+
if expectUpdate != updated {
452+
t.Errorf("want: %v, got: %v", expectUpdate, updated)
524453
}
525454
if !reflect.DeepEqual(test.expected, test.spec) {
526455
t.Fatalf("want: %+v, got: %+v", test.expected, test.spec)

runsc/specutils/specutils.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,13 @@ func ReadMounts(f *os.File) ([]specs.Mount, error) {
301301
}
302302

303303
// ChangeMountType changes m.Type to the specified type. It may do necessary
304-
// amends to m.Options.
305-
func ChangeMountType(m *specs.Mount, newType string) {
306-
m.Type = newType
304+
// amends to m.Options. It returns true if m was modified.
305+
func ChangeMountType(m *specs.Mount, newType string) bool {
306+
updated := false
307+
if m.Type != newType {
308+
m.Type = newType
309+
updated = true
310+
}
307311

308312
// OCI spec allows bind mounts to be specified in options only. So if new type
309313
// is not bind, remove bind/rbind from options.
@@ -315,10 +319,13 @@ func ChangeMountType(m *specs.Mount, newType string) {
315319
for _, opt := range m.Options {
316320
if opt != "rbind" && opt != "bind" {
317321
newOpts = append(newOpts, opt)
322+
} else {
323+
updated = true
318324
}
319325
}
320326
m.Options = newOpts
321327
}
328+
return updated
322329
}
323330

324331
// Capabilities takes in spec and returns a TaskCapabilities corresponding to

0 commit comments

Comments
 (0)