Fix build target and linting issues

This commit is contained in:
Jan Gräfen 2021-03-05 20:28:59 +01:00
parent 816a6d02fc
commit f92d993002
8 changed files with 35 additions and 49 deletions

View file

@ -2,7 +2,7 @@ name: Build
on:
push:
branches: [ master ]
branches: [ main ]
paths-ignore:
- README.md
- LICENSE
@ -18,7 +18,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v1
with:
go-version: ^1.15
go-version: ^1.16
- name: Checkout code
uses: actions/checkout@v1
- name: Install golangci-lint
@ -27,34 +27,16 @@ jobs:
- name: Run linters
run: |
export PATH=$PATH:$(go env GOPATH)/bin
go mod download
golangci-lint run
test:
name: Test
strategy:
matrix:
go-version: [ 1.15.x ]
platform: [ ubuntu-latest ]
runs-on: ${{ matrix.platform }}
needs: [ lint ]
steps:
- name: Install Go
if: success()
uses: actions/setup-go@v1
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v1
- name: Run tests
run: go test -v -race -covermode=atomic ./...
build:
runs-on: ubuntu-latest
needs: [ test ]
steps:
- name: Install Go
uses: actions/setup-go@v1
with:
go-version: ^1.15
go-version: ^1.16
- name: Checkout code
uses: actions/checkout@v1
- name: Get dependencies
@ -64,6 +46,7 @@ jobs:
- name: Build
run: |
export GO111MODULE=on
go mod download
GOOS=linux GOARCH=amd64 go build -o bin/hcloud-pricing-exporter-linux-amd64 main.go
GOOS=linux GOARCH=arm64 go build -o bin/hcloud-pricing-exporter-linux-arm64 main.go
GOOS=windows GOARCH=amd64 go build -o bin/hcloud-pricing-exporter-windows-amd64.exe main.go

View file

@ -17,7 +17,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15
go-version: 1.16
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:

View file

@ -7,6 +7,7 @@ ADD . /app/
WORKDIR /app
# Build the application
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go mod download
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build -o run .
# Add the execution user

View file

@ -8,7 +8,7 @@ import (
"github.com/hetznercloud/hcloud-go/hcloud"
)
// PriceProvider provides easy access to current HCloud prices
// PriceProvider provides easy access to current HCloud prices.
type PriceProvider struct {
Client *hcloud.Client
pricing *hcloud.Pricing

View file

@ -26,7 +26,7 @@ func (serverTraffic serverTraffic) Run(client *hcloud.Client) error {
for _, s := range servers {
location := s.Datacenter.Location
additionalTraffic := s.OutgoingTraffic - s.IncludedTraffic
additionalTraffic := int(s.OutgoingTraffic) - int(s.IncludedTraffic)
if additionalTraffic < 0 {
serverTraffic.hourly.WithLabelValues(s.Name, location.Name, s.ServerType.Name).Set(0)
serverTraffic.monthly.WithLabelValues(s.Name, location.Name, s.ServerType.Name).Set(0)

View file

@ -25,7 +25,7 @@ func (volume volume) Run(client *hcloud.Client) error {
}
for _, v := range volumes {
monthlyPrice := math.Ceil(float64(v.Size/sizeGB)) * volume.pricing.Volume()
monthlyPrice := math.Ceil(float64(v.Size)/sizeGB) * volume.pricing.Volume()
hourlyPrice := pricingPerHour(monthlyPrice)
volume.hourly.WithLabelValues(v.Name, v.Location.Name, strconv.Itoa(v.Size)).Set(hourlyPrice)

48
main.go
View file

@ -19,21 +19,13 @@ const (
defaultFetchInterval = 1 * time.Minute
)
func toScheduler(client *hcloud.Client, f func(*hcloud.Client) error) func() {
return func() {
if err := f(client); err != nil {
panic(err)
}
}
}
func main() {
var (
hcloudAPIToken string
port uint
fetchInterval time.Duration
)
var (
hcloudAPIToken string
port uint
fetchInterval time.Duration
)
func handleFlags() {
flag.StringVar(&hcloudAPIToken, "hcloud-token", "", "the token to authenticate against the HCloud API")
flag.UintVar(&port, "port", defaultPort, "the port that the exporter exposes its data on")
flag.DurationVar(&fetchInterval, "fetch-interval", defaultFetchInterval, "the interval between data fetching cycles")
@ -47,19 +39,29 @@ func main() {
if hcloudAPIToken == "" {
panic(fmt.Errorf("no API token for HCloud specified, but required"))
}
}
func toScheduler(client *hcloud.Client, f func(*hcloud.Client) error) func() {
return func() {
if err := f(client); err != nil {
panic(err)
}
}
}
func main() {
handleFlags()
client := hcloud.NewClient(hcloud.WithToken(hcloudAPIToken))
priceRepository := &fetcher.PriceProvider{Client: client}
var (
floatingIP = fetcher.NewFloatingIP(priceRepository)
loadBalancer = fetcher.NewLoadbalancer(priceRepository)
server = fetcher.NewServer(priceRepository)
serverBackup = fetcher.NewServerBackup(priceRepository)
serverTraffic = fetcher.NewServerTraffic(priceRepository)
snapshot = fetcher.NewSnapshot(priceRepository)
volume = fetcher.NewVolume(priceRepository)
)
floatingIP := fetcher.NewFloatingIP(priceRepository)
loadBalancer := fetcher.NewLoadbalancer(priceRepository)
server := fetcher.NewServer(priceRepository)
serverBackup := fetcher.NewServerBackup(priceRepository)
serverTraffic := fetcher.NewServerTraffic(priceRepository)
snapshot := fetcher.NewSnapshot(priceRepository)
volume := fetcher.NewVolume(priceRepository)
scheduler.RunTaskAtInterval(toScheduler(client, floatingIP.Run), fetchInterval, 0)
scheduler.RunTaskAtInterval(toScheduler(client, loadBalancer.Run), fetchInterval, 0)