Skip to content

Commit 7889ff6

Browse files
authored
Merge branch 'main' into main
2 parents 73d2e8a + be5d10e commit 7889ff6

File tree

4 files changed

+98
-41
lines changed

4 files changed

+98
-41
lines changed

controllers/bucket_controller.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,12 @@ func (r *BucketReconciler) auth(ctx context.Context, bucket sourcev1.Bucket) (*m
356356
return minio.New(bucket.Spec.Endpoint, &opt)
357357
}
358358

359+
// checksum calculates the SHA1 checksum of the given root directory.
360+
// It traverses the given root directory and calculates the checksum for any found file, and returns the SHA1 sum of the
361+
// list with relative file paths and their checksums.
359362
func (r *BucketReconciler) checksum(root string) (string, error) {
360-
checksum := ""
361-
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
363+
sum := sha1.New()
364+
if err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
362365
if err != nil {
363366
return err
364367
}
@@ -369,14 +372,16 @@ func (r *BucketReconciler) checksum(root string) (string, error) {
369372
if err != nil {
370373
return err
371374
}
372-
checksum += fmt.Sprintf("%x", sha1.Sum(data))
375+
relPath, err := filepath.Rel(root, path)
376+
if err != nil {
377+
return err
378+
}
379+
sum.Write([]byte(fmt.Sprintf("%x %s\n", sha1.Sum(data), relPath)))
373380
return nil
374-
})
375-
if err != nil {
381+
}); err != nil {
376382
return "", err
377383
}
378-
379-
return fmt.Sprintf("%x", sha1.Sum([]byte(checksum))), nil
384+
return fmt.Sprintf("%x", sum.Sum(nil)), nil
380385
}
381386

382387
// resetStatus returns a modified v1beta1.Bucket and a boolean indicating

controllers/bucket_controller_test.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 controllers
18+
19+
import (
20+
"io/ioutil"
21+
"os"
22+
"path/filepath"
23+
"testing"
24+
)
25+
26+
func TestBucketReconciler_checksum(t *testing.T) {
27+
tests := []struct {
28+
name string
29+
beforeFunc func(root string)
30+
want string
31+
wantErr bool
32+
}{
33+
{
34+
name: "empty root",
35+
want: "da39a3ee5e6b4b0d3255bfef95601890afd80709",
36+
},
37+
{
38+
name: "with file",
39+
beforeFunc: func(root string) {
40+
mockFile(root, "a/b/c.txt", "a dummy string")
41+
},
42+
want: "309a5e6e96b4a7eea0d1cfaabf1be8ec1c063fa0",
43+
},
44+
{
45+
name: "with file in different path",
46+
beforeFunc: func(root string) {
47+
mockFile(root, "a/b.txt", "a dummy string")
48+
},
49+
want: "e28c62b5cc488849950c4355dddc5523712616d4",
50+
},
51+
}
52+
for _, tt := range tests {
53+
t.Run(tt.name, func(t *testing.T) {
54+
root, err := ioutil.TempDir("", "bucket-checksum-")
55+
if err != nil {
56+
t.Fatal(err)
57+
}
58+
defer os.RemoveAll(root)
59+
if tt.beforeFunc != nil {
60+
tt.beforeFunc(root)
61+
}
62+
got, err := (&BucketReconciler{}).checksum(root)
63+
if (err != nil) != tt.wantErr {
64+
t.Errorf("checksum() error = %v, wantErr %v", err, tt.wantErr)
65+
return
66+
}
67+
if got != tt.want {
68+
t.Errorf("checksum() got = %v, want %v", got, tt.want)
69+
}
70+
})
71+
}
72+
}
73+
74+
func mockFile(root, path, content string) error {
75+
filePath := filepath.Join(root, path)
76+
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
77+
panic(err)
78+
}
79+
if err := ioutil.WriteFile(filePath, []byte(content), 0644); err != nil {
80+
panic(err)
81+
}
82+
return nil
83+
}

controllers/helmchart_controller.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ import (
6161

6262
sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
6363
"github.com/fluxcd/source-controller/internal/helm"
64-
"github.com/fluxcd/source-controller/internal/util"
6564
)
6665

6766
// +kubebuilder:rbac:groups=source.toolkit.fluxcd.io,resources=helmcharts,verbs=get;list;watch;create;update;patch;delete
@@ -926,7 +925,7 @@ func (r *HelmChartReconciler) requestsForHelmRepositoryChange(o client.Object) [
926925
// enqueued twice.
927926
var reqs []reconcile.Request
928927
for _, i := range list.Items {
929-
reqs = append(reqs, reconcile.Request{NamespacedName: util.ObjectKey(&i)})
928+
reqs = append(reqs, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&i)})
930929
}
931930
return reqs
932931
}
@@ -955,7 +954,7 @@ func (r *HelmChartReconciler) requestsForGitRepositoryChange(o client.Object) []
955954
// enqueued twice.
956955
var reqs []reconcile.Request
957956
for _, i := range list.Items {
958-
reqs = append(reqs, reconcile.Request{NamespacedName: util.ObjectKey(&i)})
957+
reqs = append(reqs, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&i)})
959958
}
960959
return reqs
961960
}
@@ -984,7 +983,7 @@ func (r *HelmChartReconciler) requestsForBucketChange(o client.Object) []reconci
984983
// enqueued twice.
985984
var reqs []reconcile.Request
986985
for _, i := range list.Items {
987-
reqs = append(reqs, reconcile.Request{NamespacedName: util.ObjectKey(&i)})
986+
reqs = append(reqs, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&i)})
988987
}
989988
return reqs
990989
}

internal/util/util.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)