Only parse .dockerignore once
This commit is contained in:
parent
9b01772cde
commit
7fd164deab
|
|
@ -337,6 +337,9 @@ func DoBuild(opts *config.KanikoOptions) (v1.Image, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if err := util.GetExcludedFiles(opts.SrcContext); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
for index, stage := range stages {
|
for index, stage := range stages {
|
||||||
sb, err := newStageBuilder(opts, stage)
|
sb, err := newStageBuilder(opts, stage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -382,6 +382,9 @@ var isSrcValidTests = []struct {
|
||||||
func Test_IsSrcsValid(t *testing.T) {
|
func Test_IsSrcsValid(t *testing.T) {
|
||||||
for _, test := range isSrcValidTests {
|
for _, test := range isSrcValidTests {
|
||||||
t.Run(test.name, func(t *testing.T) {
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
if err := GetExcludedFiles(buildContextPath); err != nil {
|
||||||
|
t.Fatalf("error getting excluded files: %v", err)
|
||||||
|
}
|
||||||
err := IsSrcsValid(test.srcsAndDest, test.resolvedSources, buildContextPath)
|
err := IsSrcsValid(test.srcsAndDest, test.resolvedSources, buildContextPath)
|
||||||
testutil.CheckError(t, test.shouldErr, err)
|
testutil.CheckError(t, test.shouldErr, err)
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,8 @@ var whitelist = []WhitelistEntry{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var excluded []string
|
||||||
|
|
||||||
// GetFSFromImage extracts the layers of img to root
|
// GetFSFromImage extracts the layers of img to root
|
||||||
// It returns a list of all files extracted
|
// It returns a list of all files extracted
|
||||||
func GetFSFromImage(root string, img v1.Image) ([]string, error) {
|
func GetFSFromImage(root string, img v1.Image) ([]string, error) {
|
||||||
|
|
@ -549,14 +551,25 @@ func CopyFile(src, dest, buildcontext string) (bool, error) {
|
||||||
return false, CreateFile(dest, srcFile, fi.Mode(), uid, gid)
|
return false, CreateFile(dest, srcFile, fi.Mode(), uid, gid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetExcludedFiles gets a list of files to exclude from the .dockerignore
|
||||||
|
func GetExcludedFiles(buildcontext string) error {
|
||||||
|
path := filepath.Join(buildcontext, ".dockerignore")
|
||||||
|
if !FilepathExists(path) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
contents, err := ioutil.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "parsing .dockerignore")
|
||||||
|
}
|
||||||
|
reader := bytes.NewBuffer(contents)
|
||||||
|
excluded, err = dockerignore.ReadAll(reader)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// excludeFile returns true if the .dockerignore specified this file should be ignored
|
// excludeFile returns true if the .dockerignore specified this file should be ignored
|
||||||
func excludeFile(path, buildcontext string) bool {
|
func excludeFile(path, buildcontext string) bool {
|
||||||
excluded, err := parseDockerignore(buildcontext)
|
|
||||||
if err != nil {
|
|
||||||
logrus.Errorf("unable to parse dockerignore, including %s in build: %v", path, err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if HasFilepathPrefix(path, buildcontext, false) {
|
if HasFilepathPrefix(path, buildcontext, false) {
|
||||||
|
var err error
|
||||||
path, err = filepath.Rel(buildcontext, path)
|
path, err = filepath.Rel(buildcontext, path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("unable to get relative path, including %s in build: %v", path, err)
|
logrus.Errorf("unable to get relative path, including %s in build: %v", path, err)
|
||||||
|
|
@ -571,20 +584,6 @@ func excludeFile(path, buildcontext string) bool {
|
||||||
return match
|
return match
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseDockerignore returns a list of all paths in .dockerignore
|
|
||||||
func parseDockerignore(buildcontext string) ([]string, error) {
|
|
||||||
path := filepath.Join(buildcontext, ".dockerignore")
|
|
||||||
if !FilepathExists(path) {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
contents, err := ioutil.ReadFile(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "parsing .dockerignore")
|
|
||||||
}
|
|
||||||
reader := bytes.NewBuffer(contents)
|
|
||||||
return dockerignore.ReadAll(reader)
|
|
||||||
}
|
|
||||||
|
|
||||||
// HasFilepathPrefix checks if the given file path begins with prefix
|
// HasFilepathPrefix checks if the given file path begins with prefix
|
||||||
func HasFilepathPrefix(path, prefix string, prefixMatchOnly bool) bool {
|
func HasFilepathPrefix(path, prefix string, prefixMatchOnly bool) bool {
|
||||||
path = filepath.Clean(path)
|
path = filepath.Clean(path)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue