mirror of
				https://github.com/kata-containers/kata-containers.git
				synced 2025-11-04 03:29:55 +00:00 
			
		
		
		
	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: #901
Signed-off-by: Christophe de Dinechin <dinechin@redhat.com>
			
			
This commit is contained in:
		@@ -416,36 +416,8 @@ func addHypervisorConfigOverrides(ocispec specs.Spec, config *vc.SandboxConfig,
 | 
				
			|||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if value, ok := ocispec.Annotations[vcAnnotations.HypervisorPath]; ok {
 | 
						if err := addHypervisorPathOverrides(ocispec, config, runtime); err != nil {
 | 
				
			||||||
		if !checkPathIsInGlobs(runtime.HypervisorConfig.HypervisorPathList, value) {
 | 
							return err
 | 
				
			||||||
			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 value, ok := ocispec.Annotations[vcAnnotations.MachineType]; ok {
 | 
						if value, ok := ocispec.Annotations[vcAnnotations.MachineType]; ok {
 | 
				
			||||||
@@ -522,6 +494,41 @@ func addHypervisorConfigOverrides(ocispec specs.Spec, config *vc.SandboxConfig,
 | 
				
			|||||||
	return nil
 | 
						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 {
 | 
					func addHypervisorMemoryOverrides(ocispec specs.Spec, sbConfig *vc.SandboxConfig, runtime RuntimeConfig) error {
 | 
				
			||||||
	if value, ok := ocispec.Annotations[vcAnnotations.DefaultMemory]; ok {
 | 
						if value, ok := ocispec.Annotations[vcAnnotations.DefaultMemory]; ok {
 | 
				
			||||||
		memorySz, err := strconv.ParseUint(value, 10, 32)
 | 
							memorySz, err := strconv.ParseUint(value, 10, 32)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user