Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: fix part of megacheck issue #2500

Merged
merged 1 commit into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions apis/filters/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,7 @@ func FromParam(p string) (Args, error) {

raw := []byte(p)
err := json.Unmarshal(raw, &args)
if err != nil {
return args, err
}
return args, nil
return args, err
}

// FromFilterOpts parse key=value to Args string from cli opts
Expand Down
12 changes: 3 additions & 9 deletions apis/opts/config/blkio.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ func (w *WeightDevice) Type() string {
// Value returns all values as type WeightDevice
func (w *WeightDevice) Value() []*types.WeightDevice {
var weightDevice []*types.WeightDevice
for _, v := range w.values {
weightDevice = append(weightDevice, v)
}
weightDevice = append(weightDevice, w.values...)

return weightDevice
}
Expand Down Expand Up @@ -124,9 +122,7 @@ func (t *ThrottleBpsDevice) Type() string {
// Value returns all values as type ThrottleDevice
func (t *ThrottleBpsDevice) Value() []*types.ThrottleDevice {
var throttleDevice []*types.ThrottleDevice
for _, v := range t.values {
throttleDevice = append(throttleDevice, v)
}
throttleDevice = append(throttleDevice, t.values...)

return throttleDevice
}
Expand Down Expand Up @@ -182,9 +178,7 @@ func (t *ThrottleIOpsDevice) Type() string {
// Value returns all values
func (t *ThrottleIOpsDevice) Value() []*types.ThrottleDevice {
var throttleDevice []*types.ThrottleDevice
for _, v := range t.values {
throttleDevice = append(throttleDevice, v)
}
throttleDevice = append(throttleDevice, t.values...)

return throttleDevice
}
2 changes: 1 addition & 1 deletion apis/opts/config/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (r *Runtime) Set(val string) error {
func (r *Runtime) String() string {
var str []string
for k := range *r.values {
str = append(str, fmt.Sprintf("%s", k))
str = append(str, k)
}

return fmt.Sprintf("%v", str)
Expand Down
7 changes: 2 additions & 5 deletions apis/server/image_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,8 @@ func (s *Server) saveImage(ctx context.Context, rw http.ResponseWriter, req *htt
defer r.Close()

output := newWriteFlusher(rw)
if _, err := io.Copy(output, r); err != nil {
return err
}

return nil
_, err = io.Copy(output, r)
return err
}

// getImageHistory gets image history.
Expand Down
2 changes: 1 addition & 1 deletion cli/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type container struct {
networks []string
ports []string
expose []string
publicAll bool
ZYecho marked this conversation as resolved.
Show resolved Hide resolved
publishAll bool
securityOpt []string
capAdd []string
capDrop []string
Expand Down
4 changes: 2 additions & 2 deletions cli/image_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ func (i *ImagesCommand) runImages(args []string) error {

for _, dimg := range dimgs {
if i.flagDigest {
display.AddRow([]string{dimg.id, dimg.name, dimg.digest, fmt.Sprintf("%s", dimg.size)})
display.AddRow([]string{dimg.id, dimg.name, dimg.digest, string(dimg.size)})
} else {
display.AddRow([]string{dimg.id, dimg.name, fmt.Sprintf("%s", dimg.size)})
display.AddRow([]string{dimg.id, dimg.name, string(dimg.size)})
}
}

Expand Down
1 change: 0 additions & 1 deletion cli/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ var tagDescription = "tag command is to add tag reference for the existing image
// TagCommand use to implement 'tag' command.
type TagCommand struct {
baseCommand
args []string
}

// Init initialize tag command.
Expand Down
1 change: 0 additions & 1 deletion cli/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var topDescription = "top command is to display the running processes of a conta
// TopCommand use to implement 'top' command, it displays all processes in a container.
type TopCommand struct {
baseCommand
args []string
}

// Init initialize top command.
Expand Down
1 change: 0 additions & 1 deletion cli/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var updateDescription = "Update a container's configurations, including memory,
type UpdateCommand struct {
baseCommand
container
image string
}

// Init initialize update command.
Expand Down
6 changes: 2 additions & 4 deletions client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,8 @@ func (client *APIClient) newRequest(method, path string, query url.Values, body
return nil, err
}

if header != nil {
for k, v := range header {
req.Header[k] = v
}
for k, v := range header {
req.Header[k] = v
}

return req, err
Expand Down
1 change: 0 additions & 1 deletion cri/criservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ func RunCriService(daemonconfig *config.Config, containerMgr mgr.ContainerMgr, i
readyCh <- false
err = fmt.Errorf("failed to start CRI service: invalid CRI version %s, expected to be v1alpha1 or v1alpha2", daemonconfig.CriConfig.CriVersion)
}
return
}

// Start CRI service with CRI version: v1alpha1
Expand Down
7 changes: 0 additions & 7 deletions cri/stream/portforward/httpstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,6 @@ func (h *httpStreamHandler) getStreamPair(requestID string) (*httpStreamPair, bo
return pair, true
}

// hasStreamPair returns a bool indicating if a stream pair for requestID exists.
func (h *httpStreamHandler) hasStreamPair(requestID string) bool {
ZYecho marked this conversation as resolved.
Show resolved Hide resolved
_, ok := h.streamPairs.Get(requestID).Result()

return ok
}

// removeStreamPair removes the stream pair identified by requestID from streamPairs.
func (h *httpStreamHandler) removeStreamPair(requestID string) {
h.streamPairs.Remove(requestID)
Expand Down
2 changes: 0 additions & 2 deletions cri/stream/portforward/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,4 @@ func ServePortForward(ctx context.Context, w http.ResponseWriter, req *http.Requ
logrus.Errorf("failed to serve port forward: %v", err)
return
}

return
}
2 changes: 0 additions & 2 deletions ctrd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ type Client struct {
watch *watch
lock *containerLock

rpcAddr string
ZYecho marked this conversation as resolved.
Show resolved Hide resolved

// containerd grpc pool
pool []scheduler.Factory
scheduler scheduler.Scheduler
Expand Down
3 changes: 1 addition & 2 deletions ctrd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,9 +701,8 @@ func (c *Client) waitContainer(ctx context.Context, id string) (types.ContainerW
return c.ProbeContainer(ctx, id, -1*time.Second)
}

var msg *Message
// wait for the task to exit.
msg = waitExit()
msg := waitExit()

errMsg := ""
err = msg.RawError()
Expand Down
6 changes: 2 additions & 4 deletions ctrd/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ func (c *Client) CreateSnapshot(ctx context.Context, id, ref string) error {
}

parent := identity.ChainID(diffIDs).String()
if _, err := wrapperCli.client.SnapshotService(defaultSnapshotterName).Prepare(ctx, id, parent); err != nil {
return err
}
return nil
_, err = wrapperCli.client.SnapshotService(defaultSnapshotterName).Prepare(ctx, id, parent)
return err
}

// GetSnapshot returns the snapshot's info by id.
Expand Down
2 changes: 1 addition & 1 deletion daemon/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (cfg *Config) MergeConfigurations(flagSet *pflag.FlagSet) error {
return nil
}

fileFlags := make(map[string]interface{}, 0)
fileFlags := make(map[string]interface{})
flattenConfig(origin, fileFlags)

// check conflict in command line flags and config file
Expand Down
4 changes: 2 additions & 2 deletions daemon/logger/jsonfile/jsonfile_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestReadLogMessagesWithRemoveFileInFollowMode(t *testing.T) {
os.RemoveAll(f.Name())

select {
case _, _ = <-watcher.Msgs:
case <-watcher.Msgs:
case <-time.After(250 * time.Millisecond):
// NOTE: watchTimeout is 200ms
t.Fatal("expected watcher.Msgs has been closed after removed file, but it's still alive")
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestReadLogMessagesForEmptyFileWithoutFollow(t *testing.T) {
<-time.After(100 * time.Millisecond)

select {
case _, _ = <-watcher.Msgs:
case <-watcher.Msgs:
case <-time.After(100 * time.Millisecond):
t.Fatalf("expected watcher.Msgs has been closed after removed file, but it's still alive")
}
Expand Down
20 changes: 18 additions & 2 deletions daemon/logger/jsonfile/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import (
"os"
"reflect"
"strings"
"sync"
"testing"
"time"

"github.com/alibaba/pouch/daemon/logger"
"github.com/alibaba/pouch/pkg/utils"

"github.com/pkg/errors"
)

var _ logger.LogDriver = &JSONLogFile{}
Expand Down Expand Up @@ -133,21 +136,25 @@ func TestFollowFile(t *testing.T) {
// create goroutine to read the file
watcher := logger.NewLogWatcher()
waitCh := make(chan struct{})
errchan := make(chan error)
var wg sync.WaitGroup
defer func() {
watcher.Close()
<-waitCh
}()

go func() {
// NOTE: make sure all the goutine exits
wg.Add(1)
// NOTE: make sure all the goroutine exits
defer func() {
waitCh <- struct{}{}
}()

newF, err := os.Open(f.Name())
if err != nil {
t.Fatalf("unexpected error during open file for followFile: %v", err)
errchan <- errors.Errorf("unexpected error during open file for followFile: %v", err)
}
wg.Done()

cfg := &logger.ReadConfig{
Since: generateTime(t, "2018-05-09T10:00:01.1Z"),
Expand All @@ -156,6 +163,15 @@ func TestFollowFile(t *testing.T) {
followFile(newF, cfg, newUnmarshal, watcher)
}()

go func() {
wg.Wait()
close(errchan)
}()

if err := <-errchan; err != nil {
t.Fatal(err)
}

// should read three log message
for _, el := range expectedMsgs[:2] {
checkExpectedLogMessage(t, watcher, el)
Expand Down
37 changes: 0 additions & 37 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
containerdtypes "github.com/containerd/containerd/api/types"
"github.com/containerd/containerd/mount"
"github.com/docker/go-units"
"github.com/docker/libnetwork"
"github.com/go-openapi/strfmt"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
Expand Down Expand Up @@ -1525,41 +1524,6 @@ func (mgr *ContainerManager) connectToNetwork(ctx context.Context, container *Co
return mgr.updateNetworkConfig(container, networkIDOrName, endpoint.EndpointConfig)
}

// FIXME: remove this useless functions
func (mgr *ContainerManager) updateNetworkSettings(container *Container, n libnetwork.Network) error {
if container.NetworkSettings == nil {
container.NetworkSettings = &types.NetworkSettings{Networks: make(map[string]*types.EndpointSettings)}
}

if !IsHost(container.HostConfig.NetworkMode) && IsHost(n.Type()) {
return fmt.Errorf("container cannot be connected to host network")
}

for s := range container.NetworkSettings.Networks {
sn, err := mgr.NetworkMgr.Get(context.Background(), s)
if err != nil {
continue
}

if sn.Name == n.Name() {
// Avoid duplicate config
return nil
}
if !IsPrivate(sn.Type) || !IsPrivate(n.Type()) {
return fmt.Errorf("container sharing network namespace with another container or host cannot be connected to any other network")
}
if IsNone(sn.Name) || IsNone(n.Name()) {
return fmt.Errorf("container cannot be connected to multiple networks with one of the networks in none mode")
}
}

if _, ok := container.NetworkSettings.Networks[n.Name()]; !ok {
container.NetworkSettings.Networks[n.Name()] = new(types.EndpointSettings)
}

return nil
}

func (mgr *ContainerManager) initContainerIO(c *Container) (*containerio.IO, error) {
if io := mgr.IOs.Get(c.ID); io != nil {
return nil, errors.Wrap(errtypes.ErrConflict, "failed to create containerIO")
Expand Down Expand Up @@ -1814,7 +1778,6 @@ func (mgr *ContainerManager) resetContainerIOs(containerID string) {
}

io.Reset()
return
}

// buildContainerEndpoint builds Endpoints according to container
Expand Down
1 change: 0 additions & 1 deletion daemon/mgr/container_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,4 @@ func (mgr *ContainerManager) SetContainerLogPath(c *Container) {
if c.HostConfig.LogConfig.LogDriver == "json-file" {
c.LogPath = filepath.Join(mgr.Config.HomeDir, "containers", c.ID, "json.log")
}
return
}
2 changes: 1 addition & 1 deletion daemon/mgr/container_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ func copyImageContent(source, destination string) error {

// destination directory is not exist, so mkdir for it.
logrus.Warnf("(%s) is not exist", destination)
if err := os.MkdirAll(destination, 755); err != nil && !os.IsExist(err) {
if err := os.MkdirAll(destination, 0755); err != nil && !os.IsExist(err) {
return err
}
} else if !fi.IsDir() {
Expand Down
2 changes: 1 addition & 1 deletion daemon/mgr/container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ func (c *Container) UnsetMergedDir() {
// SetSnapshotterMeta sets snapshotter for container
func (c *Container) SetSnapshotterMeta(mounts []mount.Mount) {
// TODO(ziren): now we only support overlayfs
data := make(map[string]string, 0)
data := make(map[string]string)
for _, opt := range mounts[0].Options {
if strings.HasPrefix(opt, "upperdir=") {
data["UpperDir"] = strings.TrimPrefix(opt, "upperdir=")
Expand Down
3 changes: 1 addition & 2 deletions daemon/mgr/image_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,8 @@ func (store *imageStore) Search(ref reference.Named) (digest.Digest, reference.N

func (store *imageStore) searchIDs(refID string) (digest.Digest, error) {
var ids []digest.Digest
var id string

id = refID
id := refID
if !strings.HasPrefix(refID, digest.Canonical.String()) {
id = fmt.Sprintf("%s:%s", digest.Canonical.String(), refID)
}
Expand Down
2 changes: 1 addition & 1 deletion lxcfs/lxcfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func CheckLxcfsMount() error {
break
}
}
if isMount == false {
if !isMount {
return fmt.Errorf("%s is not a mount point, please run \" lxcfs %s \" before Pouchd", LxcfsHomeDir, LxcfsHomeDir)
}
return nil
Expand Down
7 changes: 2 additions & 5 deletions pkg/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@ func tarFromDir(src string, writer io.Writer) error {
defer f.Close()

// copy file data into tar writer
if _, err := io.Copy(tw, f); err != nil {
return err
}

return nil
_, err = io.Copy(tw, f)
return err
})
}

Expand Down
Loading