Skip to content

fix: set k8s.io/client-go version ldflags to fix User-Agent header - #444

Merged
cert-manager-prow[bot] merged 1 commit into
cert-manager:mainfrom
msasadzaidi:fix/user-agent-version
Mar 27, 2026
Merged

cert-manager-prow[bot] merged 1 commit into
cert-manager:mainfrom
msasadzaidi:fix/user-agent-version

Conversation

@msasadzaidi

Copy link
Copy Markdown
Contributor

Fix User-Agent header reporting v0.0.0 instead of actual version

Fixes #128

Problem

The cmctl binary reports v0.0.0 in the HTTP User-Agent header for all
requests made to the Kubernetes API server, regardless of the release version:

User-Agent: cmctl/v0.0.0 (linux/amd64) kubernetes/$Format

The clientVersion output correctly shows the version (e.g. v2.4.1), but the
User-Agent header — which is what API servers and audit logs see — is wrong.

Root Cause

The build pipeline sets ldflags for github.com/cert-manager/cert-manager/pkg/util
(which controls the clientVersion output), but does not set ldflags for
k8s.io/client-go/pkg/version,
which is what the Kubernetes REST client uses to construct the User-Agent header.

The default values in k8s.io/client-go/pkg/version/base.go are:

gitVersion   string = "v0.0.0-master+$Format:%H$"
gitCommit    string = "$Format:%H$"

These $Format placeholders are git-archive substitution tokens
that are never expanded during a normal go build or goreleaser build. The
file itself documents: "This is the fallback data used when version information
from git is not provided via go ldflags."

The current ldflags in make/00_mod.mk
only set two variables:

go_cmctl_ldflags := \
    -X github.com/cert-manager/cert-manager/pkg/util.AppVersion=$(VERSION) \
    -X github.com/cert-manager/cert-manager/pkg/util.AppGitCommit=$(GITCOMMIT)

Steps to Reproduce

1. Build locally with go install (no ldflags)

$ go install github.com/cert-manager/cmctl/[email protected]
$ cmctl version -o yaml -v 7 2>&1 | grep User-Agent
    User-Agent: cmctl/v0.0.0 (linux/amd64) kubernetes/$Format

2. Download the official release binary from the pipeline

$ curl -sL -o cmctl https://github.com/cert-manager/cmctl/releases/download/v2.4.1/cmctl_linux_amd64
$ chmod +x cmctl
$ ./cmctl version -o yaml -v 7 2>&1 | grep User-Agent
    User-Agent: cmctl/v0.0.0 (linux/amd64) kubernetes/$Format

The clientVersion.gitVersion correctly reports v2.4.1, but the User-Agent
header still shows v0.0.0 — confirming the release pipeline has the same gap.

3. Rebuild with the missing ldflags

$ go install -ldflags "\
    -X github.com/cert-manager/cert-manager/pkg/util.AppVersion=v2.4.1 \
    -X github.com/cert-manager/cert-manager/pkg/util.AppGitCommit=abc123 \
    -X k8s.io/client-go/pkg/version.gitVersion=v2.4.1 \
    -X k8s.io/client-go/pkg/version.gitCommit=abc123" \
    github.com/cert-manager/cmctl/[email protected]
$ cmctl version -o yaml -v 7 2>&1 | grep User-Agent
    User-Agent: cmctl/v2.4.1 (linux/amd64) kubernetes/abc123

4. Verified the target variables exist and are overridable

The variables gitVersion and gitCommit are declared as package-level var
(not const) in k8s.io/client-go/pkg/version/base.go,
making them valid targets for -ldflags -X.

Fix

Add the k8s.io/client-go/pkg/version ldflags to go_cmctl_ldflags in
make/00_mod.mk:

go_cmctl_ldflags := \
    -X github.com/cert-manager/cert-manager/pkg/util.AppVersion=$(VERSION) \
    -X github.com/cert-manager/cert-manager/pkg/util.AppGitCommit=$(GITCOMMIT) \
    -X k8s.io/client-go/pkg/version.gitVersion=$(VERSION) \
    -X k8s.io/client-go/pkg/version.gitCommit=$(GITCOMMIT)

This is the same pattern used by Kubernetes itself and other tools built on
client-go (e.g., kubectl, helm).

@cert-manager-prow cert-manager-prow Bot added dco-signoff: no Indicates that at least one commit in this pull request is missing the DCO sign-off message. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Mar 24, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

Hi @msasadzaidi. Thanks for your PR.

I'm waiting for a cert-manager member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@cert-manager-prow cert-manager-prow Bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Mar 24, 2026
The User-Agent header in HTTP requests to the Kubernetes API server
reports 'cmctl/v0.0.0 (linux/amd64) kubernetes/$Format' instead of
the actual release version.

The build pipeline sets ldflags for cert-manager/pkg/util (controlling
the clientVersion output) but not for k8s.io/client-go/pkg/version,
which is what the Kubernetes REST client uses to construct the
User-Agent header.

Add -X ldflags for k8s.io/client-go/pkg/version.gitVersion and
gitCommit so the User-Agent correctly reflects the release version.

Fixes cert-manager#128

Co-authored-by: Copilot <[email protected]>
Signed-off-by: Asad Zaidi <[email protected]>
@msasadzaidi
msasadzaidi force-pushed the fix/user-agent-version branch from 5d9c9d8 to 742f960 Compare March 24, 2026 16:04
@cert-manager-prow cert-manager-prow Bot added dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. and removed dco-signoff: no Indicates that at least one commit in this pull request is missing the DCO sign-off message. labels Mar 24, 2026
@erikgb
erikgb requested a review from Copilot March 27, 2026 11:05

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Updates the cmctl build ldflags so Kubernetes client-go version metadata is embedded into the binary, fixing API request User-Agent headers that currently report v0.0.0 / $Format instead of the actual cmctl release version and git commit (per #128).

Changes:

  • Extend go_cmctl_ldflags to also set k8s.io/client-go/pkg/version.gitVersion.
  • Extend go_cmctl_ldflags to also set k8s.io/client-go/pkg/version.gitCommit.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@erikgb

erikgb commented Mar 27, 2026

Copy link
Copy Markdown
Member

/ok-to-test

@cert-manager-prow cert-manager-prow Bot added ok-to-test and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Mar 27, 2026
@erikgb

erikgb commented Mar 27, 2026

Copy link
Copy Markdown
Member

@erikgb erikgb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks a lot @msasadzaidi! And thank you for attending our ContribFest at KubeCon EU 2026. This was a really nice fix! We hope to see more future contributions to cert-manager projects from you! 🚀

/lgtm
/approve

@cert-manager-prow cert-manager-prow Bot added the lgtm Indicates that a PR is ready to be merged. label Mar 27, 2026
@cert-manager-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: erikgb

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@cert-manager-prow cert-manager-prow Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 27, 2026
@cert-manager-prow
cert-manager-prow Bot merged commit e264148 into cert-manager:main Mar 27, 2026
9 checks passed
@maelvls

maelvls commented Mar 27, 2026

Copy link
Copy Markdown
Member

Excellent PR. I was able to test this by running mitmproxy, but the easiest way to see that this PR works is to run the command that was shown in the PR description:

$ make _bin/bin/cmctl
$ _bin/bin/cmctl version -o yaml -v 7 2>&1 | grep User-Agent
        User-Agent: cmctl/v2.4.1 (darwin/arm64) kubernetes/742f960
        User-Agent: cmctl/v2.4.1 (darwin/arm64) kubernetes/742f960
        User-Agent: cmctl/v2.4.1 (darwin/arm64) kubernetes/742f960

Very impressed, well done!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. dco-signoff: yes Indicates that all commits in the pull request have the valid DCO sign-off message. lgtm Indicates that a PR is ready to be merged. ok-to-test size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cmctl always reports v0.0.0 in the user-agent header

4 participants