Skip to content

Commit e880a45

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 include exclusion files files. It currently takes `.sourceignore` and in the root of the given directory into account. In addition to this the Git VCS related files that are ignored have been extended to not only include the .git/ directory, but also the .gitignore, .gitmodules and .gitattributes files. Mimicking part of the --exclude-vcs flag not available on all tar versions.
1 parent 913c2ee commit e880a45

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

controllers/gitrepository_controller.go

Lines changed: 1 addition & 1 deletion
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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ import (
3333
"github.com/fluxcd/source-controller/internal/lockedfile"
3434
)
3535

36+
const (
37+
excludeFile = ".sourceignore"
38+
excludeVCS = ".git/,.gitignore,.gitmodules,.gitattributes"
39+
defaultExcludes = "jpg,jpeg,gif,png,wmv,flv,tar.gz,zip"
40+
)
41+
3642
// Storage manages artifacts
3743
type Storage struct {
3844
// BasePath is the local directory path where the source artifacts are stored.
@@ -112,17 +118,22 @@ func (s *Storage) ArtifactExist(artifact sourcev1.Artifact) bool {
112118
return true
113119
}
114120

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-
121+
// Archive creates a tar.gz to the artifact path from the given dir excluding any VCS specific
122+
// files and directories, or any of the excludes defined in the excludeFiles.
123+
func (s *Storage) Archive(artifact sourcev1.Artifact, dir string, integrityCheck bool) error {
121124
ctx, cancel := context.WithTimeout(context.Background(), s.Timeout)
122125
defer cancel()
123126

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)
127+
var tarExcludes []string
128+
if _, err := os.Stat(filepath.Join(dir, excludeFile)); !os.IsNotExist(err) {
129+
tarExcludes = append(tarExcludes, "--exclude-file="+excludeFile)
130+
} else {
131+
tarExcludes = append(tarExcludes, fmt.Sprintf("--exclude=\\*.{%s}", defaultExcludes))
132+
}
133+
for _, excl := range strings.Split(excludeVCS, ",") {
134+
tarExcludes = append(tarExcludes, "--exclude="+excl)
135+
}
136+
cmd := fmt.Sprintf("cd %s && tar -c %s -f - . | gzip > %s", dir, strings.Join(tarExcludes, " "), artifact.Path)
126137
command := exec.CommandContext(ctx, "/bin/sh", "-c", cmd)
127138

128139
err := command.Run()

0 commit comments

Comments
 (0)