Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initial version #1

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
go.work.sum

# env file
.env

# Custom part
ovh-exporter
vendor
16 changes: 16 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: 2
updates:
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "weekly"
labels:
- "dependencies"
- "bot"
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
labels:
- "dependencies"
- "bot"
131 changes: 131 additions & 0 deletions .github/workflows/build_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
name: Build and release

on:
push:
branches:
- main
tags:
- 'v*.*.*'
pull_request:
branches:
- main

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
BINARY_NAME: ovh-exporter

jobs:
golang:
name: Golang
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-24.04
include:
- os: ubuntu-24.04
platform: linux
arch: amd64
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.23.1'

- name: Install packages
run: |
sudo apt-get update
sudo apt install -yq --no-install-recommends make

- name: Build
env:
GOOS: ${{ matrix.platform }}
GOARCH: ${{ matrix.arch }}
run: make build

- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: ${{ env.BINARY_NAME }}-${{ matrix.platform }}-${{ matrix.arch }}
path: ${{ env.BINARY_NAME }}
if-no-files-found: error

container:
name: Containerfile
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-24.04
include:
- os: ubuntu-24.04
platform: linux
arch: amd64
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Login to Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata
uses: docker/metadata-action@v5
id: metadata
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build image
uses: docker/build-push-action@v6
with:
context: .
file: Containerfile
push: ${{ github.event_name != 'pull_request' }}
platforms: ${{ matrix.platform }}/${{ matrix.arch }}
tags: ${{ steps.metadata.outputs.tags }}
labels: ${{ steps.metadata.outputs.labels }}

release:
runs-on: ubuntu-24.04
needs:
- golang
- container
if: startsWith(github.ref, 'refs/tags/')
steps:
- name: Download artifacts
uses: actions/download-artifact@v4

- name: Package
run: |
for folder in ./*; do
if [ -d "$folder" ]; then
echo "Processing folder: $folder"
cd $folder
tar -czf ../$folder.tar.gz -T <(\ls -1)
cd ..
sha256sum $folder.tar.gz > $folder.tar.gz.sha256
fi
done

- name: Release
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: '*.tar.gz*'
tag: ${{ github.ref }}
file_glob: true
22 changes: 22 additions & 0 deletions .github/workflows/compliance.yml.disabled
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Compliance

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
commit:
name: Commit
runs-on: ubuntu-24.04
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Conventional commit check
uses: cocogitto/[email protected]
33 changes: 33 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Lint

on:
pull_request:
branches:
- main

jobs:
golang:
name: Golang
runs-on: ubuntu-24.04
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.23.1'

- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.61

container:
name: Containerfile
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: hadolint/[email protected]
with:
dockerfile: Containerfile
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ go.work.sum

# env file
.env

# Custom part
ovh-exporter
vendor
25 changes: 25 additions & 0 deletions Containerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM golang:1.23-alpine AS build

ENV CGO_ENABLED=0
ENV GOOS=linux
ENV GOARCH=amd64

ARG JQ_VERSION=1.7

# hadolint ignore=DL3018
RUN apk update && apk add --no-cache bash git make binutils wget \
&& wget --progress=dot:giga "https://github.com/jqlang/jq/releases/download/jq-${JQ_VERSION}/jq-${GOOS}-${GOARCH}" -O /usr/bin/jq \
&& chmod +x /usr/bin/jq

WORKDIR $GOPATH/src/github.com/wiremind/ovh-exporter

COPY . .

RUN make ovh-exporter && mv ovh-exporter /usr/bin/


FROM busybox:stable AS runtime

COPY --from=build /usr/bin/ovh-exporter /usr/bin/ovh-exporter

ENTRYPOINT ["/usr/bin/ovh-exporter"]
49 changes: 49 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
GO?=go

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

BINARY_NAME=ovh-exporter
# TODO less bruteforce ?
BINARY_FILES=$(shell find ${PWD} -type f -name "*.go")
GOFMT_FILES?=$(shell find . -not -path "./vendor/*" -type f -name '*.go')

VERSION ?= $(shell git describe --match 'v[0-9]*' --dirty='.m' --always)
REVISION=$(shell git rev-parse HEAD)$(shell if ! git diff --no-ext-diff --quiet --exit-code; then echo .m; fi)
PKG=github.com/wiremind/ovh-exporter

# Control if static or dynamically linked (static by default)
export CGO_ENABLED:=0

GO_GCFLAGS?=
GO_LDFLAGS=-ldflags '-X $(PKG)/version.Version=$(VERSION) -X $(PKG)/version.Revision=$(REVISION) -X $(PKG)/version.Package=$(PKG)'

.PHONY: build
build: ${BINARY_NAME}

${BINARY_NAME}: ${BINARY_FILES}
${GO} build ${GO_GCFLAGS} ${GO_LDFLAGS} -o $@ cmd/${BINARY_NAME}/*.go
strip -x $@

## Lints all the go code in the application.
.PHONY: lint
lint: dependencies
gofmt -w $(GOFMT_FILES)
$(GOBIN)/goimports -w $(GOFMT_FILES)
$(GOBIN)/gofumpt -l -w $(GOFMT_FILES)
$(GOBIN)/gci write $(GOFMT_FILES) --skip-generated
$(GOBIN)/golangci-lint run

## Loads all the dependencies to vendor directory
.PHONY: dependencies
dependencies:
go install golang.org/x/tools/cmd/[email protected]
go install mvdan.cc/[email protected]
go install github.com/daixiang0/[email protected]
go install github.com/golangci/golangci-lint/cmd/[email protected]
go mod vendor
go mod tidy
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
# ovh-exporter
Prometheus exporter for the OVH API

# Configuration

Run this command to generate a unique link with correct ovh permissions needed for this project
```bash
ovh-exporter credentials
```

Then source a .env file containing these variables
```bash
export OVH_ENDPOINT="ovh-eu"
export OVH_APP_KEY=""
export OVH_APP_SECRET=""
export OVH_CONSUMER_KEY=""
export OVH_CLOUD_PROJECT_INSTANCE_BILLING_PROJECT_ID=""
export OVH_CACHE_UPDATE_INTERVAL="60"
export SERVER_PORT="8080"
```

# Running

```bash
ovh-exporter serve
```
43 changes: 43 additions & 0 deletions cmd/ovh-exporter/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"log/slog"
"os"

"github.com/urfave/cli"
"github.com/wiremind/ovh-exporter/pkg/cmd"
"github.com/wiremind/ovh-exporter/pkg/credentials"
"github.com/wiremind/ovh-exporter/pkg/network"
)

func main() {
var exitValue int

app := cli.NewApp()
app.Name = "ovh-exporter"
app.Usage = "Prometheus exporter for the OVH API"
app.Version = cmd.Version

app.Flags = []cli.Flag{
cli.IntFlag{
Name: "exit, e",
Value: 1,
Usage: "value returned on error",
Destination: &exitValue,
},
}

app.Commands = []cli.Command{
credentials.GenerateCommand,
network.ServeCommand,
}

err := app.Run(os.Args)
if err != nil {
slog.Error(
"main() error",
slog.String("error", err.Error()),
)
os.Exit(exitValue)
}
}
3 changes: 3 additions & 0 deletions cog.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ignore_merge_commits = true
from_latest_tag = true
tag_prefix = "v"
Loading