Skip to content

Commit

Permalink
runtime: fix static check errors
Browse files Browse the repository at this point in the history
It turns out we have managed to break the static checker in many
difference places with the absence of static checker in github action.
Let's fix them while enabling static checker in github actions...

Signed-off-by: Peng Tao <bergwolf@hyper.sh>
  • Loading branch information
bergwolf committed Mar 24, 2021
1 parent a2dee1f commit 74192d1
Show file tree
Hide file tree
Showing 37 changed files with 202 additions and 377 deletions.
3 changes: 1 addition & 2 deletions src/runtime/cli/kata-check.go
Expand Up @@ -63,7 +63,6 @@ const (
moduleParamDir = "parameters"
successMessageCapable = "System is capable of running " + project
successMessageCreate = "System can currently create " + project
successMessageVersion = "Version consistency of " + project + " is verified"
failMessage = "System is not capable of running " + project
kernelPropertyCorrect = "Kernel property value correct"

Expand Down Expand Up @@ -398,7 +397,7 @@ EXAMPLES:
span, _ := katautils.Trace(ctx, "check")
defer span.End()

if context.Bool("no-network-checks") == false && os.Getenv(noNetworkEnvVar) == "" {
if !context.Bool("no-network-checks") && os.Getenv(noNetworkEnvVar) == "" {
cmd := RelCmdCheck

if context.Bool("only-list-releases") {
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/cli/kata-monitor/main.go
Expand Up @@ -48,9 +48,9 @@ var versionTemplate = `{{.AppName}}
`

func printVersion(ver versionInfo) {
t, err := template.New("version").Parse(versionTemplate)
t, _ := template.New("version").Parse(versionTemplate)

if err = t.Execute(os.Stdout, ver); err != nil {
if err := t.Execute(os.Stdout, ver); err != nil {
panic(err)
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/runtime/cli/main.go
Expand Up @@ -64,9 +64,6 @@ var originalLoggerLevel = logrus.WarnLevel

var debug = false

// if true, coredump when an internal error occurs or a fatal signal is received
var crashOnError = false

// concrete virtcontainer implementation
var virtcontainersImpl = &vc.VCImpl{}

Expand Down Expand Up @@ -328,7 +325,6 @@ func beforeSubcommands(c *cli.Context) error {
}
if !subCmdIsCheckCmd {
debug = runtimeConfig.Debug
crashOnError = runtimeConfig.Debug

if traceRootSpan != "" {
// Create the tracer.
Expand Down
46 changes: 2 additions & 44 deletions src/runtime/cli/main_test.go
Expand Up @@ -8,7 +8,6 @@ package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
Expand All @@ -29,7 +28,6 @@ import (
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/compatoci"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/oci"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/pkg/vcmock"
"github.com/kata-containers/kata-containers/src/runtime/virtcontainers/types"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"

Expand All @@ -44,10 +42,8 @@ const (
// small docker image used to create root filesystems from
testDockerImage = "busybox"

testSandboxID = "99999999-9999-9999-99999999999999999"
testContainerID = "1"
testBundle = "bundle"
testConsole = "/dev/pts/999"
testBundle = "bundle"
testConsole = "/dev/pts/999"
)

var (
Expand Down Expand Up @@ -387,44 +383,6 @@ func makeOCIBundle(bundleDir string) error {
return nil
}

func writeOCIConfigFile(spec specs.Spec, configPath string) error {
if configPath == "" {
return errors.New("BUG: need config file path")
}

bytes, err := json.MarshalIndent(spec, "", "\t")
if err != nil {
return err
}

return ioutil.WriteFile(configPath, bytes, testFileMode)
}

func newSingleContainerStatus(containerID string, containerState types.ContainerState, annotations map[string]string, spec *specs.Spec) vc.ContainerStatus {
return vc.ContainerStatus{
ID: containerID,
State: containerState,
Annotations: annotations,
Spec: spec,
}
}

func execCLICommandFunc(assertHandler *assert.Assertions, cliCommand cli.Command, set *flag.FlagSet, expectedErr bool) {
ctx := createCLIContext(set)
ctx.App.Name = "foo"

fn, ok := cliCommand.Action.(func(context *cli.Context) error)
assertHandler.True(ok)

err := fn(ctx)

if expectedErr {
assertHandler.Error(err)
} else {
assertHandler.Nil(err)
}
}

func createCLIContextWithApp(flagSet *flag.FlagSet, app *cli.App) *cli.Context {
ctx := cli.NewContext(app, flagSet, nil)

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/cli/release_test.go
Expand Up @@ -458,7 +458,7 @@ func TestGetNewReleaseType(t *testing.T) {
}

data := []testData{
// Check build metadata (ignored for version comparisions)
// Check build metadata (ignored for version comparisons)
{"2.0.0+build", "2.0.0", true, ""},
{"2.0.0+build-1", "2.0.0+build-2", true, ""},
{"1.12.0+build", "1.12.0", true, ""},
Expand Down
18 changes: 0 additions & 18 deletions src/runtime/cli/utils.go
Expand Up @@ -189,21 +189,3 @@ func constructVersionInfo(version string) VersionInfo {
}

}

func versionEqual(a VersionInfo, b VersionInfo) bool {
av, err := semver.Make(a.Semver)
if err != nil {
return false
}

bv, err := semver.Make(b.Semver)
if err != nil {
return false
}

if av.Major == bv.Major && av.Minor == bv.Minor && av.Patch == bv.Patch {
return true
}

return false
}
10 changes: 0 additions & 10 deletions src/runtime/cli/utils_arch_base.go

This file was deleted.

10 changes: 0 additions & 10 deletions src/runtime/cli/utils_s390x.go

This file was deleted.

34 changes: 17 additions & 17 deletions src/runtime/containerd-shim-v2/service.go
Expand Up @@ -301,7 +301,7 @@ func trace(ctx context.Context, name string) (otelTrace.Span, context.Context) {
}

func (s *service) Cleanup(ctx context.Context) (_ *taskAPI.DeleteResponse, err error) {
span, ctx := trace(s.rootCtx, "Cleanup")
span, _ := trace(s.rootCtx, "Cleanup")
defer span.End()

//Since the binary cleanup will return the DeleteResponse from stdout to
Expand Down Expand Up @@ -412,7 +412,7 @@ func (s *service) Create(ctx context.Context, r *taskAPI.CreateTaskRequest) (_ *

// Start a process
func (s *service) Start(ctx context.Context, r *taskAPI.StartRequest) (_ *taskAPI.StartResponse, err error) {
span, ctx := trace(s.rootCtx, "Start")
span, _ := trace(s.rootCtx, "Start")
defer span.End()

start := time.Now()
Expand Down Expand Up @@ -463,7 +463,7 @@ func (s *service) Start(ctx context.Context, r *taskAPI.StartRequest) (_ *taskAP

// Delete the initial process and container
func (s *service) Delete(ctx context.Context, r *taskAPI.DeleteRequest) (_ *taskAPI.DeleteResponse, err error) {
span, ctx := trace(s.rootCtx, "Delete")
span, _ := trace(s.rootCtx, "Delete")
defer span.End()

start := time.Now()
Expand Down Expand Up @@ -515,7 +515,7 @@ func (s *service) Delete(ctx context.Context, r *taskAPI.DeleteRequest) (_ *task

// Exec an additional process inside the container
func (s *service) Exec(ctx context.Context, r *taskAPI.ExecProcessRequest) (_ *ptypes.Empty, err error) {
span, ctx := trace(s.rootCtx, "Exec")
span, _ := trace(s.rootCtx, "Exec")
defer span.End()

start := time.Now()
Expand Down Expand Up @@ -553,7 +553,7 @@ func (s *service) Exec(ctx context.Context, r *taskAPI.ExecProcessRequest) (_ *p

// ResizePty of a process
func (s *service) ResizePty(ctx context.Context, r *taskAPI.ResizePtyRequest) (_ *ptypes.Empty, err error) {
span, ctx := trace(s.rootCtx, "ResizePty")
span, _ := trace(s.rootCtx, "ResizePty")
defer span.End()

start := time.Now()
Expand Down Expand Up @@ -592,7 +592,7 @@ func (s *service) ResizePty(ctx context.Context, r *taskAPI.ResizePtyRequest) (_

// State returns runtime state information for a process
func (s *service) State(ctx context.Context, r *taskAPI.StateRequest) (_ *taskAPI.StateResponse, err error) {
span, ctx := trace(s.rootCtx, "State")
span, _ := trace(s.rootCtx, "State")
defer span.End()

start := time.Now()
Expand Down Expand Up @@ -644,7 +644,7 @@ func (s *service) State(ctx context.Context, r *taskAPI.StateRequest) (_ *taskAP

// Pause the container
func (s *service) Pause(ctx context.Context, r *taskAPI.PauseRequest) (_ *ptypes.Empty, err error) {
span, ctx := trace(s.rootCtx, "Pause")
span, _ := trace(s.rootCtx, "Pause")
defer span.End()

start := time.Now()
Expand Down Expand Up @@ -683,7 +683,7 @@ func (s *service) Pause(ctx context.Context, r *taskAPI.PauseRequest) (_ *ptypes

// Resume the container
func (s *service) Resume(ctx context.Context, r *taskAPI.ResumeRequest) (_ *ptypes.Empty, err error) {
span, ctx := trace(s.rootCtx, "Resume")
span, _ := trace(s.rootCtx, "Resume")
defer span.End()

start := time.Now()
Expand Down Expand Up @@ -720,7 +720,7 @@ func (s *service) Resume(ctx context.Context, r *taskAPI.ResumeRequest) (_ *ptyp

// Kill a process with the provided signal
func (s *service) Kill(ctx context.Context, r *taskAPI.KillRequest) (_ *ptypes.Empty, err error) {
span, ctx := trace(s.rootCtx, "Kill")
span, _ := trace(s.rootCtx, "Kill")
defer span.End()

start := time.Now()
Expand Down Expand Up @@ -781,7 +781,7 @@ func (s *service) Kill(ctx context.Context, r *taskAPI.KillRequest) (_ *ptypes.E
// Since for kata, it cannot get the process's pid from VM,
// thus only return the Shim's pid directly.
func (s *service) Pids(ctx context.Context, r *taskAPI.PidsRequest) (_ *taskAPI.PidsResponse, err error) {
span, ctx := trace(s.rootCtx, "Pids")
span, _ := trace(s.rootCtx, "Pids")
defer span.End()

var processes []*task.ProcessInfo
Expand All @@ -804,7 +804,7 @@ func (s *service) Pids(ctx context.Context, r *taskAPI.PidsRequest) (_ *taskAPI.

// CloseIO of a process
func (s *service) CloseIO(ctx context.Context, r *taskAPI.CloseIORequest) (_ *ptypes.Empty, err error) {
span, ctx := trace(s.rootCtx, "CloseIO")
span, _ := trace(s.rootCtx, "CloseIO")
defer span.End()

start := time.Now()
Expand Down Expand Up @@ -845,7 +845,7 @@ func (s *service) CloseIO(ctx context.Context, r *taskAPI.CloseIORequest) (_ *pt

// Checkpoint the container
func (s *service) Checkpoint(ctx context.Context, r *taskAPI.CheckpointTaskRequest) (_ *ptypes.Empty, err error) {
span, ctx := trace(s.rootCtx, "Checkpoint")
span, _ := trace(s.rootCtx, "Checkpoint")
defer span.End()

start := time.Now()
Expand All @@ -859,7 +859,7 @@ func (s *service) Checkpoint(ctx context.Context, r *taskAPI.CheckpointTaskReque

// Connect returns shim information such as the shim's pid
func (s *service) Connect(ctx context.Context, r *taskAPI.ConnectRequest) (_ *taskAPI.ConnectResponse, err error) {
span, ctx := trace(s.rootCtx, "Connect")
span, _ := trace(s.rootCtx, "Connect")
defer span.End()

start := time.Now()
Expand All @@ -879,7 +879,7 @@ func (s *service) Connect(ctx context.Context, r *taskAPI.ConnectRequest) (_ *ta
}

func (s *service) Shutdown(ctx context.Context, r *taskAPI.ShutdownRequest) (_ *ptypes.Empty, err error) {
span, ctx := trace(s.rootCtx, "Shutdown")
span, _ := trace(s.rootCtx, "Shutdown")

start := time.Now()
defer func() {
Expand Down Expand Up @@ -907,7 +907,7 @@ func (s *service) Shutdown(ctx context.Context, r *taskAPI.ShutdownRequest) (_ *
}

func (s *service) Stats(ctx context.Context, r *taskAPI.StatsRequest) (_ *taskAPI.StatsResponse, err error) {
span, ctx := trace(s.rootCtx, "Stats")
span, _ := trace(s.rootCtx, "Stats")
defer span.End()

start := time.Now()
Expand Down Expand Up @@ -936,7 +936,7 @@ func (s *service) Stats(ctx context.Context, r *taskAPI.StatsRequest) (_ *taskAP

// Update a running container
func (s *service) Update(ctx context.Context, r *taskAPI.UpdateTaskRequest) (_ *ptypes.Empty, err error) {
span, ctx := trace(s.rootCtx, "Update")
span, _ := trace(s.rootCtx, "Update")
defer span.End()

start := time.Now()
Expand Down Expand Up @@ -968,7 +968,7 @@ func (s *service) Update(ctx context.Context, r *taskAPI.UpdateTaskRequest) (_ *

// Wait for a process to exit
func (s *service) Wait(ctx context.Context, r *taskAPI.WaitRequest) (_ *taskAPI.WaitResponse, err error) {
span, ctx := trace(s.rootCtx, "Wait")
span, _ := trace(s.rootCtx, "Wait")
defer span.End()

var ret uint32
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/containerd-shim-v2/shim_management.go
Expand Up @@ -65,8 +65,7 @@ func (s *service) serveMetrics(w http.ResponseWriter, r *http.Request) {
// encode the metrics
encoder := expfmt.NewEncoder(w, expfmt.FmtText)
for _, mf := range mfs {
if err := encoder.Encode(mf); err != nil {
}
encoder.Encode(mf)
}

// if using an old agent, only collect shim/sandbox metrics.
Expand Down Expand Up @@ -150,7 +149,7 @@ func (s *service) startManagementServer(ctx context.Context, ociSpec *specs.Spec

shimMgtLog.Info("kata management inited")

// bind hanlder
// bind handler
m := http.NewServeMux()
m.Handle("/metrics", http.HandlerFunc(s.serveMetrics))
m.Handle("/agent-url", http.HandlerFunc(s.agentURL))
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/containerd-shim-v2/shim_metrics.go
Expand Up @@ -177,7 +177,7 @@ func calcOverhead(initialSandboxStats, finishSandboxStats vc.SandboxStats, initi
cpuUsageGuest := float64(guestFinalCPU-guestInitCPU) / deltaTime * 100
cpuUsageHost := float64(hostFinalCPU-hostInitCPU) / deltaTime * 100

return float64(hostMemoryUsage - guestMemoryUsage), float64(cpuUsageHost - cpuUsageGuest)
return float64(hostMemoryUsage - guestMemoryUsage), cpuUsageHost - cpuUsageGuest
}

func (s *service) getPodOverhead(ctx context.Context) (float64, float64, error) {
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/containerd-shim-v2/shim_metrics_test.go
Expand Up @@ -97,7 +97,7 @@ func TestStatsSandbox(t *testing.T) {
sandbox.StatsFunc = getSandboxCPUFunc(2000, 110000)
sandbox.StatsContainerFunc = getStatsContainerCPUFunc(200, 400, 20000, 40000)

finishSandboxStats, finishContainersStats, err := s.statsSandbox(context.Background())
finishSandboxStats, finishContainersStats, _ := s.statsSandbox(context.Background())

// calc overhead
mem, cpu := calcOverhead(initialSandboxStats, finishSandboxStats, initialContainerStats, finishContainersStats, 1e9)
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/pkg/kata-monitor/metrics_test.go
Expand Up @@ -107,14 +107,14 @@ func TestEncodeMetricFamily(t *testing.T) {
scrapeCount.Inc()
scrapeCount.Inc()

mfs, err := prometheus.DefaultGatherer.Gather()
mfs, _ := prometheus.DefaultGatherer.Gather()

// create encoder
buf := bytes.NewBufferString("")
encoder := expfmt.NewEncoder(buf, expfmt.FmtText)

// encode metrics to text format
err = encodeMetricFamily(mfs, encoder)
err := encodeMetricFamily(mfs, encoder)
assert.Nil(err, "encodeMetricFamily should not return error")

// here will be to many metrics,
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/pkg/kata-monitor/monitor.go
Expand Up @@ -38,7 +38,7 @@ type KataMonitor struct {
// NewKataMonitor create and return a new KataMonitor instance
func NewKataMonitor(containerdAddr, containerdConfigFile string) (*KataMonitor, error) {
if containerdAddr == "" {
return nil, fmt.Errorf("Containerd serve address missing.")
return nil, fmt.Errorf("containerd serve address missing")
}

containerdConf := &srvconfig.Config{
Expand Down Expand Up @@ -82,7 +82,7 @@ func (km *KataMonitor) initSandboxCache() error {

// GetAgentURL returns agent URL
func (km *KataMonitor) GetAgentURL(w http.ResponseWriter, r *http.Request) {
sandboxID, err := getSandboxIdFromReq(r)
sandboxID, err := getSandboxIDFromReq(r)
if err != nil {
commonServeError(w, http.StatusBadRequest, err)
return
Expand Down

0 comments on commit 74192d1

Please sign in to comment.