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

controllers: control tar ignores w/ exclude files #55

Merged
merged 1 commit into from
May 13, 2020
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
2 changes: 1 addition & 1 deletion controllers/gitrepository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func (r *GitRepositoryReconciler) sync(ctx context.Context, repository sourcev1.
defer unlock()

// archive artifact and check integrity
err = r.Storage.Archive(artifact, tmpGit, "", true)
err = r.Storage.Archive(artifact, tmpGit, true)
if err != nil {
err = fmt.Errorf("storage archive error: %w", err)
return sourcev1.GitRepositoryNotReady(repository, sourcev1.StorageOperationFailedReason, err.Error()), err
Expand Down
27 changes: 19 additions & 8 deletions controllers/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ import (
"github.com/fluxcd/source-controller/internal/lockedfile"
)

const (
excludeFile = ".sourceignore"
excludeVCS = ".git/,.gitignore,.gitmodules,.gitattributes"
defaultExcludes = "jpg,jpeg,gif,png,wmv,flv,tar.gz,zip"
)

// Storage manages artifacts
type Storage struct {
// BasePath is the local directory path where the source artifacts are stored.
Expand Down Expand Up @@ -112,17 +118,22 @@ func (s *Storage) ArtifactExist(artifact sourcev1.Artifact) bool {
return true
}

// Archive creates a tar.gz to the artifact path from the given dir excluding the provided file extensions
func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, excludes string, integrityCheck bool) error {
if excludes == "" {
excludes = "jpg,jpeg,gif,png,wmv,flv,tar.gz,zip"
hiddeco marked this conversation as resolved.
Show resolved Hide resolved
}

// Archive creates a tar.gz to the artifact path from the given dir excluding any VCS specific
// files and directories, or any of the excludes defined in the excludeFiles.
func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, integrityCheck bool) error {
ctx, cancel := context.WithTimeout(context.Background(), s.Timeout)
defer cancel()

tarExcludes := fmt.Sprintf("--exclude=\\*.{%s} --exclude .git", excludes)
cmd := fmt.Sprintf("cd %s && tar -c %s -f - . | gzip > %s", dir, tarExcludes, artifact.Path)
var tarExcludes []string
if _, err := os.Stat(filepath.Join(dir, excludeFile)); !os.IsNotExist(err) {
tarExcludes = append(tarExcludes, "--exclude-file="+excludeFile)
} else {
tarExcludes = append(tarExcludes, fmt.Sprintf("--exclude=\\*.{%s}", defaultExcludes))
}
for _, excl := range strings.Split(excludeVCS, ",") {
tarExcludes = append(tarExcludes, "--exclude="+excl)
}
cmd := fmt.Sprintf("cd %s && tar -c %s -f - . | gzip > %s", dir, strings.Join(tarExcludes, " "), artifact.Path)
command := exec.CommandContext(ctx, "/bin/sh", "-c", cmd)

err := command.Run()
Expand Down