Skip to content

Commit f69fff4

Browse files
committed
Allow configuration of digest algorithm
This introduces a `--snapshot-digest-algo` flag to allow configuring a different algorithm than SHA256. This allows the user to for example configure `blake3`, which is potentially faster (and less resource intensive) on modern hardware. Signed-off-by: Hidde Beydals <hidde@hhh.computer>
1 parent 85cac77 commit f69fff4

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

internal/digest/digest.go

+11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"crypto"
2121
_ "crypto/sha256"
2222
_ "crypto/sha512"
23+
"fmt"
2324

2425
"github.com/opencontainers/go-digest"
2526
_ "github.com/opencontainers/go-digest/blake3"
@@ -39,3 +40,13 @@ func init() {
3940
// Register SHA-1 algorithm for support of legacy values checksums.
4041
digest.RegisterAlgorithm(SHA1, crypto.SHA1)
4142
}
43+
44+
// AlgorithmForName returns the digest algorithm for the given name, or an
45+
// error of type digest.ErrDigestUnsupported if the algorithm is unavailable.
46+
func AlgorithmForName(name string) (digest.Algorithm, error) {
47+
a := digest.Algorithm(name)
48+
if !a.Available() {
49+
return "", fmt.Errorf("%w: %s", digest.ErrDigestUnsupported, name)
50+
}
51+
return a, nil
52+
}

internal/digest/digest_test.go

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
Copyright 2023 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 digest
18+
19+
import (
20+
"errors"
21+
"testing"
22+
23+
. "github.com/onsi/gomega"
24+
"github.com/opencontainers/go-digest"
25+
)
26+
27+
func TestAlgorithmForName(t *testing.T) {
28+
tests := []struct {
29+
name string
30+
want digest.Algorithm
31+
wantErr error
32+
}{
33+
{
34+
name: "sha256",
35+
want: digest.SHA256,
36+
},
37+
{
38+
name: "sha384",
39+
want: digest.SHA384,
40+
},
41+
{
42+
name: "sha512",
43+
want: digest.SHA512,
44+
},
45+
{
46+
name: "blake3",
47+
want: digest.BLAKE3,
48+
},
49+
{
50+
name: "sha1",
51+
want: SHA1,
52+
},
53+
{
54+
name: "not-available",
55+
wantErr: digest.ErrDigestUnsupported,
56+
},
57+
}
58+
for _, tt := range tests {
59+
t.Run(tt.name, func(t *testing.T) {
60+
g := NewWithT(t)
61+
got, err := AlgorithmForName(tt.name)
62+
if tt.wantErr != nil {
63+
g.Expect(err).To(HaveOccurred())
64+
g.Expect(errors.Is(err, tt.wantErr)).To(BeTrue())
65+
return
66+
}
67+
g.Expect(err).ToNot(HaveOccurred())
68+
g.Expect(got).To(Equal(tt.want))
69+
})
70+
}
71+
}

main.go

+15
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ import (
4949
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
5050

5151
v2 "github.com/fluxcd/helm-controller/api/v2beta2"
52+
intdigest "github.com/fluxcd/helm-controller/internal/digest"
53+
5254
// +kubebuilder:scaffold:imports
5355

5456
intacl "github.com/fluxcd/helm-controller/internal/acl"
@@ -95,6 +97,7 @@ func main() {
9597
oomWatchMemoryThreshold uint8
9698
oomWatchMaxMemoryPath string
9799
oomWatchCurrentMemoryPath string
100+
snapshotDigestAlgo string
98101
)
99102

100103
flag.StringVar(&metricsAddr, "metrics-addr", ":8080",
@@ -121,6 +124,8 @@ func main() {
121124
"The path to the cgroup memory limit file. Requires feature gate 'OOMWatch' to be enabled. If not set, the path will be automatically detected.")
122125
flag.StringVar(&oomWatchCurrentMemoryPath, "oom-watch-current-memory-path", "",
123126
"The path to the cgroup current memory usage file. Requires feature gate 'OOMWatch' to be enabled. If not set, the path will be automatically detected.")
127+
flag.StringVar(&snapshotDigestAlgo, "snapshot-digest-algo", intdigest.Canonical.String(),
128+
"The algorithm to use to calculate the digest of Helm release storage snapshots.")
124129

125130
clientOptions.BindFlags(flag.CommandLine)
126131
logOptions.BindFlags(flag.CommandLine)
@@ -180,6 +185,16 @@ func main() {
180185
// Configure the ACL policy.
181186
intacl.AllowCrossNamespaceRef = !aclOptions.NoCrossNamespaceRefs
182187

188+
// Configure the digest algorithm.
189+
if snapshotDigestAlgo != intdigest.Canonical.String() {
190+
algo, err := intdigest.AlgorithmForName(snapshotDigestAlgo)
191+
if err != nil {
192+
setupLog.Error(err, "unable to configure canonical digest algorithm")
193+
os.Exit(1)
194+
}
195+
intdigest.Canonical = algo
196+
}
197+
183198
restConfig := client.GetConfigOrDie(clientOptions)
184199

185200
mgrConfig := ctrl.Options{

0 commit comments

Comments
 (0)