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

feat: add support for build arguments when building Docker images usi… #569

Merged

Conversation

mojtaba-esk
Copy link
Member

@mojtaba-esk mojtaba-esk commented Sep 30, 2024

Closes #567

Summary by CodeRabbit

  • New Features

    • Introduced a new test for building Docker images with customizable build arguments.
    • Enhanced the builder functionality to accept and utilize structured build arguments.
    • Updated methods to improve flexibility in image building configurations.
  • Bug Fixes

    • Simplified existing tests to ensure clarity and focus on core functionalities.
  • Documentation

    • Added new types and interfaces for handling build arguments, improving code clarity.
  • Refactor

    • Improved code organization and readability through restructuring methods and introducing helper functions.

@mojtaba-esk mojtaba-esk requested a review from a team September 30, 2024 15:18
@mojtaba-esk mojtaba-esk self-assigned this Sep 30, 2024
Copy link
Contributor

coderabbitai bot commented Sep 30, 2024

Walkthrough

The changes introduce support for build arguments when building Docker images using Kaniko. A new test function is added to validate this functionality, and several modifications are made across various files to implement a structured approach for handling build arguments. This includes updates to methods for setting images and Git repositories, as well as the introduction of new types and interfaces for managing these arguments.

Changes

File Path Change Summary
e2e/system/build_image_test.go Added TestBuildWithBuildArgs to test building Docker images with build arguments.
pkg/builder/args.go Introduced BuildArg and CustomArg types implementing ArgInterface for structured arguments.
pkg/builder/builder.go Changed Args field type in BuilderOptions from []string to []ArgInterface.
pkg/builder/kaniko/kaniko.go Restructured prepareJob method and added prepareArgs function for argument handling.
pkg/builder/kaniko/kaniko_test.go Updated tests to use ArgInterface for passing build arguments.
pkg/container/docker.go Modified NewBuilderFactory to accept args []builder.ArgInterface for build argument passing.
pkg/instance/build.go Enhanced SetImage and SetGitRepo methods to accept builder.ArgInterface as additional args.

Assessment against linked issues

Objective Addressed Explanation
Add support for build arguments when building Docker images using Kaniko (567)

Possibly related PRs

Suggested labels

knuu

Suggested reviewers

  • sysrex
  • tty47
  • smuu
  • MSevey

Poem

In the garden where we play,
New arguments come out to sway.
With Kaniko's build, so bright and bold,
Docker images, a sight to behold!
Hop along, let’s build with cheer,
For every change brings joy near! 🐇✨


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.

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: 0

🧹 Outside diff range and nitpick comments (7)
pkg/builder/args.go (2)

10-23: LGTM: BuildArg struct and methods are well-implemented.

The BuildArg struct and its methods correctly implement the ArgInterface. The use of a type assertion var _ ArgInterface = &BuildArg{} is a good practice to ensure the interface is properly implemented at compile-time.

Consider adding a brief comment explaining the purpose of the type assertion:

// Ensure BuildArg implements ArgInterface
var _ ArgInterface = &BuildArg{}

1-39: Overall implementation is solid with room for minor improvements.

The args.go file introduces a well-structured approach to handling build arguments, aligning closely with the PR objectives. The use of an interface (ArgInterface) and two implementing types (BuildArg and CustomArg) provides flexibility and extensibility.

Consider the following suggestions to further improve the implementation:

  1. Add package-level documentation explaining the purpose and usage of this file.
  2. Include examples in the documentation for both BuildArg and CustomArg to illustrate their intended use cases.
  3. If CustomArg is intended for future extensibility, consider adding a comment explaining this rationale.

Example package-level documentation:

// Package builder provides functionality for handling build arguments
// when building Docker images using Kaniko. It defines interfaces and
// types for managing both standard build arguments and custom arguments.

These improvements would enhance the code's readability and maintainability.

e2e/system/build_image_test.go (2)

95-134: LGTM: New test function effectively verifies build argument support.

The TestBuildWithBuildArgs function is well-structured and aligns with the PR objectives. It effectively tests the new functionality of passing build arguments when building Docker images using Kaniko.

A minor suggestion for improvement:

Consider adding a comment explaining the purpose of strings.TrimSpace on line 131. This will help future maintainers understand why this step is necessary. For example:

// Trim whitespace to ensure consistent comparison
gotData = []byte(strings.TrimSpace(string(gotData)))

Removing the start command impacts the test's functionality.

The TestBuildFromGitWithModifications function relies on SetStartCommand to define how the target instance starts. Since there is no Dockerfile setting ENTRYPOINT or CMD, removing this line will prevent the instance from starting correctly, potentially causing the test to fail or behave unexpectedly.

🔗 Analysis chain

Line range hint 54-93: Verify the impact of removing the start command.

The removal of the line setting the start command for the target instance simplifies the test setup. However, please ensure that this change doesn't affect the test's effectiveness or introduce any potential issues.

To verify the impact, please run the following script:


To ensure the removal of the start command doesn't negatively impact the test, please run the following scripts:


To further verify the impact of removing the start command, please execute the following script:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if the start command is set elsewhere or if it's necessary for the test.

# Test 1: Search for any other occurrences of setting the start command
echo "Searching for other occurrences of setting the start command:"
rg --type go 'SetStartCommand|StartCommand' e2e/

# Test 2: Check if the Dockerfile in the test branch sets an entrypoint or CMD
echo "Checking Dockerfile in the test branch for entrypoint or CMD:"
git show test/build-from-git:Dockerfile | grep -E '(ENTRYPOINT|CMD)'

Length of output: 1731


Script:

#!/bin/bash
# Description: Verify the existence of the 'test/build-from-git' branch and locate Dockerfiles.

# Test 1: List all branches to confirm 'test/build-from-git' exists
echo "Listing all branches to verify 'test/build-from-git' exists:"
git branch -a

# Test 2: If 'test/build-from-git' exists, search for Dockerfiles in that branch
if git show-ref --verify --quiet refs/heads/test/build-from-git; then
    echo "Searching for Dockerfiles in the 'test/build-from-git' branch:"
    git ls-tree -r test/build-from-git --name-only | grep -i 'Dockerfile'
else
    echo "Branch 'test/build-from-git' does not exist."
fi

Length of output: 403


Script:

#!/bin/bash
# Description: Locate the Dockerfile in the 'main' branch and check for ENTRYPOINT or CMD directives.

# Test 1: Search for Dockerfiles in the 'main' branch
echo "Searching for Dockerfiles in the 'main' branch:"
git ls-tree -r main --name-only | grep -i 'Dockerfile'

# Test 2: If Dockerfile is found, check for ENTRYPOINT or CMD
DOCKERFILE=$(git ls-tree -r main --name-only | grep -i 'Dockerfile' | head -n 1)
if [ -n "$DOCKERFILE" ]; then
    echo "Checking '$DOCKERFILE' for ENTRYPOINT or CMD directives:"
    git show main:"$DOCKERFILE" | grep -E '(ENTRYPOINT|CMD)'
else
    echo "No Dockerfile found in the 'main' branch."
fi

Length of output: 364

pkg/builder/kaniko/kaniko_test.go (1)

49-49: LGTM! Consider enhancing the test case.

The change from a slice of strings to a slice of builder.ArgInterface for the Args field aligns well with the PR objectives to add support for build arguments. This new structure provides more flexibility in handling build arguments.

To further improve the test coverage, consider enhancing the test case to verify that the build argument is correctly passed to and used by the Kaniko builder. This could involve:

  1. Modifying the completeAllJobInFakeClientset function to capture the Job specification.
  2. Adding an assertion to check if the build argument is present in the Job's container arguments.

Here's a sample implementation:

func completeAllJobInFakeClientset(t *testing.T, clientset *fake.Clientset, namespace string) *batchv1.Job {
	ctx := context.Background()

	jobs, err := clientset.BatchV1().Jobs(namespace).List(ctx, metav1.ListOptions{})
	assert.NoError(t, err)

	var capturedJob *batchv1.Job
	for _, j := range jobs.Items {
		j.Status.Succeeded = 1
		updatedJob, err := clientset.BatchV1().Jobs(namespace).Update(ctx, &j, metav1.UpdateOptions{})
		require.NoError(t, err)
		capturedJob = updatedJob

		// Create a Pod with the same name as the Job
		pod := createPodFromJob(&j)
		_, err = clientset.CoreV1().Pods(namespace).Create(ctx, pod, metav1.CreateOptions{})
		require.NoError(t, err)
	}
	return capturedJob
}

// In the test case:
capturedJob := completeAllJobInFakeClientset(t, k8sCS, k8sNamespace)
assert.Contains(t, capturedJob.Spec.Template.Spec.Containers[0].Args, "--build-arg=SOME_ARG=some_value", "Build argument should be passed to the Kaniko container")

This enhancement would provide stronger assurance that the new build argument functionality is working as expected.

pkg/container/docker.go (2)

101-101: LGTM with a minor suggestion: Updated PushBuilderImage to use build arguments

The addition of Args: f.args to the BuilderOptions correctly passes the build arguments to the underlying builder. This change aligns with the PR objectives.

Consider adding a nil check before setting the Args field to handle cases where no build arguments are provided:

if len(f.args) > 0 {
    Args: f.args,
}

This would prevent passing an empty slice of arguments when none are specified.


139-139: LGTM with a minor suggestion: Updated BuildImageFromGitRepo to use build arguments

The addition of Args: f.args to the BuilderOptions correctly passes the build arguments to the underlying builder when building an image from a Git repository. This change aligns with the PR objectives.

Similar to the suggestion for PushBuilderImage, consider adding a nil check:

if len(f.args) > 0 {
    Args: f.args,
}

This would prevent passing an empty slice of arguments when none are specified.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between f694c3a and 599e47c.

📒 Files selected for processing (7)
  • e2e/system/build_image_test.go (2 hunks)
  • pkg/builder/args.go (1 hunks)
  • pkg/builder/builder.go (1 hunks)
  • pkg/builder/kaniko/kaniko.go (3 hunks)
  • pkg/builder/kaniko/kaniko_test.go (1 hunks)
  • pkg/container/docker.go (4 hunks)
  • pkg/instance/build.go (4 hunks)
🔇 Additional comments (17)
pkg/builder/args.go (3)

1-3: LGTM: Package declaration and constant definition are appropriate.

The package name builder is suitable for the functionality being implemented. The constant buildArgKey with the value "--build-arg" aligns well with the PR objectives of supporting build arguments for Docker image building using Kaniko.


5-8: LGTM: Well-designed interface for argument handling.

The ArgInterface is well-defined with clear and descriptive method names. This interface provides a common contract for different types of arguments, allowing for flexibility in implementing various argument types. This design choice aligns well with the goal of supporting both build arguments and potentially other types of arguments in the future.


25-39: LGTM: CustomArg struct and methods are well-implemented.

The CustomArg struct and its methods correctly implement the ArgInterface. The use of a type assertion var _ ArgInterface = &CustomArg{} is a good practice to ensure the interface is properly implemented at compile-time.

Consider adding a brief comment explaining the purpose of the type assertion:

// Ensure CustomArg implements ArgInterface
var _ ArgInterface = &CustomArg{}

Could you please clarify the intended use cases for CustomArg? While it provides flexibility, it's not immediately clear from the PR objectives how it will be utilized. This information would be helpful for understanding the full scope of the implementation.

pkg/builder/builder.go (1)

17-17: Approved change, but additional context needed.

The change from []string to []ArgInterface for the Args field in BuilderOptions aligns with the PR objective of adding support for build arguments. This structured approach should provide more flexibility in handling different types of arguments.

However, could you please provide more information on the following:

  1. Where is ArgInterface defined?
  2. What methods does ArgInterface declare?
  3. Are there any existing implementations of ArgInterface?

This information will help in understanding the full scope of this change and its impact on the rest of the codebase.

Let's verify the definition and usage of ArgInterface:

e2e/system/build_image_test.go (2)

5-5: LGTM: Import statement addition is appropriate.

The addition of the "fmt" package import is necessary for the new functionality in the TestBuildWithBuildArgs function.


Line range hint 1-134: Overall implementation aligns well with PR objectives.

The changes in this file effectively implement and test the new build argument functionality for Docker image builds using Kaniko. The new TestBuildWithBuildArgs function provides good coverage for the feature, and the modifications to the existing tests maintain consistency in the testing approach.

The implementation aligns well with the PR objectives of adding support for build arguments, as outlined in issue #567. It allows for overriding specific variables during the build process, similar to Docker's --build-arg feature.

pkg/container/docker.go (3)

24-24: LGTM: Addition of args field to BuilderFactory

The addition of the args []builder.ArgInterface field to the BuilderFactory struct is a good approach to support build arguments. This change aligns well with the PR objective of enabling the passing of build-time variables.


28-28: LGTM: Updated NewBuilderFactory to support build arguments

The changes to the NewBuilderFactory function correctly implement the support for build arguments:

  1. The function signature now includes args []builder.ArgInterface.
  2. The args field is properly initialized in the returned BuilderFactory.

These modifications align well with the PR objectives and enable the passing of build-time variables.

Also applies to: 38-38


Line range hint 1-189: Overall implementation looks good, but some clarification needed

The changes successfully implement support for build arguments in the BuilderFactory, PushBuilderImage, and BuildImageFromGitRepo methods. This aligns well with the PR objective of adding support for build arguments when building Docker images using Kaniko.

However, to fully meet the PR objectives, please clarify or address the following:

  1. How are specific global variables like MAX_SQUARE_SIZE and UPGRADE_HEIGHT_DELAY handled or overridden using this implementation?

  2. Is there a need to add any validation or parsing of the build arguments to ensure they match the expected format for overriding these global variables?

  3. Consider adding documentation or comments explaining how users can utilize this new functionality to override specific global variables, similar to the example provided in the linked issue:

    docker build --build-arg MAX_SQUARE_SIZE=64 --build-arg UPGRADE_HEIGHT_DELAY=1000 -t celestia-app:latest .

Addressing these points will ensure that the implementation fully meets the PR objectives and provides clear guidance for users.

pkg/builder/kaniko/kaniko.go (4)

146-149: LGTM: Improved code structure and argument handling.

The changes to the variable declarations and the introduction of the prepareArgs function improve code readability and maintainability. This refactoring aligns well with the PR objectives by providing a centralized mechanism for handling build arguments.

Also applies to: 163-163


Line range hint 1-283: Overall feedback: Good implementation with some areas for improvement

The changes in this file successfully implement support for build arguments when building Docker images using Kaniko, aligning well with the PR objectives. The introduction of the prepareArgs function improves code structure and maintainability.

However, there are a few areas that need attention:

  1. Clarify how MAX_SQUARE_SIZE and UPGRADE_HEIGHT_DELAY arguments are being handled.
  2. Address or provide context for the TODO comments in the prepareArgs function.
  3. Consider adding more documentation to explain the purpose of different argument groups.

Once these points are addressed, the implementation will be more robust and easier to maintain in the future.


255-283: LGTM with suggestions: New prepareArgs function.

The new prepareArgs function effectively centralizes the argument preparation logic and implements the handling of cache options and build arguments as per the PR objectives. However, there are a few points to consider:

  1. The function doesn't explicitly handle the MAX_SQUARE_SIZE and UPGRADE_HEIGHT_DELAY arguments mentioned in the PR objectives. Can you clarify how these specific arguments are being handled?

  2. There are several TODO comments in the function. Could you provide more context on these items and when they will be addressed?

  3. Consider adding comments to explain the purpose of each argument group (context, destination, cache, etc.) for better readability.

To ensure that the MAX_SQUARE_SIZE and UPGRADE_HEIGHT_DELAY arguments are being handled correctly, please run the following script:

#!/bin/bash
# Description: Verify that MAX_SQUARE_SIZE and UPGRADE_HEIGHT_DELAY are being handled in the codebase.

# Test: Search for MAX_SQUARE_SIZE and UPGRADE_HEIGHT_DELAY in the codebase
echo "Searching for MAX_SQUARE_SIZE:"
rg --type go "MAX_SQUARE_SIZE"

echo "Searching for UPGRADE_HEIGHT_DELAY:"
rg --type go "UPGRADE_HEIGHT_DELAY"

# Test: Check if these arguments are being passed to the kaniko builder
echo "Checking if arguments are passed to kaniko builder:"
rg --type go -A 10 "kaniko.*Build.*Options"

258-259: Address TODO comments and create follow-up tasks.

There are several TODO comments in the prepareArgs function that need attention:

  1. Git options (lines 258-259)
  2. Authentication token for the registry (lines 261-262)
  3. Authentication token for the cache repo (line 266)

These comments indicate important features that are yet to be implemented. To ensure these don't get overlooked:

  1. Could you provide more context on the requirements for these features?
  2. Consider creating separate GitHub issues for each of these items to track their implementation.
  3. If these features are not required for the current PR, please remove the TODO comments and address them in future PRs.

To get a better understanding of the current state of these features, please run the following script:

Also applies to: 261-262, 266-266

pkg/instance/build.go (4)

48-48: Addition of build arguments to 'SetImage' method

The method SetImage now includes variadic arguments args ...builder.ArgInterface, enabling the passing of build arguments. This change aligns with the PR objectives to support build arguments when building Docker images using Kaniko.


57-57: Passing build arguments to 'NewBuilderFactory' in 'SetImage'

The args are correctly passed to container.NewBuilderFactory, allowing the builder to utilize the specified build arguments during the image build process.


69-69: Addition of build arguments to 'SetGitRepo' method

The method SetGitRepo now includes variadic arguments args ...builder.ArgInterface, allowing build arguments to be passed when building images from a Git repository. This enhancement supports the PR's goal of adding build argument functionality.


83-83: Passing build arguments to 'NewBuilderFactory' in 'SetGitRepo'

The args are appropriately passed to container.NewBuilderFactory, ensuring that build arguments are utilized when building the image from the Git repository.

Copy link
Member

@smuu smuu left a comment

Choose a reason for hiding this comment

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

Nice 👍

@mojtaba-esk mojtaba-esk enabled auto-merge (squash) October 1, 2024 06:53
@mojtaba-esk mojtaba-esk merged commit 339f749 into main Oct 1, 2024
17 checks passed
@mojtaba-esk mojtaba-esk deleted the mojtaba/567-feat-support-for-build-arguments-in-kaniko branch October 1, 2024 07:43
@coderabbitai coderabbitai bot mentioned this pull request Nov 8, 2024
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.

Add support for build arguments when building Docker images using Kaniko
3 participants