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

SKIP-1159 - A better labeler #557

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open

SKIP-1159 - A better labeler #557

wants to merge 8 commits into from

Conversation

SnorreSelmer
Copy link
Contributor

@SnorreSelmer SnorreSelmer commented Nov 6, 2024

Redid logic to make it more robust

Summary by CodeRabbit

  • New Features

    • Enhanced image version handling with a user-friendly format for deployment, job, and service labels.
    • Introduced a function to check string patterns against regular expressions.
    • Added a constant for maximum label value length.
  • Bug Fixes

    • Improved logic for handling various image version strings, including defaulting to "latest".
  • Tests

    • Consolidated multiple test cases for image version scenarios into a single, more efficient test function.
    • Updated test configurations to reflect changes in version labeling.

Copy link
Contributor

coderabbitai bot commented Nov 6, 2024

Walkthrough

The changes involve modifications to the helpers.go and helpers_test.go files within the resourceutils package. A new constant LabelValueMaxLength is introduced, along with a new function matchesRegex. The GetImageVersion function is significantly updated to include checks for version strings and to enforce length limitations. In the test file, the test cases for GetImageVersion have been consolidated into a single, more organized test function using a table-driven approach. Additionally, the Generate functions in the deployment, job, and service packages have been updated to utilize the new HumanReadableVersion function for setting version labels.

Changes

File Path Change Summary
pkg/resourcegenerator/resourceutils/helpers.go Added constant LabelValueMaxLength, added function matchesRegex, and modified GetImageVersion to HumanReadableVersion.
pkg/resourcegenerator/resourceutils/helpers_test.go Consolidated individual tests for GetImageVersion into a single table-driven test function.
pkg/resourcegenerator/deployment/deployment.go Updated Generate function to use HumanReadableVersion for version label in pod template.
pkg/resourcegenerator/job/job.go Updated Generate function to use HumanReadableVersion for job version label.
pkg/resourcegenerator/service/service.go Updated Generate function to use HumanReadableVersion for service version label.
tests/application/labels-imageversion/application-assert.yaml Updated app.kubernetes.io/version label from "1234567890123456" to "latest" in Service and Pod.
tests/application/labels-imageversion/application-patch.yaml Changed metadata.name from imageversionshalabel to imageversionlabelsha.

Possibly related PRs

  • Add missing app pod label to job pods #518: The changes in this PR modify the Generate function in pkg/resourcegenerator/job/job.go, which now uses resourceutils.HumanReadableVersion instead of GetImageVersion, directly relating to the changes made in the main PR where GetImageVersion was renamed and modified.
  • SKIP-1159 - Add recommended labels. #527: This PR also modifies the pkg/resourcegenerator/job/job.go file to include a new version label using resourceutils.GetImageVersion, which is relevant since the main PR changes the function to HumanReadableVersion, affecting how version labels are set in job specifications.

Suggested reviewers

  • eliihen
  • evenh
  • omaen

Poem

In the code where rabbits hop,
New constants and functions never stop.
Regex checks and versions refined,
In the forest of logic, clarity we find.
With tests now tidy, we leap with glee,
Celebrating changes, as happy as can be! 🐇✨

Warning

Tool Failures:

Tool Failure Count:

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between c27959e and 62f7219.

📒 Files selected for processing (2)
  • tests/application/labels-imageversion/application-assert.yaml (1 hunks)
  • tests/application/labels-imageversion/application-patch.yaml (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • tests/application/labels-imageversion/application-assert.yaml
  • tests/application/labels-imageversion/application-patch.yaml

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Co-authored-by: Martin Haram Nygård <martin.nygard@kartverket.no>
Copy link
Contributor

@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

🧹 Outside diff range and nitpick comments (4)
pkg/resourcegenerator/resourceutils/helpers_test.go (1)

9-39: Consider adding negative test cases.

While the current test cases are comprehensive for valid inputs, consider adding negative test cases to verify proper handling of:

  • Invalid image strings
  • Malformed version strings
  • Empty strings
  • Very long version strings that exceed LabelValueMaxLength

This would further improve the robustness of the labeler.

pkg/resourcegenerator/resourceutils/helpers.go (3)

24-28: Handle errors explicitly in MatchesRegex function

Currently, the function checks err == nil in the return statement. For better clarity and error handling, consider checking if err is not nil before returning the result. This helps in debugging and maintaining the code.

Apply this diff to handle errors explicitly:

func MatchesRegex(s string, pattern string) bool {
	obj, err := regexp.Match(pattern, []byte(s))
-	return obj && err == nil
+	if err != nil {
+		// Handle or log the error as needed
+		return false
+	}
+	return obj
}

33-36: Ensure consistent handling of image digests

When an image string contains an "@", the code replaces everything after it with ":unknown". This could lead to confusion or mislabeling. Consider using a more descriptive tag or handling the digest appropriately.

For example, you might retain the digest information or use a more meaningful placeholder:

if strings.Contains(imageVersionString, "@") {
	imageVersionString = strings.Split(imageVersionString, "@")[0]
-	imageVersionString = imageVersionString + ":unknown"
+	imageVersionString = imageVersionString + ":digest"
}

57-58: Prevent potential issues in version string sanitization loop

The loop that removes leading characters not matching [a-zA-Z0-9] may result in an empty versionPart, which could cause issues downstream if an empty string is not an acceptable version label. Consider adding a check to handle this scenario.

For example:

for len(versionPart) > 0 && !MatchesRegex(versionPart[:1], "[a-zA-Z0-9]") {
	versionPart = versionPart[1:]
}
+ if len(versionPart) == 0 {
+     versionPart = "unknown"
+ }

This ensures that versionPart will have a valid value even if all leading characters were removed.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 8010f87 and eb0ab78.

📒 Files selected for processing (2)
  • pkg/resourcegenerator/resourceutils/helpers.go (2 hunks)
  • pkg/resourcegenerator/resourceutils/helpers_test.go (1 hunks)
🔇 Additional comments (8)
pkg/resourcegenerator/resourceutils/helpers_test.go (3)

1-7: LGTM! Clean import section.

The import section is well-organized with only the necessary testing dependency.


9-13: Excellent use of table-driven tests!

The test structure follows Go best practices, making it easy to maintain and extend with new test cases.


34-39: Well-structured test execution!

The use of subtests with descriptive names and clear assertions will make test failures easy to diagnose.

pkg/resourcegenerator/resourceutils/helpers.go (5)

6-6: Approved: Necessary imports added

The addition of "regexp" and "strings" imports is appropriate and necessary for the functions introduced later in the code.


10-11: Approved: Define LabelValueMaxLength constant

Defining LabelValueMaxLength as 63 correctly sets the maximum length for Kubernetes label values, adhering to Kubernetes specifications.


39-41: Approved: Default to "latest" when no version is specified

The logic correctly assumes "latest" when no version tag is present in the image string.


49-49: Approved: Replace "+" with "-" in version strings

Replacing "+" with "-" in the version string helps maintain compatibility with label value requirements.


52-53: Approved: Enforce maximum label value length

Truncating the versionPart to LabelValueMaxLength characters ensures compliance with Kubernetes label length constraints.

Comment on lines 14 to 32
{"image", "latest"},
{"image:latest", "latest"},
{"image:1.2.3-dev-123abc", "1.2.3-dev-123abc"},
{"image:1.2.3", "1.2.3"},
{"ghcr.io/org/repo@sha256:54d7ea8b48d0e7569766e0e10b9e38da778a5f65d764168dd7db76a37d6b8", "unknown"},
{"ghcr.io/org/one-app:sha-b15dc91c27ad2387bea81294593d5ce5a686bcc4@sha256:3cda54f1d25458f25fdde0398130da57a4ebb4a4cd759bc49035b7ebf9d83619", "sha-b15dc91c27ad2387bea81294593d5ce5a686bcc4"},
{"ghcr.io/org/another-app:3fb7048", "3fb7048"},
{"ghcr.io/org/some-team/third-app:v1.2.54", "v1.2.54"},
{"ghcr.io/org/another-team/fourth-app:4.0.0.rc-36", "4.0.0.rc-36"},
{"ghcr.io/org/another-team/fifth-app:4.0.0.rc-36-master-latest", "4.0.0.rc-36-master-latest"},
{"ghcr.io/kartverket/vulnerability-disclosure-program@sha256:ab85022d117168585bdedc71cf9c67c3ca327533dc7cd2c5bcc42a83f308ea5d", "unknown"},
{"ghcr.io/kartverket/vulnerability-disclosure-program:4.0.1@sha256:ab85022d117168585bdedc71cf9c67c3ca327533dc7cd2c5bcc42a83f308ea5d", "4.0.1"},
{"nginxinc/nginx-unprivileged:1.20.0-alpine", "1.20.0-alpine"},
{"foo/bar:1.2.3+build.4", "1.2.3-build.4"},
{"foo/bar:1.2.3+somethingLongXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "1.2.3-somethingLongXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"},
{"foo/bar:-1.2.3", "1.2.3"},
{"foo/bar:__1.2.3", "1.2.3"},
{"foo/bar:.1.2.3", "1.2.3"},
}
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Documentation needs improvement for version string transformations

The implementation in GetImageVersion includes several important transformations that should be better documented in the function's header comment:

  • Handling of SHA digests (returns "unknown")
  • Replacing "+" with "-" in version strings
  • Truncating version strings longer than LabelValueMaxLength
  • Stripping non-alphanumeric leading characters
  • Default "latest" tag handling

The current comment // GetImageVersion returns the version part of an image string is too brief given the complexity of the transformations.

🔗 Analysis chain

Comprehensive test coverage with clear test cases!

The test cases cover a wide range of scenarios including:

  • Basic image tags
  • Semantic versions
  • SHA digests
  • Special character handling
  • Version string transformations

Let's verify if these transformations are documented in the implementation:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if the version string transformation rules are documented
# Expected: Comments or documentation explaining the transformation rules

# Look for documentation about version transformations
rg -A 5 "GetImageVersion" pkg/resourcegenerator/resourceutils/helpers.go

Length of output: 429


Script:

#!/bin/bash
# Let's see the complete implementation of GetImageVersion to understand all transformations
rg -A 20 "GetImageVersion" pkg/resourcegenerator/resourceutils/helpers.go

# Also check for any related tests that might document the behavior
rg -A 10 "TestGetImageVersion" pkg/resourcegenerator/resourceutils/helpers_test.go

Length of output: 901


Script:

#!/bin/bash
# Let's try with ast-grep to get the complete function implementation
ast-grep --pattern 'func GetImageVersion($_) string {
  $$$
}'

# And get the test function as well
ast-grep --pattern 'func TestGetImageVersion($_) $_ {
  $$$
}'

Length of output: 2719

pkg/resourcegenerator/resourceutils/helpers.go Outdated Show resolved Hide resolved
Copy link
Contributor

@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

🧹 Outside diff range and nitpick comments (1)
pkg/resourcegenerator/resourceutils/helpers.go (1)

30-30: Add comprehensive function documentation

The function should document its behavior for different input formats and edge cases:

  • Image strings with digests (@sha256:...)
  • Images without tags
  • Version sanitization rules

Add this documentation:

-// GetImageVersion returns the version part of an image string
+// GetImageVersion extracts and sanitizes the version tag from an image string.
+// It handles various formats:
+// - Images with digests (e.g., image@sha256:123) -> returns "unknown"
+// - Images without tags (e.g., nginx) -> returns "latest"
+// - Images with tags (e.g., nginx:1.19) -> returns sanitized tag
+// The returned tag is sanitized to be valid as a Kubernetes label value:
+// - Maximum length of 63 characters
+// - Must start with alphanumeric character
+// - '+' characters replaced with '-'
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between eb0ab78 and 63f32df.

📒 Files selected for processing (1)
  • pkg/resourcegenerator/resourceutils/helpers.go (2 hunks)
🧰 Additional context used
🪛 GitHub Check: Build and run tests
pkg/resourcegenerator/resourceutils/helpers.go

[failure] 57-57:
undefined: MatchesRegex

🔇 Additional comments (3)
pkg/resourcegenerator/resourceutils/helpers.go (3)

6-6: LGTM! Follows Kubernetes conventions

The LabelValueMaxLength constant of 63 correctly adheres to Kubernetes label value length limitations.

Also applies to: 10-10


30-61: Verify usage and test coverage

Let's ensure all callers handle the new edge cases (empty string -> "unknown") and that test coverage is adequate.

✅ Verification successful

The previous output showed that GetImageVersion is used in deployment, service, and job generators to set the version label. We need to see the test coverage to ensure edge cases are handled properly, especially for the "@" handling and empty string cases.


Test coverage is comprehensive and implementation is correct

The test coverage is thorough with test cases covering all edge cases including:

  • Images with SHA digests (both with and without tags)
  • Empty versions defaulting to "latest"
  • Version strings with "+" being converted to "-"
  • Long version strings being truncated
  • Version strings with invalid starting characters
  • Various real-world version formats

The implementation correctly handles all these cases and is being used consistently across deployment, service, and job generators to set the version label. No issues found.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Find all usages of GetImageVersion
echo "=== GetImageVersion usage ==="
rg "GetImageVersion\(" -A 2

# Check test coverage for edge cases
echo -e "\n=== Test coverage ==="
rg "TestVersion" -A 10 ".*_test\.go$"

Length of output: 1873


Script:

#!/bin/bash
# Let's check the test file content specifically
echo "=== Test content ==="
cat pkg/resourcegenerator/resourceutils/helpers_test.go

# Let's also check the actual implementation to compare with tests
echo -e "\n=== Implementation ==="
cat pkg/resourcegenerator/resourceutils/helpers.go

Length of output: 3786

🧰 Tools
🪛 GitHub Check: Build and run tests

[failure] 57-57:
undefined: MatchesRegex


43-45: ⚠️ Potential issue

Fix handling of image strings with multiple colons

The current implementation can break with registry URLs containing ports (e.g., "registry:5000/image:tag").

Apply this diff:

-	// Split image string into parts
-	parts := strings.Split(imageVersionString, ":")
-
-	versionPart := parts[1]
+	// Extract version using last colon
+	lastColon := strings.LastIndex(imageVersionString, ":")
+	if lastColon == -1 || lastColon == len(imageVersionString)-1 {
+		return "latest"
+	}
+	versionPart := imageVersionString[lastColon+1:]

Likely invalid or redundant comment.

pkg/resourcegenerator/resourceutils/helpers.go Outdated Show resolved Hide resolved
pkg/resourcegenerator/resourceutils/helpers.go Outdated Show resolved Hide resolved
Copy link
Member

@evenh evenh left a comment

Choose a reason for hiding this comment

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

The more special cases I think of the more I think that the return type should be (string, error). What do you think @martinhny @omaen @BardOve?

pkg/resourcegenerator/resourceutils/helpers.go Outdated Show resolved Hide resolved
pkg/resourcegenerator/resourceutils/helpers.go Outdated Show resolved Hide resolved
pkg/resourcegenerator/resourceutils/helpers.go Outdated Show resolved Hide resolved
pkg/resourcegenerator/resourceutils/helpers.go Outdated Show resolved Hide resolved
pkg/resourcegenerator/resourceutils/helpers.go Outdated Show resolved Hide resolved
pkg/resourcegenerator/resourceutils/helpers.go Outdated Show resolved Hide resolved
pkg/resourcegenerator/resourceutils/helpers.go Outdated Show resolved Hide resolved
@omaen
Copy link
Contributor

omaen commented Nov 6, 2024

The more special cases I think of the more I think that the return type should be (string, error). What do you think @martinhny @omaen @BardOve?

Depends on how important the label is for further use. I feel like the label is kind of soft/best effort, but it would be nice to actually be able to identify errors if they do happen. Should we in that case log the error in skiperator stdout or create a condition for it?

@evenh
Copy link
Member

evenh commented Nov 8, 2024

I'm for logging a WARN since it's a best effort thing done mainly for ourselves

- Refactored function-name to better reflect what it does
Copy link
Contributor

@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

🧹 Outside diff range and nitpick comments (4)
pkg/resourcegenerator/resourceutils/helpers.go (3)

27-29: Move constant declaration to package level.

The LabelValueMaxLength constant should be declared at package level since it's a fixed value that could be reused across the package.

Apply this diff:

package resourceutils

import (
	...
)

+const LabelValueMaxLength = 63
+
 // HumanReadableVersion returns the version part of an image string
 func HumanReadableVersion(imageReference string) string {
-	const LabelValueMaxLength = 63

45-48: Simplify prefix trimming logic using strings.TrimLeftFunc.

The current implementation using a loop can be simplified using strings.TrimLeftFunc.

Apply this diff:

-	// While first character is not part of regex [a-z0-9A-Z] then remove it
-	for len(versionPart) > 0 && !matchesRegex(versionPart[:1], "[a-zA-Z0-9]") {
-		versionPart = versionPart[1:]
-	}
+	// Trim non-alphanumeric prefix
+	versionPart = strings.TrimLeftFunc(versionPart, func(r rune) bool {
+		return !alphanumericPattern.MatchString(string(r))
+	})

50-59: Consider using strings.Map for character replacement.

The current implementation using strings.Builder could be simplified using strings.Map.

Apply this diff:

-	// For each character in versionPart, replace characters that are not allowed in label-value
-	var result strings.Builder
-	for _, c := range versionPart {
-		if allowedChars.MatchString(string(c)) {
-			result.WriteRune(c)
-		} else {
-			result.WriteRune('-')
-		}
-	}
-	versionPart = result.String()
+	// Replace characters that are not allowed in label-value
+	versionPart = strings.Map(func(r rune) rune {
+		if allowedChars.MatchString(string(r)) {
+			return r
+		}
+		return '-'
+	}, versionPart)
pkg/resourcegenerator/job/job.go (1)

Line range hint 142-147: Remove commented out code.

Since this functionality has been replaced with resourceutils.HumanReadableVersion, the commented-out function should be removed to maintain code cleanliness.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 63f32df and c27959e.

📒 Files selected for processing (5)
  • pkg/resourcegenerator/deployment/deployment.go (1 hunks)
  • pkg/resourcegenerator/job/job.go (1 hunks)
  • pkg/resourcegenerator/resourceutils/helpers.go (2 hunks)
  • pkg/resourcegenerator/resourceutils/helpers_test.go (1 hunks)
  • pkg/resourcegenerator/service/service.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/resourcegenerator/resourceutils/helpers_test.go
🔇 Additional comments (4)
pkg/resourcegenerator/resourceutils/helpers.go (2)

10-13: 🛠️ Refactor suggestion

Improve regex pattern handling efficiency and error handling.

The current implementation recompiles the regex pattern on every call and masks potential regexp compilation errors. Consider using regexp.MustCompile at package level for frequently used patterns.

Apply this diff:

-func matchesRegex(s string, pattern string) bool {
-	obj, err := regexp.Match(pattern, []byte(s))
-	return obj && err == nil
+var alphanumericPattern = regexp.MustCompile(`[a-zA-Z0-9]`)
+
+func matchesRegex(s string) bool {
+	return alphanumericPattern.MatchString(s)
}

Likely invalid or redundant comment.


33-36: Consider handling malformed digest format.

The current implementation assumes the digest format is valid. Consider handling cases where multiple "@" symbols might exist.

pkg/resourcegenerator/service/service.go (1)

40-40: LGTM! Verify consistent usage across resources.

The change to use HumanReadableVersion aligns with the PR's goal of improving the labeler's robustness.

Let's verify that this change is consistently applied across all resource generators:

Note: Based on the PR discussion about error handling, consider adding warning-level logging in HumanReadableVersion for cases where version extraction fails, rather than silently falling back to defaults.

✅ Verification successful

Version labeling is consistently implemented across resources

The verification confirms that HumanReadableVersion is consistently used for version labels in all relevant resource generators:

  • Service generator: service.Labels["app.kubernetes.io/version"]
  • Job generator: labels["app.kubernetes.io/version"]
  • Deployment generator: podTemplateLabels["app.kubernetes.io/version"]
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify consistent version label implementation across resource generators
# Expected: All resource generators should use HumanReadableVersion for version labels

# Test: Search for version label implementations
rg -A 2 "app.kubernetes.io/version" pkg/resourcegenerator/

Length of output: 928

pkg/resourcegenerator/job/job.go (1)

148-148: Consider error handling for version extraction.

Based on the PR discussion about error handling, consider logging warnings when version extraction fails or returns unexpected results. This aligns with the consensus in the PR comments about using warning logs for error cases.

Let's verify the error handling in the new HumanReadableVersion function:

Comment on lines +61 to 64
// Limit label-value to 63 characters
if len(versionPart) > LabelValueMaxLength {
versionPart = versionPart[:LabelValueMaxLength]
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add validation for empty version after processing.

The function should handle cases where the version might become empty after all the processing.

Apply this diff:

 	if len(versionPart) > LabelValueMaxLength {
 		versionPart = versionPart[:LabelValueMaxLength]
 	}
+
+	// Return "unknown" if version is empty after processing
+	if len(versionPart) == 0 {
+		return "unknown"
+	}

Committable suggestion skipped: line range outside the PR's diff.

@@ -108,7 +108,7 @@ func Generate(r reconciliation.Reconciliation) error {
} else {
podTemplateLabels = util.GetPodAppSelector(application.Name)
}
podTemplateLabels["app.kubernetes.io/version"] = resourceutils.GetImageVersion(application.Spec.Image)
podTemplateLabels["app.kubernetes.io/version"] = resourceutils.HumanReadableVersion(application.Spec.Image)
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Inconsistent version labeling found in job generator

The verification revealed that GetImageVersion is still being used in pkg/resourcegenerator/job/job.go while other resource generators have been updated to use HumanReadableVersion. This inconsistency needs to be addressed.

  • pkg/resourcegenerator/job/job.go: Contains both GetImageVersion and HumanReadableVersion usage
  • Other generators correctly use HumanReadableVersion:
    • pkg/resourcegenerator/service/service.go
    • pkg/resourcegenerator/deployment/deployment.go
🔗 Analysis chain

LGTM! Consider adding error logging for labeling failures.

The change to use HumanReadableVersion aligns with the PR's goal of improving labeling robustness and maintains consistency across deployment, job, and service resources.

Let's verify the consistent implementation across other files:

Based on the PR discussion, consider adding error logging when the version label cannot be generated correctly, as this is a best-effort feature. This would help with debugging while maintaining the non-blocking nature of the labeling.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify consistent usage of HumanReadableVersion across resource generators

# Test: Check if GetImageVersion is still used anywhere
echo "Checking for any remaining usage of GetImageVersion..."
rg "GetImageVersion\(" pkg/resourcegenerator/

# Test: Verify HumanReadableVersion is used consistently
echo "Verifying HumanReadableVersion usage..."
rg "HumanReadableVersion\(" pkg/resourcegenerator/

Length of output: 1064

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.

4 participants