Compare commits
8 Commits
45ebc96c13
...
update-and
| Author | SHA1 | Date | |
|---|---|---|---|
|
2d1aa62c61
|
|||
|
e779c5a38b
|
|||
|
a467baf847
|
|||
|
ae783cf9d6
|
|||
|
9858f89140
|
|||
|
69d6147582
|
|||
| c392bfe7eb | |||
|
31da09ef6f
|
@@ -2,15 +2,18 @@ when:
|
|||||||
- event: push
|
- event: push
|
||||||
branch: main
|
branch: main
|
||||||
|
|
||||||
|
labels:
|
||||||
|
uploadfilter24.eu/instance-role: Woodpecker
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
test:
|
test:
|
||||||
image: golang:1.21
|
image: golang:1.25
|
||||||
commands:
|
commands:
|
||||||
- go test ./...
|
- go test ./...
|
||||||
pre-release:
|
pre-release:
|
||||||
image: woodpeckerci/plugin-docker-buildx
|
image: woodpeckerci/plugin-docker-buildx
|
||||||
settings:
|
settings:
|
||||||
platforms: linux/arm64/v8
|
platforms: linux/arm64/v8,linux/amd64
|
||||||
repo: lerentis/woodpecker-autoscaler
|
repo: lerentis/woodpecker-autoscaler
|
||||||
tags:
|
tags:
|
||||||
- latest
|
- latest
|
||||||
|
|||||||
@@ -1,15 +1,18 @@
|
|||||||
when:
|
when:
|
||||||
- event: pull_request
|
- event: pull_request
|
||||||
|
|
||||||
|
labels:
|
||||||
|
uploadfilter24.eu/instance-role: Woodpecker
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
test:
|
test:
|
||||||
image: golang:1.21
|
image: golang:1.25
|
||||||
commands:
|
commands:
|
||||||
- go test ./...
|
- go test ./...
|
||||||
pr-build:
|
pr-build:
|
||||||
image: woodpeckerci/plugin-docker-buildx
|
image: woodpeckerci/plugin-docker-buildx
|
||||||
settings:
|
settings:
|
||||||
platforms: linux/arm64/v8
|
platforms: linux/arm64/v8,linux/amd64
|
||||||
repo: lerentis/woodpecker-autoscaler
|
repo: lerentis/woodpecker-autoscaler
|
||||||
tags:
|
tags:
|
||||||
- latest
|
- latest
|
||||||
|
|||||||
@@ -1,15 +1,18 @@
|
|||||||
when:
|
when:
|
||||||
- event: tag
|
- event: tag
|
||||||
|
|
||||||
|
labels:
|
||||||
|
uploadfilter24.eu/instance-role: Woodpecker
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
test:
|
test:
|
||||||
image: golang:1.21
|
image: golang:1.25
|
||||||
commands:
|
commands:
|
||||||
- go test ./...
|
- go test ./...
|
||||||
release:
|
release:
|
||||||
image: woodpeckerci/plugin-docker-buildx
|
image: woodpeckerci/plugin-docker-buildx
|
||||||
settings:
|
settings:
|
||||||
platforms: linux/arm64/v8
|
platforms: linux/arm64/v8,linux/amd64
|
||||||
repo: lerentis/woodpecker-autoscaler
|
repo: lerentis/woodpecker-autoscaler
|
||||||
tags:
|
tags:
|
||||||
- latest
|
- latest
|
||||||
|
|||||||
17
CHANGELOG
17
CHANGELOG
@@ -1,4 +1,17 @@
|
|||||||
CHANGELOG
|
CHANGELOG
|
||||||
|
|
||||||
|
v1.1.1
|
||||||
|
- Updated Dependencies
|
||||||
|
- Added: unit tests
|
||||||
|
- Added `internal/hetzner/hetzneragent_extra_test.go` covering userdata generation and mocked runtime checks.
|
||||||
|
- Added `internal/woodpecker/agent_test.go` covering `CreateWoodpeckerAgent`, `GetAgentIdByName`, `DecomAgent` (httptest-based).
|
||||||
|
- Added `internal/woodpecker/metrics_test.go` covering `QueueInfo`, `CheckPending`, and `CheckRunning`.
|
||||||
|
- Implemented `internal/logging/logging_test.go` assertions for `ConfigureLogger` levels.
|
||||||
|
- Changed: code to improve testability
|
||||||
|
- Introduced `refreshNodeInfo` indirection in `internal/hetzner/hetzneragent.go` to allow mocking in tests.
|
||||||
|
- Updated `hetzner` userdata test expectations and fixed JSON encoding in tests.
|
||||||
|
- Notes:
|
||||||
|
- No functional behavior changes except testability refactor (indirection).
|
||||||
|
|
||||||
v1.1.0
|
v1.1.0
|
||||||
|
|
||||||
@@ -16,4 +29,4 @@ First stable release
|
|||||||
|
|
||||||
v0.0.1
|
v0.0.1
|
||||||
|
|
||||||
First test release
|
First test release
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
FROM golang:1.21 as build
|
FROM golang:1.25 AS build
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
|
|||||||
30
go.mod
30
go.mod
@@ -1,28 +1,28 @@
|
|||||||
module git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler
|
module git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler
|
||||||
|
|
||||||
go 1.21.1
|
go 1.25.0
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/gorilla/mux v1.8.1
|
github.com/gorilla/mux v1.8.1
|
||||||
github.com/hetznercloud/hcloud-go v1.53.0
|
github.com/hetznercloud/hcloud-go v1.59.2
|
||||||
github.com/jinzhu/configor v1.2.2
|
github.com/jinzhu/configor v1.2.2
|
||||||
github.com/sirupsen/logrus v1.9.3
|
github.com/sirupsen/logrus v1.9.3
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/BurntSushi/toml v1.3.2 // indirect
|
github.com/BurntSushi/toml v1.6.0 // indirect
|
||||||
github.com/beorn7/perks v1.0.1 // indirect
|
github.com/beorn7/perks v1.0.1 // indirect
|
||||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||||
github.com/kr/text v0.2.0 // indirect
|
github.com/klauspost/compress v1.18.2 // indirect
|
||||||
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||||
github.com/prometheus/client_golang v1.17.0 // indirect
|
github.com/prometheus/client_golang v1.23.2 // indirect
|
||||||
github.com/prometheus/client_model v0.5.0 // indirect
|
github.com/prometheus/client_model v0.6.2 // indirect
|
||||||
github.com/prometheus/common v0.45.0 // indirect
|
github.com/prometheus/common v0.67.4 // indirect
|
||||||
github.com/prometheus/procfs v0.12.0 // indirect
|
github.com/prometheus/procfs v0.19.2 // indirect
|
||||||
github.com/rogpeppe/go-internal v1.12.0 // indirect
|
go.yaml.in/yaml/v2 v2.4.3 // indirect
|
||||||
golang.org/x/net v0.19.0 // indirect
|
golang.org/x/net v0.48.0 // indirect
|
||||||
golang.org/x/sys v0.15.0 // indirect
|
golang.org/x/sys v0.39.0 // indirect
|
||||||
golang.org/x/text v0.14.0 // indirect
|
golang.org/x/text v0.32.0 // indirect
|
||||||
google.golang.org/protobuf v1.31.0 // indirect
|
google.golang.org/protobuf v1.36.11 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
69
go.sum
69
go.sum
@@ -1,59 +1,62 @@
|
|||||||
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
|
||||||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||||
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
|
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||||
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
|
||||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
|
||||||
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
|
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
|
||||||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
|
||||||
github.com/hetznercloud/hcloud-go v1.53.0 h1:xThhlJc6MdpvDAqVB7bAw+nAQuCpQMwsf3yanCis4rM=
|
github.com/hetznercloud/hcloud-go v1.59.2 h1:NkCPwYiPv85FnOV3IW9/gxfW61TPIUSwyPHRSLwCkHA=
|
||||||
github.com/hetznercloud/hcloud-go v1.53.0/go.mod h1:VzDWThl47lOnZXY0q5/LPFD+M62pfe/52TV+mOrpp9Q=
|
github.com/hetznercloud/hcloud-go v1.59.2/go.mod h1:oTebZCjd+osj75jlI76Z+zjN1sTxmMiQ1MWoO8aRl1c=
|
||||||
github.com/jinzhu/configor v1.2.2 h1:sLgh6KMzpCmaQB4e+9Fu/29VErtBUqsS2t8C9BNIVsA=
|
github.com/jinzhu/configor v1.2.2 h1:sLgh6KMzpCmaQB4e+9Fu/29VErtBUqsS2t8C9BNIVsA=
|
||||||
github.com/jinzhu/configor v1.2.2/go.mod h1:iFFSfOBKP3kC2Dku0ZGB3t3aulfQgTGJknodhFavsU8=
|
github.com/jinzhu/configor v1.2.2/go.mod h1:iFFSfOBKP3kC2Dku0ZGB3t3aulfQgTGJknodhFavsU8=
|
||||||
|
github.com/klauspost/compress v1.18.2 h1:iiPHWW0YrcFgpBYhsA6D1+fqHssJscY/Tm/y2Uqnapk=
|
||||||
|
github.com/klauspost/compress v1.18.2/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=
|
||||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg=
|
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||||
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k=
|
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||||
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
|
||||||
|
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q=
|
github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o=
|
||||||
github.com/prometheus/client_golang v1.17.0/go.mod h1:VeL+gMmOAxkS2IqfCq0ZmHSL+LjWfWDUmp1mBz9JgUY=
|
github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg=
|
||||||
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
|
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
|
||||||
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
|
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
|
||||||
github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM=
|
github.com/prometheus/common v0.67.4 h1:yR3NqWO1/UyO1w2PhUvXlGQs/PtFmoveVO0KZ4+Lvsc=
|
||||||
github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY=
|
github.com/prometheus/common v0.67.4/go.mod h1:gP0fq6YjjNCLssJCQp0yk4M8W6ikLURwkdd/YKtTbyI=
|
||||||
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
|
github.com/prometheus/procfs v0.19.2 h1:zUMhqEW66Ex7OXIiDkll3tl9a1ZdilUOd/F6ZXw4Vws=
|
||||||
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
|
github.com/prometheus/procfs v0.19.2/go.mod h1:M0aotyiemPhBCM0z5w87kL22CxfcH05ZpYlu+b4J7mw=
|
||||||
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
|
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
|
||||||
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
|
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
|
||||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||||
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
|
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||||
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
|
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||||
|
go.yaml.in/yaml/v2 v2.4.3 h1:6gvOSjQoTB3vt1l+CU+tSyi/HOjfOjRLJ4YwYZGwRO0=
|
||||||
|
go.yaml.in/yaml/v2 v2.4.3/go.mod h1:zSxWcmIDjOzPXpjlTTbAsKokqkDNAVtZO0WOMiT90s8=
|
||||||
|
golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU=
|
||||||
|
golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY=
|
||||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
|
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
|
||||||
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
|
||||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY=
|
||||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
|
||||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||||
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
|
|
||||||
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -36,7 +35,7 @@ func GenConfig() (cfg *Config, err error) {
|
|||||||
Silent: true,
|
Silent: true,
|
||||||
AutoReloadInterval: time.Minute}).Load(cfg, "config.json")
|
AutoReloadInterval: time.Minute}).Load(cfg, "config.json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New(fmt.Sprintf("Error generating Config: %s", err.Error()))
|
return nil, fmt.Errorf("Error generating Config: %s", err.Error())
|
||||||
}
|
}
|
||||||
return cfg, nil
|
return cfg, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package hetzner
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -40,6 +39,8 @@ runcmd:
|
|||||||
- [ sh, -xc, "cd /root; docker run --rm --privileged multiarch/qemu-user-static --reset -p yes; docker compose up -d" ]
|
- [ sh, -xc, "cd /root; docker run --rm --privileged multiarch/qemu-user-static --reset -p yes; docker compose up -d" ]
|
||||||
`
|
`
|
||||||
|
|
||||||
|
var refreshNodeInfo = RefreshNodeInfo
|
||||||
|
|
||||||
type UserDataConfig struct {
|
type UserDataConfig struct {
|
||||||
Image string
|
Image string
|
||||||
EnvConfig map[string]interface{}
|
EnvConfig map[string]interface{}
|
||||||
@@ -60,12 +61,12 @@ func generateConfig(cfg *config.Config, name string, agentToken string) (string,
|
|||||||
}
|
}
|
||||||
tmpl, err := template.New("userdata").Parse(USER_DATA_TEMPLATE)
|
tmpl, err := template.New("userdata").Parse(USER_DATA_TEMPLATE)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.New(fmt.Sprintf("Errors in userdata template: %s", err.Error()))
|
return "", fmt.Errorf("Errors in userdata template: %s", err.Error())
|
||||||
}
|
}
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err = tmpl.Execute(&buf, &config)
|
err = tmpl.Execute(&buf, &config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.New(fmt.Sprintf("Could not render userdata template: %s", err.Error()))
|
return "", fmt.Errorf("Could not render userdata template: %s", err.Error())
|
||||||
}
|
}
|
||||||
return buf.String(), nil
|
return buf.String(), nil
|
||||||
}
|
}
|
||||||
@@ -113,7 +114,7 @@ func CreateNewAgent(cfg *config.Config, woodpeckerAgent *models.Agent) (*hcloud.
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New(fmt.Sprintf("Could not create new Agent: %s", err.Error()))
|
return nil, fmt.Errorf("Could not create new Agent: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
@@ -127,7 +128,7 @@ func ListAgents(cfg *config.Config) ([]hcloud.Server, error) {
|
|||||||
client := hcloud.NewClient(hcloud.WithToken(cfg.HcloudToken))
|
client := hcloud.NewClient(hcloud.WithToken(cfg.HcloudToken))
|
||||||
allServers, err := client.Server.All(context.Background())
|
allServers, err := client.Server.All(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New(fmt.Sprintf("Could not query Server list: %s", err.Error()))
|
return nil, fmt.Errorf("Could not query Server list: %s", err.Error())
|
||||||
}
|
}
|
||||||
myServers := []hcloud.Server{}
|
myServers := []hcloud.Server{}
|
||||||
for _, server := range allServers {
|
for _, server := range allServers {
|
||||||
@@ -161,7 +162,7 @@ func DecomNode(cfg *config.Config, server *hcloud.Server) (int64, error) {
|
|||||||
}).Debugf("Deleting %s node", server.Name)
|
}).Debugf("Deleting %s node", server.Name)
|
||||||
_, _, err := client.Server.DeleteWithResult(context.Background(), server)
|
_, _, err := client.Server.DeleteWithResult(context.Background(), server)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return woodpeckerAgentID, errors.New(fmt.Sprintf("Could not delete Agent: %s", err.Error()))
|
return woodpeckerAgentID, fmt.Errorf("Could not delete Agent: %s", err.Error())
|
||||||
}
|
}
|
||||||
return woodpeckerAgentID, nil
|
return woodpeckerAgentID, nil
|
||||||
}
|
}
|
||||||
@@ -170,16 +171,16 @@ func RefreshNodeInfo(cfg *config.Config, serverID int) (*hcloud.Server, error) {
|
|||||||
client := hcloud.NewClient(hcloud.WithToken(cfg.HcloudToken))
|
client := hcloud.NewClient(hcloud.WithToken(cfg.HcloudToken))
|
||||||
server, _, err := client.Server.GetByID(context.Background(), serverID)
|
server, _, err := client.Server.GetByID(context.Background(), serverID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New(fmt.Sprintf("Could not refresh server info: %s", err.Error()))
|
return nil, fmt.Errorf("Could not refresh server info: %s", err.Error())
|
||||||
}
|
}
|
||||||
return server, nil
|
return server, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckRuntime(cfg *config.Config, server *hcloud.Server) (time.Time, error) {
|
func CheckRuntime(cfg *config.Config, server *hcloud.Server) (time.Time, error) {
|
||||||
server, err := RefreshNodeInfo(cfg, server.ID)
|
server, err := refreshNodeInfo(cfg, server.ID)
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return time.Time{}, errors.New(fmt.Sprintf("Could not check Runtime: %s", err.Error()))
|
return time.Time{}, fmt.Errorf("Could not check Runtime: %s", err.Error())
|
||||||
}
|
}
|
||||||
return server.Created.Add(time.Duration(now.Minute())), nil
|
return server.Created.Add(time.Duration(now.Minute())), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
package hetzner
|
package hetzner
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config"
|
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config"
|
||||||
|
"github.com/hetznercloud/hcloud-go/hcloud"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGenerateUserData(t *testing.T) {
|
func TestGenerateUserData(t *testing.T) {
|
||||||
@@ -54,3 +57,78 @@ runcmd:
|
|||||||
t.Errorf("got:\n%v\n, wanted:\n%v", got, wanted)
|
t.Errorf("got:\n%v\n, wanted:\n%v", got, wanted)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGenerateUserData_MultipleCases(t *testing.T) {
|
||||||
|
base := config.Config{
|
||||||
|
WoodpeckerGrpc: "grpc-test.woodpecker.test.tld:443",
|
||||||
|
WoodpeckerLabelSelector: "uploadfilter24.eu/instance-role=WoodpeckerTest",
|
||||||
|
WoodpeckerAgentVersion: "latest",
|
||||||
|
}
|
||||||
|
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
cfg config.Config
|
||||||
|
agentName string
|
||||||
|
agentToken string
|
||||||
|
wantContains []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "basic",
|
||||||
|
cfg: base,
|
||||||
|
agentName: "test-instance",
|
||||||
|
agentToken: "Geheim1!",
|
||||||
|
wantContains: []string{
|
||||||
|
"image: woodpeckerci/woodpecker-agent:latest",
|
||||||
|
"- WOODPECKER_AGENT_SECRET=Geheim1!",
|
||||||
|
"- WOODPECKER_FILTER_LABELS=uploadfilter24.eu/instance-role=WoodpeckerTest",
|
||||||
|
"- WOODPECKER_SERVER=grpc-test.woodpecker.test.tld:443",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "empty token",
|
||||||
|
cfg: base,
|
||||||
|
agentName: "no-token",
|
||||||
|
agentToken: "",
|
||||||
|
wantContains: []string{
|
||||||
|
"image: woodpeckerci/woodpecker-agent:latest",
|
||||||
|
"- WOODPECKER_AGENT_SECRET=",
|
||||||
|
"- WOODPECKER_HOSTNAME=no-token",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
got, err := generateConfig(&tc.cfg, tc.agentName, tc.agentToken)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s: generateConfig returned error: %v", tc.name, err)
|
||||||
|
}
|
||||||
|
for _, want := range tc.wantContains {
|
||||||
|
if !strings.Contains(got, want) {
|
||||||
|
t.Errorf("%s: expected generated userdata to contain %q, got:\n%s", tc.name, want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCheckRuntime_MockedRefresh(t *testing.T) {
|
||||||
|
// Mock refreshNodeInfo to return a server with a known Created time
|
||||||
|
orig := refreshNodeInfo
|
||||||
|
defer func() { refreshNodeInfo = orig }()
|
||||||
|
|
||||||
|
created := time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC)
|
||||||
|
refreshNodeInfo = func(cfg *config.Config, serverID int) (*hcloud.Server, error) {
|
||||||
|
return &hcloud.Server{Created: created}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg := config.Config{}
|
||||||
|
// Capture minute before call to avoid flakiness across minute boundary
|
||||||
|
minute := time.Now().Minute()
|
||||||
|
got, err := CheckRuntime(&cfg, &hcloud.Server{ID: 123})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CheckRuntime returned error: %v", err)
|
||||||
|
}
|
||||||
|
want := created.Add(time.Duration(minute))
|
||||||
|
if !got.Equal(want) {
|
||||||
|
t.Fatalf("unexpected runtime: got %v, want %v", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
40
internal/logging/logging_test.go
Normal file
40
internal/logging/logging_test.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package logging
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLoggingDebug(t *testing.T) {
|
||||||
|
cfg := config.Config{LogLevel: "Debug"}
|
||||||
|
ConfigureLogger(&cfg)
|
||||||
|
if log.GetLevel() != log.DebugLevel {
|
||||||
|
t.Fatalf("expected DebugLevel, got %v", log.GetLevel())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoggingInfo(t *testing.T) {
|
||||||
|
cfg := config.Config{LogLevel: "Info"}
|
||||||
|
ConfigureLogger(&cfg)
|
||||||
|
if log.GetLevel() != log.InfoLevel {
|
||||||
|
t.Fatalf("expected InfoLevel, got %v", log.GetLevel())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoggingWarning(t *testing.T) {
|
||||||
|
cfg := config.Config{LogLevel: "Warn"}
|
||||||
|
ConfigureLogger(&cfg)
|
||||||
|
if log.GetLevel() != log.WarnLevel {
|
||||||
|
t.Fatalf("expected WarnLevel, got %v", log.GetLevel())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoggingError(t *testing.T) {
|
||||||
|
cfg := config.Config{LogLevel: "Error"}
|
||||||
|
ConfigureLogger(&cfg)
|
||||||
|
if log.GetLevel() != log.ErrorLevel {
|
||||||
|
t.Fatalf("expected ErrorLevel, got %v", log.GetLevel())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,6 @@ package woodpecker
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -18,7 +17,7 @@ func DecomAgent(cfg *config.Config, agentId int64) error {
|
|||||||
apiRoute := fmt.Sprintf("%s/api/agents/%d", cfg.WoodpeckerInstance, agentId)
|
apiRoute := fmt.Sprintf("%s/api/agents/%d", cfg.WoodpeckerInstance, agentId)
|
||||||
req, err := http.NewRequest("DELETE", apiRoute, nil)
|
req, err := http.NewRequest("DELETE", apiRoute, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New(fmt.Sprintf("Could not create delete request: %s", err.Error()))
|
return fmt.Errorf("Could not create delete request: %s", err.Error())
|
||||||
}
|
}
|
||||||
req.Header.Set("Accept", "text/plain")
|
req.Header.Set("Accept", "text/plain")
|
||||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
|
||||||
@@ -29,7 +28,7 @@ func DecomAgent(cfg *config.Config, agentId int64) error {
|
|||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New(fmt.Sprintf("Could not delete agent: %s", err.Error()))
|
return fmt.Errorf("Could not delete agent: %s", err.Error())
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
return nil
|
return nil
|
||||||
@@ -39,24 +38,24 @@ func GetAgentIdByName(cfg *config.Config, name string) (int, error) {
|
|||||||
apiRoute := fmt.Sprintf("%s/api/agents?page=1&perPage=100", cfg.WoodpeckerInstance)
|
apiRoute := fmt.Sprintf("%s/api/agents?page=1&perPage=100", cfg.WoodpeckerInstance)
|
||||||
req, err := http.NewRequest(http.MethodGet, apiRoute, nil)
|
req, err := http.NewRequest(http.MethodGet, apiRoute, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.New(fmt.Sprintf("Could not create agent query request: %s", err.Error()))
|
return 0, fmt.Errorf("Could not create agent query request: %s", err.Error())
|
||||||
}
|
}
|
||||||
req.Header.Set("Accept", "application/json")
|
req.Header.Set("Accept", "application/json")
|
||||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.New(fmt.Sprintf("Could not query agent list: %s", err.Error()))
|
return 0, fmt.Errorf("Could not query agent list: %s", err.Error())
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return 0, errors.New(fmt.Sprintf("Invalid status code from API: %d", resp.StatusCode))
|
return 0, fmt.Errorf("Invalid status code from API: %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
agentList := new(models.AgentList)
|
agentList := new(models.AgentList)
|
||||||
err = json.NewDecoder(resp.Body).Decode(agentList)
|
err = json.NewDecoder(resp.Body).Decode(agentList)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.New(fmt.Sprintf("Could not unmarshal api response: %s", err.Error()))
|
return 0, fmt.Errorf("Could not unmarshal api response: %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, agent := range agentList.Agents {
|
for _, agent := range agentList.Agents {
|
||||||
@@ -67,7 +66,7 @@ func GetAgentIdByName(cfg *config.Config, name string) (int, error) {
|
|||||||
return int(agent.ID), nil
|
return int(agent.ID), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0, errors.New(fmt.Sprintf("Agent with name %s is not in server", name))
|
return 0, fmt.Errorf("Agent with name %s is not in server", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ListAgents(cfg *config.Config) (*models.AgentList, error) {
|
func ListAgents(cfg *config.Config) (*models.AgentList, error) {
|
||||||
@@ -75,23 +74,23 @@ func ListAgents(cfg *config.Config) (*models.AgentList, error) {
|
|||||||
apiRoute := fmt.Sprintf("%s/api/agents?page=1&perPage=100", cfg.WoodpeckerInstance)
|
apiRoute := fmt.Sprintf("%s/api/agents?page=1&perPage=100", cfg.WoodpeckerInstance)
|
||||||
req, err := http.NewRequest(http.MethodGet, apiRoute, nil)
|
req, err := http.NewRequest(http.MethodGet, apiRoute, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return agentList, errors.New(fmt.Sprintf("Could not create agent query request: %s", err.Error()))
|
return agentList, fmt.Errorf("Could not create agent query request: %s", err.Error())
|
||||||
}
|
}
|
||||||
req.Header.Set("Accept", "application/json")
|
req.Header.Set("Accept", "application/json")
|
||||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return agentList, errors.New(fmt.Sprintf("Could not query agent list: %s", err.Error()))
|
return agentList, fmt.Errorf("Could not query agent list: %s", err.Error())
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return agentList, errors.New(fmt.Sprintf("Invalid status code from API: %d", resp.StatusCode))
|
return agentList, fmt.Errorf("Invalid status code from API: %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
err = json.NewDecoder(resp.Body).Decode(agentList)
|
err = json.NewDecoder(resp.Body).Decode(agentList)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return agentList, errors.New(fmt.Sprintf("Could not unmarshal api response: %s", err.Error()))
|
return agentList, fmt.Errorf("Could not unmarshal api response: %s", err.Error())
|
||||||
}
|
}
|
||||||
return agentList, nil
|
return agentList, nil
|
||||||
}
|
}
|
||||||
@@ -111,24 +110,24 @@ func CreateWoodpeckerAgent(cfg *config.Config) (*models.Agent, error) {
|
|||||||
}).Debugf("Sending the following data to %s: %s", apiRoute, jsonBody)
|
}).Debugf("Sending the following data to %s: %s", apiRoute, jsonBody)
|
||||||
req, err := http.NewRequest(http.MethodPost, apiRoute, bodyReader)
|
req, err := http.NewRequest(http.MethodPost, apiRoute, bodyReader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New(fmt.Sprintf("Could not create agent request: %s", err.Error()))
|
return nil, fmt.Errorf("Could not create agent request: %s", err.Error())
|
||||||
}
|
}
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New(fmt.Sprintf("Could not create new Agent: %s", err.Error()))
|
return nil, fmt.Errorf("Could not create new Agent: %s", err.Error())
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return nil, errors.New(fmt.Sprintf("Invalid status code from API: %d", resp.StatusCode))
|
return nil, fmt.Errorf("Invalid status code from API: %d", resp.StatusCode)
|
||||||
}
|
}
|
||||||
newAgent := new(models.Agent)
|
newAgent := new(models.Agent)
|
||||||
err = json.NewDecoder(resp.Body).Decode(newAgent)
|
err = json.NewDecoder(resp.Body).Decode(newAgent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.New(fmt.Sprintf("Could not unmarshal api response: %s", err.Error()))
|
return nil, fmt.Errorf("Could not unmarshal api response: %s", err.Error())
|
||||||
}
|
}
|
||||||
return newAgent, nil
|
return newAgent, nil
|
||||||
|
|
||||||
|
|||||||
110
internal/woodpecker/agent_test.go
Normal file
110
internal/woodpecker/agent_test.go
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
package woodpecker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config"
|
||||||
|
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCreateAndGetAndDeleteAgent(t *testing.T) {
|
||||||
|
// prepare a fake agent to return
|
||||||
|
createdAgent := models.Agent{
|
||||||
|
ID: 42,
|
||||||
|
Name: "woodpecker-autoscaler-agent-abcde",
|
||||||
|
Token: "tok",
|
||||||
|
}
|
||||||
|
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/api/agents", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method == http.MethodPost {
|
||||||
|
// ensure content-type
|
||||||
|
if ct := r.Header.Get("Content-Type"); ct != "application/json" {
|
||||||
|
t.Fatalf("expected json content-type, got %s", ct)
|
||||||
|
}
|
||||||
|
body, _ := io.ReadAll(r.Body)
|
||||||
|
defer r.Body.Close()
|
||||||
|
if !strings.Contains(string(body), "woodpecker-autoscaler-agent-") {
|
||||||
|
t.Fatalf("unexpected agent request body: %s", string(body))
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
_ = json.NewEncoder(w).Encode(createdAgent)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// For GET listing, return an AgentList
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
list := models.AgentList{Agents: []models.Agent{createdAgent}}
|
||||||
|
_ = json.NewEncoder(w).Encode(list)
|
||||||
|
})
|
||||||
|
|
||||||
|
mux.HandleFunc("/api/agents?page=1&perPage=100", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// return list in expected format for GetAgentIdByName
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
// GetAgentIdByName expects a models.AgentList; encode accordingly
|
||||||
|
list := models.AgentList{Agents: []models.Agent{createdAgent}}
|
||||||
|
_ = json.NewEncoder(w).Encode(list)
|
||||||
|
})
|
||||||
|
|
||||||
|
// handle delete
|
||||||
|
mux.HandleFunc(fmt.Sprintf("/api/agents/%d", createdAgent.ID), func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodDelete {
|
||||||
|
t.Fatalf("expected DELETE, got %s", r.Method)
|
||||||
|
}
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
})
|
||||||
|
|
||||||
|
srv := httptest.NewServer(mux)
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
cfg := config.Config{
|
||||||
|
WoodpeckerInstance: srv.URL,
|
||||||
|
WoodpeckerApiToken: "testtoken",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test CreateWoodpeckerAgent
|
||||||
|
a, err := CreateWoodpeckerAgent(&cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateWoodpeckerAgent failed: %v", err)
|
||||||
|
}
|
||||||
|
if a == nil || !strings.HasPrefix(a.Name, "woodpecker-autoscaler-agent-") {
|
||||||
|
t.Fatalf("unexpected agent returned: %#v", a)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test GetAgentIdByName
|
||||||
|
id, err := GetAgentIdByName(&cfg, a.Name)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetAgentIdByName failed: %v", err)
|
||||||
|
}
|
||||||
|
if id != int(a.ID) {
|
||||||
|
t.Fatalf("unexpected id: got %d want %d", id, a.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test DecomAgent
|
||||||
|
if err := DecomAgent(&cfg, a.ID); err != nil {
|
||||||
|
t.Fatalf("DecomAgent failed: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetAgentIdByName_NotFound(t *testing.T) {
|
||||||
|
// server returns empty list
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/api/agents?page=1&perPage=100", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
list := models.AgentList{Agents: []models.Agent{{ID: 1, Name: "other"}}}
|
||||||
|
_ = json.NewEncoder(w).Encode(list)
|
||||||
|
})
|
||||||
|
srv := httptest.NewServer(mux)
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
cfg := config.Config{WoodpeckerInstance: srv.URL, WoodpeckerApiToken: "t"}
|
||||||
|
_, err := GetAgentIdByName(&cfg, "nonexistent")
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("expected error for unknown agent name")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,7 +2,6 @@ package woodpecker
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -17,19 +16,19 @@ func QueueInfo(cfg *config.Config, target interface{}) error {
|
|||||||
apiRoute := fmt.Sprintf("%s/api/queue/info", cfg.WoodpeckerInstance)
|
apiRoute := fmt.Sprintf("%s/api/queue/info", cfg.WoodpeckerInstance)
|
||||||
req, err := http.NewRequest(http.MethodGet, apiRoute, nil)
|
req, err := http.NewRequest(http.MethodGet, apiRoute, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New(fmt.Sprintf("Could not create queue request: %s", err.Error()))
|
return fmt.Errorf("Could not create queue request: %s", err.Error())
|
||||||
}
|
}
|
||||||
req.Header.Set("Accept", "application/json")
|
req.Header.Set("Accept", "application/json")
|
||||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cfg.WoodpeckerApiToken))
|
||||||
|
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.New(fmt.Sprintf("Could not query queue info: %s", err.Error()))
|
return fmt.Errorf("Could not query queue info: %s", err.Error())
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != 200 {
|
if resp.StatusCode != 200 {
|
||||||
return errors.New(fmt.Sprintf("Error from queue info api: %s", err.Error()))
|
return fmt.Errorf("Error from queue info api: %s", resp.Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
return json.NewDecoder(resp.Body).Decode(target)
|
return json.NewDecoder(resp.Body).Decode(target)
|
||||||
@@ -40,7 +39,7 @@ func CheckPending(cfg *config.Config) (int, error) {
|
|||||||
queueInfo := new(models.QueueInfo)
|
queueInfo := new(models.QueueInfo)
|
||||||
err := QueueInfo(cfg, queueInfo)
|
err := QueueInfo(cfg, queueInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.New(fmt.Sprintf("Error from QueueInfo: %s", err.Error()))
|
return 0, fmt.Errorf("Error from QueueInfo: %s", err.Error())
|
||||||
}
|
}
|
||||||
count := 0
|
count := 0
|
||||||
if queueInfo.Stats.PendingCount > 0 {
|
if queueInfo.Stats.PendingCount > 0 {
|
||||||
@@ -64,7 +63,7 @@ func CheckRunning(cfg *config.Config) (int, error) {
|
|||||||
queueInfo := new(models.QueueInfo)
|
queueInfo := new(models.QueueInfo)
|
||||||
err := QueueInfo(cfg, queueInfo)
|
err := QueueInfo(cfg, queueInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.New(fmt.Sprintf("Error from QueueInfo: %s", err.Error()))
|
return 0, fmt.Errorf("Error from QueueInfo: %s", err.Error())
|
||||||
}
|
}
|
||||||
count := 0
|
count := 0
|
||||||
if queueInfo.Stats.RunningCount > 0 {
|
if queueInfo.Stats.RunningCount > 0 {
|
||||||
|
|||||||
66
internal/woodpecker/metrics_test.go
Normal file
66
internal/woodpecker/metrics_test.go
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
package woodpecker
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/config"
|
||||||
|
"git.uploadfilter24.eu/covidnetes/woodpecker-autoscaler/internal/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestQueueInfoAndChecks(t *testing.T) {
|
||||||
|
// Create queue info with one pending job matching label and one running matching
|
||||||
|
qi := models.QueueInfo{
|
||||||
|
Pending: []models.JobInformation{
|
||||||
|
{ID: "1", Labels: map[string]string{"role": "worker"}},
|
||||||
|
},
|
||||||
|
Running: []models.JobInformation{
|
||||||
|
{ID: "2", Labels: map[string]string{"role": "worker"}},
|
||||||
|
},
|
||||||
|
Stats: models.Stats{PendingCount: 1, RunningCount: 1},
|
||||||
|
}
|
||||||
|
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path != "/api/queue/info" {
|
||||||
|
w.WriteHeader(http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = json.NewEncoder(w).Encode(qi)
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
cfg := config.Config{
|
||||||
|
WoodpeckerInstance: srv.URL,
|
||||||
|
WoodpeckerApiToken: "t",
|
||||||
|
WoodpeckerLabelSelector: "role=worker",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test QueueInfo
|
||||||
|
var got models.QueueInfo
|
||||||
|
if err := QueueInfo(&cfg, &got); err != nil {
|
||||||
|
t.Fatalf("QueueInfo failed: %v", err)
|
||||||
|
}
|
||||||
|
if got.Stats.PendingCount != 1 || got.Stats.RunningCount != 1 {
|
||||||
|
t.Fatalf("unexpected stats: %#v", got.Stats)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test CheckPending
|
||||||
|
pending, err := CheckPending(&cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CheckPending error: %v", err)
|
||||||
|
}
|
||||||
|
if pending != 1 {
|
||||||
|
t.Fatalf("expected 1 pending, got %d", pending)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test CheckRunning
|
||||||
|
running, err := CheckRunning(&cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CheckRunning error: %v", err)
|
||||||
|
}
|
||||||
|
if running != 1 {
|
||||||
|
t.Fatalf("expected 1 running, got %d", running)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user