This commit is contained in:
Zhiyuan Chen 2025-06-03 19:58:30 -04:00 committed by GitHub
commit 4392ed725d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 29 additions and 9 deletions

View File

@ -88,6 +88,7 @@ _If you are interested in contributing to kaniko, see
- [Flag `--custom-platform`](#flag---custom-platform) - [Flag `--custom-platform`](#flag---custom-platform)
- [Flag `--digest-file`](#flag---digest-file) - [Flag `--digest-file`](#flag---digest-file)
- [Flag `--dockerfile`](#flag---dockerfile) - [Flag `--dockerfile`](#flag---dockerfile)
- [Flag `--exclude-root-dir-tarball`](#flag---exclude-root-dir-tarball)
- [Flag `--force`](#flag---force) - [Flag `--force`](#flag---force)
- [Flag `--git`](#flag---git) - [Flag `--git`](#flag---git)
- [Flag `--image-name-with-digest-file`](#flag---image-name-with-digest-file) - [Flag `--image-name-with-digest-file`](#flag---image-name-with-digest-file)
@ -899,6 +900,10 @@ the digest to that file, which is picked up by Kubernetes automatically as the
Path to the dockerfile to be built. (default "Dockerfile") Path to the dockerfile to be built. (default "Dockerfile")
#### Flag `--exclude-root-dir-tarball`
Set this flag to exclude root directory in tar archive. This can enable tools like Flux that use non-GNU tar to extract images created by Kaniko.
#### Flag `--force` #### Flag `--force`
Force building outside of a container Force building outside of a container

View File

@ -280,6 +280,7 @@ func addKanikoOptionsFlags() {
RootCmd.PersistentFlags().VarP(&opts.IgnorePaths, "ignore-path", "", "Ignore these paths when taking a snapshot. Set it repeatedly for multiple paths.") RootCmd.PersistentFlags().VarP(&opts.IgnorePaths, "ignore-path", "", "Ignore these paths when taking a snapshot. Set it repeatedly for multiple paths.")
RootCmd.PersistentFlags().BoolVarP(&opts.ForceBuildMetadata, "force-build-metadata", "", false, "Force add metadata layers to build image") RootCmd.PersistentFlags().BoolVarP(&opts.ForceBuildMetadata, "force-build-metadata", "", false, "Force add metadata layers to build image")
RootCmd.PersistentFlags().BoolVarP(&opts.SkipPushPermissionCheck, "skip-push-permission-check", "", false, "Skip check of the push permission") RootCmd.PersistentFlags().BoolVarP(&opts.SkipPushPermissionCheck, "skip-push-permission-check", "", false, "Skip check of the push permission")
RootCmd.PersistentFlags().BoolVarP(&opts.ExcludeRootDirTarball, "exclude-root-dir-tarball", "", false, "Exclude root directory in tarball")
// Deprecated flags. // Deprecated flags.
RootCmd.PersistentFlags().StringVarP(&opts.SnapshotModeDeprecated, "snapshotMode", "", "", "This flag is deprecated. Please use '--snapshot-mode'.") RootCmd.PersistentFlags().StringVarP(&opts.SnapshotModeDeprecated, "snapshotMode", "", "", "This flag is deprecated. Please use '--snapshot-mode'.")

View File

@ -91,6 +91,7 @@ type KanikoOptions struct {
ForceBuildMetadata bool ForceBuildMetadata bool
InitialFSUnpacked bool InitialFSUnpacked bool
SkipPushPermissionCheck bool SkipPushPermissionCheck bool
ExcludeRootDirTarball bool
} }
type KanikoGitOptions struct { type KanikoGitOptions struct {

View File

@ -112,6 +112,7 @@ func newStageBuilder(args *dockerfile.BuildArgs, opts *config.KanikoOptions, sta
} }
l := snapshot.NewLayeredMap(hasher) l := snapshot.NewLayeredMap(hasher)
snapshotter := snapshot.NewSnapshotter(l, config.RootDir) snapshotter := snapshot.NewSnapshotter(l, config.RootDir)
snapshotter.SetExcludeRootDirTarball(opts.ExcludeRootDirTarball)
digest, err := sourceImage.Digest() digest, err := sourceImage.Digest()
if err != nil { if err != nil {

View File

@ -39,9 +39,10 @@ var snapshotPathPrefix = ""
// Snapshotter holds the root directory from which to take snapshots, and a list of snapshots taken // Snapshotter holds the root directory from which to take snapshots, and a list of snapshots taken
type Snapshotter struct { type Snapshotter struct {
l *LayeredMap l *LayeredMap
directory string directory string
ignorelist []util.IgnoreListEntry ignorelist []util.IgnoreListEntry
excludeRootDirTarball bool
} }
// NewSnapshotter creates a new snapshotter rooted at d // NewSnapshotter creates a new snapshotter rooted at d
@ -114,7 +115,7 @@ func (s *Snapshotter) TakeSnapshot(files []string, shdCheckDelete bool, forceBui
t := util.NewTar(f) t := util.NewTar(f)
defer t.Close() defer t.Close()
if err := writeToTar(t, filesToAdd, filesToWhiteout); err != nil { if err := writeToTar(t, filesToAdd, filesToWhiteout, s.excludeRootDirTarball); err != nil {
return "", err return "", err
} }
return f.Name(), nil return f.Name(), nil
@ -136,12 +137,17 @@ func (s *Snapshotter) TakeSnapshotFS() (string, error) {
return "", err return "", err
} }
if err := writeToTar(t, filesToAdd, filesToWhiteOut); err != nil { if err := writeToTar(t, filesToAdd, filesToWhiteOut, s.excludeRootDirTarball); err != nil {
return "", err return "", err
} }
return f.Name(), nil return f.Name(), nil
} }
// SetExcludeRootDirTarball sets the flag to exclude root directory from the tar archive.
func (s *Snapshotter) SetExcludeRootDirTarball(e bool) {
s.excludeRootDirTarball = e
}
func (s *Snapshotter) getSnashotPathPrefix() string { func (s *Snapshotter) getSnashotPathPrefix() string {
if snapshotPathPrefix == "" { if snapshotPathPrefix == "" {
return config.KanikoDir return config.KanikoDir
@ -230,7 +236,7 @@ func removeObsoleteWhiteouts(deletedFiles map[string]struct{}) (filesToWhiteout
return filesToWhiteout return filesToWhiteout
} }
func writeToTar(t util.Tar, files, whiteouts []string) error { func writeToTar(t util.Tar, files, whiteouts []string, excludeRootDirTarball bool) error {
timer := timing.Start("Writing tar file") timer := timing.Start("Writing tar file")
defer timing.DefaultRun.Stop(timer) defer timing.DefaultRun.Stop(timer)
@ -246,7 +252,7 @@ func writeToTar(t util.Tar, files, whiteouts []string) error {
continue continue
} }
if err := addParentDirectories(t, addedPaths, path); err != nil { if err := addParentDirectories(t, addedPaths, path, excludeRootDirTarball); err != nil {
return err return err
} }
if err := t.Whiteout(path); err != nil { if err := t.Whiteout(path); err != nil {
@ -255,12 +261,15 @@ func writeToTar(t util.Tar, files, whiteouts []string) error {
} }
for _, path := range files { for _, path := range files {
if err := addParentDirectories(t, addedPaths, path); err != nil { if err := addParentDirectories(t, addedPaths, path, excludeRootDirTarball); err != nil {
return err return err
} }
if _, pathAdded := addedPaths[path]; pathAdded { if _, pathAdded := addedPaths[path]; pathAdded {
continue continue
} }
if path == config.RootDir && excludeRootDirTarball {
continue
}
if err := t.AddFileToTar(path); err != nil { if err := t.AddFileToTar(path); err != nil {
return err return err
} }
@ -284,11 +293,14 @@ func parentPathIncludesNonDirectory(path string) (bool, error) {
return false, nil return false, nil
} }
func addParentDirectories(t util.Tar, addedPaths map[string]bool, path string) error { func addParentDirectories(t util.Tar, addedPaths map[string]bool, path string, excludeRootDirTarball bool) error {
for _, parentPath := range util.ParentDirectories(path) { for _, parentPath := range util.ParentDirectories(path) {
if _, pathAdded := addedPaths[parentPath]; pathAdded { if _, pathAdded := addedPaths[parentPath]; pathAdded {
continue continue
} }
if parentPath == config.RootDir && excludeRootDirTarball {
continue
}
if err := t.AddFileToTar(parentPath); err != nil { if err := t.AddFileToTar(parentPath); err != nil {
return err return err
} }