diff options
author | Paul Holzinger <pholzing@redhat.com> | 2022-04-25 15:15:52 +0200 |
---|---|---|
committer | Paul Holzinger <pholzing@redhat.com> | 2022-04-26 18:12:22 +0200 |
commit | 51fbf3da9ee34a8143df5baeda6032c1747446d2 (patch) | |
tree | f5edbd047f5e4aea72b710403a0aa208b238c83b /pkg/specgen | |
parent | 216d9243077f478a9c7ffda1c6e0b1fcbad9ee76 (diff) | |
download | podman-51fbf3da9ee34a8143df5baeda6032c1747446d2.tar.gz podman-51fbf3da9ee34a8143df5baeda6032c1747446d2.tar.bz2 podman-51fbf3da9ee34a8143df5baeda6032c1747446d2.zip |
enable gocritic linter
The linter ensures a common code style.
- use switch/case instead of else if
- use if instead of switch/case for single case statement
- add space between comment and text
- detect the use of defer with os.Exit()
- use short form var += "..." instead of var = var + "..."
- detect problems with append()
```
newSlice := append(orgSlice, val)
```
This could lead to nasty bugs because the orgSlice will be changed in
place if it has enough capacity too hold the new elements. Thus we
newSlice might not be a copy.
Of course most of the changes are just cosmetic and do not cause any
logic errors but I think it is a good idea to enforce a common style.
This should help maintainability.
Signed-off-by: Paul Holzinger <pholzing@redhat.com>
Diffstat (limited to 'pkg/specgen')
-rw-r--r-- | pkg/specgen/container_validate.go | 12 | ||||
-rw-r--r-- | pkg/specgen/generate/container.go | 10 | ||||
-rw-r--r-- | pkg/specgen/generate/container_create.go | 20 | ||||
-rw-r--r-- | pkg/specgen/generate/kube/kube.go | 13 | ||||
-rw-r--r-- | pkg/specgen/generate/kube/kube_test.go | 1 | ||||
-rw-r--r-- | pkg/specgen/generate/namespaces.go | 8 | ||||
-rw-r--r-- | pkg/specgen/generate/oci.go | 7 | ||||
-rw-r--r-- | pkg/specgen/generate/validate.go | 6 | ||||
-rw-r--r-- | pkg/specgen/winpath.go | 7 |
9 files changed, 40 insertions, 44 deletions
diff --git a/pkg/specgen/container_validate.go b/pkg/specgen/container_validate.go index e06cd9a29..355fbc368 100644 --- a/pkg/specgen/container_validate.go +++ b/pkg/specgen/container_validate.go @@ -122,19 +122,19 @@ func (s *SpecGenerator) Validate() error { } // TODO the specgen does not appear to handle this? Should it - //switch config.Cgroup.Cgroups { - //case "disabled": + // switch config.Cgroup.Cgroups { + // case "disabled": // if addedResources { // return errors.New("cannot specify resource limits when cgroups are disabled is specified") // } // configSpec.Linux.Resources = &spec.LinuxResources{} - //case "enabled", "no-conmon", "": + // case "enabled", "no-conmon", "": // // Do nothing - //default: + // default: // return errors.New("unrecognized option for cgroups; supported are 'default', 'disabled', 'no-conmon'") - //} + // } invalidUlimitFormatError := errors.New("invalid default ulimit definition must be form of type=soft:hard") - //set ulimits if not rootless + // set ulimits if not rootless if len(s.ContainerResourceConfig.Rlimits) < 1 && !rootless.IsRootless() { // Containers common defines this as something like nproc=4194304:4194304 tmpnproc := containerConfig.Ulimits() diff --git a/pkg/specgen/generate/container.go b/pkg/specgen/generate/container.go index 81286b962..831c1d7b9 100644 --- a/pkg/specgen/generate/container.go +++ b/pkg/specgen/generate/container.go @@ -395,7 +395,7 @@ func ConfigToSpec(rt *libpod.Runtime, specg *specgen.SpecGenerator, contaierID s } else { switch nameSpaces[i] { case "pid": - specg.PidNS = specgen.Namespace{NSMode: specgen.Default} //default + specg.PidNS = specgen.Namespace{NSMode: specgen.Default} // default case "net": switch { case conf.NetMode.IsBridge(): @@ -435,7 +435,7 @@ func ConfigToSpec(rt *libpod.Runtime, specg *specgen.SpecGenerator, contaierID s specg.NetNS = specgen.Namespace{NSMode: specgen.FromPod, Value: strings.Split(string(conf.NetMode), ":")[1]} } case "cgroup": - specg.CgroupNS = specgen.Namespace{NSMode: specgen.Default} //default + specg.CgroupNS = specgen.Namespace{NSMode: specgen.Default} // default case "ipc": switch conf.ShmDir { case "/dev/shm": @@ -443,15 +443,15 @@ func ConfigToSpec(rt *libpod.Runtime, specg *specgen.SpecGenerator, contaierID s case "": specg.IpcNS = specgen.Namespace{NSMode: specgen.None} default: - specg.IpcNS = specgen.Namespace{NSMode: specgen.Default} //default + specg.IpcNS = specgen.Namespace{NSMode: specgen.Default} // default } case "uts": - specg.UtsNS = specgen.Namespace{NSMode: specgen.Default} //default + specg.UtsNS = specgen.Namespace{NSMode: specgen.Default} // default case "user": if conf.AddCurrentUserPasswdEntry { specg.UserNS = specgen.Namespace{NSMode: specgen.KeepID} } else { - specg.UserNS = specgen.Namespace{NSMode: specgen.Default} //default + specg.UserNS = specgen.Namespace{NSMode: specgen.Default} // default } } } diff --git a/pkg/specgen/generate/container_create.go b/pkg/specgen/generate/container_create.go index 50454cbab..8b9ed8ffe 100644 --- a/pkg/specgen/generate/container_create.go +++ b/pkg/specgen/generate/container_create.go @@ -434,20 +434,18 @@ func createContainerOptions(rt *libpod.Runtime, s *specgen.SpecGenerator, pod *l // Security options if len(s.SelinuxOpts) > 0 { options = append(options, libpod.WithSecLabels(s.SelinuxOpts)) - } else { - if pod != nil && len(compatibleOptions.SelinuxOpts) == 0 { - // duplicate the security options from the pod - processLabel, err := pod.ProcessLabel() + } else if pod != nil && len(compatibleOptions.SelinuxOpts) == 0 { + // duplicate the security options from the pod + processLabel, err := pod.ProcessLabel() + if err != nil { + return nil, err + } + if processLabel != "" { + selinuxOpts, err := label.DupSecOpt(processLabel) if err != nil { return nil, err } - if processLabel != "" { - selinuxOpts, err := label.DupSecOpt(processLabel) - if err != nil { - return nil, err - } - options = append(options, libpod.WithSecLabels(selinuxOpts)) - } + options = append(options, libpod.WithSecLabels(selinuxOpts)) } } options = append(options, libpod.WithPrivileged(s.Privileged)) diff --git a/pkg/specgen/generate/kube/kube.go b/pkg/specgen/generate/kube/kube.go index 51f9fa535..4c11e4bff 100644 --- a/pkg/specgen/generate/kube/kube.go +++ b/pkg/specgen/generate/kube/kube.go @@ -449,12 +449,13 @@ func setupLivenessProbe(s *specgen.SpecGenerator, containerYAML v1.Container, re } // configure healthcheck on the basis of Handler Actions. - if probeHandler.Exec != nil { + switch { + case probeHandler.Exec != nil: execString := strings.Join(probeHandler.Exec.Command, " ") commandString = fmt.Sprintf("%s || %s", execString, failureCmd) - } else if probeHandler.HTTPGet != nil { + case probeHandler.HTTPGet != nil: commandString = fmt.Sprintf("curl %s://%s:%d/%s || %s", probeHandler.HTTPGet.Scheme, probeHandler.HTTPGet.Host, probeHandler.HTTPGet.Port.IntValue(), probeHandler.HTTPGet.Path, failureCmd) - } else if probeHandler.TCPSocket != nil { + case probeHandler.TCPSocket != nil: commandString = fmt.Sprintf("nc -z -v %s %d || %s", probeHandler.TCPSocket.Host, probeHandler.TCPSocket.Port.IntValue(), failureCmd) } s.HealthConfig, err = makeHealthCheck(commandString, probe.PeriodSeconds, probe.FailureThreshold, probe.TimeoutSeconds, probe.InitialDelaySeconds) @@ -490,17 +491,17 @@ func makeHealthCheck(inCmd string, interval int32, retries int32, timeout int32, } if interval < 1 { - //kubernetes interval defaults to 10 sec and cannot be less than 1 + // kubernetes interval defaults to 10 sec and cannot be less than 1 interval = 10 } hc.Interval = (time.Duration(interval) * time.Second) if retries < 1 { - //kubernetes retries defaults to 3 + // kubernetes retries defaults to 3 retries = 3 } hc.Retries = int(retries) if timeout < 1 { - //kubernetes timeout defaults to 1 + // kubernetes timeout defaults to 1 timeout = 1 } timeoutDuration := (time.Duration(timeout) * time.Second) diff --git a/pkg/specgen/generate/kube/kube_test.go b/pkg/specgen/generate/kube/kube_test.go index 0898d427d..9c52c03bb 100644 --- a/pkg/specgen/generate/kube/kube_test.go +++ b/pkg/specgen/generate/kube/kube_test.go @@ -5,7 +5,6 @@ import ( v1 "github.com/containers/podman/v4/pkg/k8s.io/api/core/v1" "github.com/stretchr/testify/assert" - //"github.com/stretchr/testify/require" ) func testPropagation(t *testing.T, propagation v1.MountPropagationMode, expected string) { diff --git a/pkg/specgen/generate/namespaces.go b/pkg/specgen/generate/namespaces.go index 2362f61c4..37d561ec2 100644 --- a/pkg/specgen/generate/namespaces.go +++ b/pkg/specgen/generate/namespaces.go @@ -202,10 +202,8 @@ func namespaceOptions(s *specgen.SpecGenerator, rt *libpod.Runtime, pod *libpod. if s.IDMappings != nil { if pod == nil { toReturn = append(toReturn, libpod.WithIDMappings(*s.IDMappings)) - } else { - if pod.HasInfraContainer() && (len(s.IDMappings.UIDMap) > 0 || len(s.IDMappings.GIDMap) > 0) { - return nil, errors.Wrapf(define.ErrInvalidArg, "cannot specify a new uid/gid map when entering a pod with an infra container") - } + } else if pod.HasInfraContainer() && (len(s.IDMappings.UIDMap) > 0 || len(s.IDMappings.GIDMap) > 0) { + return nil, errors.Wrapf(define.ErrInvalidArg, "cannot specify a new uid/gid map when entering a pod with an infra container") } } if s.User != "" { @@ -482,7 +480,7 @@ func GetNamespaceOptions(ns []string, netnsIsHost bool) ([]libpod.PodCreateOptio var options []libpod.PodCreateOption var erroredOptions []libpod.PodCreateOption if ns == nil { - //set the default namespaces + // set the default namespaces ns = strings.Split(specgen.DefaultKernelNamespaces, ",") } for _, toShare := range ns { diff --git a/pkg/specgen/generate/oci.go b/pkg/specgen/generate/oci.go index 95bcea8f0..b77c00f50 100644 --- a/pkg/specgen/generate/oci.go +++ b/pkg/specgen/generate/oci.go @@ -298,7 +298,8 @@ func SpecGenToOCI(ctx context.Context, s *specgen.SpecGenerator, rt *libpod.Runt g.AddAnnotation(key, val) } - if compatibleOptions.InfraResources == nil && s.ResourceLimits != nil { + switch { + case compatibleOptions.InfraResources == nil && s.ResourceLimits != nil: out, err := json.Marshal(s.ResourceLimits) if err != nil { return nil, err @@ -307,7 +308,7 @@ func SpecGenToOCI(ctx context.Context, s *specgen.SpecGenerator, rt *libpod.Runt if err != nil { return nil, err } - } else if s.ResourceLimits != nil { // if we have predefined resource limits we need to make sure we keep the infra and container limits + case s.ResourceLimits != nil: // if we have predefined resource limits we need to make sure we keep the infra and container limits originalResources, err := json.Marshal(s.ResourceLimits) if err != nil { return nil, err @@ -325,7 +326,7 @@ func SpecGenToOCI(ctx context.Context, s *specgen.SpecGenerator, rt *libpod.Runt return nil, err } g.Config.Linux.Resources = s.ResourceLimits - } else { + default: g.Config.Linux.Resources = compatibleOptions.InfraResources } // Devices diff --git a/pkg/specgen/generate/validate.go b/pkg/specgen/generate/validate.go index 8da3f2936..44c7818e7 100644 --- a/pkg/specgen/generate/validate.go +++ b/pkg/specgen/generate/validate.go @@ -47,10 +47,8 @@ func verifyContainerResourcesCgroupV1(s *specgen.SpecGenerator) ([]string, error if !sysInfo.MemorySwappiness { warnings = append(warnings, "Your kernel does not support memory swappiness capabilities, or the cgroup is not mounted. Memory swappiness discarded.") memory.Swappiness = nil - } else { - if *memory.Swappiness > 100 { - return warnings, errors.Errorf("invalid value: %v, valid memory swappiness range is 0-100", *memory.Swappiness) - } + } else if *memory.Swappiness > 100 { + return warnings, errors.Errorf("invalid value: %v, valid memory swappiness range is 0-100", *memory.Swappiness) } } if memory.Reservation != nil && !sysInfo.MemoryReservation { diff --git a/pkg/specgen/winpath.go b/pkg/specgen/winpath.go index f4249fab1..0df4ebdd7 100644 --- a/pkg/specgen/winpath.go +++ b/pkg/specgen/winpath.go @@ -47,11 +47,12 @@ func ConvertWinMountPath(path string) (string, error) { path = strings.TrimPrefix(path, `\\?\`) // Drive installed via wsl --mount - if strings.HasPrefix(path, `\\.\`) { + switch { + case strings.HasPrefix(path, `\\.\`): path = "/mnt/wsl/" + path[4:] - } else if len(path) > 1 && path[1] == ':' { + case len(path) > 1 && path[1] == ':': path = "/mnt/" + strings.ToLower(path[0:1]) + path[2:] - } else { + default: return path, errors.New("unsupported UNC path") } |