From 2235d5d6a08ebe3ae664508eb33adb13c743d89b Mon Sep 17 00:00:00 2001 From: Christophe de Dinechin Date: Fri, 11 Sep 2020 12:52:10 +0200 Subject: [PATCH] annotations: Split addHypervisorOverrides to reduce complexity Warning from gocyclo during make check: virtcontainers/pkg/oci/utils.go:404:1: cyclomatic complexity 37 of func `addHypervisorConfigOverrides` is high (> 30) (gocyclo) func addHypervisorConfigOverrides(ocispec specs.Spec, config *vc.SandboxConfig, runtime RuntimeConfig) error { ^ Fixes: #3004 Signed-off-by: Christophe de Dinechin --- virtcontainers/pkg/oci/utils.go | 67 ++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/virtcontainers/pkg/oci/utils.go b/virtcontainers/pkg/oci/utils.go index c810d6a752..8e6a3dbc10 100644 --- a/virtcontainers/pkg/oci/utils.go +++ b/virtcontainers/pkg/oci/utils.go @@ -424,36 +424,8 @@ func addHypervisorConfigOverrides(ocispec specs.Spec, config *vc.SandboxConfig, return err } - if value, ok := ocispec.Annotations[vcAnnotations.HypervisorPath]; ok { - if !checkPathIsInGlobs(runtime.HypervisorConfig.HypervisorPathList, value) { - return fmt.Errorf("hypervisor %v required from annotation is not valid", value) - } - config.HypervisorConfig.HypervisorPath = value - } - - if value, ok := ocispec.Annotations[vcAnnotations.JailerPath]; ok { - if !checkPathIsInGlobs(runtime.HypervisorConfig.JailerPathList, value) { - return fmt.Errorf("jailer %v required from annotation is not valid", value) - } - config.HypervisorConfig.JailerPath = value - } - - if value, ok := ocispec.Annotations[vcAnnotations.CtlPath]; ok { - if !checkPathIsInGlobs(runtime.HypervisorConfig.HypervisorCtlPathList, value) { - return fmt.Errorf("hypervisor control %v required from annotation is not valid", value) - } - config.HypervisorConfig.HypervisorCtlPath = value - } - - if value, ok := ocispec.Annotations[vcAnnotations.KernelParams]; ok { - if value != "" { - params := vc.DeserializeParams(strings.Fields(value)) - for _, param := range params { - if err := config.HypervisorConfig.AddKernelParam(param); err != nil { - return fmt.Errorf("Error adding kernel parameters in annotation kernel_params : %v", err) - } - } - } + if err := addHypervisorPathOverrides(ocispec, config, runtime); err != nil { + return err } if value, ok := ocispec.Annotations[vcAnnotations.MachineType]; ok { @@ -540,6 +512,41 @@ func addHypervisorConfigOverrides(ocispec specs.Spec, config *vc.SandboxConfig, return nil } +func addHypervisorPathOverrides(ocispec specs.Spec, config *vc.SandboxConfig, runtime RuntimeConfig) error { + if value, ok := ocispec.Annotations[vcAnnotations.HypervisorPath]; ok { + if !checkPathIsInGlobs(runtime.HypervisorConfig.HypervisorPathList, value) { + return fmt.Errorf("hypervisor %v required from annotation is not valid", value) + } + config.HypervisorConfig.HypervisorPath = value + } + + if value, ok := ocispec.Annotations[vcAnnotations.JailerPath]; ok { + if !checkPathIsInGlobs(runtime.HypervisorConfig.JailerPathList, value) { + return fmt.Errorf("jailer %v required from annotation is not valid", value) + } + config.HypervisorConfig.JailerPath = value + } + + if value, ok := ocispec.Annotations[vcAnnotations.CtlPath]; ok { + if !checkPathIsInGlobs(runtime.HypervisorConfig.HypervisorCtlPathList, value) { + return fmt.Errorf("hypervisor control %v required from annotation is not valid", value) + } + config.HypervisorConfig.HypervisorCtlPath = value + } + + if value, ok := ocispec.Annotations[vcAnnotations.KernelParams]; ok { + if value != "" { + params := vc.DeserializeParams(strings.Fields(value)) + for _, param := range params { + if err := config.HypervisorConfig.AddKernelParam(param); err != nil { + return fmt.Errorf("Error adding kernel parameters in annotation kernel_params : %v", err) + } + } + } + } + return nil +} + func addHypervisorMemoryOverrides(ocispec specs.Spec, sbConfig *vc.SandboxConfig, runtime RuntimeConfig) error { if value, ok := ocispec.Annotations[vcAnnotations.DefaultMemory]; ok { memorySz, err := strconv.ParseUint(value, 10, 32)