Skip to content

Commit

Permalink
add version information (#46)
Browse files Browse the repository at this point in the history
Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
  • Loading branch information
everettraven committed Apr 20, 2023
1 parent 5006fd6 commit 70758e6
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 18 deletions.
7 changes: 7 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ builds:
- arm64
- ppc64le
- s390x
ldflags:
- -X {{ .Env.VERSION_PKG }}.gitVersion={{ .Env.GIT_VERSION }}
# TODO: When the apiserver is working properly, uncomment this
# - id: catalogd-server
# main: ./cmd/apiserver/
Expand All @@ -29,6 +31,11 @@ builds:
# - arm64
# - ppc64le
# - s390x
# ldflags:
# - -X {{ .Env.VERSION_PKG }}.gitVersion={{ .Env.GIT_VERSION }}
# - -X {{ .Env.VERSION_PKG }}.gitCommit={{ .Env.GIT_COMMIT }}
# - -X {{ .Env.VERSION_PKG }}.gitTreeState={{ .Env.GIT_TREE_STATE }}
# - -X {{ .Env.VERSION_PKG }}.commitDate={{ .Env.COMMIT_DATE }}
dockers:
- image_templates:
- "{{ .Env.CONTROLLER_IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}-amd64"
Expand Down
36 changes: 23 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@

# Build info

GIT_COMMIT ?= $(shell git rev-parse HEAD)
VERSION_FLAGS ?= -ldflags "GitCommit=$(GIT_COMMIT)"
GIT_VERSION ?= $(shell git describe --tags --always --dirty)
GIT_STATUS ?= $(shell git status --porcelain)
GIT_TREE_STATE ?= $(shell [ -z "${GIT_STATUS}" ] && echo "clean" || echo "dirty")
COMMIT_DATE ?= $(shell git show -s --date=format:'%Y-%m-%dT%H:%M:%SZ' --format=%cd)
ORG ?= github.com/operator-framework
REPO ?= $(ORG)/catalogd
VERSION_PKG ?= $(REPO)/internal/version
CTRL_LDFLAGS ?= -ldflags="-X '$(VERSION_PKG).gitVersion=$(GIT_VERSION)'"
SERVER_LDFLAGS ?= -ldflags "-X '$(VERSION_PKG).gitVersion=$(GIT_VERSION)' -X '$(VERSION_PKG).gitCommit=$(GIT_COMMIT)' -X '$(VERSION_PKG).gitTreeState=$(GIT_TREE_STATE)' -X '$(VERSION_PKG).commitDate=$(COMMIT_DATE)'"
GO_BUILD_TAGS ?= upstream
VERSION ?= $(shell git describe --tags --always --dirty)
# Image URL to use all building/pushing controller image targets
CONTROLLER_IMG ?= quay.io/operator-framework/catalogd-controller
# Image URL to use all building/pushing apiserver image targets
Expand Down Expand Up @@ -77,12 +82,12 @@ verify: tidy fmt generate ## Verify the current code generation and lint

.PHONY: build-controller
build-controller: generate fmt vet ## Build manager binary.
CGO_ENABLED=0 GOOS=linux go build -tags $(GO_BUILD_TAGS) $(VERSION_FLAGS) -o bin/manager cmd/manager/main.go
CGO_ENABLED=0 GOOS=linux go build -tags $(GO_BUILD_TAGS) $(CTRL_LDFLAGS) -o bin/manager cmd/manager/main.go

# TODO: When the apiserver is working properly, uncomment this target:
# .PHONY: build-server
# build-server: fmt vet ## Build api-server binary.
# CGO_ENABLED=0 GOOS=linux go build -tags $(GO_BUILD_TAGS) $(VERSION_FLAGS) -o bin/apiserver cmd/apiserver/main.go
# CGO_ENABLED=0 GOOS=linux go build -tags $(GO_BUILD_TAGS) $(SERVER_LDFLAGS) -o bin/apiserver cmd/apiserver/main.go

.PHONY: run
run: generate fmt vet ## Run a controller from your host.
Expand Down Expand Up @@ -159,11 +164,16 @@ wait:
##@ Release

export ENABLE_RELEASE_PIPELINE ?= false
export GORELEASER_ARGS ?= --snapshot --clean
export CONTROLLER_IMAGE_REPO ?= $(CONTROLLER_IMG)
export GORELEASER_ARGS ?= --snapshot --clean
export CONTROLLER_IMAGE_REPO ?= $(CONTROLLER_IMG)
# TODO: When the apiserver is working properly, uncomment this line:
# export APISERVER_IMAGE_REPO ?= $(SERVER_IMG)
export IMAGE_TAG ?= $(IMG_TAG)
export IMAGE_TAG ?= $(IMG_TAG)
export VERSION_PKG ?= $(VERSION_PKG)
export GIT_VERSION ?= $(GIT_VERSION)
export GIT_COMMIT ?= $(GIT_COMMIT)
export GIT_TREE_STATE ?= $(GIT_TREE_STATE)
export COMMIT_DATE ?= $(COMMIT_DATE)
release: goreleaser ## Runs goreleaser for catalogd. By default, this will run only as a snapshot and will not publish any artifacts unless it is run with different arguments. To override the arguments, run with "GORELEASER_ARGS=...". When run as a github action from a tag, this target will publish a full release.
$(GORELEASER) $(GORELEASER_ARGS)

Expand All @@ -179,11 +189,11 @@ $(TOOLS_BIN_DIR):
mkdir -p $(TOOLS_BIN_DIR)


KUSTOMIZE_VERSION ?= v5.0.1
KIND_VERSION ?= v0.15.0
KUSTOMIZE_VERSION ?= v5.0.1
KIND_VERSION ?= v0.15.0
CONTROLLER_TOOLS_VERSION ?= v0.10.0
GORELEASER_VERSION ?= v1.16.2
ENVTEST_VERSION ?= latest
GORELEASER_VERSION ?= v1.16.2
ENVTEST_VERSION ?= latest

##@ hack/tools:

Expand Down
5 changes: 5 additions & 0 deletions cmd/apiserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ import (
"sigs.k8s.io/apiserver-runtime/pkg/builder"

// +kubebuilder:scaffold:resource-imports

corev1beta1 "github.com/operator-framework/catalogd/pkg/apis/core/v1beta1"
)

// TODO: We can't properly set the version for the APIServer using the apiserver-runtime
// package because they hardcode the version here: https://github.com/kubernetes-sigs/apiserver-runtime/blob/33c90185692756252ad3e36c5a940167d0de8f41/internal/sample-apiserver/pkg/apiserver/apiserver.go#L86-L89
// To be able to update this we would need to create a PR to fix it OR create the apiserver w/o using the apiserver-runtime tooling

func main() {
err := builder.APIServer.
// +kubebuilder:scaffold:resource-register
Expand Down
21 changes: 16 additions & 5 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"flag"
"fmt"
"os"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
Expand All @@ -31,6 +32,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

"github.com/operator-framework/catalogd/internal/version"
"github.com/operator-framework/catalogd/pkg/apis/core/v1beta1"
corecontrollers "github.com/operator-framework/catalogd/pkg/controllers/core"
"github.com/operator-framework/catalogd/pkg/profile"
Expand All @@ -50,11 +52,14 @@ func init() {
}

func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
var opmImage string
var profiling bool
var (
metricsAddr string
enableLeaderElection bool
probeAddr string
opmImage string
profiling bool
catalogdVersion bool
)
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
Expand All @@ -65,9 +70,15 @@ func main() {
Development: true,
}
flag.BoolVar(&profiling, "profiling", false, "enable profiling endpoints to allow for using pprof")
flag.BoolVar(&catalogdVersion, "version", false, "print the catalogd version and exit")
opts.BindFlags(flag.CommandLine)
flag.Parse()

if catalogdVersion {
fmt.Printf("catalogd version: %s", version.ControllerVersion())
os.Exit(0)
}

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Expand Down
33 changes: 33 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package version

import (
"fmt"
"runtime"

genericversion "k8s.io/apimachinery/pkg/version"
)

var (
gitVersion = "unknown"
gitCommit = "unknown" // sha1 from git, output of $(git rev-parse HEAD)
gitTreeState = "unknown" // state of git tree, either "clean" or "dirty"
commitDate = "unknown" // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
)

// ControllerVersion returns a version string for the controller
func ControllerVersion() string {
return gitVersion
}

// ApiserverVersion returns a version.Info object for the apiserver
func ApiserverVersion() *genericversion.Info {
return &genericversion.Info{
GitVersion: gitVersion,
GitCommit: gitCommit,
GitTreeState: gitTreeState,
BuildDate: commitDate,
GoVersion: runtime.Version(),
Compiler: runtime.Compiler,
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
}
}

0 comments on commit 70758e6

Please sign in to comment.