Skip to content
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

Only add whiteout files once #270

Merged
merged 3 commits into from
Aug 2, 2018
Merged
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
1 change: 1 addition & 0 deletions integration/dockerfiles/Dockerfile_test_mv_add
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
FROM busybox@sha256:1bd6df27274fef1dd36eb529d0f4c8033f61c675d6b04213dd913f902f7cafb5
ADD context/tars /tmp/tars
RUN mv /tmp/tars /foo
RUN echo "hi"
24 changes: 22 additions & 2 deletions pkg/snapshot/layered_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (
)

type LayeredMap struct {
layers []map[string]string
hasher func(string) (string, error)
layers []map[string]string
whiteouts []map[string]string
hasher func(string) (string, error)
}

func NewLayeredMap(h func(string) (string, error)) *LayeredMap {
Expand All @@ -35,6 +36,7 @@ func NewLayeredMap(h func(string) (string, error)) *LayeredMap {
}

func (l *LayeredMap) Snapshot() {
l.whiteouts = append(l.whiteouts, map[string]string{})
l.layers = append(l.layers, map[string]string{})
}

Expand Down Expand Up @@ -62,6 +64,24 @@ func (l *LayeredMap) Get(s string) (string, bool) {
return "", false
}

func (l *LayeredMap) GetWhiteout(s string) (string, bool) {
for i := len(l.whiteouts) - 1; i >= 0; i-- {
if v, ok := l.whiteouts[i][s]; ok {
return v, ok
}
}
return "", false
}

func (l *LayeredMap) MaybeAddWhiteout(s string) (bool, error) {
whiteout, ok := l.GetWhiteout(s)
if ok && whiteout == s {
return false, nil
}
l.whiteouts[len(l.whiteouts)-1][s] = s
return true, nil
}

func (l *LayeredMap) MaybeAdd(s string) (bool, error) {
oldV, ok := l.Get(s)
newV, err := l.hasher(s)
Expand Down
23 changes: 15 additions & 8 deletions pkg/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ package snapshot
import (
"archive/tar"
"bytes"
"github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/sirupsen/logrus"
"io"
"io/ioutil"
"os"
"path/filepath"

"github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/sirupsen/logrus"
)

// Snapshotter holds the root directory from which to take snapshots, and a list of snapshots taken
Expand Down Expand Up @@ -103,11 +104,11 @@ func (s *Snapshotter) snapshotFiles(f io.Writer, files []string) (bool, error) {
return false, err
}
// Only add to the tar if we add it to the layeredmap.
maybeAdd, err := s.l.MaybeAdd(file)
addFile, err := s.l.MaybeAdd(file)
if err != nil {
return false, err
}
if maybeAdd {
if addFile {
filesAdded = true
if err := util.AddToTar(file, info, s.hardlinks, w); err != nil {
return false, err
Expand Down Expand Up @@ -141,10 +142,16 @@ func (s *Snapshotter) snapShotFS(f io.Writer) (bool, error) {
// Only add the whiteout if the directory for the file still exists.
dir := filepath.Dir(path)
if _, ok := memFs[dir]; ok {
logrus.Infof("Adding whiteout for %s", path)
filesAdded = true
if err := util.Whiteout(path, w); err != nil {
return false, err
addWhiteout, err := s.l.MaybeAddWhiteout(path)
if err != nil {
return false, nil
}
if addWhiteout {
logrus.Infof("Adding whiteout for %s", path)
filesAdded = true
if err := util.Whiteout(path, w); err != nil {
return false, err
}
}
}
}
Expand Down