diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ca5bce0..e16ff85 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3c853fe..4c78166 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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: diff --git a/.golangreleaser.yml b/.goreleaser.yml similarity index 100% rename from .golangreleaser.yml rename to .goreleaser.yml diff --git a/Dockerfile b/Dockerfile index 0fc6e80..6b42d45 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/fetcher/prices.go b/fetcher/prices.go index 0462ad8..9c67a14 100644 --- a/fetcher/prices.go +++ b/fetcher/prices.go @@ -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 diff --git a/fetcher/server_traffic.go b/fetcher/server_traffic.go index 1ef003a..2c9cd64 100644 --- a/fetcher/server_traffic.go +++ b/fetcher/server_traffic.go @@ -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) diff --git a/fetcher/volume.go b/fetcher/volume.go index 4c6311f..d9bb6bc 100644 --- a/fetcher/volume.go +++ b/fetcher/volume.go @@ -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) diff --git a/main.go b/main.go index c285bf3..6ba822e 100644 --- a/main.go +++ b/main.go @@ -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)