diff --git a/.goreleaser.yaml b/.goreleaser.yaml index dca0db03..c37f2213 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -20,6 +20,9 @@ builds: - "amd64" - "arm64" - "s390x" + ldflags: + - -w -s # strip debug info and symbol table + - -X "github.com/rancher/k3k/pkg/buildinfo.Version={{ .Tag }}" - id: k3k-kubelet main: ./k3k-kubelet @@ -32,7 +35,10 @@ builds: - "amd64" - "arm64" - "s390x" - + ldflags: + - -w -s # strip debug info and symbol table + - -X "github.com/rancher/k3k/pkg/buildinfo.Version={{ .Tag }}" + - id: k3kcli main: ./cli binary: k3kcli @@ -41,6 +47,9 @@ builds: goarch: - "amd64" - "arm64" + ldflags: + - -w -s # strip debug info and symbol table + - -X "github.com/rancher/k3k/pkg/buildinfo.Version={{ .Tag }}" archives: - format: binary diff --git a/cli/main.go b/cli/main.go index fba1a568..6fc7c5e7 100644 --- a/cli/main.go +++ b/cli/main.go @@ -1,28 +1,28 @@ package main import ( + "fmt" "os" "github.com/rancher/k3k/cli/cmds" "github.com/rancher/k3k/cli/cmds/cluster" "github.com/rancher/k3k/cli/cmds/kubeconfig" + "github.com/rancher/k3k/pkg/buildinfo" "github.com/sirupsen/logrus" "github.com/urfave/cli" ) -const ( - program = "k3kcli" - version = "dev" - gitCommit = "HEAD" -) - func main() { app := cmds.NewApp() + app.Version = buildinfo.Version + cli.VersionPrinter = func(cCtx *cli.Context) { + fmt.Println("k3kcli Version: " + buildinfo.Version) + } + app.Commands = []cli.Command{ cluster.NewCommand(), kubeconfig.NewCommand(), } - app.Version = version + " (" + gitCommit + ")" if err := app.Run(os.Args); err != nil { logrus.Fatal(err) diff --git a/main.go b/main.go index b0911f4e..f3bb6029 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "github.com/go-logr/zapr" "github.com/rancher/k3k/cli/cmds" "github.com/rancher/k3k/pkg/apis/k3k.io/v1alpha1" + "github.com/rancher/k3k/pkg/buildinfo" "github.com/rancher/k3k/pkg/controller/cluster" "github.com/rancher/k3k/pkg/controller/clusterset" "github.com/rancher/k3k/pkg/log" @@ -22,12 +23,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/manager" ) -const ( - program = "k3k" - version = "dev" - gitCommit = "HEAD" -) - var ( scheme = runtime.NewScheme() clusterCIDR string @@ -73,7 +68,7 @@ func main() { app := cmds.NewApp() app.Flags = flags app.Action = run - app.Version = version + " (" + gitCommit + ")" + app.Version = buildinfo.Version app.Before = func(clx *cli.Context) error { logger = log.New(debug) return nil @@ -81,12 +76,13 @@ func main() { if err := app.Run(os.Args); err != nil { logger.Fatalw("failed to run k3k controller", zap.Error(err)) } - } func run(clx *cli.Context) error { ctx := context.Background() + logger.Info("Starting k3k - Version: " + buildinfo.Version) + restConfig, err := clientcmd.BuildConfigFromFlags("", kubeconfig) if err != nil { return fmt.Errorf("failed to create config from kubeconfig file: %v", err) diff --git a/pkg/buildinfo/buildinfo.go b/pkg/buildinfo/buildinfo.go new file mode 100644 index 00000000..83b0ae5b --- /dev/null +++ b/pkg/buildinfo/buildinfo.go @@ -0,0 +1,3 @@ +package buildinfo + +var Version = "dev" diff --git a/scripts/build b/scripts/build index 721cc569..7b61f48b 100755 --- a/scripts/build +++ b/scripts/build @@ -4,15 +4,21 @@ set -e pipefail TAG=$(git describe --tag --always) +if [ -n "$(git status --porcelain --untracked-files=no)" ]; then + TAG="${TAG}-dirty" +fi + +LDFLAGS="-X \"github.com/rancher/k3k/pkg/buildinfo.Version=${TAG}\"" + echo "Building k3k..." echo "Current TAG: ${TAG}" export CGO_ENABLED=0 -GOOS=linux GOARCH=amd64 go build -o bin/k3k -GOOS=linux GOARCH=amd64 go build -o bin/k3k-kubelet ./k3k-kubelet +GOOS=linux GOARCH=amd64 go build -ldflags="${LDFLAGS}" -o bin/k3k +GOOS=linux GOARCH=amd64 go build -ldflags="${LDFLAGS}" -o bin/k3k-kubelet ./k3k-kubelet # build the cli for the local OS and ARCH -go build -o bin/k3kcli ./cli +go build -ldflags="${LDFLAGS}" -o bin/k3kcli ./cli docker build -f package/Dockerfile -t rancher/k3k:dev -t rancher/k3k:${TAG} . docker build -f package/Dockerfile.kubelet -t rancher/k3k-kubelet:dev -t rancher/k3k-kubelet:${TAG} .