Skip to content

Commit

Permalink
refactor: use Go 1.18 buildinfo (#2541)
Browse files Browse the repository at this point in the history
  • Loading branch information
sozercan authored Jan 28, 2023
1 parent 819c190 commit 5888706
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 54 deletions.
14 changes: 2 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,12 @@ ENV GO111MODULE=on \
GOARM=${TARGETVARIANT}

WORKDIR /go/src/github.com/open-policy-agent/gatekeeper

COPY pkg/ pkg/
COPY third_party/ third_party/
COPY vendor/ vendor/
COPY main.go main.go
COPY apis/ apis/
COPY go.mod .

RUN go build -mod vendor -a -ldflags "${LDFLAGS:--X github.com/open-policy-agent/gatekeeper/pkg/version.Version=latest}" -o manager main.go
COPY . .
RUN go build -mod vendor -a -ldflags "${LDFLAGS:--X github.com/open-policy-agent/gatekeeper/pkg/version.Version=latest}" -o manager

FROM $BASEIMAGE

WORKDIR /

COPY --from=builder /go/src/github.com/open-policy-agent/gatekeeper/manager .

USER 65532:65532

ENTRYPOINT ["/manager"]
7 changes: 0 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,7 @@ BENCHMARK_FILE_NAME ?= benchmarks.txt
ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
BIN_DIR := $(abspath $(ROOT_DIR)/bin)

BUILD_COMMIT := $(shell ./build/get-build-commit.sh)
BUILD_TIMESTAMP := $(shell ./build/get-build-timestamp.sh)
BUILD_HOSTNAME := $(shell ./build/get-build-hostname.sh)

LDFLAGS := "-X github.com/open-policy-agent/gatekeeper/pkg/version.Version=$(VERSION) \
-X github.com/open-policy-agent/gatekeeper/pkg/version.Vcs=$(BUILD_COMMIT) \
-X github.com/open-policy-agent/gatekeeper/pkg/version.Timestamp=$(BUILD_TIMESTAMP) \
-X github.com/open-policy-agent/gatekeeper/pkg/version.Hostname=$(BUILD_HOSTNAME) \
-X main.frameworksVersion=$(FRAMEWORKS_VERSION) \
-X main.opaVersion=$(OPA_VERSION)"

Expand Down
9 changes: 0 additions & 9 deletions build/get-build-commit.sh

This file was deleted.

3 changes: 0 additions & 3 deletions build/get-build-hostname.sh

This file was deleted.

3 changes: 0 additions & 3 deletions build/get-build-timestamp.sh

This file was deleted.

8 changes: 0 additions & 8 deletions build/vendormanifests.go

This file was deleted.

6 changes: 2 additions & 4 deletions gator.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ ENV GO111MODULE=on \
GOARCH=${TARGETARCH} \
GOARM=${TARGETVARIANT}

COPY . /tmp/gatekeeper

WORKDIR /tmp/gatekeeper/cmd/gator

COPY . /go/src/github.com/open-policy-agent/gatekeeper
WORKDIR /go/src/github.com/open-policy-agent/gatekeeper/cmd/gator
RUN go build -mod vendor -a -ldflags "${LDFLAGS:--X github.com/open-policy-agent/gatekeeper/pkg/version.Version=latest -X main.frameworksVersion=latest -X main.opaVersion=latest}" -o /gator

FROM --platform=$BUILDPLATFORM $BASEIMAGE as build
Expand Down
30 changes: 22 additions & 8 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,32 @@ package version
import (
"fmt"
"runtime"
"runtime/debug"
)

// Vcs is is the commit hash for the binary build.
var Vcs string

// Timestamp is the date for the binary build.
var Timestamp string

// Version is the gatekeeper version.
var Version string

// GetUserAgent returns a user agent of the format: gatekeeper/<version> (<goos>/<goarch>) <vcs>/<timestamp>.
// GetUserAgent returns a user agent of the format: gatekeeper/<version> (<goos>/<goarch>) <vcsrevision><-vcsdirty>/<vcstimestamp>.
func GetUserAgent() string {
return fmt.Sprintf("gatekeeper/%s (%s/%s) %s/%s", Version, runtime.GOOS, runtime.GOARCH, Vcs, Timestamp)
vcsrevision := "unknown"
vcstimestamp := "unknown"
vcsdirty := ""

if info, ok := debug.ReadBuildInfo(); ok {
for _, v := range info.Settings {
switch v.Key {
case "vcs.revision":
vcsrevision = v.Value
case "vcs.modified":
if v.Value == "true" {
vcsdirty = "-dirty"
}
case "vcs.time":
vcstimestamp = v.Value
}
}
}

return fmt.Sprintf("gatekeeper/%s (%s/%s) %s%s/%s", Version, runtime.GOOS, runtime.GOARCH, vcsrevision, vcsdirty, vcstimestamp)
}

0 comments on commit 5888706

Please sign in to comment.