Skip to content
Closed
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
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,12 @@ update-cni-manifests: update-calico-cni update-cilium-cni update-flannel-cni upd
.PHONY: build
build: generate ## Build manager binary.
echo "Git commit hash: ${GIT_COMMIT_HASH}"
CGO_ENABLED=0 go build -ldflags "-X main.gitCommitHash=${GIT_COMMIT_HASH}" -o bin/manager main.go
CGO_ENABLED=0 go build -ldflags "-X main.gitCommitHash=${GIT_COMMIT_HASH} -X github.com/nutanix-cloud-native/cluster-api-provider-nutanix/pkg/version.GitCommit=${GIT_COMMIT_HASH}" -o bin/manager main.go

.PHONY: build-e2e
build-e2e: generate ## Build e2e binary.
echo "Git commit hash: ${GIT_COMMIT_HASH}"
CGO_ENABLED=0 go build -ldflags "-X main.gitCommitHash=${GIT_COMMIT_HASH}" -tags=e2e -o bin/e2e test/e2e/*.go
CGO_ENABLED=0 go build -ldflags "-X main.gitCommitHash=${GIT_COMMIT_HASH} -X github.com/nutanix-cloud-native/cluster-api-provider-nutanix/pkg/version.GitCommit=${GIT_COMMIT_HASH}" -tags=e2e -o bin/e2e test/e2e/*.go

.PHONY: run
run: manifests generate ## Run a controller from your host.
Expand All @@ -256,12 +256,12 @@ run: manifests generate ## Run a controller from your host.
docker-build: ## Build docker image with the manager.
$(select_container_engine)
echo "Git commit hash: ${GIT_COMMIT_HASH}"
DOCKER_HOST=$(DOCKER_SOCKET) KO_DOCKER_REPO=ko.local GOFLAGS="-buildvcs=false -ldflags=-X=main.gitCommitHash=${GIT_COMMIT_HASH}" ko build -B --platform=${PLATFORMS} -t ${IMG_TAG} .
DOCKER_HOST=$(DOCKER_SOCKET) KO_DOCKER_REPO=ko.local GOFLAGS="-buildvcs=false -ldflags=-X=main.gitCommitHash=${GIT_COMMIT_HASH} -X=github.com/nutanix-cloud-native/cluster-api-provider-nutanix/pkg/version.GitCommit=${GIT_COMMIT_HASH}" ko build -B --platform=${PLATFORMS} -t ${IMG_TAG} .

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
$(select_container_engine)
DOCKER_HOST=$(DOCKER_SOCKET) KO_DOCKER_REPO=${IMG_REPO} GOFLAGS="-buildvcs=false -ldflags=-X=main.gitCommitHash=${GIT_COMMIT_HASH}" ko build --bare --platform=${PLATFORMS} -t ${IMG_TAG} .
DOCKER_HOST=$(DOCKER_SOCKET) KO_DOCKER_REPO=${IMG_REPO} GOFLAGS="-buildvcs=false -ldflags=-X=main.gitCommitHash=${GIT_COMMIT_HASH} -X=github.com/nutanix-cloud-native/cluster-api-provider-nutanix/pkg/version.GitCommit=${GIT_COMMIT_HASH}" ko build --bare --platform=${PLATFORMS} -t ${IMG_TAG} .

.PHONY: docker-push-kind
docker-push-kind: ## Make docker image available to kind cluster.
Expand Down Expand Up @@ -357,7 +357,7 @@ cluster-templates: ## Generate cluster templates for all flavors
docker-build-e2e: ## Build docker image with the manager with e2e tag.
$(select_container_engine)
echo "Git commit hash: ${GIT_COMMIT_HASH}"
DOCKER_HOST=$(DOCKER_SOCKET) KO_DOCKER_REPO=ko.local GOFLAGS="-buildvcs=false -ldflags=-X=main.gitCommitHash=${GIT_COMMIT_HASH}" ko build -B --platform=${PLATFORMS_E2E} -t ${IMG_TAG} .
DOCKER_HOST=$(DOCKER_SOCKET) KO_DOCKER_REPO=ko.local GOFLAGS="-buildvcs=false -ldflags=-X=main.gitCommitHash=${GIT_COMMIT_HASH} -X=github.com/nutanix-cloud-native/cluster-api-provider-nutanix/pkg/version.GitCommit=${GIT_COMMIT_HASH}" ko build -B --platform=${PLATFORMS_E2E} -t ${IMG_TAG} .
docker tag ko.local/cluster-api-provider-nutanix:${IMG_TAG} ${IMG_REPO}:${IMG_TAG}

.PHONY: prepare-local-clusterctl
Expand Down
3 changes: 2 additions & 1 deletion controllers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (
infrav1 "github.com/nutanix-cloud-native/cluster-api-provider-nutanix/api/v1beta1"
nutanixclient "github.com/nutanix-cloud-native/cluster-api-provider-nutanix/pkg/client"
nctx "github.com/nutanix-cloud-native/cluster-api-provider-nutanix/pkg/context"
"github.com/nutanix-cloud-native/cluster-api-provider-nutanix/pkg/version"
"github.com/nutanix-cloud-native/prism-go-client/converged"
v4Converged "github.com/nutanix-cloud-native/prism-go-client/converged/v4"
prismclientv3 "github.com/nutanix-cloud-native/prism-go-client/v3"
Expand Down Expand Up @@ -2472,7 +2473,7 @@ func getPrismCentralClientForCluster(ctx context.Context, cluster *infrav1.Nutan
v3Client, err := nutanixclient.NutanixClientCache.GetOrCreate(&nutanixclient.CacheParams{
NutanixCluster: cluster,
PrismManagementEndpoint: managementEndpoint,
})
}, prismclientv3.WithUserAgent(version.UserAgent()))
if err != nil {
log.Error(err, "error occurred while getting nutanix prism v3 Client from cache")
v1beta1conditions.MarkFalse(cluster, infrav1.PrismCentralClientCondition, infrav1.PrismCentralClientInitializationFailed, capiv1beta1.ConditionSeverityError, "%s", err.Error())
Expand Down
45 changes: 45 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copyright 2025 Nutanix

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"

// GitCommit is the git commit hash of the build, set via ldflags
// Example: go build -ldflags "-X github.com/nutanix-cloud-native/cluster-api-provider-nutanix/pkg/version.GitCommit=<hash>"
var GitCommit = "dev"

// UserAgent returns the User-Agent string for CAPX API requests
// Format: capx/<version> or cluster-api-provider-nutanix/<version>
// Uses GitCommit if set, otherwise "dev"
func UserAgent() string {
version := GitCommit
if version == "" {
version = "dev"
}
// Use short component name "capx" as used in repo (CAPX)
// Full name would be "cluster-api-provider-nutanix" but short is more common in logs
return fmt.Sprintf("capx/%s", version)
}

// UserAgentWithComponent returns User-Agent with custom component name
func UserAgentWithComponent(component string) string {
version := GitCommit
if version == "" {
version = "dev"
}
return fmt.Sprintf("%s/%s", component, version)
}
45 changes: 45 additions & 0 deletions pkg/version/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package version

import (
"strings"
"testing"
)

func TestUserAgent(t *testing.T) {
// Default should be capx/dev when GitCommit is dev
original := GitCommit
defer func() { GitCommit = original }()

GitCommit = "dev"
ua := UserAgent()
if ua != "capx/dev" {
t.Fatalf("expected capx/dev, got %s", ua)
}

GitCommit = "abc123"
ua = UserAgent()
if ua != "capx/abc123" {
t.Fatalf("expected capx/abc123, got %s", ua)
}

GitCommit = ""
ua = UserAgent()
if ua != "capx/dev" {
t.Fatalf("expected capx/dev for empty, got %s", ua)
}

if !strings.HasPrefix(ua, "capx/") {
t.Fatalf("UserAgent should start with capx/, got %s", ua)
}
}

func TestUserAgentWithComponent(t *testing.T) {
original := GitCommit
defer func() { GitCommit = original }()
GitCommit = "v1.10.0"

ua := UserAgentWithComponent("cluster-api-provider-nutanix")
if ua != "cluster-api-provider-nutanix/v1.10.0" {
t.Fatalf("expected cluster-api-provider-nutanix/v1.10.0, got %s", ua)
}
}
Loading