Skip to content

Commit 6b290e2

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 6b290e2

File tree

6 files changed

+27
-13
lines changed

6 files changed

+27
-13
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-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func newStageBuilder(args *dockerfile.BuildArgs, opts *config.KanikoOptions, sta
111111
return nil, err
112112
}
113113
l := snapshot.NewLayeredMap(hasher)
114-
snapshotter := snapshot.NewSnapshotter(l, config.RootDir)
114+
snapshotter := snapshot.NewSnapshotter(l, config.RootDir, opts.ExcludeRootDirTarball)
115115

116116
digest, err := sourceImage.Digest()
117117
if err != nil {

Diff for: pkg/snapshot/snapshot.go

+18-11
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ 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
48-
func NewSnapshotter(l *LayeredMap, d string) *Snapshotter {
49-
return &Snapshotter{l: l, directory: d, ignorelist: util.IgnoreList()}
49+
func NewSnapshotter(l *LayeredMap, d string, excludeRootDirTarball bool) *Snapshotter {
50+
return &Snapshotter{l: l, directory: d, ignorelist: util.IgnoreList(), excludeRootDirTarball: excludeRootDirTarball}
5051
}
5152

5253
// Init initializes a new snapshotter
@@ -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,7 +137,7 @@ 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
@@ -230,7 +231,7 @@ func removeObsoleteWhiteouts(deletedFiles map[string]struct{}) (filesToWhiteout
230231
return filesToWhiteout
231232
}
232233

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

@@ -246,7 +247,7 @@ func writeToTar(t util.Tar, files, whiteouts []string) error {
246247
continue
247248
}
248249

249-
if err := addParentDirectories(t, addedPaths, path); err != nil {
250+
if err := addParentDirectories(t, addedPaths, path, excludeRootDirTarball); err != nil {
250251
return err
251252
}
252253
if err := t.Whiteout(path); err != nil {
@@ -255,12 +256,15 @@ func writeToTar(t util.Tar, files, whiteouts []string) error {
255256
}
256257

257258
for _, path := range files {
258-
if err := addParentDirectories(t, addedPaths, path); err != nil {
259+
if err := addParentDirectories(t, addedPaths, path, excludeRootDirTarball); err != nil {
259260
return err
260261
}
261262
if _, pathAdded := addedPaths[path]; pathAdded {
262263
continue
263264
}
265+
if path == config.RootDir && excludeRootDirTarball {
266+
continue
267+
}
264268
if err := t.AddFileToTar(path); err != nil {
265269
return err
266270
}
@@ -284,11 +288,14 @@ func parentPathIncludesNonDirectory(path string) (bool, error) {
284288
return false, nil
285289
}
286290

287-
func addParentDirectories(t util.Tar, addedPaths map[string]bool, path string) error {
291+
func addParentDirectories(t util.Tar, addedPaths map[string]bool, path string, excludeRootDirTarball bool) error {
288292
for _, parentPath := range util.ParentDirectories(path) {
289293
if _, pathAdded := addedPaths[parentPath]; pathAdded {
290294
continue
291295
}
296+
if parentPath == config.RootDir && excludeRootDirTarball {
297+
continue
298+
}
292299
if err := t.AddFileToTar(parentPath); err != nil {
293300
return err
294301
}

Diff for: pkg/snapshot/snapshot_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ func setUpTest(t *testing.T) (string, *Snapshotter, func(), error) {
658658

659659
// Take the initial snapshot
660660
l := NewLayeredMap(util.Hasher())
661-
snapshotter := NewSnapshotter(l, testDir)
661+
snapshotter := NewSnapshotter(l, testDir, false)
662662
if err := snapshotter.Init(); err != nil {
663663
return "", nil, nil, errors.Wrap(err, "initializing snapshotter")
664664
}

0 commit comments

Comments
 (0)