diff --git a/Makefile b/Makefile index c3e128c..3d55ff0 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,12 @@ else GOBIN=$(shell go env GOBIN) endif +# version settings +RELEASE?=$(shell git rev-parse HEAD) +COMMIT?=$(shell git rev-parse HEAD) +BUILD_DATE?=$(shell date -u +'%Y-%m-%dT%H:%M:%SZ') +PROJECT?=github.com/falcosecurity/k8s-metacollector + # Setting SHELL to bash allows bash commands to be executed by recipes. # Options are set to exit when a recipe line exits non-zero or a piped command fails. SHELL = /usr/bin/env bash -o pipefail @@ -58,7 +64,11 @@ test: manifests generate fmt vet envtest ## Run tests. .PHONY: build build: manifests generate fmt vet ## Build manager binary. - go build -o bin/manager main.go + go build -ldflags \ + "-X '${PROJECT}/pkg/version.semVersion=${RELEASE}' \ + -X '${PROJECT}/pkg/version.gitCommit=${COMMIT}' \ + -X '${PROJECT}/pkg/version.buildDate=${BUILD_DATE}'" \ + -o manager . .PHONY: run run: manifests generate fmt vet ## Run a controller from your host. diff --git a/main.go b/main.go index c24269b..f1784d1 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ package main import ( "context" "flag" + "fmt" "os" "github.com/falcosecurity/k8s-metacollector/broker" @@ -25,6 +26,7 @@ import ( "github.com/falcosecurity/k8s-metacollector/pkg/events" "github.com/falcosecurity/k8s-metacollector/pkg/resource" "github.com/falcosecurity/k8s-metacollector/pkg/subscriber" + "github.com/falcosecurity/k8s-metacollector/pkg/version" v1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" discoveryv1 "k8s.io/api/discovery/v1" @@ -59,12 +61,14 @@ func main() { var brokerAddr string var certFilePath string var keyFilePath string + var versionFlag 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.StringVar(&brokerAddr, "broker-bind-address", ":45000", "The address the broker endpoint binds to.") flag.StringVar(&certFilePath, "broker-server-cert", "", "Cert file path for grpc server.") flag.StringVar(&keyFilePath, "broker-server-key", "", "Key file path for grpc server.") + flag.BoolVar(&versionFlag, "version", false, "Print version.") opts := zap.Options{ Development: false, @@ -72,6 +76,11 @@ func main() { opts.BindFlags(flag.CommandLine) flag.Parse() + if versionFlag { + fmt.Println(version.Version()) + os.Exit(0) + } + ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) setupLog := ctrl.Log.WithName("setup") diff --git a/pkg/version/doc.go b/pkg/version/doc.go new file mode 100644 index 0000000..01c1bf2 --- /dev/null +++ b/pkg/version/doc.go @@ -0,0 +1,17 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2023 The Falco Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Package version hold the logic for the version. +package version diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 0000000..ac3d345 --- /dev/null +++ b/pkg/version/version.go @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2023 The Falco Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package version + +import ( + "fmt" + "runtime" +) + +var ( + // Semantic version that refers to ghe version (git tag). + // For prerelease versions, the build metadata on the + // semantic version is a git hash some as the gitCommit. + semVersion = "v0.0.0-master" + + // sha1 from git, output of $(git rev-parse HEAD). + gitCommit = "" + + // build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ'). + buildDate = "1970-01-01T00:00:00Z" +) + +// Version returns the version. +func Version() string { + return fmt.Sprintf("semVersion %s, gitCommit %s, buildDate %s, goVersion %s, compiler %s, platform %s/%s", + semVersion, gitCommit, buildDate, runtime.Version(), runtime.Compiler, runtime.GOOS, runtime.GOARCH) +}