aboutsummaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
Diffstat (limited to 'pkg')
-rw-r--r--pkg/adapter/containers.go16
-rw-r--r--pkg/adapter/runtime.go7
-rw-r--r--pkg/adapter/shortcuts/shortcuts.go7
-rw-r--r--pkg/api/handlers/utils/handler.go6
-rw-r--r--pkg/cgroups/cgroups.go6
-rw-r--r--pkg/spec/namespaces.go42
-rw-r--r--pkg/spec/storage.go7
-rw-r--r--pkg/timetype/timestamp.go2
-rw-r--r--pkg/util/utils.go24
9 files changed, 64 insertions, 53 deletions
diff --git a/pkg/adapter/containers.go b/pkg/adapter/containers.go
index 4228c3dca..c348d2130 100644
--- a/pkg/adapter/containers.go
+++ b/pkg/adapter/containers.go
@@ -609,11 +609,12 @@ func (r *LocalRuntime) Restore(ctx context.Context, c *cliconfig.RestoreValues)
return state == define.ContainerStateExited
})
- if c.Import != "" {
+ switch {
+ case c.Import != "":
containers, err = crImportCheckpoint(ctx, r.Runtime, c.Import, c.Name)
- } else if c.All {
+ case c.All:
containers, err = r.GetContainers(filterFuncs...)
- } else {
+ default:
containers, err = shortcuts.GetContainersByContext(false, c.Latest, c.InputArgs, r.Runtime)
}
if err != nil {
@@ -835,25 +836,26 @@ func (r *LocalRuntime) Restart(ctx context.Context, c *cliconfig.RestartValues)
inputTimeout := c.Timeout
// Handle --latest
- if c.Latest {
+ switch {
+ case c.Latest:
lastCtr, err := r.Runtime.GetLatestContainer()
if err != nil {
return nil, nil, errors.Wrapf(err, "unable to get latest container")
}
restartContainers = append(restartContainers, lastCtr)
- } else if c.Running {
+ case c.Running:
containers, err = r.GetRunningContainers()
if err != nil {
return nil, nil, err
}
restartContainers = append(restartContainers, containers...)
- } else if c.All {
+ case c.All:
containers, err = r.Runtime.GetAllContainers()
if err != nil {
return nil, nil, err
}
restartContainers = append(restartContainers, containers...)
- } else {
+ default:
for _, id := range c.InputArgs {
ctr, err := r.Runtime.LookupContainer(id)
if err != nil {
diff --git a/pkg/adapter/runtime.go b/pkg/adapter/runtime.go
index 121f85755..40089797d 100644
--- a/pkg/adapter/runtime.go
+++ b/pkg/adapter/runtime.go
@@ -407,7 +407,8 @@ func (r *LocalRuntime) Events(c *cliconfig.EventValues) error {
}
w := bufio.NewWriter(os.Stdout)
for event := range eventChannel {
- if c.Format == formats.JSONString {
+ switch {
+ case c.Format == formats.JSONString:
jsonStr, err := event.ToJSONString()
if err != nil {
return errors.Wrapf(err, "unable to format json")
@@ -415,11 +416,11 @@ func (r *LocalRuntime) Events(c *cliconfig.EventValues) error {
if _, err := w.Write([]byte(jsonStr)); err != nil {
return err
}
- } else if len(c.Format) > 0 {
+ case len(c.Format) > 0:
if err := tmpl.Execute(w, event); err != nil {
return err
}
- } else {
+ default:
if _, err := w.Write([]byte(event.ToHumanReadable())); err != nil {
return err
}
diff --git a/pkg/adapter/shortcuts/shortcuts.go b/pkg/adapter/shortcuts/shortcuts.go
index 4f6cfd6a3..8a8459c6c 100644
--- a/pkg/adapter/shortcuts/shortcuts.go
+++ b/pkg/adapter/shortcuts/shortcuts.go
@@ -42,12 +42,13 @@ func GetContainersByContext(all, latest bool, names []string, runtime *libpod.Ru
var ctr *libpod.Container
ctrs = []*libpod.Container{}
- if all {
+ switch {
+ case all:
ctrs, err = runtime.GetAllContainers()
- } else if latest {
+ case latest:
ctr, err = runtime.GetLatestContainer()
ctrs = append(ctrs, ctr)
- } else {
+ default:
for _, n := range names {
ctr, e := runtime.LookupContainer(n)
if e != nil {
diff --git a/pkg/api/handlers/utils/handler.go b/pkg/api/handlers/utils/handler.go
index 0815e6eca..8c2110f97 100644
--- a/pkg/api/handlers/utils/handler.go
+++ b/pkg/api/handlers/utils/handler.go
@@ -12,19 +12,19 @@ import (
// WriteResponse encodes the given value as JSON or string and renders it for http client
func WriteResponse(w http.ResponseWriter, code int, value interface{}) {
- switch value.(type) {
+ switch v := value.(type) {
case string:
w.Header().Set("Content-Type", "text/plain; charset=us-ascii")
w.WriteHeader(code)
- if _, err := fmt.Fprintln(w, value); err != nil {
+ if _, err := fmt.Fprintln(w, v); err != nil {
log.Errorf("unable to send string response: %q", err)
}
case *os.File:
w.Header().Set("Content-Type", "application/octet; charset=us-ascii")
w.WriteHeader(code)
- if _, err := io.Copy(w, value.(*os.File)); err != nil {
+ if _, err := io.Copy(w, v); err != nil {
log.Errorf("unable to copy to response: %q", err)
}
default:
diff --git a/pkg/cgroups/cgroups.go b/pkg/cgroups/cgroups.go
index 9711e8120..6b28b2759 100644
--- a/pkg/cgroups/cgroups.go
+++ b/pkg/cgroups/cgroups.go
@@ -155,7 +155,7 @@ func (c *CgroupControl) getCgroupv1Path(name string) string {
}
// createCgroupv2Path creates the cgroupv2 path and enables all the available controllers
-func createCgroupv2Path(path string) (Err error) {
+func createCgroupv2Path(path string) (deferredError error) {
content, err := ioutil.ReadFile("/sys/fs/cgroup/cgroup.controllers")
if err != nil {
return errors.Wrapf(err, "read /sys/fs/cgroup/cgroup.controllers")
@@ -169,7 +169,7 @@ func createCgroupv2Path(path string) (Err error) {
if i == 0 {
res = fmt.Sprintf("+%s", c)
} else {
- res = res + fmt.Sprintf(" +%s", c)
+ res += fmt.Sprintf(" +%s", c)
}
}
resByte := []byte(res)
@@ -186,7 +186,7 @@ func createCgroupv2Path(path string) (Err error) {
} else {
// If the directory was created, be sure it is not left around on errors.
defer func() {
- if Err != nil {
+ if deferredError != nil {
os.Remove(current)
}
}()
diff --git a/pkg/spec/namespaces.go b/pkg/spec/namespaces.go
index 8e95a3ca0..e62d4ed0a 100644
--- a/pkg/spec/namespaces.go
+++ b/pkg/spec/namespaces.go
@@ -44,7 +44,8 @@ func (c *NetworkConfig) ToCreateOptions(runtime *libpod.Runtime, userns *UserCon
}
}
- if c.NetMode.IsNS() {
+ switch {
+ case c.NetMode.IsNS():
ns := c.NetMode.NS()
if ns == "" {
return nil, errors.Errorf("invalid empty user-defined network namespace")
@@ -53,13 +54,13 @@ func (c *NetworkConfig) ToCreateOptions(runtime *libpod.Runtime, userns *UserCon
if err != nil {
return nil, err
}
- } else if c.NetMode.IsContainer() {
+ case c.NetMode.IsContainer():
connectedCtr, err := runtime.LookupContainer(c.NetMode.Container())
if err != nil {
return nil, errors.Wrapf(err, "container %q not found", c.NetMode.Container())
}
options = append(options, libpod.WithNetNSFrom(connectedCtr))
- } else if !c.NetMode.IsHost() && !c.NetMode.IsNone() {
+ case !c.NetMode.IsHost() && !c.NetMode.IsNone():
postConfigureNetNS := userns.getPostConfigureNetNS()
options = append(options, libpod.WithNetNS(portBindings, postConfigureNetNS, string(c.NetMode), networks))
}
@@ -102,29 +103,31 @@ func (c *NetworkConfig) ToCreateOptions(runtime *libpod.Runtime, userns *UserCon
// state of the NetworkConfig.
func (c *NetworkConfig) ConfigureGenerator(g *generate.Generator) error {
netMode := c.NetMode
- if netMode.IsHost() {
+ netCtr := netMode.Container()
+ switch {
+ case netMode.IsHost():
logrus.Debug("Using host netmode")
if err := g.RemoveLinuxNamespace(string(spec.NetworkNamespace)); err != nil {
return err
}
- } else if netMode.IsNone() {
+ case netMode.IsNone():
logrus.Debug("Using none netmode")
- } else if netMode.IsBridge() {
+ case netMode.IsBridge():
logrus.Debug("Using bridge netmode")
- } else if netCtr := netMode.Container(); netCtr != "" {
+ case netCtr != "":
logrus.Debugf("using container %s netmode", netCtr)
- } else if IsNS(string(netMode)) {
+ case IsNS(string(netMode)):
logrus.Debug("Using ns netmode")
if err := g.AddOrReplaceLinuxNamespace(string(spec.NetworkNamespace), NS(string(netMode))); err != nil {
return err
}
- } else if IsPod(string(netMode)) {
+ case IsPod(string(netMode)):
logrus.Debug("Using pod netmode, unless pod is not sharing")
- } else if netMode.IsSlirp4netns() {
+ case netMode.IsSlirp4netns():
logrus.Debug("Using slirp4netns netmode")
- } else if netMode.IsUserDefined() {
+ case netMode.IsUserDefined():
logrus.Debug("Using user defined netmode")
- } else {
+ default:
return errors.Errorf("unknown network mode")
}
@@ -220,7 +223,8 @@ func (c *CgroupConfig) ToCreateOptions(runtime *libpod.Runtime) ([]libpod.CtrCre
// ToCreateOptions converts the input to container create options.
func (c *UserConfig) ToCreateOptions(runtime *libpod.Runtime) ([]libpod.CtrCreateOption, error) {
options := make([]libpod.CtrCreateOption, 0)
- if c.UsernsMode.IsNS() {
+ switch {
+ case c.UsernsMode.IsNS():
ns := c.UsernsMode.NS()
if ns == "" {
return nil, errors.Errorf("invalid empty user-defined user namespace")
@@ -230,13 +234,13 @@ func (c *UserConfig) ToCreateOptions(runtime *libpod.Runtime) ([]libpod.CtrCreat
return nil, err
}
options = append(options, libpod.WithIDMappings(*c.IDMappings))
- } else if c.UsernsMode.IsContainer() {
+ case c.UsernsMode.IsContainer():
connectedCtr, err := runtime.LookupContainer(c.UsernsMode.Container())
if err != nil {
return nil, errors.Wrapf(err, "container %q not found", c.UsernsMode.Container())
}
options = append(options, libpod.WithUserNSFrom(connectedCtr))
- } else {
+ default:
options = append(options, libpod.WithIDMappings(*c.IDMappings))
}
@@ -413,20 +417,22 @@ func (c *UtsConfig) ToCreateOptions(runtime *libpod.Runtime, pod *libpod.Pod) ([
// of the UtsConfig.
func (c *UtsConfig) ConfigureGenerator(g *generate.Generator, net *NetworkConfig, runtime *libpod.Runtime) error {
hostname := c.Hostname
+ utsCtrID := c.UtsMode.Container()
var err error
if hostname == "" {
- if utsCtrID := c.UtsMode.Container(); utsCtrID != "" {
+ switch {
+ case utsCtrID != "":
utsCtr, err := runtime.GetContainer(utsCtrID)
if err != nil {
return errors.Wrapf(err, "unable to retrieve hostname from dependency container %s", utsCtrID)
}
hostname = utsCtr.Hostname()
- } else if net.NetMode.IsHost() || c.UtsMode.IsHost() {
+ case net.NetMode.IsHost() || c.UtsMode.IsHost():
hostname, err = os.Hostname()
if err != nil {
return errors.Wrap(err, "unable to retrieve hostname of the host")
}
- } else {
+ default:
logrus.Debug("No hostname set; container's hostname will default to runtime default")
}
}
diff --git a/pkg/spec/storage.go b/pkg/spec/storage.go
index dbdab0030..0e2098c1d 100644
--- a/pkg/spec/storage.go
+++ b/pkg/spec/storage.go
@@ -409,9 +409,10 @@ func getBindMount(args []string) (spec.Mount, error) {
// ro=[true|false]
// rw
// rw=[true|false]
- if len(kv) == 1 {
+ switch len(kv) {
+ case 1:
newMount.Options = append(newMount.Options, kv[0])
- } else if len(kv) == 2 {
+ case 2:
switch strings.ToLower(kv[1]) {
case "true":
newMount.Options = append(newMount.Options, kv[0])
@@ -424,7 +425,7 @@ func getBindMount(args []string) (spec.Mount, error) {
default:
return newMount, errors.Wrapf(optionArgError, "%s must be set to true or false, instead received %q", kv[0], kv[1])
}
- } else {
+ default:
return newMount, errors.Wrapf(optionArgError, "badly formatted option %q", val)
}
case "nosuid", "suid":
diff --git a/pkg/timetype/timestamp.go b/pkg/timetype/timestamp.go
index eb904a574..2de1a005f 100644
--- a/pkg/timetype/timestamp.go
+++ b/pkg/timetype/timestamp.go
@@ -34,7 +34,7 @@ func GetTimestamp(value string, reference time.Time) (string, error) {
// if the string has a Z or a + or three dashes use parse otherwise use parseinlocation
parseInLocation := !(strings.ContainsAny(value, "zZ+") || strings.Count(value, "-") == 3)
- if strings.Contains(value, ".") {
+ if strings.Contains(value, ".") { // nolint(gocritic)
if parseInLocation {
format = rFC3339NanoLocal
} else {
diff --git a/pkg/util/utils.go b/pkg/util/utils.go
index 9269f6115..6aa3c221e 100644
--- a/pkg/util/utils.go
+++ b/pkg/util/utils.go
@@ -321,14 +321,14 @@ func ParseSignal(rawSignal string) (syscall.Signal, error) {
}
// ParseIDMapping takes idmappings and subuid and subgid maps and returns a storage mapping
-func ParseIDMapping(mode namespaces.UsernsMode, UIDMapSlice, GIDMapSlice []string, subUIDMap, subGIDMap string) (*storage.IDMappingOptions, error) {
+func ParseIDMapping(mode namespaces.UsernsMode, uidMapSlice, gidMapSlice []string, subUIDMap, subGIDMap string) (*storage.IDMappingOptions, error) {
options := storage.IDMappingOptions{
HostUIDMapping: true,
HostGIDMapping: true,
}
if mode.IsKeepID() {
- if len(UIDMapSlice) > 0 || len(GIDMapSlice) > 0 {
+ if len(uidMapSlice) > 0 || len(gidMapSlice) > 0 {
return nil, errors.New("cannot specify custom mappings with --userns=keep-id")
}
if len(subUIDMap) > 0 || len(subGIDMap) > 0 {
@@ -384,17 +384,17 @@ func ParseIDMapping(mode namespaces.UsernsMode, UIDMapSlice, GIDMapSlice []strin
if subUIDMap == "" && subGIDMap != "" {
subUIDMap = subGIDMap
}
- if len(GIDMapSlice) == 0 && len(UIDMapSlice) != 0 {
- GIDMapSlice = UIDMapSlice
+ if len(gidMapSlice) == 0 && len(uidMapSlice) != 0 {
+ gidMapSlice = uidMapSlice
}
- if len(UIDMapSlice) == 0 && len(GIDMapSlice) != 0 {
- UIDMapSlice = GIDMapSlice
+ if len(uidMapSlice) == 0 && len(gidMapSlice) != 0 {
+ uidMapSlice = gidMapSlice
}
- if len(UIDMapSlice) == 0 && subUIDMap == "" && os.Getuid() != 0 {
- UIDMapSlice = []string{fmt.Sprintf("0:%d:1", os.Getuid())}
+ if len(uidMapSlice) == 0 && subUIDMap == "" && os.Getuid() != 0 {
+ uidMapSlice = []string{fmt.Sprintf("0:%d:1", os.Getuid())}
}
- if len(GIDMapSlice) == 0 && subGIDMap == "" && os.Getuid() != 0 {
- GIDMapSlice = []string{fmt.Sprintf("0:%d:1", os.Getgid())}
+ if len(gidMapSlice) == 0 && subGIDMap == "" && os.Getuid() != 0 {
+ gidMapSlice = []string{fmt.Sprintf("0:%d:1", os.Getgid())}
}
if subUIDMap != "" && subGIDMap != "" {
@@ -405,11 +405,11 @@ func ParseIDMapping(mode namespaces.UsernsMode, UIDMapSlice, GIDMapSlice []strin
options.UIDMap = mappings.UIDs()
options.GIDMap = mappings.GIDs()
}
- parsedUIDMap, err := idtools.ParseIDMap(UIDMapSlice, "UID")
+ parsedUIDMap, err := idtools.ParseIDMap(uidMapSlice, "UID")
if err != nil {
return nil, err
}
- parsedGIDMap, err := idtools.ParseIDMap(GIDMapSlice, "GID")
+ parsedGIDMap, err := idtools.ParseIDMap(gidMapSlice, "GID")
if err != nil {
return nil, err
}