From 87ba3a16a87097fb4e6ad84d33fd5e18c53bbcce Mon Sep 17 00:00:00 2001 From: Phil Porada Date: Mon, 20 May 2024 11:44:22 -0400 Subject: [PATCH 1/3] Add build flags --- .github/workflows/release.yml | 2 +- unbound_exporter.go | 7 +++- util/buildVars.go | 61 +++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 util/buildVars.go diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d7c3822..aad52c2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -23,7 +23,7 @@ jobs: persist-credentials: false - name: build binary - run: go build + run: go build -ldflags "-X github.com/letsencrypt/unbound_exporter/util.BuildID=$(git rev-parse --short HEAD) -X github.com/letsencrypt/unbound_exporter/util.BuildBranch=$(git rev-parse --abbrev-ref HEAD) -X github.com/letsencrypt/unbound_exporter/util.BuildTime=$(date +%F-%T -u)" - name: install nfpm run: go install github.com/goreleaser/nfpm/v2/cmd/nfpm@v2.15.1 diff --git a/unbound_exporter.go b/unbound_exporter.go index 291bb00..3929e62 100644 --- a/unbound_exporter.go +++ b/unbound_exporter.go @@ -25,12 +25,14 @@ import ( "net/url" "os" "regexp" + "runtime" "strconv" "strings" "sort" "github.com/go-kit/log/level" + "github.com/letsencrypt/unbound_exporter/util" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/promlog" @@ -548,7 +550,10 @@ func main() { ) flag.Parse() - _ = level.Info(log).Log("Starting unbound_exporter") + _ = level.Info(log).Log( + "msg", "Starting unbound_exporter", + "version", fmt.Sprintf("(version=%s, branch=%s, revision=%s)", runtime.Version(), util.GetBuildBranch(), util.GetBuildID()), + ) exporter, err := NewUnboundExporter(*unboundHost, *unboundCa, *unboundCert, *unboundKey) if err != nil { panic(err) diff --git a/util/buildVars.go b/util/buildVars.go new file mode 100644 index 0000000..4d9b259 --- /dev/null +++ b/util/buildVars.go @@ -0,0 +1,61 @@ +package util + +import ( + "expvar" +) + +const Unspecified = "Unspecified" + +// BuildID is set by the compiler (using -ldflags "-X util.BuildID $(git rev-parse --short HEAD)") +// and is used by GetBuildID +var BuildID string + +// BuildHost is set by the compiler and is used by GetBuildHost +var BuildHost string + +// BuildTime is set by the compiler and is used by GetBuildTime +var BuildTime string + +// BuildBranch is set by the compiler and is used by GetBuildBranch +var BuildBranch string + +func init() { + expvar.NewString("BuildID").Set(BuildID) + expvar.NewString("BuildTime").Set(BuildTime) +} + +// GetBuildID identifies what build is running. +func GetBuildID() (retID string) { + retID = BuildID + if retID == "" { + retID = Unspecified + } + return +} + +// GetBuildTime identifies when this build was made +func GetBuildTime() (retID string) { + retID = BuildTime + if retID == "" { + retID = Unspecified + } + return +} + +// GetBuildHost identifies the building host +func GetBuildHost() (retID string) { + retID = BuildHost + if retID == "" { + retID = Unspecified + } + return +} + +// GetBuildBranch identifies the building host +func GetBuildBranch() (retID string) { + retID = BuildBranch + if retID == "" { + retID = Unspecified + } + return +} From 9a89f5d0b785d68bc88413e2bc4b07e7efe96a96 Mon Sep 17 00:00:00 2001 From: Phil Porada Date: Mon, 20 May 2024 13:38:16 -0400 Subject: [PATCH 2/3] Fix integration test and address comments --- .dockerignore | 14 +++++++++++ Dockerfile | 7 +----- build/vars.go | 28 +++++++++++++++++++++ unbound_exporter.go | 4 +-- util/buildVars.go | 61 --------------------------------------------- 5 files changed, 45 insertions(+), 69 deletions(-) create mode 100644 .dockerignore create mode 100644 build/vars.go delete mode 100644 util/buildVars.go diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..9da8389 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,14 @@ +# Ignore files +*.md +*.sh +*.yml +*.yaml +.gitignore + +# Ignore folders +.github/ +.git/ +contrib/ + +# Don't include a previously built binary +unbound_exporter diff --git a/Dockerfile b/Dockerfile index 3977962..23d8e50 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,9 @@ FROM --platform=$BUILDPLATFORM docker.io/library/golang:1.21.4-bookworm AS build -WORKDIR /go/src/app - -COPY go.mod . -COPY go.sum . +VOLUME . /go/src/app RUN go mod download -COPY *.go . - ENV CGO_ENABLED=0 RUN GOOS=$TARGETOS GOARCH=$TARGETPLATFORM go build -v -o /go/bin/unbound_exporter ./... diff --git a/build/vars.go b/build/vars.go new file mode 100644 index 0000000..0591395 --- /dev/null +++ b/build/vars.go @@ -0,0 +1,28 @@ +package build + +const Unspecified = "Unspecified" + +// BuildID is set by the compiler (using -ldflags "-X util.BuildID $(git rev-parse --short HEAD)") +// and is used by GetID +var BuildID string + +// BuildBranch is set by the compiler and is used by GetBranch +var BuildBranch string + +// GetBuildID identifies what build is running. +func GetID() (BuildID string) { + if BuildID != "" { + return BuildID + } + + return Unspecified +} + +// GetBranch identifies the building host +func GetBranch() (BuildBranch string) { + if BuildBranch != "" { + return BuildBranch + } + + return Unspecified +} diff --git a/unbound_exporter.go b/unbound_exporter.go index 3929e62..cc0aacb 100644 --- a/unbound_exporter.go +++ b/unbound_exporter.go @@ -32,7 +32,7 @@ import ( "sort" "github.com/go-kit/log/level" - "github.com/letsencrypt/unbound_exporter/util" + "github.com/letsencrypt/unbound_exporter/build" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/promlog" @@ -552,7 +552,7 @@ func main() { _ = level.Info(log).Log( "msg", "Starting unbound_exporter", - "version", fmt.Sprintf("(version=%s, branch=%s, revision=%s)", runtime.Version(), util.GetBuildBranch(), util.GetBuildID()), + "version", fmt.Sprintf("(version=%s, branch=%s, revision=%s)", runtime.Version(), build.GetBranch(), build.GetID()), ) exporter, err := NewUnboundExporter(*unboundHost, *unboundCa, *unboundCert, *unboundKey) if err != nil { diff --git a/util/buildVars.go b/util/buildVars.go deleted file mode 100644 index 4d9b259..0000000 --- a/util/buildVars.go +++ /dev/null @@ -1,61 +0,0 @@ -package util - -import ( - "expvar" -) - -const Unspecified = "Unspecified" - -// BuildID is set by the compiler (using -ldflags "-X util.BuildID $(git rev-parse --short HEAD)") -// and is used by GetBuildID -var BuildID string - -// BuildHost is set by the compiler and is used by GetBuildHost -var BuildHost string - -// BuildTime is set by the compiler and is used by GetBuildTime -var BuildTime string - -// BuildBranch is set by the compiler and is used by GetBuildBranch -var BuildBranch string - -func init() { - expvar.NewString("BuildID").Set(BuildID) - expvar.NewString("BuildTime").Set(BuildTime) -} - -// GetBuildID identifies what build is running. -func GetBuildID() (retID string) { - retID = BuildID - if retID == "" { - retID = Unspecified - } - return -} - -// GetBuildTime identifies when this build was made -func GetBuildTime() (retID string) { - retID = BuildTime - if retID == "" { - retID = Unspecified - } - return -} - -// GetBuildHost identifies the building host -func GetBuildHost() (retID string) { - retID = BuildHost - if retID == "" { - retID = Unspecified - } - return -} - -// GetBuildBranch identifies the building host -func GetBuildBranch() (retID string) { - retID = BuildBranch - if retID == "" { - retID = Unspecified - } - return -} From d19cc94bdaad6143c49750e3bd826d99678555bb Mon Sep 17 00:00:00 2001 From: Phil Porada Date: Mon, 20 May 2024 13:55:24 -0400 Subject: [PATCH 3/3] Adjust dockerfile to my liking --- Dockerfile | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/Dockerfile b/Dockerfile index 23d8e50..f33c42e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,15 +1,9 @@ FROM --platform=$BUILDPLATFORM docker.io/library/golang:1.21.4-bookworm AS build - -VOLUME . /go/src/app - +WORKDIR /go/src/app +COPY . . RUN go mod download - -ENV CGO_ENABLED=0 - -RUN GOOS=$TARGETOS GOARCH=$TARGETPLATFORM go build -v -o /go/bin/unbound_exporter ./... +RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETPLATFORM go build -v -o /go/bin/unbound_exporter FROM gcr.io/distroless/static-debian12 - COPY --from=build /go/bin/unbound_exporter / - ENTRYPOINT ["/unbound_exporter"]