-
Notifications
You must be signed in to change notification settings - Fork 43
feat: implement reproducible build and get cached image #213
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d0b4d6a
feat: push final image to cache repo
mafredri 5a50f4a
explicitly set nopush if no destinations
mafredri 6cb1217
wrap
mafredri 7764535
feat: implement reproducible build and get cached image
mafredri d54ca81
gen
mafredri ea91cd5
gomod
mafredri 1979ffd
chore: add unit tests for push image / cache mode
johnstcn a962a26
unused code
johnstcn d804fc9
update options.golden
johnstcn 3b18839
Merge branch 'main' into mafredri/feat-fake-build
johnstcn b7502ee
add test for multistage build
johnstcn aacdbfc
Apply suggestions from code review
johnstcn 149bdb3
fix help output
johnstcn 354d885
update kaniko to remove fake verbiage when probing build cache
johnstcn 15904df
Merge branch 'main' into mafredri/feat-fake-build
johnstcn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,9 @@ func Run(ctx context.Context, options Options) error { | |
if options.InitCommand == "" { | ||
options.InitCommand = "/bin/sh" | ||
} | ||
if options.CacheRepo == "" && options.PushImage { | ||
return fmt.Errorf("--cache-repo must be set when using --push-image") | ||
} | ||
// Default to the shell! | ||
initArgs := []string{"-c", options.InitScript} | ||
if options.InitArgs != "" { | ||
|
@@ -118,11 +121,11 @@ func Run(ctx context.Context, options Options) error { | |
options.WorkspaceFolder = f | ||
} | ||
|
||
stageNumber := 1 | ||
stageNumber := 0 | ||
startStage := func(format string, args ...any) func(format string, args ...any) { | ||
now := time.Now() | ||
stageNum := stageNumber | ||
stageNumber++ | ||
stageNum := stageNumber | ||
options.Logger(notcodersdk.LogLevelInfo, "#%d: %s", stageNum, fmt.Sprintf(format, args...)) | ||
|
||
return func(format string, args ...any) { | ||
|
@@ -341,7 +344,7 @@ func Run(ctx context.Context, options Options) error { | |
|
||
HijackLogrus(func(entry *logrus.Entry) { | ||
for _, line := range strings.Split(entry.Message, "\r") { | ||
options.Logger(notcodersdk.LogLevelInfo, "#2: %s", color.HiBlackString(line)) | ||
options.Logger(notcodersdk.LogLevelInfo, "#%d: %s", stageNumber, color.HiBlackString(line)) | ||
} | ||
}) | ||
|
||
|
@@ -471,20 +474,24 @@ func Run(ctx context.Context, options Options) error { | |
cacheTTL = time.Hour * 24 * time.Duration(options.CacheTTLDays) | ||
} | ||
|
||
endStage := startStage("ποΈ Building image...") | ||
// At this point we have all the context, we can now build! | ||
registryMirror := []string{} | ||
if val, ok := os.LookupEnv("KANIKO_REGISTRY_MIRROR"); ok { | ||
registryMirror = strings.Split(val, ";") | ||
} | ||
image, err := executor.DoBuild(&config.KanikoOptions{ | ||
var destinations []string | ||
if options.CacheRepo != "" { | ||
destinations = append(destinations, options.CacheRepo) | ||
} | ||
opts := &config.KanikoOptions{ | ||
// Boilerplate! | ||
CustomPlatform: platforms.Format(platforms.Normalize(platforms.DefaultSpec())), | ||
SnapshotMode: "redo", | ||
RunV2: true, | ||
RunStdout: stdoutWriter, | ||
RunStderr: stderrWriter, | ||
Destinations: []string{"local"}, | ||
Destinations: destinations, | ||
NoPush: !options.PushImage || len(destinations) == 0, | ||
CacheRunLayers: true, | ||
CacheCopyLayers: true, | ||
CompressedCaching: true, | ||
|
@@ -515,11 +522,40 @@ func Run(ctx context.Context, options Options) error { | |
RegistryMirrors: registryMirror, | ||
}, | ||
SrcContext: buildParams.BuildContext, | ||
}) | ||
|
||
// For cached image utilization, produce reproducible builds. | ||
Reproducible: options.PushImage, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is this getting used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See coder/kaniko#12 |
||
} | ||
|
||
if options.GetCachedImage { | ||
endStage := startStage("ποΈ Checking for cached image...") | ||
image, err := executor.DoCacheProbe(opts) | ||
if err != nil { | ||
return nil, xerrors.Errorf("get cached image: %w", err) | ||
} | ||
digest, err := image.Digest() | ||
if err != nil { | ||
return nil, xerrors.Errorf("get cached image digest: %w", err) | ||
} | ||
endStage("ποΈ Found cached image!") | ||
_, _ = fmt.Fprintf(os.Stdout, "%s@%s\n", options.CacheRepo, digest.String()) | ||
os.Exit(0) | ||
} | ||
|
||
endStage := startStage("ποΈ Building image...") | ||
image, err := executor.DoBuild(opts) | ||
if err != nil { | ||
return nil, err | ||
return nil, xerrors.Errorf("do build: %w", err) | ||
} | ||
endStage("ποΈ Built image!") | ||
if options.PushImage { | ||
endStage = startStage("ποΈ Pushing image...") | ||
if err := executor.DoPush(image, opts); err != nil { | ||
return nil, xerrors.Errorf("do push: %w", err) | ||
} | ||
endStage("ποΈ Pushed image!") | ||
} | ||
|
||
return image, err | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm assuming the reason for the logical disjuction here is that Kaniko will push all intermediate cache layers to
destinations
, while specifyingoptions.PushImage
signifies to push only the final result?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.
Yep ππ»