Skip to content

feat: Add an command line option to exclude root directory in the tar… #3425

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ _If you are interested in contributing to kaniko, see
- [Flag `--custom-platform`](#flag---custom-platform)
- [Flag `--digest-file`](#flag---digest-file)
- [Flag `--dockerfile`](#flag---dockerfile)
- [Flag `--exclude-root-dir-tarball`](#flag---exclude-root-dir-tarball)
- [Flag `--force`](#flag---force)
- [Flag `--git`](#flag---git)
- [Flag `--image-name-with-digest-file`](#flag---image-name-with-digest-file)
Expand Down Expand Up @@ -891,6 +892,10 @@ the digest to that file, which is picked up by Kubernetes automatically as the

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`

Force building outside of a container
Expand Down
1 change: 1 addition & 0 deletions cmd/executor/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().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.ExcludeRootDirTarball, "exclude-root-dir-tarball", "", false, "Exclude root directory in tarball")

// Deprecated flags.
RootCmd.PersistentFlags().StringVarP(&opts.SnapshotModeDeprecated, "snapshotMode", "", "", "This flag is deprecated. Please use '--snapshot-mode'.")
Expand Down
1 change: 1 addition & 0 deletions pkg/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type KanikoOptions struct {
ForceBuildMetadata bool
InitialFSUnpacked bool
SkipPushPermissionCheck bool
ExcludeRootDirTarball bool
}

type KanikoGitOptions struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/executor/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func newStageBuilder(args *dockerfile.BuildArgs, opts *config.KanikoOptions, sta
}
l := snapshot.NewLayeredMap(hasher)
snapshotter := snapshot.NewSnapshotter(l, config.RootDir)
snapshotter.SetExcludeRootDirTarball(opts.ExcludeRootDirTarball)

digest, err := sourceImage.Digest()
if err != nil {
Expand Down
30 changes: 21 additions & 9 deletions pkg/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ var snapshotPathPrefix = ""

// Snapshotter holds the root directory from which to take snapshots, and a list of snapshots taken
type Snapshotter struct {
l *LayeredMap
directory string
ignorelist []util.IgnoreListEntry
l *LayeredMap
directory string
ignorelist []util.IgnoreListEntry
excludeRootDirTarball bool
}

// NewSnapshotter creates a new snapshotter rooted at d
Expand Down Expand Up @@ -114,7 +115,7 @@ func (s *Snapshotter) TakeSnapshot(files []string, shdCheckDelete bool, forceBui

t := util.NewTar(f)
defer t.Close()
if err := writeToTar(t, filesToAdd, filesToWhiteout); err != nil {
if err := writeToTar(t, filesToAdd, filesToWhiteout, s.excludeRootDirTarball); err != nil {
return "", err
}
return f.Name(), nil
Expand All @@ -136,12 +137,17 @@ func (s *Snapshotter) TakeSnapshotFS() (string, error) {
return "", err
}

if err := writeToTar(t, filesToAdd, filesToWhiteOut); err != nil {
if err := writeToTar(t, filesToAdd, filesToWhiteOut, s.excludeRootDirTarball); err != nil {
return "", err
}
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 {
if snapshotPathPrefix == "" {
return config.KanikoDir
Expand Down Expand Up @@ -230,7 +236,7 @@ func removeObsoleteWhiteouts(deletedFiles map[string]struct{}) (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")
defer timing.DefaultRun.Stop(timer)

Expand All @@ -246,7 +252,7 @@ func writeToTar(t util.Tar, files, whiteouts []string) error {
continue
}

if err := addParentDirectories(t, addedPaths, path); err != nil {
if err := addParentDirectories(t, addedPaths, path, excludeRootDirTarball); err != nil {
return err
}
if err := t.Whiteout(path); err != nil {
Expand All @@ -255,12 +261,15 @@ func writeToTar(t util.Tar, files, whiteouts []string) error {
}

for _, path := range files {
if err := addParentDirectories(t, addedPaths, path); err != nil {
if err := addParentDirectories(t, addedPaths, path, excludeRootDirTarball); err != nil {
return err
}
if _, pathAdded := addedPaths[path]; pathAdded {
continue
}
if path == config.RootDir && excludeRootDirTarball {
continue
}
if err := t.AddFileToTar(path); err != nil {
return err
}
Expand All @@ -284,11 +293,14 @@ func parentPathIncludesNonDirectory(path string) (bool, error) {
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) {
if _, pathAdded := addedPaths[parentPath]; pathAdded {
continue
}
if parentPath == config.RootDir && excludeRootDirTarball {
continue
}
if err := t.AddFileToTar(parentPath); err != nil {
return err
}
Expand Down