Skip to content

Commit

Permalink
apply lint suggestions, minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Jul 22, 2020
1 parent 5343446 commit 27761b4
Show file tree
Hide file tree
Showing 20 changed files with 305 additions and 140 deletions.
62 changes: 62 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,72 @@ run:
timeout: 5m
tests: false
skip-dirs:
- vendor
- tools
modules-download-mode: readonly

issues:
exclude-rules:
- linters:
- staticcheck
text: "SA1019:"

linters-settings:
maligned:
suggest-new: true
gocritic:
disabled-checks:
- captLocal

linters:
disable-all: true
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- gochecknoinits
- goconst
- gocyclo
- gofmt
- goimports
- golint
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- interfacer
- misspell
- nakedret
- nolintlint
- rowserrcheck
- scopelint
- staticcheck
- structcheck
- typecheck
- unconvert
- unparam
- unused
- varcheck
- asciicheck
- nestif
- errcheck
- gocritic
#- gocognit
#- gomnd
#- dupl
#Consider this
# - godox
# - funlen
# - lll
# - gochecknoglobals
# don't enable:
# - whitespace
# - goerr113
# - godot
# - maligned
# - prealloc
# - testpackage
# - wsl
# - stylecheck
5 changes: 4 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ builds:
- CGO_ENABLED=0
ldflags:
- -s -w
- -X github.com/projecteru2/agent/common.EruAgentVersion={{.Env.VERSION}}
- -X github.com/projecteru2/agent/versioninfo.REVISION={{.Commit}}
- -X github.com/projecteru2/agent/versioninfo.VERSION={{.Env.VERSION}}
- -X github.com/projecteru2/agent/versioninfo.BUILTAT={{.Date}}
goos:
- darwin
- linux
goarch:
- amd64
Expand Down
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
.PHONY: deps build test binary

REPO_PATH := github.com/projecteru2/agent
REVISION := $(shell git rev-parse HEAD || unknown)
BUILTAT := $(shell date +%Y-%m-%dT%H:%M:%S)
VERSION := $(shell git describe --tags $(shell git rev-list --tags --max-count=1))
GO_LDFLAGS ?= -s -w -X $(REPO_PATH)/common.EruAgentVersion=$(VERSION)
GO_LDFLAGS ?= -s -X $(REPO_PATH)/versioninfo.REVISION=$(REVISION) \
-X $(REPO_PATH)/versioninfo.BUILTAT=$(BUILTAT) \
-X $(REPO_PATH)/versioninfo.VERSION=$(VERSION)

deps:
env GO111MODULE=on go mod download
Expand All @@ -18,3 +22,6 @@ test: deps
sed -i.bak "143s/\*http.Transport/http.RoundTripper/" ./vendor/github.com/docker/docker/client/client.go
go vet `go list ./... | grep -v '/vendor/'`
go test -v `go list ./... | grep -v '/vendor/'`

lint:
golangci-lint run
11 changes: 8 additions & 3 deletions agent.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package main

import (
"fmt"
"os"

_ "go.uber.org/automaxprocs"

"github.com/jinzhu/configor"
"github.com/projecteru2/agent/api"
"github.com/projecteru2/agent/common"
"github.com/projecteru2/agent/engine"
"github.com/projecteru2/agent/types"
"github.com/projecteru2/agent/utils"
"github.com/projecteru2/agent/versioninfo"
"github.com/projecteru2/agent/watcher"
log "github.com/sirupsen/logrus"
cli "github.com/urfave/cli/v2"
Expand Down Expand Up @@ -64,10 +65,14 @@ func serve(c *cli.Context) error {
}

func main() {
cli.VersionPrinter = func(c *cli.Context) {
fmt.Print(versioninfo.VersionString())
}

app := &cli.App{
Name: "Eru-Agent",
Name: versioninfo.NAME,
Usage: "Run eru agent",
Version: common.EruAgentVersion,
Version: versioninfo.VERSION,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Expand Down
10 changes: 5 additions & 5 deletions api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"runtime/pprof"

// enable profile
_ "net/http/pprof"
_ "net/http/pprof" // nolint

"github.com/projecteru2/agent/common"
"github.com/projecteru2/agent/types"
"github.com/projecteru2/agent/versioninfo"
"github.com/projecteru2/agent/watcher"
coreutils "github.com/projecteru2/core/utils"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand All @@ -26,14 +26,14 @@ type Handler struct {
}

// URL /version/
func (h *Handler) version(w http.ResponseWriter, req *http.Request) {
func (h *Handler) version(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(JSON{"version": common.EruAgentVersion})
_ = json.NewEncoder(w).Encode(JSON{"version": versioninfo.VERSION})
}

// URL /profile/
func (h *Handler) profile(w http.ResponseWriter, req *http.Request) {
func (h *Handler) profile(w http.ResponseWriter, _ *http.Request) {
r := JSON{}
for _, p := range pprof.Profiles() {
r[p.Name()] = p.Count()
Expand Down
8 changes: 3 additions & 5 deletions common/common.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package common

// EruAgentVersion for eru agent version
var EruAgentVersion = "unknown"

const (
// DockerCliVersion for docker cli version
DockerCliVersion = "1.35"
Expand All @@ -11,12 +8,13 @@ const (
StatusDie = "die"
// StatusStart for start status
StatusStart = "start"
// StatusDestory for destory status
StatusDestory = "destroy"

// DateTimeFormat for datetime format
DateTimeFormat = "2006-01-02 15:04:05.999999"

// DOCKERIZED detect agent in docker
DOCKERIZED = "AGENT_IN_DOCKER"

// LocalIP .
LocalIP = "127.0.0.1"
)
7 changes: 7 additions & 0 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
corestore "github.com/projecteru2/agent/store/core"
"github.com/projecteru2/agent/types"
"github.com/projecteru2/agent/utils"
dockerengine "github.com/projecteru2/core/engine/docker"
coretypes "github.com/projecteru2/core/types"
coreutils "github.com/projecteru2/core/utils"
"github.com/shirou/gopsutil/cpu"
Expand All @@ -25,6 +26,7 @@ type Engine struct {
config *types.Config
docker *engineapi.Client
node *coretypes.Node
nodeIP string
cpuCore float64 // 因为到时候要乘以 float64 所以就直接转换成 float64 吧
memory int64

Expand Down Expand Up @@ -57,6 +59,11 @@ func NewEngine(config *types.Config) (*Engine, error) {
engine.store = store
engine.docker = docker
engine.node = node
engine.nodeIP = dockerengine.GetIP(node.Endpoint)
if engine.nodeIP == "" {
engine.nodeIP = common.LocalIP
}
log.Infof("[NewEngine] Host IP %s", engine.nodeIP)
engine.dockerized = os.Getenv(common.DOCKERIZED) != ""
if engine.dockerized {
os.Setenv("HOST_PROC", "/hostProc")
Expand Down
1 change: 1 addition & 0 deletions engine/health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func checkOneURL(url string, expectedCode int, timeout time.Duration) bool {
log.Warnf("[checkOneURL] Error when checking %s, %s", url, err.Error())
return false
}
defer resp.Body.Close()
if expectedCode == 0 {
return resp.StatusCode < 500 && resp.StatusCode >= 200
}
Expand Down
6 changes: 3 additions & 3 deletions engine/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
enginetypes "github.com/docker/docker/api/types"
enginecontainer "github.com/docker/docker/api/types/container"
enginefilters "github.com/docker/docker/api/types/filters"
"github.com/projecteru2/agent/common"
"github.com/projecteru2/agent/engine/status"
"github.com/projecteru2/agent/types"
"github.com/projecteru2/core/cluster"
engine "github.com/projecteru2/core/engine/docker"
coreutils "github.com/projecteru2/core/utils"
)

Expand Down Expand Up @@ -66,8 +66,8 @@ func (e *Engine) detectContainer(ID string) (*types.Container, error) {
for name, endpoint := range c.NetworkSettings.Networks {
networkmode := enginecontainer.NetworkMode(name)
if networkmode.IsHost() {
container.LocalIP = "127.0.0.1"
networks[name] = engine.GetIP(e.node.Endpoint)
container.LocalIP = common.LocalIP
networks[name] = e.nodeIP
} else {
container.LocalIP = endpoint.IPAddress
networks[name] = endpoint.IPAddress
Expand Down
1 change: 1 addition & 0 deletions engine/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package engine

import (
"context"

coreutils "github.com/projecteru2/core/utils"
log "github.com/sirupsen/logrus"
)
Expand Down
16 changes: 8 additions & 8 deletions engine/logs/enc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// Encoder .
type Encoder interface{
type Encoder interface {
Encode(*types.Log) error
Close() error
}
Expand All @@ -26,7 +26,7 @@ type StreamEncoder struct {
func NewStreamEncoder(wt io.WriteCloser) *StreamEncoder {
return &StreamEncoder{
Encoder: json.NewEncoder(wt),
wt: wt,
wt: wt,
}
}

Expand Down Expand Up @@ -59,12 +59,12 @@ func CreateJournalEncoder() (*JournalEncoder, error) {
func (c *JournalEncoder) Encode(logline *types.Log) error {
vars := map[string]string{
"SYSLOG_IDENTIFIER": logline.Name,
"ID": logline.ID,
"TYPE": logline.Type,
"ENTRY_POINT": logline.EntryPoint,
"IDENT": logline.Ident,
"DATE_TIME": logline.Datetime,
"EXTRA": fmt.Sprintf("%v", logline.Extra),
"ID": logline.ID,
"TYPE": logline.Type,
"ENTRY_POINT": logline.EntryPoint,
"IDENT": logline.Ident,
"DATE_TIME": logline.Datetime,
"EXTRA": fmt.Sprintf("%v", logline.Extra),
}

c.Lock()
Expand Down
51 changes: 24 additions & 27 deletions engine/logs/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,38 +92,35 @@ func (w *Writer) checkConn() error {
// normal
return nil
}
if !w.connecting {
// double check
if w.connecting {
return ErrConnecting
}
w.connecting = true
go func() {
log.Debugf("[writer] Begin trying to connect to %s", w.addr)
// retrying up to 4 times to prevent infinite loop
for i := 0; i < 4; i++ {
enc, err := w.createEncoder()
if err == nil {
w.Lock()
w.enc = enc
w.connecting = false
w.Unlock()
break
} else {
log.Warnf("[writer] Failed to connect to %s: %s", w.addr, err)
time.Sleep(30 * time.Second)
}
}
if w.enc == nil {
log.Warnf("[writer] Connect to %s failed for 4 times", w.addr)
if w.connecting {
return ErrConnecting
}
w.connecting = true
go func() {
log.Debugf("[writer] Begin trying to connect to %s", w.addr)
// retrying up to 4 times to prevent infinite loop
for i := 0; i < 4; i++ {
enc, err := w.createEncoder()
if err == nil {
w.Lock()
w.enc = enc
w.connecting = false
w.Unlock()
break
} else {
log.Debugf("[writer] Connect to %s successfully", w.addr)
log.Warnf("[writer] Failed to connect to %s: %s", w.addr, err)
time.Sleep(30 * time.Second)
}
}()
}
}
if w.enc == nil {
log.Warnf("[writer] Connect to %s failed for 4 times", w.addr)
w.Lock()
w.connecting = false
w.Unlock()
} else {
log.Debugf("[writer] Connect to %s successfully", w.addr)
}
}()
return ErrConnecting
}

Expand Down
Loading

0 comments on commit 27761b4

Please sign in to comment.