Skip to content

Commit 2ced1da

Browse files
committed
controllers: control tar ignores w/ exclude files
This commit changes the file excludes for tarballs generated for Git repository artifacts from a fixed set of strings to an exclude file based system. It currently takes `.sourceignore` and `.tarignore` in the root of the given directory into account. In addition to this, it adds the `--exclude-vcs` option to the tar command so that all VCS related files are always excluded.
1 parent 913c2ee commit 2ced1da

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

controllers/gitrepository_controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func (r *GitRepositoryReconciler) sync(ctx context.Context, repository sourcev1.
345345
defer unlock()
346346

347347
// archive artifact and check integrity
348-
err = r.Storage.Archive(artifact, tmpGit, "", true)
348+
err = r.Storage.Archive(artifact, tmpGit, true)
349349
if err != nil {
350350
err = fmt.Errorf("storage archive error: %w", err)
351351
return sourcev1.GitRepositoryNotReady(repository, sourcev1.StorageOperationFailedReason, err.Error()), err

controllers/storage.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ import (
3333
"github.com/fluxcd/source-controller/internal/lockedfile"
3434
)
3535

36+
var excludeFiles = []string{
37+
".sourceignore",
38+
".tarignore",
39+
}
40+
3641
// Storage manages artifacts
3742
type Storage struct {
3843
// BasePath is the local directory path where the source artifacts are stored.
@@ -112,17 +117,17 @@ func (s *Storage) ArtifactExist(artifact sourcev1.Artifact) bool {
112117
return true
113118
}
114119

115-
// Archive creates a tar.gz to the artifact path from the given dir excluding the provided file extensions
116-
func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, excludes string, integrityCheck bool) error {
117-
if excludes == "" {
118-
excludes = "jpg,jpeg,gif,png,wmv,flv,tar.gz,zip"
119-
}
120-
120+
// Archive creates a tar.gz to the artifact path from the given dir excluding any VCS specific
121+
// files and directories, or any of the excludes defined in the excludeFiles.
122+
func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, integrityCheck bool) error {
121123
ctx, cancel := context.WithTimeout(context.Background(), s.Timeout)
122124
defer cancel()
123125

124-
tarExcludes := fmt.Sprintf("--exclude=\\*.{%s} --exclude .git", excludes)
125-
cmd := fmt.Sprintf("cd %s && tar -c %s -f - . | gzip > %s", dir, tarExcludes, artifact.Path)
126+
tarExcludes := []string{"--exclude-vcs"}
127+
for _, f := range excludeFiles {
128+
tarExcludes = append(tarExcludes, "--exclude-file="+f)
129+
}
130+
cmd := fmt.Sprintf("cd %s && tar -c %s -f - . | gzip > %s", dir, strings.Join(tarExcludes, " "), artifact.Path)
126131
command := exec.CommandContext(ctx, "/bin/sh", "-c", cmd)
127132

128133
err := command.Run()

0 commit comments

Comments
 (0)