Skip to content

Commit

Permalink
All observeFuncs make take into account existing OCI Digest in snapshots
Browse files Browse the repository at this point in the history
Signed-off-by: Soule BA <bah.soule@gmail.com>
  • Loading branch information
souleb committed Apr 14, 2024
1 parent 48fa9eb commit 4ce7e3c
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 3 deletions.
4 changes: 3 additions & 1 deletion internal/reconcile/rollback_remediation.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ func observeRollback(obj *v2.HelmRelease) storage.ObserveFunc {
for i := range obj.Status.History {
snap := obj.Status.History[i]
if snap.Targets(rls.Name, rls.Namespace, rls.Version) {
newSnap := release.ObservedToSnapshot(release.ObserveRelease(rls))
obs := release.ObserveRelease(rls)
obs.OCIDigest = snap.OCIDigest
newSnap := release.ObservedToSnapshot(obs)
newSnap.SetTestHooks(snap.GetTestHooks())
obj.Status.History[i] = newSnap
return
Expand Down
43 changes: 43 additions & 0 deletions internal/reconcile/rollback_remediation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,4 +613,47 @@ func Test_observeRollback(t *testing.T) {
expect,
}))
})

t.Run("rollback with update to previous deployed with OCI Digest", func(t *testing.T) {
g := NewWithT(t)

previous := &v2.Snapshot{
Name: mockReleaseName,
Namespace: mockReleaseNamespace,
Version: 2,
Status: helmrelease.StatusFailed.String(),
OCIDigest: "sha256:fcdc2b0de1581a3633ada4afee3f918f6eaa5b5ab38c3fef03d5b48d3f85d9f6",
}
latest := &v2.Snapshot{
Name: mockReleaseName,
Namespace: mockReleaseNamespace,
Version: 3,
Status: helmrelease.StatusDeployed.String(),
OCIDigest: "sha256:aedc2b0de1576a3633ada4afee3f918f6eaa5b5ab38c3fef03d5b48d3f85d9f6",
}

obj := &v2.HelmRelease{
Status: v2.HelmReleaseStatus{
History: v2.Snapshots{
latest,
previous,
},
},
}
rls := helmrelease.Mock(&helmrelease.MockReleaseOptions{
Name: previous.Name,
Namespace: previous.Namespace,
Version: previous.Version,
Status: helmrelease.StatusSuperseded,
})
obs := release.ObserveRelease(rls)
obs.OCIDigest = "sha256:fcdc2b0de1581a3633ada4afee3f918f6eaa5b5ab38c3fef03d5b48d3f85d9f6"
expect := release.ObservedToSnapshot(obs)

observeRollback(obj)(rls)
g.Expect(obj.Status.History).To(testutil.Equal(v2.Snapshots{
latest,
expect,
}))
})
}
5 changes: 4 additions & 1 deletion internal/reconcile/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ func observeTest(obj *v2.HelmRelease) storage.ObserveFunc {
}

// Update the latest snapshot with the test result.
tested := release.ObservedToSnapshot(release.ObserveRelease(rls))
latest := obj.Status.History.Latest()
obs := release.ObserveRelease(rls)
obs.OCIDigest = latest.OCIDigest
tested := release.ObservedToSnapshot(obs)
tested.SetTestHooks(release.TestHooksFromRelease(rls))
obj.Status.History[0] = tested
}
Expand Down
32 changes: 32 additions & 0 deletions internal/reconcile/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,38 @@ func Test_observeTest(t *testing.T) {
}))
})

t.Run("test with current OCI Digest", func(t *testing.T) {
g := NewWithT(t)

obj := &v2.HelmRelease{
Status: v2.HelmReleaseStatus{
History: v2.Snapshots{
&v2.Snapshot{
Name: mockReleaseName,
Namespace: mockReleaseNamespace,
Version: 1,
OCIDigest: "sha256:fcdc2b0de1581a3633ada4afee3f918f6eaa5b5ab38c3fef03d5b48d3f85d9f6",
},
},
},
}
rls := testutil.BuildRelease(&helmrelease.MockReleaseOptions{
Name: mockReleaseName,
Namespace: mockReleaseNamespace,
Version: 1,
}, testutil.ReleaseWithHooks(testHookFixtures))

obs := release.ObserveRelease(rls)
obs.OCIDigest = "sha256:fcdc2b0de1581a3633ada4afee3f918f6eaa5b5ab38c3fef03d5b48d3f85d9f6"
expect := release.ObservedToSnapshot(obs)
expect.SetTestHooks(release.TestHooksFromRelease(rls))

observeTest(obj)(rls)
g.Expect(obj.Status.History).To(testutil.Equal(v2.Snapshots{
expect,
}))
})

t.Run("test targeting different version than latest", func(t *testing.T) {
g := NewWithT(t)

Expand Down
4 changes: 3 additions & 1 deletion internal/reconcile/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ func observeUninstall(obj *v2.HelmRelease) storage.ObserveFunc {
for i := range obj.Status.History {
snap := obj.Status.History[i]
if snap.Targets(rls.Name, rls.Namespace, rls.Version) {
newSnap := release.ObservedToSnapshot(release.ObserveRelease(rls))
obs := release.ObserveRelease(rls)
obs.OCIDigest = snap.OCIDigest
newSnap := release.ObservedToSnapshot(obs)
newSnap.SetTestHooks(snap.GetTestHooks())
obj.Status.History[i] = newSnap
return
Expand Down
32 changes: 32 additions & 0 deletions internal/reconcile/uninstall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,4 +702,36 @@ func Test_observeUninstall(t *testing.T) {
current,
}))
})
t.Run("uninstall of current with OCI Digest", func(t *testing.T) {
g := NewWithT(t)

current := &v2.Snapshot{
Name: mockReleaseName,
Namespace: mockReleaseNamespace,
Version: 1,
Status: helmrelease.StatusDeployed.String(),
OCIDigest: "sha256:fcdc2b0de1581a3633ada4afee3f918f6eaa5b5ab38c3fef03d5b48d3f85d9f6",
}
obj := &v2.HelmRelease{
Status: v2.HelmReleaseStatus{
History: v2.Snapshots{
current,
},
},
}
rls := testutil.BuildRelease(&helmrelease.MockReleaseOptions{
Name: current.Name,
Namespace: current.Namespace,
Version: current.Version,
Status: helmrelease.StatusUninstalled,
})
obs := release.ObserveRelease(rls)
obs.OCIDigest = "sha256:fcdc2b0de1581a3633ada4afee3f918f6eaa5b5ab38c3fef03d5b48d3f85d9f6"
expect := release.ObservedToSnapshot(obs)

observeUninstall(obj)(rls)
g.Expect(obj.Status.History).To(testutil.Equal(v2.Snapshots{
expect,
}))
})
}

0 comments on commit 4ce7e3c

Please sign in to comment.