@@ -39,9 +39,10 @@ var snapshotPathPrefix = ""
39
39
40
40
// Snapshotter holds the root directory from which to take snapshots, and a list of snapshots taken
41
41
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
45
46
}
46
47
47
48
// NewSnapshotter creates a new snapshotter rooted at d
@@ -114,7 +115,7 @@ func (s *Snapshotter) TakeSnapshot(files []string, shdCheckDelete bool, forceBui
114
115
115
116
t := util .NewTar (f )
116
117
defer t .Close ()
117
- if err := writeToTar (t , filesToAdd , filesToWhiteout ); err != nil {
118
+ if err := writeToTar (t , filesToAdd , filesToWhiteout , s . excludeRootDirTarball ); err != nil {
118
119
return "" , err
119
120
}
120
121
return f .Name (), nil
@@ -136,12 +137,17 @@ func (s *Snapshotter) TakeSnapshotFS() (string, error) {
136
137
return "" , err
137
138
}
138
139
139
- if err := writeToTar (t , filesToAdd , filesToWhiteOut ); err != nil {
140
+ if err := writeToTar (t , filesToAdd , filesToWhiteOut , s . excludeRootDirTarball ); err != nil {
140
141
return "" , err
141
142
}
142
143
return f .Name (), nil
143
144
}
144
145
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
+
145
151
func (s * Snapshotter ) getSnashotPathPrefix () string {
146
152
if snapshotPathPrefix == "" {
147
153
return config .KanikoDir
@@ -230,7 +236,7 @@ func removeObsoleteWhiteouts(deletedFiles map[string]struct{}) (filesToWhiteout
230
236
return filesToWhiteout
231
237
}
232
238
233
- func writeToTar (t util.Tar , files , whiteouts []string ) error {
239
+ func writeToTar (t util.Tar , files , whiteouts []string , excludeRootDirTarball bool ) error {
234
240
timer := timing .Start ("Writing tar file" )
235
241
defer timing .DefaultRun .Stop (timer )
236
242
@@ -246,7 +252,7 @@ func writeToTar(t util.Tar, files, whiteouts []string) error {
246
252
continue
247
253
}
248
254
249
- if err := addParentDirectories (t , addedPaths , path ); err != nil {
255
+ if err := addParentDirectories (t , addedPaths , path , excludeRootDirTarball ); err != nil {
250
256
return err
251
257
}
252
258
if err := t .Whiteout (path ); err != nil {
@@ -255,12 +261,15 @@ func writeToTar(t util.Tar, files, whiteouts []string) error {
255
261
}
256
262
257
263
for _ , path := range files {
258
- if err := addParentDirectories (t , addedPaths , path ); err != nil {
264
+ if err := addParentDirectories (t , addedPaths , path , excludeRootDirTarball ); err != nil {
259
265
return err
260
266
}
261
267
if _ , pathAdded := addedPaths [path ]; pathAdded {
262
268
continue
263
269
}
270
+ if path == config .RootDir && excludeRootDirTarball {
271
+ continue
272
+ }
264
273
if err := t .AddFileToTar (path ); err != nil {
265
274
return err
266
275
}
@@ -284,11 +293,14 @@ func parentPathIncludesNonDirectory(path string) (bool, error) {
284
293
return false , nil
285
294
}
286
295
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 {
288
297
for _ , parentPath := range util .ParentDirectories (path ) {
289
298
if _ , pathAdded := addedPaths [parentPath ]; pathAdded {
290
299
continue
291
300
}
301
+ if parentPath == config .RootDir && excludeRootDirTarball {
302
+ continue
303
+ }
292
304
if err := t .AddFileToTar (parentPath ); err != nil {
293
305
return err
294
306
}
0 commit comments