new implementation for run

This commit is contained in:
Tejal Desai 2020-08-13 13:14:40 -07:00
parent 1bf66ef435
commit 11024f258d
3 changed files with 59 additions and 31 deletions

View File

@ -88,9 +88,9 @@ integration-test-misc:
.PHONY: images
images:
docker build ${BUILD_ARG} --build-arg=GOARCH=$(GOARCH) -t $(REGISTRY)/executor:latest-1317 -f deploy/Dockerfile .
docker build ${BUILD_ARG} --build-arg=GOARCH=$(GOARCH) -t $(REGISTRY)/executor:debug-1317 -f deploy/Dockerfile_debug .
docker build ${BUILD_ARG} --build-arg=GOARCH=$(GOARCH) -t $(REGISTRY)/warmer:latest-1317 -f deploy/Dockerfile_warmer .
docker build ${BUILD_ARG} --build-arg=GOARCH=$(GOARCH) -t $(REGISTRY)/executor:latest -f deploy/Dockerfile .
docker build ${BUILD_ARG} --build-arg=GOARCH=$(GOARCH) -t $(REGISTRY)/executor:debug -f deploy/Dockerfile_debug .
docker build ${BUILD_ARG} --build-arg=GOARCH=$(GOARCH) -t $(REGISTRY)/warmer:latest -f deploy/Dockerfile_warmer .
.PHONY: push
push:

View File

@ -17,10 +17,7 @@ limitations under the License.
package commands
import (
"fmt"
"io/ioutil"
"os"
"time"
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/GoogleContainerTools/kaniko/pkg/util"
@ -38,33 +35,12 @@ type RunMarkerCommand struct {
func (r *RunMarkerCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
// run command `touch filemarker`
logrus.Debugf("using new RunMarker command")
markerFile, err := ioutil.TempFile("", "marker")
if err != nil {
return fmt.Errorf("could not place a marker file")
}
defer func() {
os.Remove(markerFile.Name())
}()
markerInfo, err := os.Stat(markerFile.Name())
if err != nil {
return fmt.Errorf("could not place a marker file")
}
// introduce a delay
time.Sleep(time.Second)
prevFilesMap, _ := util.GetFSInfoMap("/", map[string]os.FileInfo{})
if err := runCommandInExec(config, buildArgs, r.cmd); err != nil {
return err
}
_, r.Files = util.GetFSInfoMap("/", prevFilesMap)
// run command find to find all new files generate
isNewer := func(p string) (bool, error) {
fi, err := os.Stat(p)
if err != nil {
logrus.Warnf("error retrieving stat for file %s: %v", p, err)
return false, nil
}
return fi.ModTime().After(markerInfo.ModTime()), nil
}
r.Files, _ = util.WalkFS("/", map[string]struct{}{}, isNewer)
logrus.Debugf("files changed %s", r.Files)
return nil
}

View File

@ -883,7 +883,10 @@ func UpdateInitialIgnoreList(ignoreVarRun bool) {
})
}
func WalkFS(dir string, existingPaths map[string]struct{}, f func(string) (bool, error)) ([]string, map[string]struct{}) {
// WalkFS given a directory and list of existing files,
// returns a list of changed filed determined by changeFunc and a list
// of deleted files.
func WalkFS(dir string, existingPaths map[string]struct{}, changeFunc func(string) (bool, error)) ([]string, map[string]struct{}) {
foundPaths := make([]string, 0)
timer := timing.Start("Walking filesystem")
godirwalk.Walk(dir, &godirwalk.Options{
@ -897,7 +900,7 @@ func WalkFS(dir string, existingPaths map[string]struct{}, f func(string) (bool,
return nil
}
delete(existingPaths, path)
if t, err := f(path); err != nil {
if t, err := changeFunc(path); err != nil {
return err
} else if t {
foundPaths = append(foundPaths, path)
@ -910,3 +913,52 @@ func WalkFS(dir string, existingPaths map[string]struct{}, f func(string) (bool,
timing.DefaultRun.Stop(timer)
return foundPaths, existingPaths
}
// GetFSInfoMap given a directory gets a map of FileInfo for all files
func GetFSInfoMap(dir string, existing map[string]os.FileInfo) (map[string]os.FileInfo, []string) {
fileMap := map[string]os.FileInfo{}
foundPaths := []string{}
timer := timing.Start("Walking filesystem with Stat")
godirwalk.Walk(dir, &godirwalk.Options{
Callback: func(path string, ent *godirwalk.Dirent) error {
if IsInIgnoreList(path) {
if IsDestDir(path) {
logrus.Tracef("Skipping paths under %s, as it is a ignored directory", path)
return filepath.SkipDir
}
return nil
}
if fi, err := os.Lstat(path); err == nil {
if fiPrevious, ok := existing[path]; ok {
// check if file changed
if !isSame(fiPrevious, fi) {
fileMap[path] = fi
foundPaths = append(foundPaths, path)
}
} else {
// new path
fileMap[path] = fi
foundPaths = append(foundPaths, path)
}
}
return nil
},
Unsorted: true,
},
)
timing.DefaultRun.Stop(timer)
return fileMap, foundPaths
}
func isSame(fi1, fi2 os.FileInfo) bool {
return fi1.Mode() == fi2.Mode() &&
// file modification time
fi1.ModTime() == fi2.ModTime() &&
// file size
fi1.Size() == fi2.Size() &&
// file user id
uint64(fi1.Sys().(*syscall.Stat_t).Uid) == uint64(fi2.Sys().(*syscall.Stat_t).Uid) &&
// file group id is
uint64(fi1.Sys().(*syscall.Stat_t).Gid) == uint64(fi2.Sys().(*syscall.Stat_t).Gid)
}