Skip to content

Commit 4e49fa2

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 f802d4e commit 4e49fa2

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,44 @@ type jsonStabilityTest struct {
330330
expectedJSON string
331331
}
332332

333+
func testVirtioNetVfkitMagicJson(t *testing.T, vfkitMagic bool) {
334+
jsonTemplate := `{"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"%s}]}`
335+
var jsonStr string
336+
var unmarshalledVM VirtualMachine
337+
338+
if vfkitMagic {
339+
jsonStr = fmt.Sprintf(jsonTemplate, ``)
340+
} else {
341+
jsonStr = fmt.Sprintf(jsonTemplate, `,"vfkitMagic":false`)
342+
}
343+
err := json.Unmarshal([]byte(jsonStr), &unmarshalledVM)
344+
require.NoError(t, err)
345+
netDevs := unmarshalledVM.VirtioNetDevices()
346+
require.Len(t, netDevs, 1)
347+
require.Equal(t, netDevs[0].VfkitMagic, vfkitMagic)
348+
349+
vm := newLinuxVM(t)
350+
dev, err := VirtioNetNew("00:11:22:33:44:55")
351+
require.NoError(t, err)
352+
dev.SetUnixSocketPath("/some/path/to/socket")
353+
if !vfkitMagic {
354+
dev.VfkitMagic = false
355+
}
356+
err = vm.AddDevice(dev)
357+
require.NoError(t, err)
358+
require.Equal(t, dev.VfkitMagic, vfkitMagic)
359+
360+
require.Equal(t, *vm, unmarshalledVM)
361+
}
362+
363+
func TestVirtioNetBackwardsCompat(t *testing.T) {
364+
/* Check that the vfkitMagic default is true when deserializing json */
365+
t.Run("VfkitMagicJsonDefault", func(t *testing.T) { testVirtioNetVfkitMagicJson(t, true) })
366+
367+
/* Check that the vfkitMagic default can be overridden */
368+
t.Run("VfkitMagicJsonDefaultOverride", func(t *testing.T) { testVirtioNetVfkitMagicJson(t, false) })
369+
}
370+
333371
func TestJSON(t *testing.T) {
334372
t.Run("json", func(t *testing.T) {
335373
for name := range jsonTests {

0 commit comments

Comments
 (0)