Skip to content

fix(clustertest): resolve version from git when env var is empty#322

Open
tsivaprasad wants to merge 5 commits intomainfrom
PLAT-533-clustertest-image-build-fails-with-invalid-tag-when-control-plane-version-is-empty
Open

fix(clustertest): resolve version from git when env var is empty#322
tsivaprasad wants to merge 5 commits intomainfrom
PLAT-533-clustertest-image-build-fails-with-invalid-tag-when-control-plane-version-is-empty

Conversation

@tsivaprasad
Copy link
Copy Markdown
Contributor

Summary

This PR fixes a CI failure where cluster tests crash immediately on image build
because CONTROL_PLANE_VERSION is empty when docker buildx bake runs,
producing an invalid image tag (repo:).

Changes

  • Add resolveVersion() in main_test.go that reads CONTROL_PLANE_VERSION
    from the environment and falls back to git describe --tags --abbrev=0 --match 'v*' when the var is absent or empty
  • Update buildImage() in utils_test.go to strip any inherited
    CONTROL_PLANE_VERSION= (including empty) from the child process
    environment and replace it with the resolved value, preventing an
    empty string from blocking make's ?= default

Testing

  • Verified by the CI run that triggered this fix (cluster tests were
    previously failing with invalid tag "172.17.0.1:5000/control-plane:": invalid reference format)

Checklist

  • Tests added

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 28, 2026

📝 Walkthrough

Walkthrough

Refactored image tag initialization and version resolution logic. Moved -image-tag default computation from flag definition to runtime in TestMain with improved error handling. Updated version resolution in common.mk to use unconditional git tag fetching with explicit fallback handling.

Changes

Cohort / File(s) Summary
Test Flag Initialization
clustertest/main_test.go
Changed -image-tag flag default from computed value to empty string, then added runtime logic in TestMain to detect unset tags, read CONTROL_PLANE_VERSION, construct the default tag, and conditionally trigger image build with improved skip-build handling.
Version Resolution
common.mk
Updated CONTROL_PLANE_VERSION computation to unconditionally run git fetch --tags and use git describe with CHANGIE_LATEST pattern matching, adding explicit fallback to echo the version when git describe fails instead of relying on previous git fetch behavior.

Poem

🐰 With whiskers twitching and a hop so grand,
We've tidied up the version-tag land,
Git fetches and describes in patterns true,
While TestMain awaits each flag anew—
No more computed defaults in the dawn,
Just clean deferred logic, stronger and drawn!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main fix: resolving the version from git when an environment variable is empty, which directly addresses the root cause of the CI failure.
Description check ✅ Passed The description covers Summary, Changes, and Testing sections with concrete details. However, the Checklist is incomplete: it only marks 'Tests added' but omits several required items (Documentation, Issue linking, Changelog entry, Breaking changes).

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch PLAT-533-clustertest-image-build-fails-with-invalid-tag-when-control-plane-version-is-empty
⚔️ Resolve merge conflicts
  • Resolve merge conflict in branch PLAT-533-clustertest-image-build-fails-with-invalid-tag-when-control-plane-version-is-empty

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
clustertest/main_test.go (1)

28-37: Consider failing early when version cannot be resolved.

If the environment variable is unset and git describe also fails (e.g., no matching tags, shallow clone, or git unavailable), resolveVersion() returns an empty string. This will produce an invalid image tag like 127.0.0.1:5000/control-plane: and the build will fail with the same cryptic error the PR aims to fix.

A clear early failure would improve debuggability:

Proposed improvement
 func resolveVersion() string {
 	if v := os.Getenv("CONTROL_PLANE_VERSION"); v != "" {
 		return v
 	}
 	out, err := exec.Command("git", "-C", "..", "describe", "--tags", "--abbrev=0", "--match", "v*").Output()
 	if err != nil {
-		return ""
+		log.Printf("warning: unable to resolve CONTROL_PLANE_VERSION from git: %v", err)
+		return ""
 	}
 	return strings.TrimSpace(string(out))
 }

Or in TestMain, validate before use:

version := resolveVersion()
if version == "" {
    log.Fatal("CONTROL_PLANE_VERSION is empty and git describe failed; set the env var or ensure tags exist")
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@clustertest/main_test.go` around lines 28 - 37, resolveVersion() can return
an empty string (when env var unset and `git describe` fails) which yields an
invalid image tag; update the test bootstrap to validate the version early: in
TestMain (or wherever resolveVersion() is consumed) call version :=
resolveVersion() and if version == "" call log.Fatalf (or t.Fatalf) with a clear
message telling the user to set CONTROL_PLANE_VERSION or ensure tags/git are
available, so the process fails immediately with a helpful error instead of
producing an invalid image tag.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@clustertest/main_test.go`:
- Around line 28-37: resolveVersion() can return an empty string (when env var
unset and `git describe` fails) which yields an invalid image tag; update the
test bootstrap to validate the version early: in TestMain (or wherever
resolveVersion() is consumed) call version := resolveVersion() and if version ==
"" call log.Fatalf (or t.Fatalf) with a clear message telling the user to set
CONTROL_PLANE_VERSION or ensure tags/git are available, so the process fails
immediately with a helpful error instead of producing an invalid image tag.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 776d6125-4c4f-4eae-beda-251e57696594

📥 Commits

Reviewing files that changed from the base of the PR and between 236bb0a and 792e6b1.

📒 Files selected for processing (2)
  • clustertest/main_test.go
  • clustertest/utils_test.go

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@clustertest/main_test.go`:
- Around line 29-31: When reading CONTROL_PLANE_VERSION via
os.Getenv("CONTROL_PLANE_VERSION") (variable v), trim whitespace (e.g.,
strings.TrimSpace) and treat the value as unset if the trimmed string is empty;
update the branch that currently returns v to instead trim v, check if the
trimmed value is non-empty, and only then return it so whitespace-only env
values won't be treated as a valid image tag.
- Around line 40-44: The code currently calls resolveVersion() and fatals if
empty before flags are parsed, preventing runs that supply -image-tag from
working; change the logic so resolveVersion() is only required when no image-tag
flag is provided: parse flags first (or check the image-tag flag), and if
imageTag flag is empty then call resolveVersion() and fatal only if it returns
empty; set defaultImageTag using the resolved version when needed (referencing
resolveVersion(), defaultImageTag, and the image-tag flag/variable).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a1007faf-b9ab-4e70-b7b4-d4fee0ee852c

📥 Commits

Reviewing files that changed from the base of the PR and between 792e6b1 and 6ccc04b.

📒 Files selected for processing (1)
  • clustertest/main_test.go

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (1)
clustertest/main_test.go (1)

35-37: ⚠️ Potential issue | 🟡 Minor

Treat whitespace-only CONTROL_PLANE_VERSION as unset.

At Line 35, the env var is used without trimming. Values like " " bypass emptiness intent and can still yield invalid image tags.

Suggested fix
 import (
 	"flag"
 	"log"
 	"os"
+	"strings"
 	"testing"
@@
-		version := os.Getenv("CONTROL_PLANE_VERSION")
+		version := strings.TrimSpace(os.Getenv("CONTROL_PLANE_VERSION"))
 		if version == "" {
 			log.Fatal("CONTROL_PLANE_VERSION is not set; ensure common.mk version resolution succeeded or pass -image-tag explicitly")
 		}
#!/bin/bash
rg -n -C2 'CONTROL_PLANE_VERSION|TrimSpace' clustertest/main_test.go
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@clustertest/main_test.go` around lines 35 - 37, The code reads version :=
os.Getenv("CONTROL_PLANE_VERSION") and treats any non-empty string (including
whitespace-only) as valid; change to trim whitespace and treat whitespace-only
as unset by using strings.TrimSpace on the os.Getenv result before the empty
check (i.e., replace direct use of os.Getenv("CONTROL_PLANE_VERSION") with a
trimmed value) so the subsequent if version == "" correctly catches values like
"   ". Ensure you import the strings package if not already used.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@common.mk`:
- Around line 12-18: The CONTROL_PLANE_VERSION assignment can evaluate to empty
when both git describe (matching $(CHANGIE_LATEST)) and $(CHANGIE_LATEST) are
empty; update the deferred-evaluation fallback so it tries three tiers: first
git describe with the pattern $(CHANGIE_LATEST)*, then git describe with a
generic tag match like v* (to pick the latest semver tag), and finally a
hardcoded safe default such as v0.0.0-dev; implement this inside the same
deferred-simple-variable-expansion expression that sets CONTROL_PLANE_VERSION so
the shell pipeline attempts the first git describe, if that fails attempts a
second git describe --match 'v*', and if that fails echoes 'v0.0.0-dev',
ensuring CONTROL_PLANE_VERSION is never empty.

---

Duplicate comments:
In `@clustertest/main_test.go`:
- Around line 35-37: The code reads version :=
os.Getenv("CONTROL_PLANE_VERSION") and treats any non-empty string (including
whitespace-only) as valid; change to trim whitespace and treat whitespace-only
as unset by using strings.TrimSpace on the os.Getenv result before the empty
check (i.e., replace direct use of os.Getenv("CONTROL_PLANE_VERSION") with a
trimmed value) so the subsequent if version == "" correctly catches values like
"   ". Ensure you import the strings package if not already used.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0b4e2c67-dd85-435c-9973-b019138f87de

📥 Commits

Reviewing files that changed from the base of the PR and between 7ba621d and fcef96a.

📒 Files selected for processing (2)
  • clustertest/main_test.go
  • common.mk

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants