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

feat: add containerimage support, solve #1734 #3305

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion cmd/executor/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,12 @@ func copyDockerfile() error {
return errors.Wrap(err, "copying dockerfile")
}
dockerignorePath := opts.DockerfilePath + ".dockerignore"
if util.FilepathExists(dockerignorePath) {
containerignorePath := opts.DockerfilePath + ".containerignore"
if util.FilepathExists(containerignorePath) {
if _, err := util.CopyFile(containerignorePath, config.DockerfilePath+".containerignore", util.FileContext{}, util.DoNotChangeUID, util.DoNotChangeGID, fs.FileMode(0o600), true); err != nil {
return errors.Wrap(err, "copying Dockerfile.containerignore")
}
} else if util.FilepathExists(dockerignorePath) {
if _, err := util.CopyFile(dockerignorePath, config.DockerfilePath+".dockerignore", util.FileContext{}, util.DoNotChangeUID, util.DoNotChangeGID, fs.FileMode(0o600), true); err != nil {
return errors.Wrap(err, "copying Dockerfile.dockerignore")
}
Expand Down
31 changes: 22 additions & 9 deletions pkg/util/fs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,19 +773,32 @@ func NewFileContextFromDockerfile(dockerfilePath, buildcontext string) (FileCont
return fileContext, nil
}

// getExcludedFiles returns a list of files to exclude from the .dockerignore
func getExcludedFiles(dockerfilePath, buildcontext string) ([]string, error) {
path := dockerfilePath + ".dockerignore"
if !FilepathExists(path) {
func getIgnoreFilePath(dockerfilePath, buildcontext string) (string, error) {
var path string

dockerignorePath := dockerfilePath + ".dockerignore"
containerignorePath := dockerfilePath + ".containerignore"
if FilepathExists(containerignorePath) {
path = containerignorePath
} else if FilepathExists(filepath.Join(buildcontext, ".containerignore")) {
path = filepath.Join(buildcontext, ".containerignore")
} else if FilepathExists(dockerignorePath) {
path = dockerignorePath
} else if FilepathExists(filepath.Join(buildcontext, ".dockerignore")) {
path = filepath.Join(buildcontext, ".dockerignore")
} else {
return "", errors.New("Ignore file not found")
}
if !FilepathExists(path) {
return nil, nil
}
logrus.Infof("Using dockerignore file: %v", path)
return path, nil
}

// getExcludedFiles returns a list of files to exclude from the .containerignore or .dockerignore
func getExcludedFiles(dockerfilePath, buildcontext string) ([]string, error) {
path, _ := getIgnoreFilePath(dockerfilePath, buildcontext)
logrus.Infof("Using ignorefile: %v", path)
contents, err := os.ReadFile(path)
if err != nil {
return nil, errors.Wrap(err, "parsing .dockerignore")
return nil, nil
}
reader := bytes.NewBuffer(contents)
return dockerignore.ReadAll(reader)
Expand Down
79 changes: 79 additions & 0 deletions pkg/util/fs_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -920,6 +921,84 @@ func Test_childDirInSkiplist(t *testing.T) {
}
}

func Test_getIgnoreFilePath(t *testing.T) {
dockerfilePath := filepath.Join(os.TempDir(), "Dockerfile")
buildcontext := filepath.Join(os.TempDir(), "buildcontext")

err := os.MkdirAll(buildcontext, 0755)
if err != nil {
t.Errorf("'%v' folder cannot be created", buildcontext)
}

cleanup := func() {
os.Remove(dockerfilePath + ".containerignore")
os.Remove(dockerfilePath + ".dockerignore")
os.Remove(filepath.Join(buildcontext, ".containerignore"))
os.Remove(filepath.Join(buildcontext, ".dockerignore"))
}

t.Run(".containerignore exists next to Dockerfile", func(t *testing.T) {
err := ioutil.WriteFile(dockerfilePath+".containerignore", []byte("foo"), 0644)
if err != nil {
t.Errorf("cannot create '%v'.containerignore", dockerfilePath)
}
path, err := getIgnoreFilePath(dockerfilePath, buildcontext)
expectedPath := dockerfilePath + ".containerignore"
if err != nil || path != expectedPath {
t.Errorf("expected '%v', got '%v', error: '%v'", expectedPath, path, err)
}

cleanup()
})

t.Run(".containerignore exists in the buildcontext", func(t *testing.T) {
err := ioutil.WriteFile(filepath.Join(buildcontext, ".containerignore"), []byte("foo"), 0644)
if err != nil {
t.Errorf("cannot create '%v'.containerignore", buildcontext)
}
path, err := getIgnoreFilePath(dockerfilePath, buildcontext)
expectedPath := filepath.Join(buildcontext, ".containerignore")
if err != nil || path != expectedPath {
t.Errorf("expected '%v', got '%v', error: '%v'", expectedPath, path, err)
}

cleanup()
})

t.Run(".dockerignore exists next to Dockerfile", func(t *testing.T) {
err := ioutil.WriteFile(dockerfilePath+".dockerignore", []byte("foo"), 0644)
if err != nil {
t.Errorf("cannot create '%v'.dockerignore", dockerfilePath)
}
path, err := getIgnoreFilePath(dockerfilePath, buildcontext)
expectedPath := dockerfilePath + ".dockerignore"
if err != nil || path != expectedPath {
t.Errorf("expected '%v', got '%v', error: '%v'", expectedPath, path, err)
}

cleanup()
})

t.Run(".dockerignore exists in the buildcontext", func(t *testing.T) {
err := ioutil.WriteFile(filepath.Join(buildcontext, ".dockerignore"), []byte("foo"), 0644)
if err != nil {
t.Errorf("cannot create '%v'.dockerignore", buildcontext)
}
path, err := getIgnoreFilePath(dockerfilePath, buildcontext)
expectedPath := filepath.Join(buildcontext, ".dockerignore")
if err != nil || path != expectedPath {
t.Errorf("expected '%v', got '%v', error: '%v'", expectedPath, path, err)
}

cleanup()
})

err = os.RemoveAll(buildcontext)
if err != nil {
t.Errorf("cannot clean '%v' folder", buildcontext)
}
}

func Test_correctDockerignoreFileIsUsed(t *testing.T) {
type args struct {
dockerfilepath string
Expand Down