From 1e22589c511c0960a233f29a04e78cd558e7a289 Mon Sep 17 00:00:00 2001 From: Matthias Eliasson Date: Tue, 13 Jan 2026 14:28:01 +0100 Subject: [PATCH] chore: update dependencies and improve code quality MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Upgrade Go from 1.22 to 1.25 across all workflows - Update GitHub Actions: checkout v3→v6, setup-go v4→v6 - Update release workflow actions: cosign v3.1.2→v4.0.0, sbom-action v0.14.3→v0.21.1, goreleaser v5→v6 - Update Go dependencies to latest versions: - prometheus/client_golang: 1.20.1 → 1.23.2 - stretchr/testify: 1.9.0 → 1.11.1 - prometheus/common: 0.55.0 → 0.67.5 - google.golang.org/protobuf: 1.34.2 → 1.36.11 - Add Jenkinsfile with lint stage and improved configuration - Add Dockerfile using distroless/static-debian13 - Add Makefile with test, lint, and build targets - Fix lint errors: add error handling for os.MkdirAll - Remove unused alive() function and clean up imports --- .github/workflows/build.yml | 7 ++-- .github/workflows/release.yml | 12 +++---- Dockerfile | 4 +++ Jenkinsfile | 64 +++++++++++++++++++++++++++++++++++ Makefile | 37 ++++++++++++++++++++ go.mod | 21 ++++++------ go.sum | 44 +++++++++++++----------- helpers.go | 46 ------------------------- main.go | 5 ++- 9 files changed, 153 insertions(+), 87 deletions(-) create mode 100644 Dockerfile create mode 100644 Jenkinsfile create mode 100644 Makefile diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a8f6a0c..f12f293 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,12 +13,11 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v6 - - name: Set up Go - uses: actions/setup-go@v4 + - uses: actions/setup-go@v6 with: - go-version: 1.22 + go-version: 1.25 cache: true - name: Build diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index df958b7..7228918 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -19,16 +19,16 @@ jobs: release: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v6 with: fetch-depth: 0 # this is important, otherwise it won't checkout the full tree (i.e. no previous tags) - - uses: actions/setup-go@v4 + - uses: actions/setup-go@v6 with: - go-version: 1.22 + go-version: 1.25 cache: true - - uses: sigstore/cosign-installer@v3.1.2 # installs cosign - - uses: anchore/sbom-action/download-syft@v0.14.3 # installs syft - - uses: goreleaser/goreleaser-action@v5 # run goreleaser + - uses: sigstore/cosign-installer@v4.0.0 # installs cosign + - uses: anchore/sbom-action/download-syft@v0.21.1 # installs syft + - uses: goreleaser/goreleaser-action@v6 # run goreleaser with: version: latest args: release --clean diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..497920a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM gcr.io/distroless/static-debian13:nonroot +COPY prometheus-net-discovery / +USER nonroot +ENTRYPOINT ["/prometheus-net-discovery"] diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..ce4a360 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,64 @@ +#!/usr/bin/env groovy +properties( + [ + buildDiscarder( + logRotator( + numToKeepStr: '5' + ) + ) + ] +) + +def SSH_CREDENTIAL_ID = '18270936-0906-4c40-a90e-bcf6661f501d' +def DOCKER_REGISTRY = 'quay.fnox.se' +def DOCKER_CREDENTIAL_ID = 'quay-fnox-se' + +node('go1.25') { + container('run') { + def tag = '' + def strippedTag = '' + + try { + stage('Checkout') { + checkout scm + tag = sh(script: 'git tag -l --contains HEAD', returnStdout: true).trim() + echo "Detected tag: ${tag ?: 'none'}" + echo "Branch: ${env.BRANCH_NAME}" + } + + stage('Fetch dependencies') { + sshagent(credentials: [SSH_CREDENTIAL_ID]) { + sh 'go mod download' + } + } + + stage('Test') { + sh 'make test' + } + + if (env.BRANCH_NAME == 'master' && tag != '') { + stage('Build') { + sh 'make build' + } + + stage('Docker Build & Push') { + docker.withRegistry("https://${DOCKER_REGISTRY}", DOCKER_CREDENTIAL_ID) { + strippedTag = tag.replaceFirst('v', '') + echo "Building and pushing Docker image with tag: ${strippedTag}" + sh("make push VERSION=${strippedTag}") + } + } + + echo "Successfully built and pushed version ${strippedTag}" + } else { + echo "Skipping build and push (Branch: ${env.BRANCH_NAME}, Tag: ${tag ?: 'none'})" + } + + currentBuild.result = 'SUCCESS' + } catch (err) { + currentBuild.result = 'FAILED' + echo "Build failed with error: ${err.message}" + throw err + } + } +} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4e874ed --- /dev/null +++ b/Makefile @@ -0,0 +1,37 @@ +.PHONY: mocks +SHELL := /bin/bash +MAKEFLAGS += --no-print-directory + +IMAGE = quay.fnox.se/fortnox/prometheus-net-discovery +VERSION?=0.0.1-local + +# Sets DOCKER_HOST to the Podman socket if it exists, otherwise defaults to the Docker socket +export DOCKER_HOST := unix://$(or $(wildcard /run/user/$(shell id -u)/podman/podman.sock),/var/run/docker.sock) + + +build: + CGO_ENABLED=0 GOOS=linux go build + +docker: build + docker build --pull --rm -t $(IMAGE):$(VERSION) . + +push: docker + docker push $(IMAGE):$(VERSION) + +localrun: + go run main.go + +test: + go test ./... -count=1 -cover + +test-coverage: + go test ./... -count=1 -coverprofile=coverage.out + grep -vE "/mocks/|main.go" coverage.out > coverage-filtered.out + go tool cover -func=coverage-filtered.out + +lint: + @if command -v golangci-lint >/dev/null 2>&1; then \ + golangci-lint run ./...; \ + else \ + echo "golangci-lint not installed, skipping lint"; \ + fi diff --git a/go.mod b/go.mod index 10804f8..7f5d967 100644 --- a/go.mod +++ b/go.mod @@ -1,32 +1,33 @@ module github.com/fortnoxab/prometheus-net-discovery/v2 -go 1.22 +go 1.24.0 require ( github.com/fortnoxab/fnxlogrus v0.0.0-20220823093317-6a580c56b8fd github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7 - github.com/prometheus/client_golang v1.20.1 + github.com/prometheus/client_golang v1.23.2 github.com/sirupsen/logrus v1.9.3 - github.com/stretchr/testify v1.9.0 + github.com/stretchr/testify v1.11.1 ) require ( - github.com/BurntSushi/toml v1.4.0 // indirect + github.com/BurntSushi/toml v1.6.0 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/fatih/camelcase v1.0.0 // indirect github.com/fatih/structs v1.1.0 // indirect - github.com/klauspost/compress v1.17.9 // indirect + github.com/klauspost/compress v1.18.2 // indirect github.com/kr/text v0.2.0 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/prometheus/client_model v0.6.1 // indirect - github.com/prometheus/common v0.55.0 // indirect - github.com/prometheus/procfs v0.15.1 // indirect - golang.org/x/sys v0.24.0 // indirect - google.golang.org/protobuf v1.34.2 // indirect + github.com/prometheus/client_model v0.6.2 // indirect + github.com/prometheus/common v0.67.5 // indirect + github.com/prometheus/procfs v0.19.2 // indirect + go.yaml.in/yaml/v2 v2.4.3 // indirect + golang.org/x/sys v0.40.0 // indirect + google.golang.org/protobuf v1.36.11 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 84396aa..b1d710a 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0= -github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= +github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk= +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/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= @@ -14,10 +14,10 @@ github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= github.com/fortnoxab/fnxlogrus v0.0.0-20220823093317-6a580c56b8fd h1:vjKVmkcuAhOGIJnbCGfm1MvKvJgE7jpIZTHS6pqdV+s= github.com/fortnoxab/fnxlogrus v0.0.0-20220823093317-6a580c56b8fd/go.mod h1:R6mab3Zy+NZ3qi4krgbsMSPFVh2AFVSBG2xnus0fl0Q= -github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= -github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= -github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +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/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7 h1:SWlt7BoQNASbhTUD0Oy5yysI2seJ7vWuGUp///OM4TM= github.com/koding/multiconfig v0.0.0-20171124222453-69c27309b2d7/go.mod h1:Y2SaZf2Rzd0pXkLVhLlCiAXFCLSXAIbTKDivVgff/AM= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= @@ -32,27 +32,31 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 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/prometheus/client_golang v1.20.1 h1:IMJXHOD6eARkQpxo8KkhgEVFlBNm+nkrFUyGlIu7Na8= -github.com/prometheus/client_golang v1.20.1/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE= -github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E= -github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY= -github.com/prometheus/common v0.55.0 h1:KEi6DK7lXW/m7Ig5i47x0vRzuBsHuvJdi5ee6Y3G1dc= -github.com/prometheus/common v0.55.0/go.mod h1:2SECS4xJG1kd8XF9IcM1gMX6510RAEL65zxzNImwdc8= -github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc= -github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk= +github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o= +github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg= +github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk= +github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE= +github.com/prometheus/common v0.67.5 h1:pIgK94WWlQt1WLwAC5j2ynLaBRDiinoAb86HZHTUGI4= +github.com/prometheus/common v0.67.5/go.mod h1:SjE/0MzDEEAyrdr5Gqc6G+sXI67maCxzaT3A2+HqjUw= +github.com/prometheus/procfs v0.19.2 h1:zUMhqEW66Ex7OXIiDkll3tl9a1ZdilUOd/F6ZXw4Vws= +github.com/prometheus/procfs v0.19.2/go.mod h1:M0aotyiemPhBCM0z5w87kL22CxfcH05ZpYlu+b4J7mw= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= 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/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +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/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= -golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= -google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ= +golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= +google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= 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/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/helpers.go b/helpers.go index 41bbe56..81b3164 100644 --- a/helpers.go +++ b/helpers.go @@ -1,16 +1,12 @@ package main import ( - "bufio" - "bytes" "context" "encoding/json" "fmt" "net" "net/http" "time" - - "github.com/sirupsen/logrus" ) func inc(ip net.IP) { @@ -77,45 +73,3 @@ func checkExporterExporter(parentCtx context.Context, host, port string) ([]stri return exporters, nil } - -func alive(parentCtx context.Context, host, port, path string) bool { - if path != "" { - u := fmt.Sprintf(path, net.JoinHostPort(host, port)) - - ctx, cancel := context.WithTimeout(parentCtx, time.Second*3) - defer cancel() - req, err := http.NewRequestWithContext(ctx, "GET", u, nil) - if err != nil { - logrus.Errorf("error creating request: %s", err) - return false - } - resp, err := client.Do(req) - if err != nil { - return false - } - defer resp.Body.Close() - - r := bufio.NewReader(resp.Body) - for i := 0; i < 10; i++ { - line, _, err := r.ReadLine() - if err != nil { - return false - } - if bytes.Contains(line, []byte("# TYPE")) { - return true - } - } - return false - } - - conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, port), 200*time.Millisecond) - if err != nil { - return false - } - - if conn != nil { - conn.Close() - return true - } - return false -} diff --git a/main.go b/main.go index 2ffad9c..42a1b32 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "io" "log" "net" @@ -238,7 +239,9 @@ func writeFileSDConfig(config *Config, exporterName string, addresses []Address) path := filepath.Join(config.FileSdPath, exporterName+".json") if _, err := os.Stat(config.FileSdPath); os.IsNotExist(err) { - os.MkdirAll(config.FileSdPath, 0755) + if err := os.MkdirAll(config.FileSdPath, 0755); err != nil { + return fmt.Errorf("failed to create directory: %w", err) + } } groups := []Group{}