Skip to content

Commit

Permalink
adds ignore-path command
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Friesen committed Apr 9, 2021
1 parent 0477900 commit 7916482
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
7 changes: 7 additions & 0 deletions cmd/executor/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ var RootCmd = &cobra.Command{
}
// Update ignored paths
util.UpdateInitialIgnoreList(opts.IgnoreVarRun)
for _, p := range opts.IgnorePaths {
util.AddToBaseIgnoreList(util.IgnoreListEntry{
Path: p,
PrefixMatchOnly: false,
})
}
}
return nil
},
Expand Down Expand Up @@ -185,6 +191,7 @@ func addKanikoOptionsFlags() {
RootCmd.PersistentFlags().BoolVarP(&opts.RunV2, "use-new-run", "", false, "Use the experimental run implementation for detecting changes without requiring file system snapshots.")
RootCmd.PersistentFlags().Var(&opts.Git, "git", "Branch to clone if build context is a git repository")
RootCmd.PersistentFlags().BoolVarP(&opts.CacheCopyLayers, "cache-copy-layers", "", false, "Caches copy layers")
RootCmd.PersistentFlags().VarP(&opts.IgnorePaths, "ignore-path", "", "Ignore these paths when taking a snapshot. Set it repeatedly for multiple paths.")
}

// addHiddenFlags marks certain flags as hidden from the executor help text
Expand Down
1 change: 1 addition & 0 deletions pkg/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type KanikoOptions struct {
RunV2 bool
CacheCopyLayers bool
Git KanikoGitOptions
IgnorePaths multiArg
}

type KanikoGitOptions struct {
Expand Down
13 changes: 9 additions & 4 deletions pkg/util/fs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type IgnoreListEntry struct {
PrefixMatchOnly bool
}

var initialIgnoreList = []IgnoreListEntry{
var defaultIgnoreList = []IgnoreListEntry{
{
Path: config.KanikoDir,
PrefixMatchOnly: false,
Expand All @@ -74,7 +74,8 @@ var initialIgnoreList = []IgnoreListEntry{
},
}

var ignorelist = initialIgnoreList
var baseIgnoreList = defaultIgnoreList
var ignorelist = baseIgnoreList

var volumes = []string{}

Expand All @@ -100,6 +101,10 @@ func AddToIgnoreList(entry IgnoreListEntry) {
ignorelist = append(ignorelist, entry)
}

func AddToBaseIgnoreList(entry IgnoreListEntry) {
baseIgnoreList = append(baseIgnoreList, entry)
}

func IncludeWhiteout() FSOpt {
return func(opts *FSConfig) {
opts.includeWhiteout = true
Expand Down Expand Up @@ -414,7 +419,7 @@ func checkIgnoreListRoot(root string) bool {
// Where (5) is the mount point relative to the process's root
// From: https://www.kernel.org/doc/Documentation/filesystems/proc.txt
func DetectFilesystemIgnoreList(path string) error {
ignorelist = initialIgnoreList
ignorelist = baseIgnoreList
volumes = []string{}
f, err := os.Open(path)
if err != nil {
Expand Down Expand Up @@ -899,7 +904,7 @@ func UpdateInitialIgnoreList(ignoreVarRun bool) {
return
}
logrus.Trace("Adding /var/run to initialIgnoreList ")
initialIgnoreList = append(initialIgnoreList, IgnoreListEntry{
baseIgnoreList = append(baseIgnoreList, IgnoreListEntry{
// /var/run is a special case. It's common to mount in /var/run/docker.sock or something similar
// which leads to a special mount on the /var/run/docker.sock file itself, but the directory to exist
// in the image with no way to tell if it came from the base image or not.
Expand Down
25 changes: 20 additions & 5 deletions pkg/util/fs_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ func Test_AddToIgnoreList(t *testing.T) {
}
}

func Test_AddToBaseIgnoreList(t *testing.T) {
t.Cleanup(func() {
baseIgnoreList = defaultIgnoreList
})

AddToBaseIgnoreList(IgnoreListEntry{
Path: "/tmp",
PrefixMatchOnly: false,
})

if !IsInProvidedIgnoreList("/tmp", baseIgnoreList) {
t.Errorf("CheckIgnoreList() = %v, want %v", false, true)
}
}

var tests = []struct {
files map[string]string
directory string
Expand Down Expand Up @@ -1375,16 +1390,16 @@ func TestUpdateSkiplist(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
original := initialIgnoreList
defer func() { initialIgnoreList = original }()
original := baseIgnoreList
defer func() { baseIgnoreList = original }()
UpdateInitialIgnoreList(tt.skipVarRun)
sort.Slice(tt.expected, func(i, j int) bool {
return tt.expected[i].Path < tt.expected[j].Path
})
sort.Slice(initialIgnoreList, func(i, j int) bool {
return initialIgnoreList[i].Path < initialIgnoreList[j].Path
sort.Slice(baseIgnoreList, func(i, j int) bool {
return baseIgnoreList[i].Path < baseIgnoreList[j].Path
})
testutil.CheckDeepEqual(t, tt.expected, initialIgnoreList)
testutil.CheckDeepEqual(t, tt.expected, baseIgnoreList)
})
}
}
Expand Down

0 comments on commit 7916482

Please sign in to comment.