Skip to content

Commit

Permalink
fix requestForOCIRepository change
Browse files Browse the repository at this point in the history
Use artifact digest instead of revision to validate whether to trigger a
new reconciliation

Signed-off-by: Soule BA <[email protected]>
  • Loading branch information
souleb committed Apr 16, 2024
1 parent 94ccc4f commit 2da06bc
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions internal/controller/helmrelease_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,11 +800,19 @@ func (r *HelmReleaseReconciler) requestsForOCIRrepositoryChange(ctx context.Cont

var reqs []reconcile.Request
for i, hr := range list.Items {
// If the HelmRelease is ready and the revision of the artifact equals to the
// last attempted revision, we should not make a request for this HelmRelease
if conditions.IsReady(&list.Items[i]) && or.GetArtifact().HasRevision(hr.Status.GetLastAttemptedRevision()) {
// If the HelmRelease is ready and the digest of the artifact equals to the
// last attempted revision digest, we should not make a request for this HelmRelease,
// likewise if we cannot retrieve the artifact digest.
digest := extractDigest(or.GetArtifact().Revision)
if digest == "" {
ctrl.LoggerFrom(ctx).Error(fmt.Errorf("wrong digest for %T", or), "failed to get requests for OCIRepository change")
continue
}

if digest == hr.Status.LastAttemptedRevisionDigest {
continue
}

reqs = append(reqs, reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&list.Items[i])})
}
return reqs
Expand Down Expand Up @@ -855,6 +863,9 @@ func getNamespacedName(obj *v2.HelmRelease) (types.NamespacedName, error) {
switch {
case obj.HasChartRef() && !obj.HasChartTemplate():
namespacedName.Namespace = obj.Spec.ChartRef.Namespace
if namespacedName.Namespace == "" {
namespacedName.Namespace = obj.GetNamespace()
}
namespacedName.Name = obj.Spec.ChartRef.Name
case !obj.HasChartRef() && obj.HasChartTemplate():
namespacedName.Namespace = obj.Spec.Chart.GetNamespace(obj.GetNamespace())
Expand Down Expand Up @@ -891,7 +902,7 @@ func mutateChartWithSourceRevision(chart *chart.Chart, source source.Source) (st
}
// algotithm are sha256, sha384, sha512 with the canonical being sha256
// So every digest starts with a sha algorithm and a colon
sha, err := extractDigetString(tagD[1])
sha, err := extractDigestSubString(tagD[1])
if err != nil {
return "", err
}
Expand All @@ -903,7 +914,7 @@ func mutateChartWithSourceRevision(chart *chart.Chart, source source.Source) (st
ociDigest = tagD[1]
default:
// default to the digest
sha, err := extractDigetString(revision)
sha, err := extractDigestSubString(revision)
if err != nil {
return "", err
}
Expand All @@ -918,8 +929,9 @@ func mutateChartWithSourceRevision(chart *chart.Chart, source source.Source) (st
return ociDigest, nil
}

func extractDigetString(revision string) (string, error) {
func extractDigestSubString(revision string) (string, error) {
var sha string
// expects a revision in the <algorithm>:<digest> format
if pair := strings.Split(revision, ":"); len(pair) != 2 {
return "", fmt.Errorf("invalid artifact revision %s", revision)
} else {
Expand All @@ -930,3 +942,17 @@ func extractDigetString(revision string) (string, error) {
}
return sha[0:12], nil
}

func extractDigest(revision string) string {
if strings.Contains(revision, "@") {
// expects a revision in the <version>@<algorithm>:<digest> format
tagD := strings.Split(revision, "@")
if len(tagD) != 2 {
return ""
}
return tagD[1]
} else {
// revision in the <algorithm>:<digest> format
return revision
}
}

0 comments on commit 2da06bc

Please sign in to comment.