Skip to content

Commit 804b04a

Browse files
committed
Check ignore matches before Bucket item downloads
Signed-off-by: Hidde Beydals <hello@hidde.co>
1 parent 47492c4 commit 804b04a

File tree

3 files changed

+99
-49
lines changed

3 files changed

+99
-49
lines changed

controllers/bucket_controller.go

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package controllers
1818

1919
import (
20+
"bytes"
2021
"context"
2122
"crypto/sha1"
2223
"fmt"
@@ -49,6 +50,7 @@ import (
4950
"github.com/fluxcd/pkg/runtime/predicates"
5051

5152
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
53+
"github.com/fluxcd/source-controller/pkg/sourceignore"
5254
)
5355

5456
// +kubebuilder:rbac:groups=source.toolkit.fluxcd.io,resources=buckets,verbs=get;list;watch;create;update;patch;delete
@@ -202,6 +204,9 @@ func (r *BucketReconciler) reconcile(ctx context.Context, bucket sourcev1.Bucket
202204
return sourcev1.BucketNotReady(bucket, sourcev1.BucketOperationFailedReason, err.Error()), err
203205
}
204206

207+
ps := sourceignore.GetPatterns(bytes.NewBufferString(*bucket.Spec.Ignore), nil)
208+
matcher := sourceignore.NewMatcher(ps)
209+
205210
// download bucket content
206211
for object := range s3Client.ListObjects(ctxTimeout, bucket.Spec.BucketName, minio.ListObjectsOptions{
207212
Recursive: true,
@@ -216,6 +221,10 @@ func (r *BucketReconciler) reconcile(ctx context.Context, bucket sourcev1.Bucket
216221
continue
217222
}
218223

224+
if matcher.Match([]string{object.Key}, false) {
225+
continue
226+
}
227+
219228
localPath := filepath.Join(tempDir, object.Key)
220229
err := s3Client.FGetObject(ctxTimeout, bucket.Spec.BucketName, object.Key, localPath, minio.GetObjectOptions{})
221230
if err != nil {

controllers/storage.go

+3-49
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ package controllers
1818

1919
import (
2020
"archive/tar"
21-
"bufio"
22-
"bytes"
2321
"compress/gzip"
2422
"crypto/sha1"
2523
"fmt"
@@ -39,6 +37,7 @@ import (
3937

4038
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
4139
"github.com/fluxcd/source-controller/internal/fs"
40+
"github.com/fluxcd/source-controller/pkg/sourceignore"
4241
)
4342

4443
const (
@@ -159,11 +158,11 @@ func (s *Storage) Archive(artifact *sourcev1.Artifact, dir string, ignore *strin
159158
return fmt.Errorf("invalid dir path: %s", dir)
160159
}
161160

162-
ps, err := loadExcludePatterns(dir, ignore)
161+
ps, err := sourceignore.LoadExcludePatterns(dir, ignore)
163162
if err != nil {
164163
return err
165164
}
166-
matcher := gitignore.NewMatcher(ps)
165+
matcher := sourceignore.NewMatcher(ps)
167166

168167
localPath := s.LocalPath(*artifact)
169168
tf, err := ioutil.TempFile(filepath.Split(localPath))
@@ -400,51 +399,6 @@ func (s *Storage) LocalPath(artifact sourcev1.Artifact) string {
400399
return filepath.Join(s.BasePath, artifact.Path)
401400
}
402401

403-
// getPatterns collects ignore patterns from the given reader and returns them
404-
// as a gitignore.Pattern slice.
405-
func getPatterns(reader io.Reader, path []string) []gitignore.Pattern {
406-
var ps []gitignore.Pattern
407-
scanner := bufio.NewScanner(reader)
408-
409-
for scanner.Scan() {
410-
s := scanner.Text()
411-
if !strings.HasPrefix(s, "#") && len(strings.TrimSpace(s)) > 0 {
412-
ps = append(ps, gitignore.ParsePattern(s, path))
413-
}
414-
}
415-
416-
return ps
417-
}
418-
419-
// loadExcludePatterns loads the excluded patterns from sourceignore or other
420-
// sources.
421-
func loadExcludePatterns(dir string, ignore *string) ([]gitignore.Pattern, error) {
422-
path := strings.Split(dir, "/")
423-
424-
var ps []gitignore.Pattern
425-
for _, p := range strings.Split(excludeVCS, ",") {
426-
ps = append(ps, gitignore.ParsePattern(p, path))
427-
}
428-
429-
if ignore == nil {
430-
all := strings.Join([]string{excludeExt, excludeCI, excludeExtra}, ",")
431-
for _, p := range strings.Split(all, ",") {
432-
ps = append(ps, gitignore.ParsePattern(p, path))
433-
}
434-
435-
if f, err := os.Open(filepath.Join(dir, excludeFile)); err == nil {
436-
defer f.Close()
437-
ps = append(ps, getPatterns(f, path)...)
438-
} else if !os.IsNotExist(err) {
439-
return nil, err
440-
}
441-
} else {
442-
ps = append(ps, getPatterns(bytes.NewBufferString(*ignore), path)...)
443-
}
444-
445-
return ps, nil
446-
}
447-
448402
// newHash returns a new SHA1 hash.
449403
func newHash() hash.Hash {
450404
return sha1.New()

pkg/sourceignore/sourceignore.go

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
Copyright 2021 The Flux authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package sourceignore
18+
19+
import (
20+
"bufio"
21+
"bytes"
22+
"io"
23+
"os"
24+
"path/filepath"
25+
"strings"
26+
27+
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
28+
)
29+
30+
const (
31+
ExcludeFile = ".sourceignore"
32+
ExcludeVCS = ".git/,.gitignore,.gitmodules,.gitattributes"
33+
ExcludeExt = "*.jpg,*.jpeg,*.gif,*.png,*.wmv,*.flv,*.tar.gz,*.zip"
34+
ExcludeCI = ".github/,.circleci/,.travis.yml,.gitlab-ci.yml,appveyor.yml,.drone.yml,cloudbuild.yaml,codeship-services.yml,codeship-steps.yml"
35+
ExcludeExtra = "**/.goreleaser.yml,**/.sops.yaml,**/.flux.yaml"
36+
)
37+
38+
// NewMatcher returns a gitignore.Matcher for the given gitignore.Pattern
39+
// slice. It mainly exists to compliment the API.
40+
func NewMatcher(ps []gitignore.Pattern) gitignore.Matcher {
41+
return gitignore.NewMatcher(ps)
42+
}
43+
44+
// GetPatterns collects ignore patterns from the given reader and
45+
// returns them as a gitignore.Pattern slice.
46+
func GetPatterns(reader io.Reader, path []string) []gitignore.Pattern {
47+
var ps []gitignore.Pattern
48+
scanner := bufio.NewScanner(reader)
49+
50+
for scanner.Scan() {
51+
s := scanner.Text()
52+
if !strings.HasPrefix(s, "#") && len(strings.TrimSpace(s)) > 0 {
53+
ps = append(ps, gitignore.ParsePattern(s, path))
54+
}
55+
}
56+
57+
return ps
58+
}
59+
60+
// LoadExcludePatterns loads the excluded patterns from .sourceignore or other
61+
// sources and returns the gitignore.Pattern slice.
62+
func LoadExcludePatterns(dir string, ignore *string) ([]gitignore.Pattern, error) {
63+
path := strings.Split(dir, "/")
64+
65+
var ps []gitignore.Pattern
66+
for _, p := range strings.Split(ExcludeVCS, ",") {
67+
ps = append(ps, gitignore.ParsePattern(p, path))
68+
}
69+
70+
if ignore == nil {
71+
all := strings.Join([]string{ExcludeExt, ExcludeCI, ExcludeExtra}, ",")
72+
for _, p := range strings.Split(all, ",") {
73+
ps = append(ps, gitignore.ParsePattern(p, path))
74+
}
75+
76+
if f, err := os.Open(filepath.Join(dir, ExcludeFile)); err == nil {
77+
defer f.Close()
78+
ps = append(ps, GetPatterns(f, path)...)
79+
} else if !os.IsNotExist(err) {
80+
return nil, err
81+
}
82+
} else {
83+
ps = append(ps, GetPatterns(bytes.NewBufferString(*ignore), path)...)
84+
}
85+
86+
return ps, nil
87+
}

0 commit comments

Comments
 (0)