Repository: opencontainers/runc
File: libcontainer/specconv/spec_linux.go (Go)

Code before:
				}
			}
		}
		config.MaskPaths = spec.Linux.MaskedPaths
		config.ReadonlyPaths = spec.Linux.ReadonlyPaths
		config.MountLabel = spec.Linux.MountLabel
		config.Sysctl = spec.Linux.Sysctl
		config.TimeOffsets = spec.Linux.TimeOffsets
		if spec.Linux.Seccomp != nil {
			seccomp, err := SetupSeccomp(spec.Linux.Seccomp)
			if err != nil {
				return nil, err
			}
			config.Seccomp = seccomp
		}
		if spec.Linux.IntelRdt != nil {
			config.IntelRdt = &configs.IntelRdt{
				ClosID:        spec.Linux.IntelRdt.ClosID,
				L3CacheSchema: spec.Linux.IntelRdt.L3CacheSchema,
				MemBwSchema:   spec.Linux.IntelRdt.MemBwSchema,
			}
		}
		if spec.Linux.MemoryPolicy != nil &&
			(spec.Linux.MemoryPolicy.Mode != "" ||
				spec.Linux.MemoryPolicy.Nodes != "" ||
				len(spec.Linux.MemoryPolicy.Flags) > 0) {
			var ok bool
			specMp := spec.Linux.MemoryPolicy
			confMp := &configs.LinuxMemoryPolicy{}
			if confMp.Mode, ok = mpolModeMap[specMp.Mode]; !ok {
				return nil, fmt.Errorf("invalid memory policy mode %q", specMp.Mode)
			}
			if confMp.Nodes, err = parseListSet(specMp.Nodes, 0, maxNumaNode); err != nil {
				return nil, fmt.Errorf("invalid memory policy nodes %q: %w", specMp.Nodes, err)
			}
			for _, specFlag := range specMp.Flags {
				confModeFlag, ok := mpolModeFMap[specFlag]
				if !ok {
					return nil, fmt.Errorf("invalid memory policy flag %q", specFlag)
				}
				confMp.Mode |= confModeFlag
			}
			config.MemoryPolicy = confMp
		}
		if spec.Linux.Personality != nil {
			if len(spec.Linux.Personality.Flags) > 0 {
				logrus.Warnf("ignoring unsupported personality flags: %+v because personality flag has not supported at this time", spec.Linux.Personality.Flags)
			}
			domain, err := getLinuxPersonalityFromStr(string(spec.Linux.Personality.Domain))
			if err != nil {
				return nil, err

Review comment (suggestion):
Can this be written in a more straightforward way?

```go
if spec.Linux.MemoryPolicy != nil {
	if specMp.Mode != "" {
		// set confMp.Mode
	}
	if specMp.Nodes != "" {
		// set confMp.Nodes
	}
	if len(specMp.Flags) > 0 {
		// set confMp.Mode flags
	}
}
```

Code after:
			config.IntelRdt = &configs.IntelRdt{
				ClosID:        spec.Linux.IntelRdt.ClosID,
				Schemata:      spec.Linux.IntelRdt.Schemata,
				L3CacheSchema: spec.Linux.IntelRdt.L3CacheSchema,
				MemBwSchema:   spec.Linux.IntelRdt.MemBwSchema,
			}
		}
		if spec.Linux.MemoryPolicy != nil {
			var ok bool
			var err error
			specMp := spec.Linux.MemoryPolicy
			confMp := &configs.LinuxMemoryPolicy{}
			confMp.Mode, ok = mpolModeMap[string(specMp.Mode)]
			if !ok {
				return nil, fmt.Errorf("invalid memory policy mode %q", specMp.Mode)
			}
			confMp.Nodes, err = configs.ToCPUSet(specMp.Nodes)
			if err != nil {
				return nil, fmt.Errorf("invalid memory policy nodes %q: %w", specMp.Nodes, err)
			}
			for _, specFlag := range specMp.Flags {
				confFlag, ok := mpolModeFMap[string(specFlag)]
				if !ok {
					return nil, fmt.Errorf("invalid memory policy flag %q", specFlag)
				}
				confMp.Flags |= confFlag
			}
			config.MemoryPolicy = confMp
		}
		if spec.Linux.Personality != nil {
			if len(spec.Linux.Personality.Flags) > 0 {
				logrus.Warnf("ignoring unsupported personality flags: %+v because personality flag has not supported at this time", spec.Linux.Personality.Flags)
			}
			domain, err := getLinuxPersonalityFromStr(string(spec.Linux.Personality.Domain))
			if err != nil {
				return nil, err
			}
			config.Personality = &configs.LinuxPersonality{
				Domain: domain,
			}
		}

		for name, netdev := range spec.Linux.NetDevices {
			if config.NetDevices == nil {
				config.NetDevices = make(map[string]*configs.LinuxNetDevice)
			}
			config.NetDevices[name] = &configs.LinuxNetDevice{
				Name: netdev.Name,
			}
		}
	}