Skip to content

Commit 50f930a

Browse files
authored
Merge pull request #507 from fluxcd/chart-meta-short-sha
2 parents 59dc602 + 5ddeb09 commit 50f930a

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

controllers/helmchart_controller.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,14 +443,32 @@ func (r *HelmChartReconciler) fromTarballArtifact(ctx context.Context, source so
443443
opts.CachedChart = artifact.Path
444444
}
445445

446-
// Add revision metadata to chart build
446+
// Configure revision metadata for chart build if we should react to revision changes
447447
if c.Spec.ReconcileStrategy == sourcev1.ReconcileStrategyRevision {
448-
// Isolate the commit SHA from GitRepository type artifacts by removing the branch/ prefix.
449-
splitRev := strings.Split(source.Revision, "/")
450-
opts.VersionMetadata = splitRev[len(splitRev)-1]
448+
rev := source.Revision
449+
if c.Spec.SourceRef.Kind == sourcev1.GitRepositoryKind {
450+
// Split the reference by the `/` delimiter which may be present,
451+
// and take the last entry which contains the SHA.
452+
split := strings.Split(source.Revision, "/")
453+
rev = split[len(split)-1]
454+
}
455+
if kind := c.Spec.SourceRef.Kind; kind == sourcev1.GitRepositoryKind || kind == sourcev1.BucketKind {
456+
// The SemVer from the metadata is at times used in e.g. the label metadata for a resource
457+
// in a chart, which has a limited length of 63 characters.
458+
// To not fill most of this space with a full length SHA hex (40 characters for SHA-1, and
459+
// even more for SHA-2 for a chart from a Bucket), we shorten this to the first 12
460+
// characters taken from the hex.
461+
// For SHA-1, this has proven to be unique in the Linux kernel with over 875.000 commits
462+
// (http://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#Short-SHA-1).
463+
// Note that for a collision to be problematic, it would need to happen right after the
464+
// previous SHA for the artifact, which is highly unlikely, if not virtually impossible.
465+
// Ref: https://en.wikipedia.org/wiki/Birthday_attack
466+
rev = rev[0:12]
467+
}
468+
opts.VersionMetadata = rev
451469
}
452-
// Set the VersionMetadata to the object's Generation if ValuesFiles is defined
453-
// This ensures changes can be noticed by the Artifact consumer
470+
// Set the VersionMetadata to the object's Generation if ValuesFiles is defined,
471+
// this ensures changes can be noticed by the Artifact consumer
454472
if len(opts.GetValuesFiles()) > 0 {
455473
if opts.VersionMetadata != "" {
456474
opts.VersionMetadata += "."

controllers/helmchart_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ var _ = Describe("HelmChartReconciler", func() {
747747
storage.ArtifactExist(*got.Status.Artifact)
748748
}, timeout, interval).Should(BeTrue())
749749
Expect(got.Status.Artifact.Revision).To(ContainSubstring(updated.Status.Artifact.Revision))
750-
Expect(got.Status.Artifact.Revision).To(ContainSubstring(commit.String()))
750+
Expect(got.Status.Artifact.Revision).To(ContainSubstring(commit.String()[0:12]))
751751
})
752752

753753
When("Setting valid valuesFiles attribute", func() {

0 commit comments

Comments
 (0)