Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add go ldflags for version info injection #313

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .ko.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
14 changes: 11 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the fact the KO_DOCKER_REPO wasn't here before a bug?

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

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/resourcegraphdefinition/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
20 changes: 8 additions & 12 deletions pkg/metadata/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm being a super pedant here but:
https://google.github.io/styleguide/go/decisions#initialisms

KRO vs Kro.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This applies elsewhere in this file too.


InstanceIDLabel = LabelKroPrefix + "instance-id"
InstanceLabel = LabelKroPrefix + "instance-name"
Expand Down Expand Up @@ -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(),
}
}

Expand All @@ -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 {
Copy link
Member

@matthchr matthchr Feb 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the experience when running tests locally (in a KIND cluster, for example).

Are the labels set correctly? I looked at the makefile and I think they will be?
What about in envtest (less sure they will be there)?

Does it make sense to have this still be settable on the labeller and then have the envtest context specifically pass "something"? Or maybe we need envtest contexts to just set pkg.Version/etc correctly?

return map[string]string{
OwnedLabel: "true",
KroVersionLabel: kroVersion,
ControllerPodIDLabel: controllerPodID,
OwnedLabel: "true",
KroVersionLabel: pkg.Version,
}
}

Expand Down
24 changes: 24 additions & 0 deletions pkg/version.go
Original file line number Diff line number Diff line change
@@ -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)
)