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 - Add recommended labels. #527

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

SKIP-1159 - Add recommended labels. #527

wants to merge 14 commits into from

Conversation

SnorreSelmer
Copy link
Contributor

@SnorreSelmer SnorreSelmer commented Aug 30, 2024

Added recommended labels for app.kubernetes.io/name and version, plus some parsing and "prettyfication" of version-string.

Summary by CodeRabbit

  • New Features

    • Enhanced metadata for applications with new labels for name and version in Kubernetes.
    • Introduced a new function to derive version labels from image strings.
    • Added consistent labeling across Kubernetes resources for better management and identification.
    • Improved traceability of job and cron job resources by associating them with specific container image versions.
    • New test configurations for validating resource labeling in Kubernetes.
  • Bug Fixes

    • Improved identification of applications within Kubernetes environments through updated default labels.

… some parsing and "prettyfication" of version-string
@SnorreSelmer SnorreSelmer requested a review from a team as a code owner August 30, 2024 11:07
Copy link
Contributor

coderabbitai bot commented Aug 30, 2024

Walkthrough

The changes involve enhancements to the GetDefaultLabels method across multiple structs in the Kubernetes API, specifically adding new key-value pairs for application name and version. Additionally, new functions for processing image version strings have been introduced, improving the metadata associated with applications and jobs. New YAML files for testing and resource definitions have also been added to support these changes.

Changes

Files Change Summary
api/v1alpha1/application_types.go Added "app.kubernetes.io/name" to GetDefaultLabels.
api/v1alpha1/routing_types.go Added "app.kubernetes.io/name" to GetDefaultLabels.
api/v1alpha1/skipjob_types.go Added "app.kubernetes.io/name" to GetDefaultLabels.
api/v1alpha1/skipns_types.go Added "app.kubernetes.io/name" to GetDefaultLabels.
api/v1alpha1/skipobj_interfaces.go Introduced new function getVersionLabel for processing image version strings.
pkg/resourcegenerator/deployment/deployment.go Added "app.kubernetes.io/version" to pod template labels.
pkg/resourcegenerator/job/job.go Added "app.kubernetes.io/version" to job and cron job specifications.
pkg/resourcegenerator/resourceutils/helpers.go Introduced GetImageVersion function for processing image version strings.
tests/application/labels-imageversion/*.yaml New YAML files defining application resources and tests for version labeling.
tests/application/minimal/application-assert.yaml Added labels for application identification and versioning.
tests/skipjob/minimal-job/skipjob-assert.yaml Added version label to Kubernetes resources for tracking.

Sequence Diagram(s)

sequenceDiagram
    participant Application
    participant Routing
    participant SKIPJob
    participant SKIPNamespace
    participant VersionLabel

    Application->>Application: GetDefaultLabels()
    Application->>VersionLabel: getVersionLabel(a.Spec.Image)
    Application-->>Application: Return labels with name and version

    Routing->>Routing: GetDefaultLabels()
    Routing-->>Routing: Return labels with name

    SKIPJob->>SKIPJob: GetDefaultLabels()
    SKIPJob->>VersionLabel: getVersionLabel(skipJob.Spec.Container.Image)
    SKIPJob-->>SKIPJob: Return labels with name and version

    SKIPNamespace->>SKIPNamespace: GetDefaultLabels()
    SKIPNamespace-->>SKIPNamespace: Return labels with name
Loading

Possibly related PRs

  • Add missing app pod label to job pods #518: This PR modifies the getJobSpec function to enhance the labeling of job specifications, which is related to the overall labeling improvements seen in the main PR's changes to the GetDefaultLabels method.
  • Add AccessPolicies status #524: This PR adds a new print column for access policies in the ApplicationList struct, which aligns with the main PR's focus on enhancing metadata and labeling for better identification in Kubernetes.
  • Fix access policies not updating status when no job #532: This PR improves the status updates for access policies in the SKIPJobReconciler, which is relevant as it deals with the management of application states and labels, similar to the enhancements made in the main PR.

Suggested reviewers

  • SnorreSelmer
  • omaen
  • martinhny
  • berg-thom

Poem

🐰 In the garden where labels bloom,
New names and versions chase away gloom.
With hops of joy, we celebrate,
Metadata shines, it’s truly great!
A version here, a name so bright,
Kubernetes dreams take flight tonight! 🌼


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b38e55f and 24f8209.

Files selected for processing (1)
  • tests/application/labels-imageversion/application-patch.yaml (1 hunks)
Files skipped from review due to trivial changes (1)
  • 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 generate interesting stats about this repository and render them as a table.
    -- @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.

@SnorreSelmer SnorreSelmer marked this pull request as draft August 30, 2024 11:14
@SnorreSelmer SnorreSelmer self-assigned this Aug 30, 2024
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: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 0205584 and d2c1e02.

Files selected for processing (5)
  • api/v1alpha1/application_types.go (1 hunks)
  • api/v1alpha1/routing_types.go (1 hunks)
  • api/v1alpha1/skipjob_types.go (1 hunks)
  • api/v1alpha1/skipns_types.go (1 hunks)
  • api/v1alpha1/skipobj_interfaces.go (2 hunks)
Additional comments not posted (4)
api/v1alpha1/skipns_types.go (1)

21-21: Approved addition of new Kubernetes label.

The addition of "app.kubernetes.io/name": n.Name to the labels map is consistent with the PR's objectives to enhance labeling for Kubernetes resources. Ensure that the Name property is correctly accessible from the corev1.Namespace which SKIPNamespace embeds.

api/v1alpha1/routing_types.go (1)

111-111: Approved addition of new Kubernetes label.

The addition of "app.kubernetes.io/name": in.Name to the labels map is consistent with the PR's objectives to enhance labeling for Kubernetes resources. Ensure that the Name property is correctly accessible from the metav1.ObjectMeta which Routing embeds.

api/v1alpha1/skipjob_types.go (1)

271-272: Enhance Kubernetes metadata with new labels.

The addition of "app.kubernetes.io/name" and "app.kubernetes.io/version" labels in the GetDefaultLabels method enhances Kubernetes metadata, aligning with best practices for application identification and versioning. Ensure that the getVersionLabel function is implemented efficiently and correctly handles edge cases in version formatting.

The changes are approved, but verify the implementation of getVersionLabel.

Verification successful

The getVersionLabel function is implemented correctly.

The getVersionLabel function efficiently handles version strings by splitting the image string and defaulting to "latest" if no version is specified. This implementation aligns with typical Docker image tag conventions and handles edge cases appropriately. No further action is needed.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of `getVersionLabel` function.

# Test: Search for the function implementation. Expect: Efficient and correct handling of version strings.
rg --type python -A 5 $'getVersionLabel'

Length of output: 70


Script:

#!/bin/bash
# Description: Verify the implementation of `getVersionLabel` function in Go files.

# Test: Search for the function implementation. Expect: Efficient and correct handling of version strings.
rg --type go -A 5 'getVersionLabel'

Length of output: 1596

api/v1alpha1/application_types.go (1)

419-420: Standardize Kubernetes labels across applications.

The updated GetDefaultLabels method now includes standardized labels such as "app.kubernetes.io/name" and "app.kubernetes.io/version", which improve the clarity and manageability of Kubernetes resources. Consistency in label usage across different types (SKIPJob and Application) enhances the overall architecture. Again, ensure that the getVersionLabel function is correctly implemented.

The changes are approved, but verify the implementation of getVersionLabel.

Comment on lines 26 to 46
func getVersionLabel(imageVersionString string) string {
parts := strings.Split(imageVersionString, ":")

// Implicitly assume version "latest" if no version is specified
if len(parts) < 2 {
return "latest"
}

versionPart := parts[1]

// Remove "@sha256" from version text if version includes a hash
if strings.Contains(versionPart, "@") {
versionPart = strings.Split(versionPart, "@")[0]
}

// Add build number to version if it is specified
if len(parts) > 2 {
return versionPart + "+" + parts[2]
}
return versionPart
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Approved new function for version label processing.

The getVersionLabel function is a good addition for handling image version strings. It correctly handles cases where the version might be missing or includes a hash, and it appends a build number if specified. Consider adding unit tests to cover various scenarios and edge cases to ensure robustness.

Would you like me to help generate unit tests for this function?

@SnorreSelmer SnorreSelmer marked this pull request as ready for review September 4, 2024 13:07
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: 1

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

21-41: Add error handling and validation to improve the function.

The function can be further improved by adding the following:

  1. Error handling for invalid input, such as an empty string or a string with an invalid format.
  2. Validation of the version string format to ensure it follows the expected pattern.

Here's an example of how you can add error handling and validation:

func GetImageVersion(imageVersionString string) (string, error) {
	if imageVersionString == "" {
		return "", fmt.Errorf("empty image version string")
	}

	parts := strings.Split(imageVersionString, ":")

	if len(parts) < 2 {
		return "latest", nil
	}

	versionPart := parts[1]

	if strings.Contains(versionPart, "@") {
		versionParts := strings.Split(versionPart, "@")
		if len(versionParts) != 2 {
			return "", fmt.Errorf("invalid version format: %s", versionPart)
		}
		versionPart = versionParts[0]
	}

	if len(parts) > 2 {
		buildNumber := parts[2]
		if !isValidBuildNumber(buildNumber) {
			return "", fmt.Errorf("invalid build number: %s", buildNumber)
		}
		return versionPart + "+" + buildNumber, nil
	}

	return versionPart, nil
}

func isValidBuildNumber(buildNumber string) bool {
	// Add your validation logic here
	// For example, check if the build number consists of only digits
	_, err := strconv.Atoi(buildNumber)
	return err == nil
}

In this example:

  • The function returns an error if the input string is empty.
  • The function validates the version format when splitting by "@" and returns an error if it's invalid.
  • The function validates the build number using a separate isValidBuildNumber function and returns an error if it's invalid.

You can customize the validation logic based on your specific requirements.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between d2c1e02 and b8724f3.

Files selected for processing (14)
  • api/v1alpha1/application_types.go (1 hunks)
  • api/v1alpha1/skipjob_types.go (1 hunks)
  • pkg/resourcegenerator/deployment/deployment.go (1 hunks)
  • pkg/resourcegenerator/job/job.go (4 hunks)
  • pkg/resourcegenerator/resourceutils/helpers.go (2 hunks)
  • pkg/resourcegenerator/resourceutils/helpers_test.go (1 hunks)
  • pkg/resourcegenerator/resourceutils/metadata_test.go (1 hunks)
  • pkg/resourcegenerator/service/service.go (2 hunks)
  • tests/application/labels-imageversion/application-assert.yaml (1 hunks)
  • tests/application/labels-imageversion/application-patch.yaml (1 hunks)
  • tests/application/labels-imageversion/application.yaml (1 hunks)
  • tests/application/labels-imageversion/chainsaw-test.yaml (1 hunks)
  • tests/application/minimal/application-assert.yaml (5 hunks)
  • tests/skipjob/minimal-job/skipjob-assert.yaml (2 hunks)
Files skipped from review due to trivial changes (3)
  • tests/application/labels-imageversion/application-assert.yaml
  • tests/application/minimal/application-assert.yaml
  • tests/skipjob/minimal-job/skipjob-assert.yaml
Additional context used
yamllint
tests/application/labels-imageversion/application-patch.yaml

[error] 7-7: no new line character at the end of file

(new-line-at-end-of-file)

Additional comments not posted (17)
tests/application/labels-imageversion/application.yaml (1)

1-7: LGTM!

The file defines a valid Kubernetes Application resource for testing purposes. The resource does not specify any labels, which is likely the expected behavior for this test case.

tests/application/labels-imageversion/application-patch.yaml (1)

1-7: LGTM! The YAML manifest follows the expected structure.

The manifest defines a Kubernetes Application resource with the necessary fields:

  • apiVersion and kind are correctly set.
  • metadata.name is provided.
  • spec.image is using a SHA256 digest, which is a good practice for ensuring immutability and reproducibility of deployments.
  • spec.port is set to a commonly used port 8080.
Tools
yamllint

[error] 7-7: no new line character at the end of file

(new-line-at-end-of-file)

tests/application/labels-imageversion/chainsaw-test.yaml (1)

1-16: LGTM!

The test definition looks good. The file is correctly defining the Chainsaw test resource with the necessary steps.

pkg/resourcegenerator/resourceutils/metadata_test.go (1)

33-44: LGTM!

The code changes are approved. The new expectedLabels map and the modified assertions improve the robustness of the test by ensuring that all expected labels are present and correctly assigned.

The changes align with the suggestion from the past review comment by martinhny:

It would create a better test if you checked against an expectedLabels map with assertEqual(sa.GetLabels, expectedLabels) for example

The past review comment has been addressed by these changes.

pkg/resourcegenerator/resourceutils/helpers_test.go (4)

8-15: LGTM!

The test function is correctly implemented and tests the expected behavior of the GetImageVersion function when the image string does not have a tag.


17-24: LGTM!

The test function is correctly implemented and tests the expected behavior of the GetImageVersion function when the image string has the latest tag.


26-38: LGTM!

The test function is correctly implemented and tests the expected behavior of the GetImageVersion function when the image string has a version tag or a dev version tag.


40-47: LGTM!

The test function is correctly implemented and tests the expected behavior of the GetImageVersion function when the image string has a SHA tag.

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

8-8: LGTM!

The code changes are approved.


40-40: LGTM!

The code changes are approved.

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

9-9: LGTM!

The new import statement is necessary to use the GetImageVersion function.


31-34: LGTM!

The changes enhance the metadata associated with the job by adding a version label using the GetImageVersion function. The changes look good.


79-79: LGTM!

The changes enhance the metadata associated with the cron job by adding a version label using the GetImageVersion function. The changes look good.


135-135: LGTM!

The changes enhance the metadata associated with the job spec by adding a version label using the GetImageVersion function. The changes look good.

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

111-111: LGTM!

Adding the app.kubernetes.io/version label to the pod template is a good practice for tracking and managing application versions in Kubernetes deployments. The chosen label key follows the recommended Kubernetes label convention, and using the GetImageVersion function to extract the version from the image specification is a sensible approach.

api/v1alpha1/skipjob_types.go (1)

271-271: LGTM!

The new label "app.kubernetes.io/name" is correctly set to the Name property of the skipJob instance. This change aligns with the PR objective of adding recommended labels for the Kubernetes application.

api/v1alpha1/application_types.go (1)

420-420: LGTM!

The addition of the app.kubernetes.io/name label enhances the metadata associated with the application and aligns with Kubernetes' labeling conventions. This change improves the application's integration with Kubernetes by providing clearer identification.

Copy link
Contributor

@martinhny martinhny left a comment

Choose a reason for hiding this comment

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

had a single comment, but LGTM! :shipit:

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