Skip to content

Commit

Permalink
Merge pull request #8947 from Luap99/cleanup-code
Browse files Browse the repository at this point in the history
Fix problems reported by staticcheck
  • Loading branch information
openshift-merge-robot authored Jan 12, 2021
2 parents 0ccc888 + 8452b76 commit db5e7ec
Show file tree
Hide file tree
Showing 28 changed files with 55 additions and 105 deletions.
5 changes: 2 additions & 3 deletions cmd/podman/common/create.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package common

import (
"fmt"
"os"

"github.com/containers/common/pkg/auth"
Expand Down Expand Up @@ -181,7 +180,7 @@ func DefineCreateFlags(cmd *cobra.Command, cf *ContainerCLIOpts) {
createFlags.StringSliceVar(
&cf.Devices,
deviceFlagName, devices(),
fmt.Sprintf("Add a host device to the container"),
"Add a host device to the container",
)
_ = cmd.RegisterFlagCompletionFunc(deviceFlagName, completion.AutocompleteDefault)

Expand Down Expand Up @@ -359,7 +358,7 @@ func DefineCreateFlags(cmd *cobra.Command, cf *ContainerCLIOpts) {
&cf.InitPath,
initPathFlagName, initPath(),
// Do not use the Value field for setting the default value to determine user input (i.e., non-empty string)
fmt.Sprintf("Path to the container-init binary"),
"Path to the container-init binary",
)
_ = cmd.RegisterFlagCompletionFunc(initPathFlagName, completion.AutocompleteDefault)

Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/containers/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import (
)

var (
pruneDescription = fmt.Sprintf(`podman container prune
pruneDescription = `podman container prune
Removes all non running containers`)
Removes all non running containers`
pruneCommand = &cobra.Command{
Use: "prune [options]",
Short: "Remove all non running containers",
Expand Down
10 changes: 5 additions & 5 deletions cmd/podman/play/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,23 +127,23 @@ func kube(cmd *cobra.Command, args []string) error {

for _, pod := range report.Pods {
for _, l := range pod.Logs {
fmt.Fprintf(os.Stderr, l)
fmt.Fprint(os.Stderr, l)
}
}

ctrsFailed := 0

for _, pod := range report.Pods {
fmt.Printf("Pod:\n")
fmt.Println("Pod:")
fmt.Println(pod.ID)

switch len(pod.Containers) {
case 0:
continue
case 1:
fmt.Printf("Container:\n")
fmt.Println("Container:")
default:
fmt.Printf("Containers:\n")
fmt.Println("Containers:")
}
for _, ctr := range pod.Containers {
fmt.Println(ctr)
Expand All @@ -154,7 +154,7 @@ func kube(cmd *cobra.Command, args []string) error {
fmt.Println()
}
for _, err := range pod.ContainerErrors {
fmt.Fprintf(os.Stderr, err+"\n")
fmt.Fprintln(os.Stderr, err)
}
// Empty line for space for next block
fmt.Println()
Expand Down
5 changes: 2 additions & 3 deletions cmd/podman/pods/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pods

import (
"context"
"fmt"
"os"
"text/tabwriter"
"text/template"
Expand All @@ -21,9 +20,9 @@ var (
)

var (
inspectDescription = fmt.Sprintf(`Display the configuration for a pod by name or id
inspectDescription = `Display the configuration for a pod by name or id
By default, this will render all results in a JSON array.`)
By default, this will render all results in a JSON array.`

inspectCmd = &cobra.Command{
Use: "inspect [options] POD [POD...]",
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/pods/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
)

var (
pruneDescription = fmt.Sprintf(`podman pod prune Removes all exited pods`)
pruneDescription = `podman pod prune Removes all exited pods`

pruneCommand = &cobra.Command{
Use: "prune [options]",
Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/pods/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ type podRmOptionsWrapper struct {

var (
rmOptions = podRmOptionsWrapper{}
podRmDescription = fmt.Sprintf(`podman rm will remove one or more stopped pods and their containers from the host.
podRmDescription = `podman rm will remove one or more stopped pods and their containers from the host.
The pod name or ID can be used. A pod with containers will not be removed without --force. If --force is specified, all containers will be stopped, then removed.`)
The pod name or ID can be used. A pod with containers will not be removed without --force. If --force is specified, all containers will be stopped, then removed.`
rmCommand = &cobra.Command{
Use: "rm [options] POD [POD...]",
Short: "Remove one or more pods",
Expand Down
4 changes: 2 additions & 2 deletions cmd/podman/system/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
var (
pruneOptions = entities.SystemPruneOptions{}
filters []string
pruneDescription = fmt.Sprintf(`
pruneDescription = `
podman system prune
Remove unused data
`)
`

pruneCommand = &cobra.Command{
Use: "prune [options]",
Expand Down
2 changes: 1 addition & 1 deletion cmd/podman/system/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func service(cmd *cobra.Command, args []string) error {
}

// socket activation uses a unix:// socket in the shipped unit files but apiURI is coded as "" at this layer.
if "unix" == uri.Scheme && !registry.IsRemote() {
if uri.Scheme == "unix" && !registry.IsRemote() {
if err := syscall.Unlink(uri.Path); err != nil && !os.IsNotExist(err) {
return err
}
Expand Down
17 changes: 6 additions & 11 deletions libpod/container_internal_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
"github.com/containers/storage/pkg/idtools"
securejoin "github.com/cyphar/filepath-securejoin"
runcuser "github.com/opencontainers/runc/libcontainer/user"
"github.com/opencontainers/runtime-spec/specs-go"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
"github.com/opencontainers/selinux/go-selinux/label"
Expand Down Expand Up @@ -284,7 +283,7 @@ func (c *Container) generateSpec(ctx context.Context) (*spec.Spec, error) {
return nil, err
}

g := generate.NewFromSpec(c.config.Spec)
g := generate.Generator{Config: c.config.Spec}

// If network namespace was requested, add it now
if c.config.CreateNetNS {
Expand Down Expand Up @@ -400,7 +399,7 @@ func (c *Container) generateSpec(ctx context.Context) (*spec.Spec, error) {
return nil, errors.Wrapf(err, "failed to create TempDir in the %s directory", c.config.StaticDir)
}

var overlayMount specs.Mount
var overlayMount spec.Mount
if volume.ReadWrite {
overlayMount, err = overlay.Mount(contentDir, mountPoint, volume.Dest, c.RootUID(), c.RootGID(), c.runtime.store.GraphOptions())
} else {
Expand Down Expand Up @@ -1519,18 +1518,14 @@ func (c *Container) makeBindMounts() error {
}
if newPasswd != "" {
// Make /etc/passwd
if _, ok := c.state.BindMounts["/etc/passwd"]; ok {
// If it already exists, delete so we can recreate
delete(c.state.BindMounts, "/etc/passwd")
}
// If it already exists, delete so we can recreate
delete(c.state.BindMounts, "/etc/passwd")
c.state.BindMounts["/etc/passwd"] = newPasswd
}
if newGroup != "" {
// Make /etc/group
if _, ok := c.state.BindMounts["/etc/group"]; ok {
// If it already exists, delete so we can recreate
delete(c.state.BindMounts, "/etc/group")
}
// If it already exists, delete so we can recreate
delete(c.state.BindMounts, "/etc/group")
c.state.BindMounts["/etc/group"] = newGroup
}

Expand Down
16 changes: 7 additions & 9 deletions libpod/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/containers/common/pkg/retry"
cp "github.com/containers/image/v5/copy"
"github.com/containers/image/v5/directory"
"github.com/containers/image/v5/docker/archive"
dockerarchive "github.com/containers/image/v5/docker/archive"
"github.com/containers/image/v5/docker/reference"
"github.com/containers/image/v5/image"
Expand All @@ -37,7 +36,6 @@ import (
"github.com/containers/podman/v2/pkg/util"
"github.com/containers/storage"
digest "github.com/opencontainers/go-digest"
imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
opentracing "github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
Expand Down Expand Up @@ -185,7 +183,7 @@ func (ir *Runtime) SaveImages(ctx context.Context, namesOrIDs []string, format s

sys := GetSystemContext("", "", false)

archWriter, err := archive.NewWriter(sys, outputFile)
archWriter, err := dockerarchive.NewWriter(sys, outputFile)
if err != nil {
return err
}
Expand Down Expand Up @@ -291,7 +289,7 @@ func (ir *Runtime) LoadAllImagesFromDockerArchive(ctx context.Context, fileName
}

sc := GetSystemContext(signaturePolicyPath, "", false)
reader, err := archive.NewReader(sc, fileName)
reader, err := dockerarchive.NewReader(sc, fileName)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1148,7 +1146,7 @@ func (i *Image) GetLabel(ctx context.Context, label string) (string, error) {
}

for k, v := range labels {
if strings.ToLower(k) == strings.ToLower(label) {
if strings.EqualFold(k, label) {
return v, nil
}
}
Expand Down Expand Up @@ -1326,7 +1324,7 @@ func (ir *Runtime) Import(ctx context.Context, path, reference string, writer io

annotations := make(map[string]string)

// config imgspecv1.Image
// config ociv1.Image
err = updater.ConfigUpdate(imageConfig, annotations)
if err != nil {
return nil, errors.Wrapf(err, "error updating image config")
Expand Down Expand Up @@ -1435,7 +1433,7 @@ func (i *Image) IsParent(ctx context.Context) (bool, error) {

// historiesMatch returns the number of entries in the histories which have the
// same contents
func historiesMatch(a, b []imgspecv1.History) int {
func historiesMatch(a, b []ociv1.History) int {
i := 0
for i < len(a) && i < len(b) {
if a[i].Created != nil && b[i].Created == nil {
Expand Down Expand Up @@ -1468,7 +1466,7 @@ func historiesMatch(a, b []imgspecv1.History) int {

// areParentAndChild checks diff ID and history in the two images and return
// true if the second should be considered to be directly based on the first
func areParentAndChild(parent, child *imgspecv1.Image) bool {
func areParentAndChild(parent, child *ociv1.Image) bool {
// the child and candidate parent should share all of the
// candidate parent's diff IDs, which together would have
// controlled which layers were used
Expand Down Expand Up @@ -1621,7 +1619,7 @@ func (i *Image) Save(ctx context.Context, source, format, output string, moreTag
if err != nil {
return errors.Wrapf(err, "error getting the OCI directory ImageReference for (%q, %q)", output, destImageName)
}
manifestType = imgspecv1.MediaTypeImageManifest
manifestType = ociv1.MediaTypeImageManifest
case "docker-dir":
destRef, err = directory.NewReference(output)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion libpod/image/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func generatePruneFilterFuncs(filter, filterValue string) (ImageFilter, error) {
return false
}
for labelKey, labelValue := range labels {
if labelKey == filterKey && ("" == filterValue || labelValue == filterValue) {
if labelKey == filterKey && (filterValue == "" || labelValue == filterValue) {
return true
}
}
Expand Down
7 changes: 3 additions & 4 deletions libpod/image/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
cp "github.com/containers/image/v5/copy"
"github.com/containers/image/v5/directory"
"github.com/containers/image/v5/docker"
"github.com/containers/image/v5/docker/archive"
dockerarchive "github.com/containers/image/v5/docker/archive"
ociarchive "github.com/containers/image/v5/oci/archive"
oci "github.com/containers/image/v5/oci/layout"
Expand Down Expand Up @@ -130,7 +129,7 @@ func (ir *Runtime) getSinglePullRefPairGoal(srcRef types.ImageReference, destNam

// getPullRefPairsFromDockerArchiveReference returns a slice of pullRefPairs
// for the specified docker reference and the corresponding archive.Reader.
func (ir *Runtime) getPullRefPairsFromDockerArchiveReference(ctx context.Context, reader *archive.Reader, ref types.ImageReference, sc *types.SystemContext) ([]pullRefPair, error) {
func (ir *Runtime) getPullRefPairsFromDockerArchiveReference(ctx context.Context, reader *dockerarchive.Reader, ref types.ImageReference, sc *types.SystemContext) ([]pullRefPair, error) {
destNames, err := reader.ManifestTagsForReference(ref)
if err != nil {
return nil, err
Expand Down Expand Up @@ -178,7 +177,7 @@ func (ir *Runtime) pullGoalFromImageReference(ctx context.Context, srcRef types.
// supports pulling from docker-archive, oci, and registries
switch srcRef.Transport().Name() {
case DockerArchive:
reader, readerRef, err := archive.NewReaderForReference(sc, srcRef)
reader, readerRef, err := dockerarchive.NewReaderForReference(sc, srcRef)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -432,7 +431,7 @@ func checkRemoteImageForLabel(ctx context.Context, label string, imageInfo pullR
}
// Labels are case insensitive; so we iterate instead of simple lookup
for k := range remoteInspect.Labels {
if strings.ToLower(label) == strings.ToLower(k) {
if strings.EqualFold(label, k) {
return nil
}
}
Expand Down
20 changes: 5 additions & 15 deletions libpod/in_memory_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,8 @@ func (s *InMemoryState) RemoveContainer(ctr *Container) error {
}

// Remove our network aliases
if _, ok := s.ctrNetworkAliases[ctr.ID()]; ok {
delete(s.ctrNetworkAliases, ctr.ID())
}
if _, ok := s.ctrNetworks[ctr.ID()]; ok {
delete(s.ctrNetworks, ctr.ID())
}
delete(s.ctrNetworkAliases, ctr.ID())
delete(s.ctrNetworks, ctr.ID())

return nil
}
Expand Down Expand Up @@ -680,9 +676,7 @@ func (s *InMemoryState) NetworkDisconnect(ctr *Container, network string) error
ctrAliases = make(map[string][]string)
s.ctrNetworkAliases[ctr.ID()] = ctrAliases
}
if _, ok := ctrAliases[network]; ok {
delete(ctrAliases, network)
}
delete(ctrAliases, network)

return nil
}
Expand Down Expand Up @@ -1523,12 +1517,8 @@ func (s *InMemoryState) RemoveContainerFromPod(pod *Pod, ctr *Container) error {
}

// Remove our network aliases
if _, ok := s.ctrNetworkAliases[ctr.ID()]; ok {
delete(s.ctrNetworkAliases, ctr.ID())
}
if _, ok := s.ctrNetworks[ctr.ID()]; ok {
delete(s.ctrNetworks, ctr.ID())
}
delete(s.ctrNetworkAliases, ctr.ID())
delete(s.ctrNetworks, ctr.ID())

return nil
}
Expand Down
2 changes: 1 addition & 1 deletion libpod/network/netconflist.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func IfPassesFilter(netconf *libcni.NetworkConfigList, filters map[string][]stri
filterValue = ""
}
for labelKey, labelValue := range labels {
if labelKey == filterKey && ("" == filterValue || labelValue == filterValue) {
if labelKey == filterKey && (filterValue == "" || labelValue == filterValue) {
result = true
continue outer
}
Expand Down
1 change: 1 addition & 0 deletions libpod/oci_conmon_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,7 @@ func (r *ConmonOCIRuntime) sharedConmonArgs(ctr *Container, cuuid, bundlePath, p
logDriverArg = define.NoLogging
case define.JSONLogging:
fallthrough
//lint:ignore ST1015 the default case has to be here
default: //nolint-stylecheck
// No case here should happen except JSONLogging, but keep this here in case the options are extended
logrus.Errorf("%s logging specified but not supported. Choosing k8s-file logging instead", ctr.LogDriver())
Expand Down
2 changes: 1 addition & 1 deletion libpod/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ func WithUserNSFrom(nsCtr *Container) CtrCreateOption {
ctr.config.UserNsCtr = nsCtr.ID()
ctr.config.IDMappings = nsCtr.config.IDMappings

g := generate.NewFromSpec(ctr.config.Spec)
g := generate.Generator{Config: ctr.config.Spec}

g.ClearLinuxUIDMappings()
for _, uidmap := range nsCtr.config.IDMappings.UIDMap {
Expand Down
3 changes: 1 addition & 2 deletions pkg/api/handlers/libpod/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/containers/podman/v2/libpod"
"github.com/containers/podman/v2/libpod/define"
"github.com/containers/podman/v2/libpod/image"
image2 "github.com/containers/podman/v2/libpod/image"
"github.com/containers/podman/v2/pkg/api/handlers"
"github.com/containers/podman/v2/pkg/api/handlers/utils"
"github.com/containers/podman/v2/pkg/auth"
Expand Down Expand Up @@ -524,7 +523,7 @@ func CommitContainer(w http.ResponseWriter, r *http.Request) {
utils.Error(w, "failed to get runtime config", http.StatusInternalServerError, errors.Wrap(err, "failed to get runtime config"))
return
}
sc := image2.GetSystemContext(rtc.Engine.SignaturePolicyPath, "", false)
sc := image.GetSystemContext(rtc.Engine.SignaturePolicyPath, "", false)
tag := "latest"
options := libpod.ContainerCommitOptions{
Pause: true,
Expand Down
Loading

0 comments on commit db5e7ec

Please sign in to comment.