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

[7.17](backport #33894) Add support for excluded paths in packaging #33900

Merged
merged 2 commits into from
Nov 30, 2022
Merged
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
22 changes: 19 additions & 3 deletions dev-tools/packaging/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
"strings"
"testing"

"errors"

"github.com/blakesmith/ar"
rpm "github.com/cavaliercoder/go-rpm"
)
Expand All @@ -48,6 +50,7 @@ const (
)

var (
excludedPathsPattern = regexp.MustCompile(`node_modules`)
configFilePattern = regexp.MustCompile(`.*beat\.yml$|apm-server\.yml|elastic-agent\.yml$`)
manifestFilePattern = regexp.MustCompile(`manifest.yml`)
modulesDirPattern = regexp.MustCompile(`module/.+`)
Expand Down Expand Up @@ -528,6 +531,9 @@ func readRPM(rpmFile string) (*packageFile, *rpm.PackageFile, error) {
pf := &packageFile{Name: filepath.Base(rpmFile), Contents: map[string]packageEntry{}}

for _, file := range contents {
if excludedPathsPattern.MatchString(file.Name()) {
continue
}
pe := packageEntry{
File: file.Name(),
Mode: file.Mode(),
Expand Down Expand Up @@ -555,7 +561,7 @@ func readDeb(debFile string, dataBuffer *bytes.Buffer) (*packageFile, error) {
for {
header, err := arReader.Next()
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return nil, err
Expand Down Expand Up @@ -606,12 +612,16 @@ func readTarContents(tarName string, data io.Reader) (*packageFile, error) {
for {
header, err := tarReader.Next()
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return nil, err
}

if excludedPathsPattern.MatchString(header.Name) {
continue
}

p.Contents[header.Name] = packageEntry{
File: header.Name,
UID: header.Uid,
Expand All @@ -632,6 +642,9 @@ func readZip(zipFile string) (*packageFile, error) {

p := &packageFile{Name: filepath.Base(zipFile), Contents: map[string]packageEntry{}}
for _, f := range r.File {
if excludedPathsPattern.MatchString(f.Name) {
continue
}
p.Contents[f.Name] = packageEntry{
File: f.Name,
Mode: f.Mode(),
Expand Down Expand Up @@ -662,7 +675,7 @@ func readDocker(dockerFile string) (*packageFile, *dockerInfo, error) {
for {
header, err := tarReader.Next()
if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
break
}
return nil, nil, err
Expand Down Expand Up @@ -708,6 +721,9 @@ func readDocker(dockerFile string) (*packageFile, *dockerInfo, error) {
if strings.HasPrefix("/"+name, workingDir) || "/"+name == entrypoint {
p.Contents[name] = entry
}
if excludedPathsPattern.MatchString(name) {
continue
}
// Add also licenses
for _, licenseFile := range licenseFiles {
if strings.Contains(name, licenseFile) {
Expand Down