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

make k8s the default builder #541

Merged
merged 12 commits into from
Sep 5, 2024

Conversation

mojtaba-esk
Copy link
Member

@mojtaba-esk mojtaba-esk commented Aug 9, 2024

Closes #501

Currently knuu is using k8s builder (kaniko) by default, however when a change is done to an image and the image has to be rebuild, knuu uses docker locally to rebuild the modified image (i.e. dockerfile), this PR proposes a solution where the new changes also are built using kaniko which is done on k8s instead of local.
Another thing this PR is proposes is when user wants to extract a file from an image, i.e. when an instance is not running, but user wants to read a file from it. That was done locally by docker, this PR also suggests a solution for it to be done on k8s.

Summary by CodeRabbit

  • New Features

    • Introduced a new test function to validate file downloading from a builder instance.
    • Added a method for retrieving files directly from container images, enhancing file access capabilities.
  • Bug Fixes

    • Removed retry logic in a test function for setting the Git repository, simplifying control flow but potentially affecting test reliability in transient error scenarios.
  • Refactor

    • Simplified command execution process and Docker image building workflow, focusing on Dockerfile instruction management without direct Docker client interactions.
    • Updated methods to minimize error handling, reflecting a shift towards a less defensive programming style.

@mojtaba-esk mojtaba-esk added this to the v0.16.0 milestone Aug 9, 2024
@mojtaba-esk mojtaba-esk requested a review from a team August 9, 2024 13:36
@mojtaba-esk mojtaba-esk self-assigned this Aug 9, 2024
Copy link
Contributor

coderabbitai bot commented Aug 9, 2024

Walkthrough

The recent changes involve modifications to various components, focusing on simplifying command execution, enhancing file handling, and adjusting error management within Docker images. Key updates include the removal of error handling in certain methods, a refactor of the BuilderFactory, and the introduction of a new file retrieval method. These changes streamline interactions and improve the overall functionality of the system.

Changes

Files Change Summary
e2e/system/build_from_git_test.go, e2e/system/file_test.go Minor formatting change in build_from_git_test.go; new test for downloading a file added in file_test.go to validate file handling.
pkg/container/docker.go, pkg/instance/build.go, pkg/instance/storage.go Significant refactor of BuilderFactory in docker.go, removing Docker client interactions; error handling minimized in methods across both files.
pkg/instance/storage.go Introduced readFileFromImage method for improved file retrieval strategy directly from container images.
e2e/system/build_from_git_test.go Removed retry logic from TestBuildFromGitWithModifications, simplifying the Git repository setup.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant BuilderFactory
    participant Storage
    participant Image

    User->>BuilderFactory: Add Command
    BuilderFactory->>BuilderFactory: Add Command to Dockerfile
    User->>Storage: Add File
    Storage->>BuilderFactory: Add File to Builder
    User->>Storage: Get File Bytes
    Storage->>Image: Read File from Image
    Image-->>Storage: Return File Bytes
    Storage-->>User: Return File
Loading

🐇 In the code where rabbits hop,
Commands and files now won't stop!
With Docker we play, in a streamlined way,
Errors tucked away, come what may!
Let’s build and explore, there’s so much in store!
Hooray for the changes, we’ll bounce and adore! 🎉


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>.
    • 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 show all the console.log statements in this repository.
    • @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.

pkg/container/docker.go Outdated Show resolved Hide resolved
Copy link
Member

@MSevey MSevey left a comment

Choose a reason for hiding this comment

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

over lgtm, do we have an image rebuild test that would cover these changes?

Co-authored-by: Matthew Sevey <[email protected]>
@mojtaba-esk
Copy link
Member Author

over lgtm, do we have an image rebuild test that would cover these changes?

There are already a couple of tests that builds images and they are passing using the new change. Knuu has been using kaniko builder since long time ago, this PR proposes the changes to the underlying layers where still docker used to rebuild an image which is modified. e.g. a file added and there is a new test to test that:

func (s *Suite) TestDownloadFileFromBuilder()

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, codebase verification and nitpick comments (1)
pkg/instance/storage.go (1)

50-51: Lack of Error Handling in addFileToBuilder

The addFileToBuilder function does not handle any errors, which could lead to silent failures in the AddFile method. Consider adding error handling to ensure any issues are properly reported and managed.

  • File: pkg/instance/build.go
  • Function: addFileToBuilder
Analysis chain

Verify error handling in addFileToBuilder.

The function now returns nil when the state is StatePreparing, which could lead to silent failures if addFileToBuilder encounters an error. Ensure that addFileToBuilder handles errors appropriately.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify error handling in `addFileToBuilder`.

# Test: Search for the `addFileToBuilder` function definition and check for error handling.
ast-grep --lang go --pattern $'func (b *builder) addFileToBuilder($_, $_, $_) {
  $$$
  if $_ {
    return $_
  }
  $$$
}'

Length of output: 123


Script:

#!/bin/bash
# Description: Locate the `addFileToBuilder` function and verify its implementation.

# Step 1: Search for the definition of `addFileToBuilder` in the codebase.
rg 'func \(.*\) addFileToBuilder\(' -A 10

# Step 2: Check for error handling within the function by searching for return statements or error checks.
rg 'func \(.*\) addFileToBuilder\(' -A 50 | rg 'return|if .+err'

Length of output: 1393

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between edf5f29 and 6a9c9b7.

Files selected for processing (5)
  • e2e/system/build_from_git_test.go (1 hunks)
  • e2e/system/file_test.go (1 hunks)
  • pkg/container/docker.go (5 hunks)
  • pkg/instance/build.go (4 hunks)
  • pkg/instance/storage.go (4 hunks)
Files skipped from review due to trivial changes (1)
  • e2e/system/build_from_git_test.go
Additional comments not posted (13)
e2e/system/file_test.go (1)

86-119: Ensure proper cleanup in tests.

The test function TestDownloadFileFromBuilder correctly sets up and tears down instances. Ensure that all resources are cleaned up even if the test fails by using s.T().Cleanup as shown.

pkg/container/docker.go (6)

28-31: Ensure error handling for directory creation.

The function handles directory creation errors correctly. Ensure that any additional context-specific error handling is implemented where this function is called.


45-46: Consider handling command syntax errors.

Removing error handling in AddCmdToBuilder simplifies the method but risks silent failures if command syntax is incorrect. Consider validating commands before appending.


51-52: Consider validating file paths.

The method AddToBuilder no longer returns errors. Ensure that file path validation is performed elsewhere to prevent silent failures.


56-57: Consider validating environment variables.

The method SetEnvVar no longer returns errors. Ensure that environment variable names and values are validated to prevent silent failures.


61-62: Consider validating user settings.

The method SetUser no longer returns errors. Ensure that user settings are validated to prevent silent failures.


88-90: Ensure robust file operations.

The method PushBuilderImage handles file operations and builds the image. Ensure that file write errors and build process errors are adequately logged and handled.

pkg/instance/build.go (4)

111-112: Consider handling command execution errors.

The method ExecuteCommand no longer returns errors. Ensure that command execution issues are logged or handled to prevent silent failures.


122-124: Consider handling user setting errors.

The method SetUser no longer returns errors. Ensure that user setting issues are logged or handled to prevent silent failures.


194-197: Consider handling file addition errors.

The method addFileToBuilder no longer returns errors. Ensure that file addition issues are logged or handled to prevent silent failures.


208-209: Consider handling environment variable setting errors.

The method SetEnvironmentVariable no longer returns errors. Ensure that environment variable setting issues are logged or handled to prevent silent failures.

pkg/instance/storage.go (2)

350-390: Verify resource management in readFileFromImage.

The readFileFromImage method involves creating a temporary instance, which could lead to resource overhead. Ensure that resources are managed efficiently and suggest improvements if necessary.


Line range hint 168-172:
Ensure efficiency and error handling in readFileFromImage.

The GetFileBytes function now uses readFileFromImage for file retrieval when the state is not StateStarted. Verify that readFileFromImage is efficient and handles errors correctly.

Verification successful

Verification Successful: Efficient and Robust Error Handling in readFileFromImage

The readFileFromImage function efficiently retrieves files from an image using a temporary instance. It handles errors at each critical step and ensures cleanup with a deferred function. This design choice avoids running processes on the user's machine, enhancing both efficiency and reliability.

  • Error Handling: Errors are checked and returned at each step of the process.
  • Efficiency: Uses a temporary instance to manage resources effectively.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify efficiency and error handling in `readFileFromImage`.

# Test: Search for the `readFileFromImage` function definition and check for efficient operations and error handling.
ast-grep --lang go --pattern $'func (s *storage) readFileFromImage($_, $_) {
  $$$
  if $_ {
    return $_
  }
  $$$
}'

Length of output: 120


Script:

#!/bin/bash
# Description: Broaden the search to locate the `readFileFromImage` function definition in the Go codebase.

# Search for the function definition by name without specifying parameters.
rg 'func \(s \*storage\) readFileFromImage' -A 10 --lang go

Length of output: 470


Script:

#!/bin/bash
# Description: Correct the search to locate the `readFileFromImage` function definition in the Go codebase.

# Search for the function definition by name without specifying parameters or language.
rg 'func \(s \*storage\) readFileFromImage' -A 10

Length of output: 796


Script:

#!/bin/bash
# Description: Gather more lines of the `readFileFromImage` function to understand its implementation.

# Extract additional lines from the function to assess its efficiency and error handling.
rg 'func \(s \*storage\) readFileFromImage' -A 30

Length of output: 1713

tty47
tty47 previously approved these changes Aug 26, 2024
Copy link
Contributor

@tty47 tty47 left a comment

Choose a reason for hiding this comment

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

nice one! 🔥

MSevey
MSevey previously approved these changes Sep 3, 2024
@mojtaba-esk mojtaba-esk dismissed stale reviews from MSevey and tty47 via 6af8c7b September 4, 2024 08:51
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: 5

Outside diff range, codebase verification and nitpick comments (2)
e2e/system/file_test.go (1)

103-136: Improve the test by defining constants and using string formatting.

The test looks good overall, but there are a few minor improvements that can be made:

  1. Define the file content and file path as constants at the beginning of the test function.
  2. Use string formatting for the instance name prefix instead of string concatenation.

Apply this diff to implement the suggested improvements:

-func (s *Suite) TestDownloadFileFromBuilder() {
+func (s *Suite) TestDownloadFileFromBuilder() {
+	const (
+		namePrefix  = "download-file-builder"
+		fileContent = "Hello World!"
+		filePath    = "/hello.txt"
+	)
+
 	s.T().Parallel()
 	// Setup
 
-	target, err := s.Knuu.NewInstance(namePrefix + "-target")
+	target, err := s.Knuu.NewInstance(fmt.Sprintf("%s-target", namePrefix))
 	s.Require().NoError(err)
 
 	ctx := context.Background()
 	s.Require().NoError(target.Build().SetImage(ctx, "alpine:latest"))
 
 	s.T().Cleanup(func() {
 		if err := target.Execution().Destroy(ctx); err != nil {
 			s.T().Logf("error destroying instance: %v", err)
 		}
 	})
 
 	// Test logic
-	const (
-		fileContent = "Hello World!"
-		filePath    = "/hello.txt"
-	)
 
 	s.Require().NoError(target.Storage().AddFileBytes([]byte(fileContent), filePath, "0:0"))
 
 	// The commit is required to make the changes persistent to the image
 	s.Require().NoError(target.Build().Commit(ctx))
 
 	// Now test if the file can be downloaded correctly from the built image
 	gotContent, err := target.Storage().GetFileBytes(ctx, filePath)
 	s.Require().NoError(err, "Error getting file bytes")
 
 	s.Assert().Equal(fileContent, string(gotContent))
 }
pkg/instance/storage.go (1)

375-415: LGTM with a suggestion to refactor for better readability and maintainability.

The new readFileFromImage method correctly implements reading files from an image by creating a temporary instance. Error handling is implemented properly, and the temporary instance is cleaned up.

Consider refactoring the method into smaller functions for better readability and maintainability. For example:

func (s *storage) readFileFromImage(ctx context.Context, filePath string) ([]byte, error) {
	ti, err := s.createTempInstance(ctx)
	if err != nil {
		return nil, err
	}
	defer s.destroyTempInstance(ctx, ti)

	if err := s.setupTempInstance(ctx, ti); err != nil {
		return nil, err
	}

	return s.readFileFromTempInstance(ctx, ti, filePath)
}

func (s *storage) createTempInstance(ctx context.Context) (*Instance, error) {
	// ...
}

func (s *storage) setupTempInstance(ctx context.Context, ti *Instance) error {
	// ...
}

func (s *storage) readFileFromTempInstance(ctx context.Context, ti *Instance, filePath string) ([]byte, error) {
	// ...
}

func (s *storage) destroyTempInstance(ctx context.Context, ti *Instance) {
	// ...
}
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6a9c9b7 and 6af8c7b.

Files selected for processing (3)
  • e2e/system/file_test.go (1 hunks)
  • pkg/instance/build.go (4 hunks)
  • pkg/instance/storage.go (4 hunks)
Additional comments not posted (2)
pkg/instance/storage.go (2)

Line range hint 190-195: LGTM!

The new code block correctly handles reading files from an image when the instance is not running. Error handling is implemented properly.


416-416: LGTM!

The empty line at the end of the file improves readability without affecting functionality.

pkg/instance/build.go Show resolved Hide resolved
pkg/instance/build.go Show resolved Hide resolved
pkg/instance/build.go Show resolved Hide resolved
pkg/instance/build.go Show resolved Hide resolved
pkg/instance/storage.go 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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6af8c7b and a2877db.

Files selected for processing (2)
  • e2e/system/file_test.go (3 hunks)
  • e2e/system/suite_setup_test.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • e2e/system/suite_setup_test.go
Files skipped from review as they are similar to previous changes (1)
  • e2e/system/file_test.go

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, codebase verification and nitpick comments (1)
e2e/system/build_from_git_test.go (1)

68-73: Issues found due to the removal of the retry mechanism.

The RetryOperation function is still used in other parts of the codebase, indicating its importance for handling transient errors. The absence of error handling for the SetGitRepo call suggests that the test may not be robust against such errors. Consider reintroducing the retry mechanism or enhancing error handling to ensure test reliability.

  • RetryOperation is used in multiple files, indicating its significance.
  • No error handling found for SetGitRepo in the form of an if statement.
Analysis chain

Verify the impact of removing the retry mechanism.

The retry mechanism has been removed, and the SetGitRepo call is made directly. This change may affect the robustness of the test in scenarios where transient errors could occur during the repository setup.

Run the following script to verify the impact of removing the retry mechanism:

If the RetryOperation is not used anywhere else in the codebase and there is proper error handling for the SetGitRepo call, then the removal of the retry mechanism may be acceptable. However, if the script indicates potential issues, consider adding back the retry mechanism or enhancing the error handling to ensure the robustness of the test.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the impact of removing the retry mechanism.

# Test 1: Search for the usage of `RetryOperation` in the codebase. Expect: No occurrences.
rg --type go $'RetryOperation'

# Test 2: Search for error handling related to `SetGitRepo`. Expect: Error handling code.
ast-grep --lang go --pattern $'if err := $_.SetGitRepo($$$); err != nil {
  $$$
}'

Length of output: 931

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a2877db and 87dd4d6.

Files selected for processing (1)
  • e2e/system/build_from_git_test.go (1 hunks)

@mojtaba-esk mojtaba-esk merged commit 60ca33c into main Sep 5, 2024
11 checks passed
@mojtaba-esk mojtaba-esk deleted the mojtaba/501--make-k8s-the-default-builder branch September 5, 2024 05:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Make kubernetes the default builder
3 participants