diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 0000000..474b2fc --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,40 @@ +name: libagent + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: + - ubuntu-18.04 + - ubuntu-20.04 + - ubuntu-22.04 + - windows-2019 + - windows-2022 + - macos-10.15 + - macos-11 + - macos-12 + go: + - '1.17' + - '1.18' + - '1.19' + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: ${{ matrix.go }} + + - name: Lint + run: | + go install golang.org/x/lint/golint@latest + golint -set_exit_status ./... + go install github.com/gordonklaus/ineffassign@latest + ineffassign ./... \ No newline at end of file diff --git a/README.md b/README.md index 6e30f70..d8c5eb0 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ # libagent + agent封装类库,用于快速开发agent + +## 开发方式 + +待补充 \ No newline at end of file diff --git a/conf/rewrite.go b/conf/rewrite.go index f589d86..e9c316b 100644 --- a/conf/rewrite.go +++ b/conf/rewrite.go @@ -12,6 +12,8 @@ import ( runtime "github.com/jkstack/jkframe/utils" ) +// RewriteServer rewrite server configure +// - use $... to set value by envionment variables func (cfg *Configure) RewriteServer() { cfg.Server = strings.TrimSpace(cfg.Server) if len(cfg.Server) == 0 { @@ -28,6 +30,10 @@ func (cfg *Configure) RewriteServer() { } } +// RewriteID rewrite agent id configure +// - use $IP to set value by ip address in interface to connect server +// - use $HOSTNAME to set value by hostname +// - use $... to set value by envionment variables func (cfg *Configure) RewriteID() { cfg.RewriteServer() cfg.ID = strings.TrimSpace(cfg.ID) @@ -58,6 +64,7 @@ func (cfg *Configure) RewriteID() { logging.Info("now agent id is %s", cfg.ID) } +// SetAgentID reset agent id by value func (cfg *Configure) SetAgentID(id string) { cfg.ID = id } diff --git a/limit/cgroups_test.go b/limit/cgroups_test.go index 40bd06d..f1beef8 100644 --- a/limit/cgroups_test.go +++ b/limit/cgroups_test.go @@ -8,7 +8,7 @@ import ( func TestCGroups(t *testing.T) { var cfg Configure - cfg.CpuQuota = 100 // 1core + cfg.CPUQuota = 100 // 1core cfg.Memory = utils.Bytes(100 * 1024 * 1024) // 100MB cfg.Disks = append(cfg.Disks, DiskLimit{ Dev: "8:0", diff --git a/limit/cgroups_v1.go b/limit/cgroups_v1.go index 26a0f05..e607c16 100644 --- a/limit/cgroups_v1.go +++ b/limit/cgroups_v1.go @@ -25,7 +25,7 @@ func (cfg *Configure) doV1(agentName string) { logging.Warning("can not create cgroup %s: %v", dir, err) return } - limitCPUV1(group, cfg.CpuQuota) + limitCPUV1(group, cfg.CPUQuota) limitMemoryV1(group, int64(cfg.Memory)) limitDiskV1(group, cfg.Disks) err = group.Add(cgroups.Process{ @@ -38,7 +38,7 @@ func (cfg *Configure) doV1(agentName string) { } func wantCGroup(cfg *Configure) bool { - if cfg.CpuQuota > 0 { + if cfg.CPUQuota > 0 { return true } if cfg.Memory > 0 { diff --git a/limit/cgroups_v2.go b/limit/cgroups_v2.go index c2a9aa6..131d49f 100644 --- a/limit/cgroups_v2.go +++ b/limit/cgroups_v2.go @@ -18,7 +18,7 @@ func (cfg *Configure) doV2(agentName string) { logging.Warning("can not create cgroup: %v", err) return } - limitCPUV2(group, cfg.CpuQuota) + limitCPUV2(group, cfg.CPUQuota) limitMemoryV2(group, int64(cfg.Memory)) limitDiskV2(group, cfg.Disks) err = group.AddProc(uint64(os.Getpid())) diff --git a/limit/configure.go b/limit/configure.go index bd8d23a..945c332 100644 --- a/limit/configure.go +++ b/limit/configure.go @@ -7,8 +7,9 @@ import ( "github.com/jkstack/agent/utils" ) +// DiskLimit disk limit configure type DiskLimit struct { - // device version of lsblk command eg: 8:0 + // device number of lsblk command eg: 8:0 Dev string `json:"dev" yaml:"dev" kv:"dev"` // read bytes ReadBytes uint64 `json:"read_bytes" yaml:"read_bytes" kv:"read_bytes"` @@ -23,7 +24,7 @@ type DiskLimit struct { // Configure limit configure type Configure struct { // cpu usage 100 means 1 core - CpuQuota int64 `json:"cpu_quota" yaml:"cpu_quota" kv:"cpu_quota"` + CPUQuota int64 `json:"cpu_quota" yaml:"cpu_quota" kv:"cpu_quota"` // memory size limit in bytes Memory utils.Bytes `json:"memory_limit" yaml:"memory_limit" kv:"memory_limit"` // limit of disk diff --git a/utils/bytes.go b/utils/bytes.go index 3254e96..7772270 100644 --- a/utils/bytes.go +++ b/utils/bytes.go @@ -7,7 +7,7 @@ import ( "gopkg.in/yaml.v3" ) -// Bytes yaml bytes +// Bytes custom bytes struct type Bytes uint64 // MarshalJSON marshal bytes by json diff --git a/utils/duration.go b/utils/duration.go index 01dede6..df7f684 100644 --- a/utils/duration.go +++ b/utils/duration.go @@ -4,6 +4,7 @@ import ( "time" ) +// Duration custom duration struct type Duration time.Duration // MarshalKV marshal duration diff --git a/utils/recover.go b/utils/recover.go index 96f8cf1..f4aa17d 100644 --- a/utils/recover.go +++ b/utils/recover.go @@ -2,6 +2,7 @@ package utils import "github.com/jkstack/jkframe/logging" +// Recover error recover func Recover(key string) { if err := recover(); err != nil { logging.Error("%s: %v", key, err)