Skip to content

Commit c56ed00

Browse files
author
Donnie Adams
authored
Allow adding sections of plan to hash via annotation (#155)
Previously, only the latestVersion, serviceAccountName, and secrets were used in the hash value to track updates. This meant that updating other parts of the plan (like environment variables) would not automatically trigger an update. After this change, if the plan has an annotation (upgrade.cattle.io/digest) with a value of a comma-delimited string of pieces of the plan, they will be included in the hash and tracked for updates.
1 parent 94b9a34 commit c56ed00

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

pkg/apis/upgrade.cattle.io/constants.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ const (
66
// AnnotationTTLSecondsAfterFinished is used to store a fallback value for job.spec.ttlSecondsAfterFinished
77
AnnotationTTLSecondsAfterFinished = GroupName + `/ttl-seconds-after-finished`
88

9+
// AnnotationIncludeInDigest is used to determine parts of the plan to include in the hash for upgrading
10+
// The value should be a comma-delimited string corresponding to the sections of the plan.
11+
// For example, a value of "spec.concurrency,spec.upgrade.envs" will include
12+
// spec.concurrency and spec.upgrade.envs from the plan in the hash to track for upgrades.
13+
AnnotationIncludeInDigest = GroupName + `/digest`
14+
915
// LabelController is the name of the upgrade controller.
1016
LabelController = GroupName + `/controller`
1117

pkg/upgrade/plan/plan.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/sha256"
66
"fmt"
7+
stdhash "hash"
78
"net/http"
89
"os"
910
"path/filepath"
@@ -14,6 +15,7 @@ import (
1415
upgradeapi "github.com/rancher/system-upgrade-controller/pkg/apis/upgrade.cattle.io"
1516
upgradeapiv1 "github.com/rancher/system-upgrade-controller/pkg/apis/upgrade.cattle.io/v1"
1617
"github.com/rancher/wrangler/pkg/crd"
18+
"github.com/rancher/wrangler/pkg/data"
1719
corectlv1 "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
1820
"github.com/rancher/wrangler/pkg/schemas/openapi"
1921
"github.com/sirupsen/logrus"
@@ -61,6 +63,10 @@ func DigestStatus(plan *upgradeapiv1.Plan, secretCache corectlv1.SecretCache) (u
6163
h := sha256.New224()
6264
h.Write([]byte(plan.Status.LatestVersion))
6365
h.Write([]byte(plan.Spec.ServiceAccountName))
66+
if err := addToHashFromAnnotation(h, plan); err != nil {
67+
return plan.Status, err
68+
}
69+
6470
for _, s := range plan.Spec.Secrets {
6571
secret, err := secretCache.Get(plan.Namespace, s.Name)
6672
if err != nil {
@@ -77,6 +83,23 @@ func DigestStatus(plan *upgradeapiv1.Plan, secretCache corectlv1.SecretCache) (u
7783
return plan.Status, nil
7884
}
7985

86+
func addToHashFromAnnotation(h stdhash.Hash, plan *upgradeapiv1.Plan) error {
87+
if plan.Annotations[upgradeapi.AnnotationIncludeInDigest] == "" {
88+
return nil
89+
}
90+
91+
dataMap, err := data.Convert(plan)
92+
if err != nil {
93+
return err
94+
}
95+
96+
for _, entry := range strings.Split(plan.Annotations[upgradeapi.AnnotationIncludeInDigest], ",") {
97+
h.Write([]byte(dataMap.String(strings.Split(entry, ".")...)))
98+
}
99+
100+
return nil
101+
}
102+
80103
func MungeVersion(version string) string {
81104
return strings.ReplaceAll(version, `+`, `-`)
82105
}

0 commit comments

Comments
 (0)