Skip to content

Commit 4253081

Browse files
committed
json: Add backward compatibility with VfkitMagic
The previous commits added a VfkitMagic property which for now defaults to true for backwards compatibility with older gvproxy versions. However, this default was not used when unmarshalling a json vm configuration made with an older vfkit version. This could cause unexpected issues with networking. This commit sets VfkitMagic to true by default when unmarshalling json. Signed-off-by: Christophe Fergeau <[email protected]>
1 parent ac40bbe commit 4253081

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

pkg/config/json.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ func unmarshalIgnition(rawMsg json.RawMessage) (Ignition, error) {
110110
func unmarshalVirtioNet(rawMsg json.RawMessage) (*VirtioNet, error) {
111111
var dev virtioNetForMarshalling
112112

113+
// defaults to true for backwards compatibility with vfkit versions which did not have this field
114+
dev.VfkitMagic = true
115+
113116
err := json.Unmarshal(rawMsg, &dev)
114117
if err != nil {
115118
return nil, err
@@ -121,6 +124,10 @@ func unmarshalVirtioNet(rawMsg json.RawMessage) (*VirtioNet, error) {
121124
}
122125
dev.VirtioNet.MacAddress = macAddr
123126
}
127+
// vfkitMagic is only useful in combination with unixSocketPath
128+
if dev.UnixSocketPath == "" {
129+
dev.VfkitMagic = false
130+
}
124131
return &dev.VirtioNet, nil
125132
}
126133

pkg/config/json_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,42 @@ type jsonStabilityTest struct {
339339
expectedJSON string
340340
}
341341

342+
func TestVirtioNetBackwardsCompat(t *testing.T) {
343+
jsonStr := `{"vcpus":3,"memoryBytes":4194304000,"bootloader":{"kind":"linuxBootloader","vmlinuzPath":"/vmlinuz","initrdPath":"/initrd","kernelCmdLine":"console=hvc0"},"devices":[{"kind":"virtionet","nat":false,"unixSocketPath":"/some/path/to/socket","macAddress":"00:11:22:33:44:55"}]}`
344+
345+
var unmarshalledVM VirtualMachine
346+
err := json.Unmarshal([]byte(jsonStr), &unmarshalledVM)
347+
require.NoError(t, err)
348+
349+
vm := newLinuxVM(t)
350+
require.NoError(t, err)
351+
dev, err := VirtioNetNew("00:11:22:33:44:55")
352+
require.NoError(t, err)
353+
dev.SetUnixSocketPath("/some/path/to/socket")
354+
err = vm.AddDevice(dev)
355+
require.NoError(t, err)
356+
require.True(t, dev.VfkitMagic)
357+
358+
require.Equal(t, *vm, unmarshalledVM)
359+
360+
/* Check that the vfkitMagic default can be overridden */
361+
jsonStr = `{"vcpus":3,"memoryBytes":4194304000,"bootloader":{"kind":"linuxBootloader","vmlinuzPath":"/vmlinuz","initrdPath":"/initrd","kernelCmdLine":"console=hvc0"},"devices":[{"kind":"virtionet","nat":false,"unixSocketPath":"/some/path/to/socket","macAddress":"00:11:22:33:44:55","vfkitMagic":false}]}`
362+
363+
err = json.Unmarshal([]byte(jsonStr), &unmarshalledVM)
364+
require.NoError(t, err)
365+
366+
vm = newLinuxVM(t)
367+
require.NoError(t, err)
368+
dev, err = VirtioNetNew("00:11:22:33:44:55")
369+
require.NoError(t, err)
370+
dev.SetUnixSocketPath("/some/path/to/socket")
371+
dev.VfkitMagic = false
372+
err = vm.AddDevice(dev)
373+
require.NoError(t, err)
374+
375+
require.Equal(t, *vm, unmarshalledVM)
376+
}
377+
342378
func TestJSON(t *testing.T) {
343379
t.Run("json", func(t *testing.T) {
344380
for name := range jsonTests {

0 commit comments

Comments
 (0)