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

fix: node s2i build when node_modules present #1612

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
12 changes: 11 additions & 1 deletion pkg/builders/s2i/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -204,6 +205,9 @@ func (b *Builder) Build(ctx context.Context, f fn.Function) (err error) {

pr, pw := io.Pipe()

// s2i apparently is not excluding the files in --as-dockerfile mode
exclude := regexp.MustCompile(cfg.ExcludeRegExp)

const up = ".." + string(os.PathSeparator)
go func() {
tw := tar.NewWriter(pw)
Expand All @@ -220,6 +224,12 @@ func (b *Builder) Build(ctx context.Context, f fn.Function) (err error) {
return nil
}

p = filepath.ToSlash(p)

if exclude.MatchString(p) {
return nil
}

lnk := ""
if fi.Mode()&fs.ModeSymlink != 0 {
lnk, err = os.Readlink(path)
Expand All @@ -241,7 +251,7 @@ func (b *Builder) Build(ctx context.Context, f fn.Function) (err error) {
if err != nil {
return fmt.Errorf("cannot create tar header: %w", err)
}
hdr.Name = filepath.ToSlash(p)
hdr.Name = p

err = tw.WriteHeader(hdr)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion pkg/builders/s2i/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func TestBuildContextUpload(t *testing.T) {
return types.ImageBuildResponse{}, errors.New("bad content for a.txt")
}
default:
return types.ImageBuildResponse{}, errors.New("unexpected file")
return types.ImageBuildResponse{}, fmt.Errorf("unexpected file or directory: %q", hdr.Name)
}
}
return types.ImageBuildResponse{
Expand All @@ -354,6 +354,10 @@ func TestBuildContextUpload(t *testing.T) {
if err != nil {
return nil, err
}
err = os.Mkdir(filepath.Join(filepath.Dir(config.AsDockerfile), "node_modules"), 0755)
if err != nil {
return nil, err
}

return nil, nil
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ spec:
sed -i "s|^registry:.*$|registry: $(params.REGISTRY)|" "$func_file"
echo "Function image registry: $(params.REGISTRY)"

s2iignore_file="$(dirname "$func_file")/.s2iignore"
[ -f "$s2iignore_file" ] || echo "node_modules" >> "$s2iignore_file"

volumeMounts:
- mountPath: /gen-source
name: gen-source
Expand Down
14 changes: 11 additions & 3 deletions pkg/pipelines/tekton/pipeplines_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,17 @@ func sourcesAsTarStream(f fn.Function) *io.PipeReader {
if err != nil {
return fmt.Errorf("cannot get relative path for symlink: %w", err)
}
}
if strings.HasPrefix(lnk, up) || lnk == ".." {
return fmt.Errorf("link %q points outside source root", p)
if strings.HasPrefix(lnk, up) || lnk == ".." {
return fmt.Errorf("link %q points outside source root", p)
}
} else {
t, err := filepath.Rel(f.Root, filepath.Join(filepath.Dir(p), lnk))
if err != nil {
return fmt.Errorf("cannot get relative path for symlink: %w", err)
}
if strings.HasPrefix(t, up) || t == ".." {
return fmt.Errorf("link %q points outside source root", p)
}
}
}

Expand Down