Skip to content

Commit 609e07e

Browse files
cmainasurunc-bot[bot]
authored andcommitted
fix(urunc config): Ensure default values for monitor configuration
Make sure that the default values defined for the monitor configuration are not overwritten if the respective fields are not defined in the configuration file. PR: #829 Signed-off-by: Charalampos Mainas <cmainas@nubificus.co.uk> Reviewed-by: Anastassios Nanos <ananos@nubificus.co.uk> Approved-by: Anastassios Nanos <ananos@nubificus.co.uk>
1 parent d15edbc commit 609e07e

2 files changed

Lines changed: 108 additions & 9 deletions

File tree

pkg/unikontainers/urunc_config.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,18 @@ func defaultTimestampsConfig() UruncTimestamps {
7878
}
7979
}
8080

81+
const (
82+
defaultMonitorMemoryMB uint = 256
83+
defaultMonitorVCPUs uint = 1
84+
)
85+
8186
func defaultMonitorsConfig() map[string]types.MonitorConfig {
8287
return map[string]types.MonitorConfig{
83-
"qemu": {DefaultMemoryMB: 256, DefaultVCPUs: 1},
84-
"hvt": {DefaultMemoryMB: 256, DefaultVCPUs: 1},
85-
"spt": {DefaultMemoryMB: 256, DefaultVCPUs: 1},
86-
"firecracker": {DefaultMemoryMB: 256, DefaultVCPUs: 1},
87-
"cloud-hypervisor": {DefaultMemoryMB: 256, DefaultVCPUs: 1},
88+
"qemu": {DefaultMemoryMB: defaultMonitorMemoryMB, DefaultVCPUs: defaultMonitorVCPUs},
89+
"hvt": {DefaultMemoryMB: defaultMonitorMemoryMB, DefaultVCPUs: defaultMonitorVCPUs},
90+
"spt": {DefaultMemoryMB: defaultMonitorMemoryMB, DefaultVCPUs: defaultMonitorVCPUs},
91+
"firecracker": {DefaultMemoryMB: defaultMonitorMemoryMB, DefaultVCPUs: defaultMonitorVCPUs},
92+
"cloud-hypervisor": {DefaultMemoryMB: defaultMonitorMemoryMB, DefaultVCPUs: defaultMonitorVCPUs},
8893
}
8994
}
9095

@@ -108,11 +113,24 @@ func defaultUruncConfig() *UruncConfig {
108113
func LoadUruncConfig(path string) (*UruncConfig, error) {
109114
cfg := defaultUruncConfig()
110115
_, err := toml.DecodeFile(path, cfg)
111-
if err == nil {
112-
return cfg, nil
116+
if err != nil {
117+
uniklog.Warnf("Failed to load urunc config from %s: %v. Using default configuration.", path, err)
118+
return defaultUruncConfig(), err
113119
}
114-
uniklog.Warnf("Failed to load urunc config from %s: %v. Using default configuration.", path, err)
115-
return defaultUruncConfig(), err
120+
// Decoding a partially-specified [monitors.<name>] section (e.g. one that
121+
// only sets binary_path) zeroes the fields absent from the file, dropping
122+
// the seeded defaults. Re-apply them for any zero value.
123+
for name, mon := range cfg.Monitors {
124+
if mon.DefaultMemoryMB == 0 {
125+
mon.DefaultMemoryMB = defaultMonitorMemoryMB
126+
}
127+
if mon.DefaultVCPUs == 0 {
128+
mon.DefaultVCPUs = defaultMonitorVCPUs
129+
}
130+
cfg.Monitors[name] = mon
131+
}
132+
133+
return cfg, nil
116134
}
117135

118136
func (p *UruncConfig) Map() map[string]string {

pkg/unikontainers/urunc_config_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package unikontainers
1616

1717
import (
18+
"os"
19+
"path/filepath"
1820
"testing"
1921

2022
"github.com/stretchr/testify/assert"
@@ -537,3 +539,82 @@ func TestDefaultConfigs(t *testing.T) {
537539
assert.Equal(t, testTimestampsPath, config.Timestamps.Destination)
538540
})
539541
}
542+
543+
func writeTestConfig(t *testing.T, content string) string {
544+
t.Helper()
545+
path := filepath.Join(t.TempDir(), "config.toml")
546+
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
547+
t.Fatalf("failed to write test config: %v", err)
548+
}
549+
550+
return path
551+
}
552+
553+
func TestLoadUruncConfig(t *testing.T) {
554+
t.Run("partial monitor section keeps default memory and vcpus", func(t *testing.T) {
555+
t.Parallel()
556+
path := writeTestConfig(t, `
557+
[monitors.qemu]
558+
path = "/opt/urunc/bin/qemu-system-x86_64"
559+
data_path = "/opt/urunc"
560+
`)
561+
config, err := LoadUruncConfig(path)
562+
assert.NoError(t, err)
563+
564+
qemu := config.Monitors["qemu"]
565+
assert.Equal(t, defaultMonitorMemoryMB, qemu.DefaultMemoryMB)
566+
assert.Equal(t, defaultMonitorVCPUs, qemu.DefaultVCPUs)
567+
assert.Equal(t, "/opt/urunc/bin/qemu-system-x86_64", qemu.BinaryPath)
568+
assert.Equal(t, "/opt/urunc", qemu.DataPath)
569+
})
570+
571+
t.Run("explicit values are honored", func(t *testing.T) {
572+
t.Parallel()
573+
path := writeTestConfig(t, `
574+
[monitors.qemu]
575+
default_memory_mb = 512
576+
default_vcpus = 4
577+
`)
578+
config, err := LoadUruncConfig(path)
579+
assert.NoError(t, err)
580+
581+
qemu := config.Monitors["qemu"]
582+
assert.Equal(t, uint(512), qemu.DefaultMemoryMB)
583+
assert.Equal(t, uint(4), qemu.DefaultVCPUs)
584+
})
585+
586+
t.Run("monitors absent from the file keep their defaults", func(t *testing.T) {
587+
t.Parallel()
588+
path := writeTestConfig(t, `
589+
[monitors.qemu]
590+
default_memory_mb = 512
591+
`)
592+
config, err := LoadUruncConfig(path)
593+
assert.NoError(t, err)
594+
595+
assert.Equal(t, uint(512), config.Monitors["qemu"].DefaultMemoryMB)
596+
assert.Equal(t, defaultMonitorMemoryMB, config.Monitors["hvt"].DefaultMemoryMB)
597+
assert.Equal(t, defaultMonitorVCPUs, config.Monitors["hvt"].DefaultVCPUs)
598+
})
599+
600+
t.Run("custom monitor gets defaults for omitted fields", func(t *testing.T) {
601+
t.Parallel()
602+
path := writeTestConfig(t, `
603+
[monitors.mon]
604+
path = "/usr/bin/mon"
605+
`)
606+
config, err := LoadUruncConfig(path)
607+
assert.NoError(t, err)
608+
609+
mon := config.Monitors["mon"]
610+
assert.Equal(t, defaultMonitorMemoryMB, mon.DefaultMemoryMB)
611+
assert.Equal(t, defaultMonitorVCPUs, mon.DefaultVCPUs)
612+
})
613+
614+
t.Run("missing file returns defaults", func(t *testing.T) {
615+
t.Parallel()
616+
config, err := LoadUruncConfig(filepath.Join(t.TempDir(), "does-not-exist.toml"))
617+
assert.Error(t, err)
618+
assert.Equal(t, defaultMonitorsConfig(), config.Monitors)
619+
})
620+
}

0 commit comments

Comments
 (0)