Skip to content

Commit

Permalink
new(version): add new version flag
Browse files Browse the repository at this point in the history
Signed-off-by: Aldo Lacuku <[email protected]>
  • Loading branch information
alacuku authored and poiana committed Nov 20, 2023
1 parent f3c7dac commit 0ab54aa
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ package main
import (
"context"
"flag"
"fmt"
"os"

"github.com/falcosecurity/k8s-metacollector/broker"
"github.com/falcosecurity/k8s-metacollector/collectors"
"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"
Expand Down Expand Up @@ -59,19 +61,26 @@ 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,
}
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")
Expand Down
17 changes: 17 additions & 0 deletions pkg/version/doc.go
Original file line number Diff line number Diff line change
@@ -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
40 changes: 40 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 0ab54aa

Please sign in to comment.