From 3e8606202a375aebec4ebd7da4648b36da3a24ce Mon Sep 17 00:00:00 2001 From: fupan Date: Fri, 10 Aug 2018 17:12:40 +0800 Subject: [PATCH] containerd-shim-kata-v2: retune the code style Removed some unused variables and retuned some if/else code style. Fixes: #572 Signed-off-by: fupan --- cli/containerd-shim-kata-v2/main.go | 9 ++-- containerd-shim/kata/config.go | 3 -- containerd-shim/kata/container.go | 11 ++-- containerd-shim/kata/service.go | 81 +++++++++++++++-------------- containerd-shim/kata/start.go | 6 +++ containerd-shim/kata/stream.go | 1 + 6 files changed, 58 insertions(+), 53 deletions(-) diff --git a/cli/containerd-shim-kata-v2/main.go b/cli/containerd-shim-kata-v2/main.go index 5b9260fbc1..df0b3d78e1 100644 --- a/cli/containerd-shim-kata-v2/main.go +++ b/cli/containerd-shim-kata-v2/main.go @@ -6,11 +6,11 @@ package main import ( -"fmt" -"os" + "fmt" + "os" -"github.com/kata-containers/runtime/containerd-shim/kata" -"github.com/containerd/containerd/runtime/v2/shim" + "github.com/containerd/containerd/runtime/v2/shim" + "github.com/kata-containers/runtime/containerd-shim/kata" ) func main() { @@ -19,4 +19,3 @@ func main() { os.Exit(1) } } - diff --git a/containerd-shim/kata/config.go b/containerd-shim/kata/config.go index 410645c726..ef4a0b2d2b 100644 --- a/containerd-shim/kata/config.go +++ b/containerd-shim/kata/config.go @@ -19,7 +19,6 @@ import ( ) var defaultHypervisorPath = "/usr/bin/qemu-lite-system-x86_64" -var defaultImagePath = "/usr/share/kata-containers/kata-containers.img" var defaultKernelPath = "/usr/share/kata-containers/vmlinuz.container" var defaultInitrdPath = "/usr/share/kata-containers/kata-containers-initrd.img" var defaultFirmwarePath = "" @@ -27,7 +26,6 @@ var defaultMachineAccelerators = "" const defaultKernelParams = "" const defaultMachineType = "pc" -const defaultRootDirectory = "/var/run/kata-containers" const systemdUnitName = "kata-containers.target" const defaultVCPUCount uint32 = 1 @@ -35,7 +33,6 @@ const defaultMaxVCPUCount uint32 = 0 const defaultMemSize uint32 = 2048 // MiB const defaultBridgesCount uint32 = 1 const defaultInterNetworkingModel = "macvtap" -const defaultDisableBlockDeviceUse bool = false const defaultBlockDeviceDriver = "virtio-scsi" const defaultEnableIOThreads bool = false const defaultEnableMemPrealloc bool = false diff --git a/containerd-shim/kata/container.go b/containerd-shim/kata/container.go index a31e858efc..0477137c76 100644 --- a/containerd-shim/kata/container.go +++ b/containerd-shim/kata/container.go @@ -27,12 +27,11 @@ type Container struct { exitIOch chan struct{} exitch chan uint32 - bundle string - execs map[string]*Exec - container vc.VCContainer - status task.Status - exit uint32 - time time.Time + bundle string + execs map[string]*Exec + status task.Status + exit uint32 + time time.Time mu sync.Mutex } diff --git a/containerd-shim/kata/service.go b/containerd-shim/kata/service.go index 2359260e8a..528fff63ce 100644 --- a/containerd-shim/kata/service.go +++ b/containerd-shim/kata/service.go @@ -113,7 +113,7 @@ func (s *service) pid() uint32 { if !ok { break } else { - pidCount += 1 + pidCount++ //if it overflows, recount from 5 if pidCount < 5 { pidCount = 5 @@ -378,16 +378,16 @@ func (s *service) Start(ctx context.Context, r *taskAPI.StartRequest) (*taskAPI. return &taskAPI.StartResponse{ Pid: c.pid, }, nil - } else { //start an exec - execs, err := startExec(ctx, s, r.ID, r.ExecID) - if err != nil { - return nil, errdefs.ToGRPC(err) - } - - return &taskAPI.StartResponse{ - Pid: execs.pid, - }, nil } + //start an exec + execs, err := startExec(ctx, s, r.ID, r.ExecID) + if err != nil { + return nil, errdefs.ToGRPC(err) + } + + return &taskAPI.StartResponse{ + Pid: execs.pid, + }, nil } // Delete the initial process and container @@ -411,21 +411,21 @@ func (s *service) Delete(ctx context.Context, r *taskAPI.DeleteRequest) (*taskAP ExitedAt: c.time, Pid: c.pid, }, nil - } else { - execs, err := c.getExec(r.ExecID) - if err != nil { - return nil, err - } + } + //deal with the exec case + execs, err := c.getExec(r.ExecID) + if err != nil { + return nil, err + } - delete(s.processes, execs.pid) - delete(c.execs, r.ExecID) + delete(s.processes, execs.pid) + delete(c.execs, r.ExecID) - return &taskAPI.DeleteResponse{ - ExitStatus: uint32(execs.exitCode), - ExitedAt: execs.exitTime, - Pid: execs.pid, - }, nil - } + return &taskAPI.DeleteResponse{ + ExitStatus: uint32(execs.exitCode), + ExitedAt: execs.exitTime, + Pid: execs.pid, + }, nil } // Exec an additional process inside the container @@ -504,23 +504,26 @@ func (s *service) State(ctx context.Context, r *taskAPI.StateRequest) (*taskAPI. Terminal: c.terminal, ExitStatus: c.exit, }, nil - } else { - execs, err := c.getExec(r.ExecID) - if err != nil { - return nil, err - } - return &taskAPI.StateResponse{ - ID: execs.id, - Bundle: c.bundle, - Pid: execs.pid, - Status: execs.status, - Stdin: execs.tty.stdin, - Stdout: execs.tty.stdout, - Stderr: execs.tty.stderr, - Terminal: execs.tty.terminal, - ExitStatus: uint32(execs.exitCode), - }, nil } + + //deal with exec case + execs, err := c.getExec(r.ExecID) + if err != nil { + return nil, err + } + + return &taskAPI.StateResponse{ + ID: execs.id, + Bundle: c.bundle, + Pid: execs.pid, + Status: execs.status, + Stdin: execs.tty.stdin, + Stdout: execs.tty.stdout, + Stderr: execs.tty.stderr, + Terminal: execs.tty.terminal, + ExitStatus: uint32(execs.exitCode), + }, nil + } // Pause the container diff --git a/containerd-shim/kata/start.go b/containerd-shim/kata/start.go index 9640360524..df44d952ee 100644 --- a/containerd-shim/kata/start.go +++ b/containerd-shim/kata/start.go @@ -44,6 +44,9 @@ func startContainer(ctx context.Context, s *service, c *Container) error { return err } tty, err := newTtyIO(ctx, c.stdin, c.stdout, c.stderr, c.terminal) + if err != nil { + return err + } c.ttyio = tty go ioCopy(c.exitIOch, tty, stdin, stdout, stderr) @@ -88,6 +91,9 @@ func startExec(ctx context.Context, s *service, containerID, execID string) (*Ex return nil, err } tty, err := newTtyIO(ctx, execs.tty.stdin, execs.tty.stdout, execs.tty.stderr, execs.tty.terminal) + if err != nil { + return nil, err + } execs.ttyio = tty go ioCopy(execs.exitIOch, tty, stdin, stdout, stderr) diff --git a/containerd-shim/kata/stream.go b/containerd-shim/kata/stream.go index ad5f844937..419959da37 100644 --- a/containerd-shim/kata/stream.go +++ b/containerd-shim/kata/stream.go @@ -2,6 +2,7 @@ // // SPDX-License-Identifier: Apache-2.0 // + package kata import (