From 2c1efcc697051cdee49d4152be049c36b37c9de4 Mon Sep 17 00:00:00 2001 From: Pavel Mores Date: Thu, 14 Apr 2022 19:14:22 +0200 Subject: [PATCH] runtime: Add helpers to copy fields between tomlConfig instances These functions take a TOML key - an array of individual components, e.g. ["agent" "kata" "enable_tracing"], as returned by BurntSushi - and two 'tomlConfig' instances. They copy the value of the struct field identified by the key from the source instance to the target one if necessary. This is only done if the TOML key points to structures stored in maps by 'tomlConfig', i.e. 'hypervisor' and 'agent'. Nothing needs to be done in other cases. Signed-off-by: Pavel Mores --- src/runtime/pkg/katautils/config.go | 53 +++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/runtime/pkg/katautils/config.go b/src/runtime/pkg/katautils/config.go index 8522274dae..d342bb3b35 100644 --- a/src/runtime/pkg/katautils/config.go +++ b/src/runtime/pkg/katautils/config.go @@ -1310,6 +1310,59 @@ func decodeConfig(configPath string) (tomlConfig, string, error) { } +func applyKey(sourceConf tomlConfig, key []string, targetConf *tomlConfig) error { + // Any key that might need treatment provided by this function has to have + // (at least) three components: [ map_name map_key_name field_toml_tag ], + // e.g. [agent kata enable_tracing] or [hypervisor qemu confidential_guest]. + if len(key) < 3 { + return nil + } + switch key[0] { + case "agent": + return applyAgentKey(sourceConf, key[1:], targetConf) + case "hypervisor": + return applyHypervisorKey(sourceConf, key[1:], targetConf) + // The table the 'key' is in is not stored in a map so no special handling + // is needed. + } + return nil +} + +// Both of the following functions copy the value of a 'sourceConf' field +// identified by the TOML tag in 'key' into the corresponding field in +// 'targetConf'. +func applyAgentKey(sourceConf tomlConfig, key []string, targetConf *tomlConfig) error { + agentName := key[0] + tomlKeyName := key[1] + + sourceAgentConf := sourceConf.Agent[agentName] + targetAgentConf := targetConf.Agent[agentName] + + err := copyFieldValue(reflect.ValueOf(&sourceAgentConf).Elem(), tomlKeyName, reflect.ValueOf(&targetAgentConf).Elem()) + if err != nil { + return err + } + + targetConf.Agent[agentName] = targetAgentConf + return nil +} + +func applyHypervisorKey(sourceConf tomlConfig, key []string, targetConf *tomlConfig) error { + hypervisorName := key[0] + tomlKeyName := key[1] + + sourceHypervisorConf := sourceConf.Hypervisor[hypervisorName] + targetHypervisorConf := targetConf.Hypervisor[hypervisorName] + + err := copyFieldValue(reflect.ValueOf(&sourceHypervisorConf).Elem(), tomlKeyName, reflect.ValueOf(&targetHypervisorConf).Elem()) + if err != nil { + return err + } + + targetConf.Hypervisor[hypervisorName] = targetHypervisorConf + return nil +} + // Copies a TOML value of the source field identified by its TOML key to the // corresponding field of the target. Basically // 'target[tomlKeyName] = source[tomlKeyNmae]'.