diff --git a/.ko.yaml b/.ko.yaml index d8d3bd78..e90af2a2 100644 --- a/.ko.yaml +++ b/.ko.yaml @@ -7,3 +7,6 @@ defaultPlatforms: defaultLdflags: - -s - -w + - -X github.com/kro-run/kro/pkg.Version={{.Env.RELEASE_VERSION}} + - -X github.com/kro-run/kro/pkg.GitCommit={{.Env.GIT_COMMIT}} + - -X github.com/kro-run/kro/pkg.BuildDate={{.Env.BUILD_DATE}} diff --git a/Makefile b/Makefile index b87899bf..e6a48765 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ -RELEASE_VERSION ?= dev-$(shell git rev-parse --short HEAD) +GIT_COMMIT ?= $(shell git rev-parse --short HEAD) +BUILD_DATE ?= $(shell date -u +'%Y-%m-%dT%H:%M:%SZ') +RELEASE_VERSION ?= dev-$(GIT_COMMIT) OCI_REPO ?= ghcr.io/kro-run/kro HELM_IMAGE ?= ${OCI_REPO} @@ -9,6 +11,10 @@ KOCACHE ?= ~/.ko KO_PUSH ?= true export KIND_CLUSTER_NAME ?= kro +LDFLAGS ?= -X github.com/kro-run/kro/pkg.Version=$(RELEASE_VERSION) \ + -X github.com/kro-run/kro/pkg.GitCommit=$(GIT_COMMIT) \ + -X github.com/kro-run/kro/pkg.BuildDate=$(BUILD_DATE) + WITH_GOFLAGS = GOFLAGS="$(GOFLAGS)" HELM_DIR = ./helm @@ -106,7 +112,7 @@ lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes .PHONY: build build: manifests generate fmt vet ## Build controller binary. - go build -o bin/controller ./cmd/controller/main.go + go build -ldflags=${LDFLAGS} -o bin/controller ./cmd/controller/main.go .PHONY: run run: manifests generate fmt vet ## Run a controller from your host. @@ -188,13 +194,15 @@ $(CONTROLLER_GEN): $(LOCALBIN) build-image: ko ## Build the kro controller images using ko build echo "Building kro image $(RELEASE_VERSION).." $(WITH_GOFLAGS) KOCACHE=$(KOCACHE) KO_DOCKER_REPO=$(KO_DOCKER_REPO) \ + GIT_COMMIT=$(GIT_COMMIT) RELEASE_VERSION=$(RELEASE_VERSION) BUILD_DATE=$(BUILD_DATE) \ $(KO) build --bare github.com/kro-run/kro/cmd/controller \ --local\ --push=false --tags ${RELEASE_VERSION} --sbom=none .PHONY: publish publish-image: ko ## Publish the kro controller images to ghcr.io - $(WITH_GOFLAGS) KOCACHE=$(KOCACHE) \ + $(WITH_GOFLAGS) KOCACHE=$(KOCACHE) KO_DOCKER_REPO=$(KO_DOCKER_REPO) \ + GIT_COMMIT=$(GIT_COMMIT) RELEASE_VERSION=$(RELEASE_VERSION) BUILD_DATE=$(BUILD_DATE) \ $(KO) publish --bare github.com/kro-run/kro/cmd/controller \ --tags ${RELEASE_VERSION} --sbom=none diff --git a/pkg/controller/resourcegraphdefinition/controller.go b/pkg/controller/resourcegraphdefinition/controller.go index 6fd9dda4..3eb405e8 100644 --- a/pkg/controller/resourcegraphdefinition/controller.go +++ b/pkg/controller/resourcegraphdefinition/controller.go @@ -72,7 +72,7 @@ func NewResourceGraphDefinitionReconciler( allowCRDDeletion: allowCRDDeletion, crdManager: crdWrapper, dynamicController: dynamicController, - metadataLabeler: metadata.NewKroMetaLabeler("0.2.1", "kro-pod"), + metadataLabeler: metadata.NewKroMetaLabeler(), rgBuilder: builder, } } diff --git a/pkg/metadata/labels.go b/pkg/metadata/labels.go index 34b106ef..7a9195e9 100644 --- a/pkg/metadata/labels.go +++ b/pkg/metadata/labels.go @@ -18,6 +18,7 @@ import ( "fmt" "github.com/kro-run/kro/api/v1alpha1" + "github.com/kro-run/kro/pkg" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -29,9 +30,8 @@ const ( const ( NodeIDLabel = LabelKroPrefix + "node-id" - OwnedLabel = LabelKroPrefix + "owned" - KroVersionLabel = LabelKroPrefix + "kro-version" - ControllerPodIDLabel = LabelKroPrefix + "controller-pod-id" + OwnedLabel = LabelKroPrefix + "owned" + KroVersionLabel = LabelKroPrefix + "kro-version" InstanceIDLabel = LabelKroPrefix + "instance-id" InstanceLabel = LabelKroPrefix + "instance-name" @@ -115,8 +115,8 @@ func (gl GenericLabeler) Copy() map[string]string { // ResourceGraphDefinitionLabel and ResourceGraphDefinitionIDLabel labels on a resource. func NewResourceGraphDefinitionLabeler(rgMeta metav1.Object) GenericLabeler { return map[string]string{ - ResourceGraphDefinitionIDLabel: string(rgMeta.GetUID()), - ResourceGraphDefinitionNameLabel: rgMeta.GetName(), + ResourceGraphDefinitionIDLabel: string(rgMeta.GetUID()), + ResourceGraphDefinitionNameLabel: rgMeta.GetName(), } } @@ -133,14 +133,10 @@ func NewInstanceLabeler(instanceMeta metav1.Object) GenericLabeler { // NewKroMetaLabeler returns a new labeler that sets the OwnedLabel, // KroVersion, and ControllerPodID labels on a resource. -func NewKroMetaLabeler( - kroVersion string, - controllerPodID string, -) GenericLabeler { +func NewKroMetaLabeler() GenericLabeler { return map[string]string{ - OwnedLabel: "true", - KroVersionLabel: kroVersion, - ControllerPodIDLabel: controllerPodID, + OwnedLabel: "true", + KroVersionLabel: pkg.Version, } } diff --git a/pkg/version.go b/pkg/version.go new file mode 100644 index 00000000..a909fe4b --- /dev/null +++ b/pkg/version.go @@ -0,0 +1,24 @@ +// Copyright 2025 The Kube Resource Orchestrator Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"). You may +// not use this file except in compliance with the License. A copy of the +// License is located at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// or in the "license" file accompanying this file. This file 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 pkg + +// These variables are populated by the go compiler using -ldflags +var ( + // Version is the version of the kro binary. + Version string // -X github.com/your/repo/pkg.Version=$(VERSION) + // GitCommit is the git commit that was compiled. + GitCommit string // -X github.com/your/repo/pkg.GitCommit=$(GIT_COMMIT) + // BuildDate is the date that the kro binary was built. + BuildDate string // -X github.com/your/repo/pkg.BuildDate=$(BUILD_DATE) +)