Skip to content

Commit 295422f

Browse files
committed
feat: Add an command line option to exclude root directory in the tarball
Fixes GoogleContainerTools#1375 Signed-off-by: Zhiyuan Chen <[email protected]>
1 parent 16c43bb commit 295422f

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

Diff for: README.md

+5
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ _If you are interested in contributing to kaniko, see
8080
- [Flag `--custom-platform`](#flag---custom-platform)
8181
- [Flag `--digest-file`](#flag---digest-file)
8282
- [Flag `--dockerfile`](#flag---dockerfile)
83+
- [Flag `--exclude-root-dir-tarball`](#flag---exclude-root-dir-tarball)
8384
- [Flag `--force`](#flag---force)
8485
- [Flag `--git`](#flag---git)
8586
- [Flag `--image-name-with-digest-file`](#flag---image-name-with-digest-file)
@@ -891,6 +892,10 @@ the digest to that file, which is picked up by Kubernetes automatically as the
891892

892893
Path to the dockerfile to be built. (default "Dockerfile")
893894

895+
#### Flag `--exclude-root-dir-tarball`
896+
897+
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.
898+
894899
#### Flag `--force`
895900

896901
Force building outside of a container

Diff for: cmd/executor/cmd/root.go

+1
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ func addKanikoOptionsFlags() {
280280
RootCmd.PersistentFlags().VarP(&opts.IgnorePaths, "ignore-path", "", "Ignore these paths when taking a snapshot. Set it repeatedly for multiple paths.")
281281
RootCmd.PersistentFlags().BoolVarP(&opts.ForceBuildMetadata, "force-build-metadata", "", false, "Force add metadata layers to build image")
282282
RootCmd.PersistentFlags().BoolVarP(&opts.SkipPushPermissionCheck, "skip-push-permission-check", "", false, "Skip check of the push permission")
283+
RootCmd.PersistentFlags().BoolVarP(&opts.ExcludeRootDirTarball, "exclude-root-dir-tarball", "", false, "Exclude root directory in tarball")
283284

284285
// Deprecated flags.
285286
RootCmd.PersistentFlags().StringVarP(&opts.SnapshotModeDeprecated, "snapshotMode", "", "", "This flag is deprecated. Please use '--snapshot-mode'.")

Diff for: pkg/config/options.go

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ type KanikoOptions struct {
9191
ForceBuildMetadata bool
9292
InitialFSUnpacked bool
9393
SkipPushPermissionCheck bool
94+
ExcludeRootDirTarball bool
9495
}
9596

9697
type KanikoGitOptions struct {

Diff for: pkg/executor/build.go

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func newStageBuilder(args *dockerfile.BuildArgs, opts *config.KanikoOptions, sta
112112
}
113113
l := snapshot.NewLayeredMap(hasher)
114114
snapshotter := snapshot.NewSnapshotter(l, config.RootDir)
115+
snapshotter.SetExcludeRootDirTarball(opts.ExcludeRootDirTarball)
115116

116117
digest, err := sourceImage.Digest()
117118
if err != nil {

Diff for: pkg/snapshot/snapshot.go

+21-9
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ var snapshotPathPrefix = ""
3939

4040
// Snapshotter holds the root directory from which to take snapshots, and a list of snapshots taken
4141
type Snapshotter struct {
42-
l *LayeredMap
43-
directory string
44-
ignorelist []util.IgnoreListEntry
42+
l *LayeredMap
43+
directory string
44+
ignorelist []util.IgnoreListEntry
45+
excludeRootDirTarball bool
4546
}
4647

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

115116
t := util.NewTar(f)
116117
defer t.Close()
117-
if err := writeToTar(t, filesToAdd, filesToWhiteout); err != nil {
118+
if err := writeToTar(t, filesToAdd, filesToWhiteout, s.excludeRootDirTarball); err != nil {
118119
return "", err
119120
}
120121
return f.Name(), nil
@@ -136,12 +137,17 @@ func (s *Snapshotter) TakeSnapshotFS() (string, error) {
136137
return "", err
137138
}
138139

139-
if err := writeToTar(t, filesToAdd, filesToWhiteOut); err != nil {
140+
if err := writeToTar(t, filesToAdd, filesToWhiteOut, s.excludeRootDirTarball); err != nil {
140141
return "", err
141142
}
142143
return f.Name(), nil
143144
}
144145

146+
// SetExcludeRootDirTarball sets the flag to exclude root directory from the tar archive.
147+
func (s *Snapshotter) SetExcludeRootDirTarball(e bool) {
148+
s.excludeRootDirTarball = e
149+
}
150+
145151
func (s *Snapshotter) getSnashotPathPrefix() string {
146152
if snapshotPathPrefix == "" {
147153
return config.KanikoDir
@@ -230,7 +236,7 @@ func removeObsoleteWhiteouts(deletedFiles map[string]struct{}) (filesToWhiteout
230236
return filesToWhiteout
231237
}
232238

233-
func writeToTar(t util.Tar, files, whiteouts []string) error {
239+
func writeToTar(t util.Tar, files, whiteouts []string, excludeRootDirTarball bool) error {
234240
timer := timing.Start("Writing tar file")
235241
defer timing.DefaultRun.Stop(timer)
236242

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

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

257263
for _, path := range files {
258-
if err := addParentDirectories(t, addedPaths, path); err != nil {
264+
if err := addParentDirectories(t, addedPaths, path, excludeRootDirTarball); err != nil {
259265
return err
260266
}
261267
if _, pathAdded := addedPaths[path]; pathAdded {
262268
continue
263269
}
270+
if path == config.RootDir && excludeRootDirTarball {
271+
continue
272+
}
264273
if err := t.AddFileToTar(path); err != nil {
265274
return err
266275
}
@@ -284,11 +293,14 @@ func parentPathIncludesNonDirectory(path string) (bool, error) {
284293
return false, nil
285294
}
286295

287-
func addParentDirectories(t util.Tar, addedPaths map[string]bool, path string) error {
296+
func addParentDirectories(t util.Tar, addedPaths map[string]bool, path string, excludeRootDirTarball bool) error {
288297
for _, parentPath := range util.ParentDirectories(path) {
289298
if _, pathAdded := addedPaths[parentPath]; pathAdded {
290299
continue
291300
}
301+
if parentPath == config.RootDir && excludeRootDirTarball {
302+
continue
303+
}
292304
if err := t.AddFileToTar(parentPath); err != nil {
293305
return err
294306
}

0 commit comments

Comments
 (0)