-
Notifications
You must be signed in to change notification settings - Fork 35
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
Conversation
WalkthroughThe 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 Changes
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
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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]>
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() |
There was a problem hiding this 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 inaddFileToBuilder
The
addFileToBuilder
function does not handle any errors, which could lead to silent failures in theAddFile
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 isStatePreparing
, which could lead to silent failures ifaddFileToBuilder
encounters an error. Ensure thataddFileToBuilder
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
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 usings.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 inreadFileFromImage
.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 inreadFileFromImage
.The
GetFileBytes
function now usesreadFileFromImage
for file retrieval when the state is notStateStarted
. Verify thatreadFileFromImage
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 goLength 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 10Length 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 30Length of output: 1713
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice one! 🔥
There was a problem hiding this 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:
- Define the file content and file path as constants at the beginning of the test function.
- 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
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.
There was a problem hiding this 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
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
There was a problem hiding this 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 theSetGitRepo
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 anif
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 theSetGitRepo
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
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
Bug Fixes
Refactor