diff options
Diffstat (limited to 'libpod')
-rw-r--r-- | libpod/boltdb_state_internal.go | 6 | ||||
-rw-r--r-- | libpod/container.go | 6 | ||||
-rw-r--r-- | libpod/container.log.go | 2 | ||||
-rw-r--r-- | libpod/container_graph.go | 2 | ||||
-rw-r--r-- | libpod/container_inspect.go | 7 | ||||
-rw-r--r-- | libpod/container_internal.go | 12 | ||||
-rw-r--r-- | libpod/container_internal_linux.go | 19 | ||||
-rw-r--r-- | libpod/container_internal_test.go | 3 | ||||
-rw-r--r-- | libpod/events/events.go | 2 | ||||
-rw-r--r-- | libpod/healthcheck.go | 2 | ||||
-rw-r--r-- | libpod/image/image.go | 5 | ||||
-rw-r--r-- | libpod/image/image_test.go | 12 | ||||
-rw-r--r-- | libpod/kube.go | 17 | ||||
-rw-r--r-- | libpod/lock/file/file_lock_test.go | 4 | ||||
-rw-r--r-- | libpod/logs/log.go | 8 | ||||
-rw-r--r-- | libpod/networking_linux.go | 6 | ||||
-rw-r--r-- | libpod/oci_conmon_linux.go | 28 | ||||
-rw-r--r-- | libpod/options.go | 4 | ||||
-rw-r--r-- | libpod/runtime.go | 25 | ||||
-rw-r--r-- | libpod/runtime_cstorage.go | 14 | ||||
-rw-r--r-- | libpod/runtime_ctr.go | 13 | ||||
-rw-r--r-- | libpod/runtime_pod_linux.go | 6 | ||||
-rw-r--r-- | libpod/runtime_volume_linux.go | 4 | ||||
-rw-r--r-- | libpod/state_test.go | 137 | ||||
-rw-r--r-- | libpod/volume_internal_linux.go | 6 |
25 files changed, 199 insertions, 151 deletions
diff --git a/libpod/boltdb_state_internal.go b/libpod/boltdb_state_internal.go index 3347a3648..3f09305f5 100644 --- a/libpod/boltdb_state_internal.go +++ b/libpod/boltdb_state_internal.go @@ -652,11 +652,9 @@ func (s *BoltState) addContainer(ctr *Container, pod *Pod) error { if string(depCtrPod) != pod.ID() { return errors.Wrapf(define.ErrInvalidArg, "container %s depends on container %s which is in a different pod (%s)", ctr.ID(), dependsCtr, string(depCtrPod)) } - } else { + } else if depCtrPod != nil { // If we're not part of a pod, we cannot depend on containers in a pod - if depCtrPod != nil { - return errors.Wrapf(define.ErrInvalidArg, "container %s depends on container %s which is in a pod - containers not in pods cannot depend on containers in pods", ctr.ID(), dependsCtr) - } + return errors.Wrapf(define.ErrInvalidArg, "container %s depends on container %s which is in a pod - containers not in pods cannot depend on containers in pods", ctr.ID(), dependsCtr) } depNamespace := depCtrBkt.Get(namespaceKey) diff --git a/libpod/container.go b/libpod/container.go index a046dcafc..b3cb6334a 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -472,11 +472,9 @@ func (c *Container) specFromState() (*spec.Spec, error) { if err := json.Unmarshal(content, &returnSpec); err != nil { return nil, errors.Wrapf(err, "error unmarshalling container config") } - } else { + } else if !os.IsNotExist(err) { // ignore when the file does not exist - if !os.IsNotExist(err) { - return nil, errors.Wrapf(err, "error opening container config") - } + return nil, errors.Wrapf(err, "error opening container config") } return returnSpec, nil diff --git a/libpod/container.log.go b/libpod/container.log.go index 7d0cd5bfb..7c46dde9a 100644 --- a/libpod/container.log.go +++ b/libpod/container.log.go @@ -56,7 +56,7 @@ func (c *Container) readFromLogFile(options *logs.LogOptions, logChannel chan *l continue } if nll.Partial() { - partial = partial + nll.Msg + partial += nll.Msg continue } else if !nll.Partial() && len(partial) > 1 { nll.Msg = partial diff --git a/libpod/container_graph.go b/libpod/container_graph.go index f6988e1ac..97a12ec42 100644 --- a/libpod/container_graph.go +++ b/libpod/container_graph.go @@ -113,7 +113,7 @@ func detectCycles(graph *ContainerGraph) (bool, error) { info := new(nodeInfo) info.index = index info.lowLink = index - index = index + 1 + index++ nodes[node.id] = info diff --git a/libpod/container_inspect.go b/libpod/container_inspect.go index 3f4ab394f..01f2d93bd 100644 --- a/libpod/container_inspect.go +++ b/libpod/container_inspect.go @@ -1214,11 +1214,12 @@ func (c *Container) generateInspectContainerHostConfig(ctrSpec *spec.Spec, named // Network mode parsing. networkMode := "" - if c.config.CreateNetNS { + switch { + case c.config.CreateNetNS: networkMode = "default" - } else if c.config.NetNsCtr != "" { + case c.config.NetNsCtr != "": networkMode = fmt.Sprintf("container:%s", c.config.NetNsCtr) - } else { + default: // Find the spec's network namespace. // If there is none, it's host networking. // If there is one and it has a path, it's "ns:". diff --git a/libpod/container_internal.go b/libpod/container_internal.go index 46c83149a..0e883588c 100644 --- a/libpod/container_internal.go +++ b/libpod/container_internal.go @@ -22,7 +22,7 @@ import ( "github.com/containers/storage" "github.com/containers/storage/pkg/archive" "github.com/containers/storage/pkg/mount" - "github.com/cyphar/filepath-securejoin" + securejoin "github.com/cyphar/filepath-securejoin" spec "github.com/opencontainers/runtime-spec/specs-go" "github.com/opencontainers/runtime-tools/generate" "github.com/opencontainers/selinux/go-selinux/label" @@ -339,7 +339,7 @@ func (c *Container) handleRestartPolicy(ctx context.Context) (restarted bool, er c.newContainerEvent(events.Restart) // Increment restart count - c.state.RestartCount = c.state.RestartCount + 1 + c.state.RestartCount += 1 logrus.Debugf("Container %s now on retry %d", c.ID(), c.state.RestartCount) if err := c.save(); err != nil { return false, err @@ -1286,7 +1286,7 @@ func (c *Container) restartWithTimeout(ctx context.Context, timeout uint) (err e // TODO: Add ability to override mount label so we can use this for Mount() too // TODO: Can we use this for export? Copying SHM into the export might not be // good -func (c *Container) mountStorage() (_ string, Err error) { +func (c *Container) mountStorage() (_ string, deferredErr error) { var err error // Container already mounted, nothing to do if c.state.Mounted { @@ -1307,7 +1307,7 @@ func (c *Container) mountStorage() (_ string, Err error) { return "", errors.Wrapf(err, "failed to chown %s", c.config.ShmDir) } defer func() { - if Err != nil { + if deferredErr != nil { if err := c.unmountSHM(c.config.ShmDir); err != nil { logrus.Errorf("Error unmounting SHM for container %s after mount error: %v", c.ID(), err) } @@ -1324,7 +1324,7 @@ func (c *Container) mountStorage() (_ string, Err error) { return "", err } defer func() { - if Err != nil { + if deferredErr != nil { if err := c.unmount(false); err != nil { logrus.Errorf("Error unmounting container %s after mount error: %v", c.ID(), err) } @@ -1339,7 +1339,7 @@ func (c *Container) mountStorage() (_ string, Err error) { return "", err } defer func() { - if Err == nil { + if deferredErr == nil { return } vol.lock.Lock() diff --git a/libpod/container_internal_linux.go b/libpod/container_internal_linux.go index 6ec06943f..561dbdc1c 100644 --- a/libpod/container_internal_linux.go +++ b/libpod/container_internal_linux.go @@ -62,7 +62,7 @@ func (c *Container) unmountSHM(mount string) error { // prepare mounts the container and sets up other required resources like net // namespaces -func (c *Container) prepare() (Err error) { +func (c *Container) prepare() error { var ( wg sync.WaitGroup netNS ns.NetNS @@ -1277,21 +1277,21 @@ func (c *Container) generateResolvConf() (string, error) { } // If the user provided dns, it trumps all; then dns masq; then resolv.conf - if len(c.config.DNSServer) > 0 { + switch { + case len(c.config.DNSServer) > 0: // We store DNS servers as net.IP, so need to convert to string for _, server := range c.config.DNSServer { nameservers = append(nameservers, server.String()) } - } else if len(cniNameServers) > 0 { + case len(cniNameServers) > 0: nameservers = append(nameservers, cniNameServers...) - } else { + default: // Make a new resolv.conf nameservers = resolvconf.GetNameservers(resolv.Content) // slirp4netns has a built in DNS server. if c.config.NetMode.IsSlirp4netns() { nameservers = append([]string{"10.0.2.3"}, nameservers...) } - } search := resolvconf.GetSearchDomains(resolv.Content) @@ -1451,23 +1451,24 @@ func (c *Container) getOCICgroupPath() (string, error) { if err != nil { return "", err } - if (rootless.IsRootless() && !unified) || c.config.NoCgroups { + switch { + case (rootless.IsRootless() && !unified) || c.config.NoCgroups: return "", nil - } else if c.runtime.config.CgroupManager == define.SystemdCgroupsManager { + case c.runtime.config.CgroupManager == define.SystemdCgroupsManager: // When runc is set to use Systemd as a cgroup manager, it // expects cgroups to be passed as follows: // slice:prefix:name systemdCgroups := fmt.Sprintf("%s:libpod:%s", path.Base(c.config.CgroupParent), c.ID()) logrus.Debugf("Setting CGroups for container %s to %s", c.ID(), systemdCgroups) return systemdCgroups, nil - } else if c.runtime.config.CgroupManager == define.CgroupfsCgroupsManager { + case c.runtime.config.CgroupManager == define.CgroupfsCgroupsManager: cgroupPath, err := c.CGroupPath() if err != nil { return "", err } logrus.Debugf("Setting CGroup path for container %s to %s", c.ID(), cgroupPath) return cgroupPath, nil - } else { + default: return "", errors.Wrapf(define.ErrInvalidArg, "invalid cgroup manager %s requested", c.runtime.config.CgroupManager) } } diff --git a/libpod/container_internal_test.go b/libpod/container_internal_test.go index f1e2b70a7..5428504ef 100644 --- a/libpod/container_internal_test.go +++ b/libpod/container_internal_test.go @@ -61,7 +61,8 @@ func TestPostDeleteHooks(t *testing.T) { } stateRegexp := `{"ociVersion":"1\.0\.1-dev","id":"123abc","status":"stopped","bundle":"` + strings.TrimSuffix(os.TempDir(), "/") + `/libpod_test_[0-9]*","annotations":{"a":"b"}}` - for _, path := range []string{statePath, copyPath} { + for _, p := range []string{statePath, copyPath} { + path := p t.Run(path, func(t *testing.T) { content, err := ioutil.ReadFile(path) if err != nil { diff --git a/libpod/events/events.go b/libpod/events/events.go index 5e828bc8a..0d8c6b7d6 100644 --- a/libpod/events/events.go +++ b/libpod/events/events.go @@ -129,8 +129,6 @@ func StringToStatus(name string) (Status, error) { return Attach, nil case Checkpoint.String(): return Checkpoint, nil - case Restore.String(): - return Restore, nil case Cleanup.String(): return Cleanup, nil case Commit.String(): diff --git a/libpod/healthcheck.go b/libpod/healthcheck.go index b42e7d16a..9c274c4f3 100644 --- a/libpod/healthcheck.go +++ b/libpod/healthcheck.go @@ -238,7 +238,7 @@ func (c *Container) updateHealthCheckLog(hcl HealthCheckLog, inStartPeriod bool) } if !inStartPeriod { // increment failing streak - healthCheck.FailingStreak = healthCheck.FailingStreak + 1 + healthCheck.FailingStreak += 1 // if failing streak > retries, then status to unhealthy if healthCheck.FailingStreak >= c.HealthCheckConfig().Retries { healthCheck.Status = HealthCheckUnhealthy diff --git a/libpod/image/image.go b/libpod/image/image.go index 84da21ce5..6ea49e2a9 100644 --- a/libpod/image/image.go +++ b/libpod/image/image.go @@ -902,8 +902,7 @@ func (i *Image) Annotations(ctx context.Context) (map[string]string, error) { } } annotations := make(map[string]string) - switch manifestType { - case ociv1.MediaTypeImageManifest: + if manifestType == ociv1.MediaTypeImageManifest { var m ociv1.Manifest if err := json.Unmarshal(imageManifest, &m); err == nil { for k, v := range m.Annotations { @@ -1528,7 +1527,7 @@ func GetLayersMapWithImageInfo(imageruntime *Runtime) (map[string]*LayerInfo, er } } - // scan all layers & add all childs for each layers to layerInfo + // scan all layers & add all childid's for each layers to layerInfo for _, layer := range layers { _, ok := layerInfoMap[layer.ID] if ok { diff --git a/libpod/image/image_test.go b/libpod/image/image_test.go index 5aff7d860..3ff6210d9 100644 --- a/libpod/image/image_test.go +++ b/libpod/image/image_test.go @@ -3,7 +3,6 @@ package image import ( "context" "fmt" - "io" "io/ioutil" "os" "testing" @@ -91,8 +90,7 @@ func TestImage_NewFromLocal(t *testing.T) { RunRoot: workdir, GraphRoot: workdir, } - var writer io.Writer - writer = os.Stdout + writer := os.Stdout // Need images to be present for this test ir, err := NewImageRuntimeFromOptions(so) @@ -108,7 +106,7 @@ func TestImage_NewFromLocal(t *testing.T) { for _, image := range tm { // tag our images - image.img.TagImage(image.taggedName) + err = image.img.TagImage(image.taggedName) assert.NoError(t, err) for _, name := range image.names { newImage, err := ir.NewFromLocal(name) @@ -142,8 +140,7 @@ func TestImage_New(t *testing.T) { // Build the list of pull names names = append(names, bbNames...) names = append(names, fedoraNames...) - var writer io.Writer - writer = os.Stdout + writer := os.Stdout // Iterate over the names and delete the image // after the pull @@ -213,7 +210,7 @@ func TestImage_RepoDigests(t *testing.T) { t.Fatal(err) } - for _, test := range []struct { + for _, tt := range []struct { name string names []string expected []string @@ -234,6 +231,7 @@ func TestImage_RepoDigests(t *testing.T) { expected: []string{"docker.io/library/busybox@sha256:7173b809ca12ec5dee4506cd86be934c4596dd234ee82c0662eac04a8c2c71dc"}, }, } { + test := tt t.Run(test.name, func(t *testing.T) { image := &Image{ image: &storage.Image{ diff --git a/libpod/kube.go b/libpod/kube.go index 6ae3e3d07..7a5ab670d 100644 --- a/libpod/kube.go +++ b/libpod/kube.go @@ -15,7 +15,7 @@ import ( "github.com/opencontainers/runtime-tools/generate" "github.com/pkg/errors" "github.com/sirupsen/logrus" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" v12 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -310,13 +310,13 @@ func ocicniPortMappingToContainerPort(portMappings []ocicni.PortMapping) ([]v1.C func libpodEnvVarsToKubeEnvVars(envs []string) ([]v1.EnvVar, error) { var envVars []v1.EnvVar for _, e := range envs { - splitE := strings.SplitN(e, "=", 2) - if len(splitE) != 2 { + split := strings.SplitN(e, "=", 2) + if len(split) != 2 { return envVars, errors.Errorf("environment variable %s is malformed; should be key=value", e) } ev := v1.EnvVar{ - Name: splitE[0], - Value: splitE[1], + Name: split[0], + Value: split[1], } envVars = append(envVars, ev) } @@ -365,11 +365,12 @@ func generateKubeVolumeMount(m specs.Mount) (v1.VolumeMount, v1.Volume, error) { // neither a directory or a file lives here, default to creating a directory // TODO should this be an error instead? var hostPathType v1.HostPathType - if err != nil { + switch { + case err != nil: hostPathType = v1.HostPathDirectoryOrCreate - } else if isDir { + case isDir: hostPathType = v1.HostPathDirectory - } else { + default: hostPathType = v1.HostPathFile } vo.HostPath.Type = &hostPathType diff --git a/libpod/lock/file/file_lock_test.go b/libpod/lock/file/file_lock_test.go index 6320d6b70..7ac8bf31a 100644 --- a/libpod/lock/file/file_lock_test.go +++ b/libpod/lock/file/file_lock_test.go @@ -17,10 +17,10 @@ func TestCreateAndDeallocate(t *testing.T) { assert.NoError(t, err) defer os.RemoveAll(d) - l, err := OpenFileLock(filepath.Join(d, "locks")) + _, err = OpenFileLock(filepath.Join(d, "locks")) assert.Error(t, err) - l, err = CreateFileLock(filepath.Join(d, "locks")) + l, err := CreateFileLock(filepath.Join(d, "locks")) assert.NoError(t, err) lock, err := l.AllocateLock() diff --git a/libpod/logs/log.go b/libpod/logs/log.go index 0330df06a..9a7bcb5be 100644 --- a/libpod/logs/log.go +++ b/libpod/logs/log.go @@ -96,7 +96,7 @@ func getTailLog(path string, tail int) ([]*LogLine, error) { } nlls = append(nlls, nll) if !nll.Partial() { - tailCounter = tailCounter + 1 + tailCounter++ } if tailCounter == tail { break @@ -105,9 +105,9 @@ func getTailLog(path string, tail int) ([]*LogLine, error) { // Now we iterate the results and assemble partial messages to become full messages for _, nll := range nlls { if nll.Partial() { - partial = partial + nll.Msg + partial += nll.Msg } else { - nll.Msg = nll.Msg + partial + nll.Msg += partial tailLog = append(tailLog, nll) partial = "" } @@ -127,7 +127,7 @@ func (l *LogLine) String(options *LogOptions) string { out = fmt.Sprintf("%s ", cid) } if options.Timestamps { - out = out + fmt.Sprintf("%s ", l.Time.Format(LogTimeFormat)) + out += fmt.Sprintf("%s ", l.Time.Format(LogTimeFormat)) } return out + l.Msg } diff --git a/libpod/networking_linux.go b/libpod/networking_linux.go index 89dac2b5d..d90bcb708 100644 --- a/libpod/networking_linux.go +++ b/libpod/networking_linux.go @@ -148,7 +148,7 @@ func (r *Runtime) createNetNS(ctr *Container) (n ns.NetNS, q []*cnitypes.Result, logrus.Debugf("Made network namespace at %s for container %s", ctrNS.Path(), ctr.ID()) networkStatus := []*cnitypes.Result{} - if !rootless.IsRootless() { + if !rootless.IsRootless() && ctr.config.NetMode != "slirp4netns" { networkStatus, err = r.configureNetNS(ctr, ctrNS) } return ctrNS, networkStatus, err @@ -255,7 +255,7 @@ func (r *Runtime) setupRootlessNetNS(ctr *Container) (err error) { } defer func() { if err := cmd.Process.Release(); err != nil { - logrus.Errorf("unable to release comman process: %q", err) + logrus.Errorf("unable to release command process: %q", err) } }() @@ -462,7 +462,7 @@ func (r *Runtime) teardownNetNS(ctr *Container) error { logrus.Debugf("Tearing down network namespace at %s for container %s", ctr.state.NetNS.Path(), ctr.ID()) // rootless containers do not use the CNI plugin - if !rootless.IsRootless() { + if !rootless.IsRootless() && ctr.config.NetMode != "slirp4netns" { var requestedIP net.IP if ctr.requestedIP != nil { requestedIP = ctr.requestedIP diff --git a/libpod/oci_conmon_linux.go b/libpod/oci_conmon_linux.go index 3b0b7bc7b..5ab0e73c4 100644 --- a/libpod/oci_conmon_linux.go +++ b/libpod/oci_conmon_linux.go @@ -149,9 +149,23 @@ func (r *ConmonOCIRuntime) Path() string { return r.path } +// hasCurrentUserMapped checks whether the current user is mapped inside the container user namespace +func hasCurrentUserMapped(ctr *Container) bool { + if len(ctr.config.IDMappings.UIDMap) == 0 && len(ctr.config.IDMappings.GIDMap) == 0 { + return true + } + uid := os.Geteuid() + for _, m := range ctr.config.IDMappings.UIDMap { + if uid >= m.HostID && uid < m.HostID+m.Size { + return true + } + } + return false +} + // CreateContainer creates a container. func (r *ConmonOCIRuntime) CreateContainer(ctr *Container, restoreOptions *ContainerCheckpointOptions) (err error) { - if len(ctr.config.IDMappings.UIDMap) != 0 || len(ctr.config.IDMappings.GIDMap) != 0 { + if !hasCurrentUserMapped(ctr) { for _, i := range []string{ctr.state.RunDir, ctr.runtime.config.TmpDir, ctr.config.StaticDir, ctr.state.Mountpoint, ctr.runtime.config.VolumePath} { if err := makeAccessible(i, ctr.RootUID(), ctr.RootGID()); err != nil { return err @@ -586,7 +600,8 @@ func (r *ConmonOCIRuntime) ExecContainer(c *Container, sessionID string, options // we don't want to step on users fds they asked to preserve // Since 0-2 are used for stdio, start the fds we pass in at preserveFDs+3 - execCmd.Env = append(r.conmonEnv, fmt.Sprintf("_OCI_SYNCPIPE=%d", options.PreserveFDs+3), fmt.Sprintf("_OCI_STARTPIPE=%d", options.PreserveFDs+4), fmt.Sprintf("_OCI_ATTACHPIPE=%d", options.PreserveFDs+5)) + execCmd.Env = r.conmonEnv + execCmd.Env = append(execCmd.Env, fmt.Sprintf("_OCI_SYNCPIPE=%d", options.PreserveFDs+3), fmt.Sprintf("_OCI_STARTPIPE=%d", options.PreserveFDs+4), fmt.Sprintf("_OCI_ATTACHPIPE=%d", options.PreserveFDs+5)) execCmd.Env = append(execCmd.Env, conmonEnv...) execCmd.ExtraFiles = append(execCmd.ExtraFiles, childSyncPipe, childStartPipe, childAttachPipe) @@ -998,7 +1013,8 @@ func (r *ConmonOCIRuntime) createOCIContainer(ctr *Container, restoreOptions *Co return err } - cmd.Env = append(r.conmonEnv, fmt.Sprintf("_OCI_SYNCPIPE=%d", 3), fmt.Sprintf("_OCI_STARTPIPE=%d", 4)) + cmd.Env = r.conmonEnv + cmd.Env = append(cmd.Env, fmt.Sprintf("_OCI_SYNCPIPE=%d", 3), fmt.Sprintf("_OCI_STARTPIPE=%d", 4)) cmd.Env = append(cmd.Env, conmonEnv...) cmd.ExtraFiles = append(cmd.ExtraFiles, childSyncPipe, childStartPipe) cmd.ExtraFiles = append(cmd.ExtraFiles, envFiles...) @@ -1306,12 +1322,10 @@ func (r *ConmonOCIRuntime) moveConmonToCgroupAndSignal(ctr *Container, cmd *exec control, err := cgroups.New(cgroupPath, &spec.LinuxResources{}) if err != nil { logrus.Warnf("Failed to add conmon to cgroupfs sandbox cgroup: %v", err) - } else { + } else if err := control.AddPid(cmd.Process.Pid); err != nil { // we need to remove this defer and delete the cgroup once conmon exits // maybe need a conmon monitor? - if err := control.AddPid(cmd.Process.Pid); err != nil { - logrus.Warnf("Failed to add conmon to cgroupfs sandbox cgroup: %v", err) - } + logrus.Warnf("Failed to add conmon to cgroupfs sandbox cgroup: %v", err) } } } diff --git a/libpod/options.go b/libpod/options.go index 1d6863e7b..8bc5a541d 100644 --- a/libpod/options.go +++ b/libpod/options.go @@ -733,7 +733,9 @@ func WithExitCommand(exitCommand []string) CtrCreateOption { return define.ErrCtrFinalized } - ctr.config.ExitCommand = append(exitCommand, ctr.ID()) + ctr.config.ExitCommand = exitCommand + ctr.config.ExitCommand = append(ctr.config.ExitCommand, ctr.ID()) + return nil } } diff --git a/libpod/runtime.go b/libpod/runtime.go index b4cbde28e..8dcec82db 100644 --- a/libpod/runtime.go +++ b/libpod/runtime.go @@ -180,12 +180,13 @@ func getLockManager(runtime *Runtime) (lock.Manager, error) { // Set up the lock manager manager, err = lock.OpenSHMLockManager(lockPath, runtime.config.NumLocks) if err != nil { - if os.IsNotExist(errors.Cause(err)) { + switch { + case os.IsNotExist(errors.Cause(err)): manager, err = lock.NewSHMLockManager(lockPath, runtime.config.NumLocks) if err != nil { return nil, errors.Wrapf(err, "failed to get new shm lock manager") } - } else if errors.Cause(err) == syscall.ERANGE && runtime.doRenumber { + case errors.Cause(err) == syscall.ERANGE && runtime.doRenumber: logrus.Debugf("Number of locks does not match - removing old locks") // ERANGE indicates a lock numbering mismatch. @@ -199,7 +200,7 @@ func getLockManager(runtime *Runtime) (lock.Manager, error) { if err != nil { return nil, err } - } else { + default: return nil, err } } @@ -289,10 +290,8 @@ func makeRuntime(ctx context.Context, runtime *Runtime) (err error) { logrus.Debug("Not configuring container store") } else if runtime.noStore { logrus.Debug("No store required. Not opening container store.") - } else { - if err := runtime.configureStore(); err != nil { - return err - } + } else if err := runtime.configureStore(); err != nil { + return err } defer func() { if err != nil && store != nil { @@ -718,18 +717,14 @@ func (r *Runtime) generateName() (string, error) { // Make sure container with this name does not exist if _, err := r.state.LookupContainer(name); err == nil { continue - } else { - if errors.Cause(err) != define.ErrNoSuchCtr { - return "", err - } + } else if errors.Cause(err) != define.ErrNoSuchCtr { + return "", err } // Make sure pod with this name does not exist if _, err := r.state.LookupPod(name); err == nil { continue - } else { - if errors.Cause(err) != define.ErrNoSuchPod { - return "", err - } + } else if errors.Cause(err) != define.ErrNoSuchPod { + return "", err } return name, nil } diff --git a/libpod/runtime_cstorage.go b/libpod/runtime_cstorage.go index 2d523a7d2..cfcf4589f 100644 --- a/libpod/runtime_cstorage.go +++ b/libpod/runtime_cstorage.go @@ -107,15 +107,13 @@ func (r *Runtime) removeStorageContainer(idOrName string, force bool) error { if timesMounted > 0 { return errors.Wrapf(define.ErrCtrStateInvalid, "container %q is mounted and cannot be removed without using force", idOrName) } - } else { - if _, err := r.store.Unmount(ctr.ID, true); err != nil { - if errors.Cause(err) == storage.ErrContainerUnknown { - // Container again gone, no error - logrus.Warnf("Storage for container %s already removed", ctr.ID) - return nil - } - return errors.Wrapf(err, "error unmounting container %q", idOrName) + } else if _, err := r.store.Unmount(ctr.ID, true); err != nil { + if errors.Cause(err) == storage.ErrContainerUnknown { + // Container again gone, no error + logrus.Warnf("Storage for container %s already removed", ctr.ID) + return nil } + return errors.Wrapf(err, "error unmounting container %q", idOrName) } if err := r.store.DeleteContainer(ctr.ID); err != nil { diff --git a/libpod/runtime_ctr.go b/libpod/runtime_ctr.go index 51efc5996..de7cfd3b8 100644 --- a/libpod/runtime_ctr.go +++ b/libpod/runtime_ctr.go @@ -234,15 +234,16 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (c *Contai } case define.SystemdCgroupsManager: if ctr.config.CgroupParent == "" { - if pod != nil && pod.config.UsePodCgroup { + switch { + case pod != nil && pod.config.UsePodCgroup: podCgroup, err := pod.CgroupPath() if err != nil { return nil, errors.Wrapf(err, "error retrieving pod %s cgroup", pod.ID()) } ctr.config.CgroupParent = podCgroup - } else if rootless.IsRootless() { + case rootless.IsRootless(): ctr.config.CgroupParent = SystemdDefaultRootlessCgroupParent - } else { + default: ctr.config.CgroupParent = SystemdDefaultCgroupParent } } else if len(ctr.config.CgroupParent) < 6 || !strings.HasSuffix(path.Base(ctr.config.CgroupParent), ".slice") { @@ -361,10 +362,8 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (c *Contai if err := r.state.AddContainerToPod(pod, ctr); err != nil { return nil, err } - } else { - if err := r.state.AddContainer(ctr); err != nil { - return nil, err - } + } else if err := r.state.AddContainer(ctr); err != nil { + return nil, err } ctr.newContainerEvent(events.Create) return ctr, nil diff --git a/libpod/runtime_pod_linux.go b/libpod/runtime_pod_linux.go index 563d9728a..450c64d24 100644 --- a/libpod/runtime_pod_linux.go +++ b/libpod/runtime_pod_linux.go @@ -19,7 +19,7 @@ import ( ) // NewPod makes a new, empty pod -func (r *Runtime) NewPod(ctx context.Context, options ...PodCreateOption) (_ *Pod, Err error) { +func (r *Runtime) NewPod(ctx context.Context, options ...PodCreateOption) (_ *Pod, deferredErr error) { r.lock.Lock() defer r.lock.Unlock() @@ -65,7 +65,7 @@ func (r *Runtime) NewPod(ctx context.Context, options ...PodCreateOption) (_ *Po pod.config.LockID = pod.lock.ID() defer func() { - if Err != nil { + if deferredErr != nil { if err := pod.lock.Free(); err != nil { logrus.Errorf("Error freeing pod lock after failed creation: %v", err) } @@ -126,7 +126,7 @@ func (r *Runtime) NewPod(ctx context.Context, options ...PodCreateOption) (_ *Po return nil, errors.Wrapf(err, "error adding pod to state") } defer func() { - if Err != nil { + if deferredErr != nil { if err := r.removePod(ctx, pod, true, true); err != nil { logrus.Errorf("Error removing pod after pause container creation failure: %v", err) } diff --git a/libpod/runtime_volume_linux.go b/libpod/runtime_volume_linux.go index 5b05acea4..e1f3480ce 100644 --- a/libpod/runtime_volume_linux.go +++ b/libpod/runtime_volume_linux.go @@ -28,7 +28,7 @@ func (r *Runtime) NewVolume(ctx context.Context, options ...VolumeCreateOption) } // newVolume creates a new empty volume -func (r *Runtime) newVolume(ctx context.Context, options ...VolumeCreateOption) (_ *Volume, Err error) { +func (r *Runtime) newVolume(ctx context.Context, options ...VolumeCreateOption) (_ *Volume, deferredErr error) { volume, err := newVolume(r) if err != nil { return nil, errors.Wrapf(err, "error creating volume") @@ -98,7 +98,7 @@ func (r *Runtime) newVolume(ctx context.Context, options ...VolumeCreateOption) volume.config.LockID = volume.lock.ID() defer func() { - if Err != nil { + if deferredErr != nil { if err := volume.lock.Free(); err != nil { logrus.Errorf("Error freeing volume lock after failed creation: %v", err) } diff --git a/libpod/state_test.go b/libpod/state_test.go index d4a4149f9..39937d8e4 100644 --- a/libpod/state_test.go +++ b/libpod/state_test.go @@ -350,7 +350,8 @@ func TestAddCtrSameNamespaceSucceeds(t *testing.T) { testCtr.config.Namespace = "test1" - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) err = state.AddContainer(testCtr) assert.NoError(t, err) @@ -369,12 +370,14 @@ func TestAddCtrDifferentNamespaceFails(t *testing.T) { testCtr.config.Namespace = "test1" - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.AddContainer(testCtr) assert.Error(t, err) - state.SetNamespace("") + err = state.SetNamespace("") + assert.NoError(t, err) ctrs, err := state.AllContainers() assert.NoError(t, err) @@ -406,7 +409,8 @@ func TestGetContainerInDifferentNamespaceFails(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) _, err = state.Container(testCtr.ID()) assert.Error(t, err) @@ -423,7 +427,8 @@ func TestGetContainerInSameNamespaceSucceeds(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) ctr, err := state.Container(testCtr.ID()) assert.NoError(t, err) @@ -586,7 +591,8 @@ func TestLookupCtrInSameNamespaceSucceeds(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) ctr, err := state.LookupContainer(testCtr.ID()) assert.NoError(t, err) @@ -608,7 +614,8 @@ func TestLookupCtrInDifferentNamespaceFails(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) _, err = state.LookupContainer(testCtr.ID()) assert.Error(t, err) @@ -633,7 +640,8 @@ func TestLookupContainerMatchInDifferentNamespaceSucceeds(t *testing.T) { err = state.AddContainer(testCtr2) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) ctr, err := state.LookupContainer("000") assert.NoError(t, err) @@ -698,7 +706,8 @@ func TestHasContainerSameNamespaceIsTrue(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) exists, err := state.HasContainer(testCtr.ID()) assert.NoError(t, err) @@ -716,7 +725,8 @@ func TestHasContainerDifferentNamespaceIsFalse(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) exists, err := state.HasContainer(testCtr.ID()) assert.NoError(t, err) @@ -759,7 +769,8 @@ func TestSaveAndUpdateContainerSameNamespaceSucceeds(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) retrievedCtr, err := state.Container(testCtr.ID()) assert.NoError(t, err) @@ -806,7 +817,8 @@ func TestUpdateContainerNotInNamespaceReturnsError(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.UpdateContainer(testCtr) assert.Error(t, err) @@ -841,7 +853,8 @@ func TestSaveContainerNotInNamespaceReturnsError(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.SaveContainer(testCtr) assert.Error(t, err) @@ -894,12 +907,14 @@ func TestRemoveContainerNotInNamespaceFails(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 1, len(ctrs)) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.RemoveContainer(testCtr) assert.Error(t, err) - state.SetNamespace("") + err = state.SetNamespace("") + assert.NoError(t, err) ctrs2, err := state.AllContainers() assert.NoError(t, err) @@ -960,7 +975,8 @@ func TestGetAllContainersNoContainerInNamespace(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) ctrs, err := state.AllContainers() assert.NoError(t, err) @@ -984,7 +1000,8 @@ func TestGetContainerOneContainerInNamespace(t *testing.T) { err = state.AddContainer(testCtr2) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) ctrs, err := state.AllContainers() assert.NoError(t, err) @@ -1020,7 +1037,8 @@ func TestContainerInUseCtrNotInNamespace(t *testing.T) { err = state.AddContainer(testCtr) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) _, err = state.ContainerInUse(testCtr) assert.Error(t, err) @@ -1497,7 +1515,8 @@ func TestGetPodInNamespaceSucceeds(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) statePod, err := state.Pod(testPod.ID()) assert.NoError(t, err) @@ -1516,7 +1535,8 @@ func TestGetPodPodNotInNamespaceFails(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) _, err = state.Pod(testPod.ID()) assert.Error(t, err) @@ -1637,7 +1657,8 @@ func TestLookupPodInSameNamespaceSucceeds(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) statePod, err := state.LookupPod(testPod.ID()) assert.NoError(t, err) @@ -1656,7 +1677,8 @@ func TestLookupPodInDifferentNamespaceFails(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) _, err = state.LookupPod(testPod.ID()) assert.Error(t, err) @@ -1681,7 +1703,8 @@ func TestLookupPodOneInDifferentNamespaceFindsRightPod(t *testing.T) { err = state.AddPod(testPod2) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) pod, err := state.LookupPod(strings.Repeat("1", 5)) assert.NoError(t, err) @@ -1757,7 +1780,8 @@ func TestHasPodSameNamespaceSucceeds(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) exist, err := state.HasPod(testPod.ID()) assert.NoError(t, err) @@ -1775,7 +1799,8 @@ func TestHasPodDifferentNamespaceFails(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) exist, err := state.HasPod(testPod.ID()) assert.NoError(t, err) @@ -1913,7 +1938,8 @@ func TestAddPodSameNamespaceSucceeds(t *testing.T) { testPod.config.Namespace = "test1" - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) err = state.AddPod(testPod) assert.NoError(t, err) @@ -1933,12 +1959,14 @@ func TestAddPodDifferentNamespaceFails(t *testing.T) { testPod.config.Namespace = "test1" - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.AddPod(testPod) assert.Error(t, err) - state.SetNamespace("") + err = state.SetNamespace("") + assert.NoError(t, err) allPods, err := state.AllPods() assert.NoError(t, err) @@ -2067,12 +2095,14 @@ func TestRemovePodNotInNamespaceFails(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.RemovePod(testPod) assert.Error(t, err) - state.SetNamespace("") + err = state.SetNamespace("") + assert.NoError(t, err) allPods, err := state.AllPods() assert.NoError(t, err) @@ -2152,7 +2182,8 @@ func TestAllPodsPodInDifferentNamespaces(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) allPods, err := state.AllPods() assert.NoError(t, err) @@ -2178,7 +2209,8 @@ func TestAllPodsOnePodInDifferentNamespace(t *testing.T) { err = state.AddPod(testPod2) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) allPods, err := state.AllPods() assert.NoError(t, err) @@ -2274,7 +2306,8 @@ func TestPodHasContainerPodNotInNamespaceFails(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) _, err = state.PodHasContainer(testPod, strings.Repeat("2", 32)) assert.Error(t, err) @@ -2393,7 +2426,8 @@ func TestPodContainerByIDPodNotInNamespace(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) _, err = state.PodContainersByID(testPod) assert.Error(t, err) @@ -2513,7 +2547,8 @@ func TestPodContainersPodNotInNamespace(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) _, err = state.PodContainers(testPod) assert.Error(t, err) @@ -2686,7 +2721,8 @@ func TestRemoveContainersNotInNamespace(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.RemovePodContainers(testPod) assert.Error(t, err) @@ -3229,7 +3265,8 @@ func TestAddCtrToPodSameNamespaceSucceeds(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) err = state.AddContainerToPod(testPod, testCtr) assert.NoError(t, err) @@ -3253,15 +3290,17 @@ func TestAddCtrToPodDifferentNamespaceFails(t *testing.T) { testPod.config.Namespace = "test1" testCtr.config.Pod = testPod.ID() - state.AddPod(testPod) + err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.AddContainerToPod(testPod, testCtr) assert.Error(t, err) - state.SetNamespace("") + err = state.SetNamespace("") + assert.NoError(t, err) ctrs, err := state.AllContainers() assert.NoError(t, err) @@ -3461,7 +3500,8 @@ func TestRemoveContainerFromPodSameNamespaceSucceeds(t *testing.T) { err = state.AddContainerToPod(testPod, testCtr) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) err = state.RemoveContainerFromPod(testPod, testCtr) assert.NoError(t, err) @@ -3495,12 +3535,14 @@ func TestRemoveContainerFromPodDifferentNamespaceFails(t *testing.T) { err = state.AddContainerToPod(testPod, testCtr) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.RemoveContainerFromPod(testPod, testCtr) assert.Error(t, err) - state.SetNamespace("") + err = state.SetNamespace("") + assert.NoError(t, err) ctrs, err := state.PodContainers(testPod) assert.NoError(t, err) @@ -3539,7 +3581,8 @@ func TestUpdatePodNotInNamespaceFails(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.UpdatePod(testPod) assert.Error(t, err) @@ -3573,7 +3616,8 @@ func TestSavePodNotInNamespaceFails(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test2") + err = state.SetNamespace("test2") + assert.NoError(t, err) err = state.SavePod(testPod) assert.Error(t, err) @@ -3615,7 +3659,8 @@ func TestSaveAndUpdatePodSameNamespace(t *testing.T) { err = state.AddPod(testPod) assert.NoError(t, err) - state.SetNamespace("test1") + err = state.SetNamespace("test1") + assert.NoError(t, err) statePod, err := state.Pod(testPod.ID()) assert.NoError(t, err) diff --git a/libpod/volume_internal_linux.go b/libpod/volume_internal_linux.go index 70eccbecb..081a17325 100644 --- a/libpod/volume_internal_linux.go +++ b/libpod/volume_internal_linux.go @@ -39,7 +39,7 @@ func (v *Volume) mount() error { // If the count is non-zero, the volume is already mounted. // Nothing to do. if v.state.MountCount > 0 { - v.state.MountCount = v.state.MountCount + 1 + v.state.MountCount += 1 logrus.Debugf("Volume %s mount count now at %d", v.Name(), v.state.MountCount) return v.save() } @@ -81,7 +81,7 @@ func (v *Volume) mount() error { logrus.Debugf("Mounted volume %s", v.Name()) // Increment the mount counter - v.state.MountCount = v.state.MountCount + 1 + v.state.MountCount += 1 logrus.Debugf("Volume %s mount count now at %d", v.Name(), v.state.MountCount) return v.save() } @@ -124,7 +124,7 @@ func (v *Volume) unmount(force bool) error { } if !force { - v.state.MountCount = v.state.MountCount - 1 + v.state.MountCount -= 1 } else { v.state.MountCount = 0 } |