Skip to content

Commit

Permalink
minor revised all errors due to golangci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Jun 10, 2020
1 parent a1a76af commit 646f232
Show file tree
Hide file tree
Showing 32 changed files with 89 additions and 98 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
name: golangci-lint
on:
push:
tags:
- '!v*'
branches:
- '*'
pull_request:

jobs:
Expand Down
10 changes: 8 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
name: test

on: [push, pull_request]
on:
push:
tags:
- '!v*'
branches:
- '*'
pull_request:

jobs:
build:
unittests:
runs-on: ubuntu-latest
container: projecteru2/footstone:latest

Expand Down
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
run:
timeout: 5m
tests: false
skip-dirs:
- tools
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ libgit2:
cmake --build .
sudo cp libgit2.pc /usr/lib/pkgconfig/
sudo cp libgit2.so.1.0.1 /usr/lib
sudo rm -f /usr/lib/libgit2.so.28 /usr/lib/libgit2.so
sudo ln -s /usr/lib/libgit2.so.1.0.1 /usr/lib/libgit2.so.28
sudo ln -s /usr/lib/libgit2.so.28 /usr/lib/libgit2.so
sudo ln -s /usr/lib/libgit2.so.1.0.1 /usr/lib/libgit2.so
sudo cp -aR ../include/* /usr/local/include/

cloc:
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestListContainers(t *testing.T) {
ctx := context.Background()
ID := "testID"
containers := []*types.Container{
&types.Container{ID: ID},
{ID: ID},
}

store := &storemocks.Store{}
Expand Down
8 changes: 6 additions & 2 deletions cluster/calcium/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ func (c *Calcium) doCreateContainer(ctx context.Context, opts *types.DeployOptio
go metrics.Client.SendDeployCount(nodeInfo.Deploy)
go func(nodeInfo types.NodeInfo, index int) {
defer func() {
c.store.DeleteProcessing(context.Background(), opts, nodeInfo)
if err := c.store.DeleteProcessing(context.Background(), opts, nodeInfo); err != nil {
log.Errorf("[doCreateContainer] remove processing status failed %v", err)
}
wg.Done()
}()

Expand Down Expand Up @@ -212,7 +214,9 @@ func (c *Calcium) doCreateAndStartContainer(
// Copy data to container
if len(opts.Data) > 0 {
for dst, src := range opts.Data {
src.Seek(0, io.SeekStart)
if _, err = src.Seek(0, io.SeekStart); err != nil {
return createContainerMessage
}
if err = c.doSendFileToContainer(ctx, node.Engine, containerCreated.ID, dst, src, true, true); err != nil {
return createContainerMessage
}
Expand Down
5 changes: 1 addition & 4 deletions cluster/calcium/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ func processVirtualizationInStream(
log.Errorf("[processVirtualizationInStream] resize window error: %v", err)
return
}
return
},

string(escapeCommand): func(body []byte) {
Expand All @@ -191,8 +190,7 @@ func rawProcessVirtualizationInStream(
defer inStream.Close()

for cmd := range inCh {
cmdKey := string(cmd[:1])
if f, ok := specialPrefixCallback[cmdKey]; ok {
if f, ok := specialPrefixCallback[string(cmd[:1])]; ok {
f(cmd[1:])
continue
}
Expand Down Expand Up @@ -223,7 +221,6 @@ func processVirtualizationOutStream(
if err := scanner.Err(); err != nil {
log.Errorf("[processVirtualizationOutStream] failed to read output from output stream: %v", err)
}
return
}()
return outCh
}
4 changes: 2 additions & 2 deletions cluster/calcium/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestRemoveImage(t *testing.T) {
assert.Error(t, err)
engine := &enginemocks.API{}
nodes := []*types.Node{
&types.Node{
{
Name: "test",
Engine: engine,
},
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestCacheImage(t *testing.T) {
assert.Error(t, err)
engine := &enginemocks.API{}
nodes := []*types.Node{
&types.Node{
{
Name: "test",
Engine: engine,
},
Expand Down
8 changes: 5 additions & 3 deletions cluster/calcium/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ func (c *Calcium) RunAndWait(ctx context.Context, opts *types.DeployOptions, inC

lambda := func(message *types.CreateContainerMessage) {
defer func() {
c.doRemoveContainerSync(context.Background(), []string{message.ContainerID})
log.Infof("[RunAndWait] Container %s finished and removed", utils.ShortID(message.ContainerID))
if err := c.doRemoveContainerSync(context.Background(), []string{message.ContainerID}); err != nil {
log.Errorf("[RunAndWait] Remove lambda container failed %v", err)
} else {
log.Infof("[RunAndWait] Container %s finished and removed", utils.ShortID(message.ContainerID))
}
wg.Done()
}()

Expand Down Expand Up @@ -89,7 +92,6 @@ func (c *Calcium) RunAndWait(ctx context.Context, opts *types.DeployOptions, inC

exitData := []byte(exitDataPrefix + strconv.Itoa(int(r.Code)))
runMsgCh <- &types.AttachContainerMessage{ContainerID: message.ContainerID, Data: exitData}
return
}

wg.Add(1)
Expand Down
4 changes: 2 additions & 2 deletions cluster/calcium/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func TestWithContainersLocked(t *testing.T) {
lock.On("Unlock", mock.Anything).Return(nil)
// failed to get lock
lock.On("Lock", mock.Anything).Return(types.ErrNoETCD).Once()
store.On("GetContainers", mock.Anything, mock.Anything).Return([]*types.Container{&types.Container{}}, nil).Once()
store.On("GetContainers", mock.Anything, mock.Anything).Return([]*types.Container{{}}, nil).Once()
err := c.withContainersLocked(ctx, []string{"c1", "c2"}, func(containers map[string]*types.Container) error { return nil })
assert.Error(t, err)
// success
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestWithContainerLocked(t *testing.T) {
lock.On("Unlock", mock.Anything).Return(nil)
// failed to get lock
lock.On("Lock", mock.Anything).Return(types.ErrNoETCD).Once()
store.On("GetContainers", mock.Anything, mock.Anything).Return([]*types.Container{&types.Container{}}, nil).Once()
store.On("GetContainers", mock.Anything, mock.Anything).Return([]*types.Container{{}}, nil).Once()
err := c.withContainerLocked(ctx, "c1", func(container *types.Container) error { return nil })
assert.Error(t, err)
// success
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestSetNode(t *testing.T) {
store.On("ListNodeContainers", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD).Once()
_, err = c.SetNode(ctx, &types.SetNodeOptions{Nodename: "test", Status: 0, ContainersDown: true})
assert.Error(t, err)
containers := []*types.Container{&types.Container{Name: "wrong_name"}, &types.Container{Name: "a_b_c"}}
containers := []*types.Container{{Name: "wrong_name"}, {Name: "a_b_c"}}
store.On("ListNodeContainers", mock.Anything, mock.Anything, mock.Anything).Return(containers, nil)
store.On("SetContainerStatus",
mock.Anything, mock.Anything, mock.Anything,
Expand Down
2 changes: 1 addition & 1 deletion cluster/calcium/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestSend(t *testing.T) {
opts := &types.SendOptions{
IDs: []string{"cid"},
Data: map[string][]byte{
"/tmp/1": []byte{},
"/tmp/1": {},
},
}
store := &storemocks.Store{}
Expand Down
6 changes: 3 additions & 3 deletions cluster/calcium/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestSetContainersStatus(t *testing.T) {

// failed
store.On("GetContainer", mock.Anything, mock.Anything).Return(nil, types.ErrBadCount).Once()
_, err := c.SetContainersStatus(ctx, []*types.StatusMeta{&types.StatusMeta{ID: "123"}}, nil)
_, err := c.SetContainersStatus(ctx, []*types.StatusMeta{{ID: "123"}}, nil)
assert.Error(t, err)
container := &types.Container{
ID: "123",
Expand All @@ -47,15 +47,15 @@ func TestSetContainersStatus(t *testing.T) {
mock.Anything,
mock.Anything,
).Return(types.ErrBadCount).Once()
_, err = c.SetContainersStatus(ctx, []*types.StatusMeta{&types.StatusMeta{ID: "123"}}, nil)
_, err = c.SetContainersStatus(ctx, []*types.StatusMeta{{ID: "123"}}, nil)
assert.Error(t, err)
// success
store.On("SetContainerStatus",
mock.Anything,
mock.Anything,
mock.Anything,
).Return(nil)
r, err := c.SetContainersStatus(ctx, []*types.StatusMeta{&types.StatusMeta{ID: "123"}}, nil)
r, err := c.SetContainersStatus(ctx, []*types.StatusMeta{{ID: "123"}}, nil)
assert.NoError(t, err)
assert.Len(t, r, 1)
}
Expand Down
14 changes: 11 additions & 3 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,18 @@ func serve() {

grpcServer := grpc.NewServer(opts...)
pb.RegisterCoreRPCServer(grpcServer, vibranium)
go grpcServer.Serve(s)
go func() {
if err := grpcServer.Serve(s); err != nil {
log.Fatalf("start grpc failed %v", err)
}
}()
if config.Profile != "" {
http.Handle("/metrics", metrics.Client.ResourceMiddleware(cluster)(promhttp.Handler()))
go http.ListenAndServe(config.Profile, nil)
go func() {
if err := http.ListenAndServe(config.Profile, nil); err != nil {
log.Errorf("start http failed %v", err)
}
}()
}

log.Info("[main] Cluster started successfully.")
Expand Down Expand Up @@ -137,5 +145,5 @@ func main() {
return nil
}

app.Run(os.Args)
_ = app.Run(os.Args)
}
4 changes: 3 additions & 1 deletion engine/docker/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ func (e *Engine) preparedSource(build *types.Build, scm coresource.Source, build
artifactsDir := buildDir
if cloneDir != "" {
os.RemoveAll(cloneDir)
os.MkdirAll(cloneDir, os.ModeDir)
if err := os.MkdirAll(cloneDir, os.ModeDir); err != nil {
return "", err
}
artifactsDir = cloneDir
}
for _, artifact := range build.Artifacts {
Expand Down
2 changes: 1 addition & 1 deletion engine/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (e *Engine) VirtualizationCreate(ctx context.Context, opts *enginetypes.Vir
resource := makeResourceSetting(opts.Quota, opts.Memory, opts.CPU, opts.NUMANode, opts.SoftLimit)
// set ulimits
resource.Ulimits = []*units.Ulimit{
&units.Ulimit{Name: "nofile", Soft: 65535, Hard: 65535},
{Name: "nofile", Soft: 65535, Hard: 65535},
}
if networkMode.IsHost() {
opts.DNS = []string{}
Expand Down
6 changes: 3 additions & 3 deletions engine/docker/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func mergeStream(stream io.ReadCloser) io.Reader {
go func() {
defer stream.Close()
_, err := stdcopy.StdCopy(outw, outw, stream)
outw.CloseWithError(err)
_ = outw.CloseWithError(err)
}()

return outr
Expand Down Expand Up @@ -110,7 +110,7 @@ func makeResourceSetting(cpu float64, memory int64, cpuMap map[string]int64, num
resource.CPUQuota = -1
}

if cpuMap != nil && len(cpuMap) > 0 {
if len(cpuMap) > 0 {
cpuIDs := []string{}
for cpuID := range cpuMap {
cpuIDs = append(cpuIDs, cpuID)
Expand Down Expand Up @@ -282,7 +282,7 @@ func parseDockerImageMessages(reader io.ReadCloser) chan *enginetypes.ImageMessa
ch := make(chan *enginetypes.ImageMessage)
go func() {
defer func() {
io.Copy(ioutil.Discard, reader)
_, _ = io.Copy(ioutil.Discard, reader)
reader.Close()
close(ch)
}()
Expand Down
4 changes: 2 additions & 2 deletions engine/systemd/virtualization.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (s *SSHClient) VirtualizationStop(ctx context.Context, ID string) (err erro
// VirtualizationRemove removes a systemd service
func (s *SSHClient) VirtualizationRemove(ctx context.Context, ID string, volumes, force bool) (err error) {
if force {
s.VirtualizationStop(ctx, ID)
_ = s.VirtualizationStop(ctx, ID)
}

// rm -f $FILE
Expand All @@ -98,7 +98,7 @@ func (s *SSHClient) VirtualizationRemove(ctx context.Context, ID string, volumes
}

// systemctl daemon-reload
_, stderr, err := s.runSingleCommand(ctx, fmt.Sprintf(cmdSystemdReload), nil)
_, stderr, err := s.runSingleCommand(ctx, cmdSystemdReload, nil)
return errors.Wrap(err, stderr.String())
}

Expand Down
4 changes: 2 additions & 2 deletions engine/virt/virt.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ type Virt struct {
func MakeClient(ctx context.Context, config coretypes.Config, nodename, endpoint, ca, cert, key string) (engine.API, error) {
var uri string
if strings.HasPrefix(endpoint, HTTPPrefixKey) {
uri = fmt.Sprintf("http://%s/%s", strings.TrimLeft(endpoint, HTTPPrefixKey), config.Virt.APIVersion)
uri = fmt.Sprintf("http://%s/%s", strings.TrimPrefix(endpoint, HTTPPrefixKey), config.Virt.APIVersion)
} else if strings.HasPrefix(endpoint, GRPCPrefixKey) {
uri = "grpc://" + strings.TrimLeft(endpoint, GRPCPrefixKey)
uri = "grpc://" + strings.TrimPrefix(endpoint, GRPCPrefixKey)
} else {
return nil, fmt.Errorf("invalid endpoint: %s", endpoint)
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ require (
github.com/jinzhu/configor v1.1.1
github.com/jonboulle/clockwork v0.1.0 // indirect
github.com/json-iterator/go v1.1.6 // indirect
github.com/libgit2/git2go v0.28.4 // indirect
github.com/libgit2/git2go/v30 v30.0.3
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/libgit2/git2go v0.28.4 h1:xfXnDnt31R2p7Yea7023C//NglM/1v+w87peBBwLZ3U=
github.com/libgit2/git2go v0.28.4/go.mod h1:4bKN42efkbNYMZlvDfxGDxzl066GhpvIircZDsm8Y+Y=
github.com/libgit2/git2go/v30 v30.0.3 h1:v+KRMhx85kHS0JzkPft7Wnt3+VUrYQrD/SQ77usTO/w=
github.com/libgit2/git2go/v30 v30.0.3/go.mod h1:YReiQ7xhMoyAL4ISYFLZt+OGqn6xtLqvTC1xJ9oAH7Y=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
Expand Down
14 changes: 8 additions & 6 deletions rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,13 @@ func (v *Vibranium) RunAndWait(stream pb.CoreRPC_RunAndWaitServer) error {
}
})
}
go runAndWait(func(ch <-chan *types.AttachContainerMessage) {
for m := range ch {
log.Infof("[Async RunAndWait] %v", string(m.Data))
}
})
go func() {
_ = runAndWait(func(ch <-chan *types.AttachContainerMessage) {
for m := range ch {
log.Infof("[Async RunAndWait] %v", string(m.Data))
}
})
}()
return nil
}

Expand Down Expand Up @@ -712,7 +714,7 @@ func (v *Vibranium) ExecuteContainer(stream pb.CoreRPC_ExecuteContainerServer) (
if err != nil {
return
}
executeContainerOpts := &types.ExecuteContainerOptions{}
var executeContainerOpts *types.ExecuteContainerOptions
if executeContainerOpts, err = toCoreExecuteContainerOptions(opts); err != nil {
return
}
Expand Down
2 changes: 0 additions & 2 deletions scheduler/complex/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ func min(a, b int) int {
return b
}

type cpuInfo = resourceInfo

func cpuPriorPlan(cpu float64, memory int64, nodesInfo []types.NodeInfo, maxShareCore, coreShare int) ([]types.NodeInfo, map[string][]types.CPUMap, int, error) {
var nodeContainer = map[string][]types.CPUMap{}
volTotal := 0
Expand Down
2 changes: 1 addition & 1 deletion scheduler/complex/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestCPUPriorPlan(t *testing.T) {

func resetNodesInfo() []types.NodeInfo {
return []types.NodeInfo{
types.NodeInfo{
{
Name: "n1",
CPUMap: types.CPUMap{"1": 100, "2": 100, "3": 100, "4": 100},
MemCap: 3 * int64(units.GiB),
Expand Down
1 change: 0 additions & 1 deletion scheduler/complex/potassium.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ func (m *Potassium) SelectMemoryNodes(nodesInfo []types.NodeInfo, quota float64,
if p == nodesInfoLength {
return nil, 0, types.ErrInsufficientMEM
}
nodesInfoLength -= p
nodesInfo = nodesInfo[p:]

// 这里 memCap 一定是大于 memory 的所以不用判断 cap 内容
Expand Down
6 changes: 1 addition & 5 deletions scheduler/complex/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ func (h *host) getComplexResult(full int, fragment int64, maxShare int) []types.
for i := 0; i < baseLine; i++ {
r := types.ResourceMap{}
for id, pieces := range fullResult[i] {
if _, ok := r[id]; ok {
r[id] += pieces
} else {
r[id] = pieces
}
r[id] += pieces
}
for id, pieces := range fragmentResult[i] {
r[id] = pieces
Expand Down
Loading

0 comments on commit 646f232

Please sign in to comment.