Fix nil pointer dereference in HelmRelease inventory when history is empty

What does this MR do?

Closes #97 (closed)

Fixes a nil pointer dereference panic in getHelmReleaseInventory when processing a HelmRelease that has no history.

This regression was introduced in !544 (merged), which removed the || latest == nil check from the early return condition. The code now checks if latest != nil to set latestVersion, but continues execution without an else clause when latest is nil, leading to a panic when attempting to access latest.Name on line 254 (and 262 in the fallback case).

Changes

Restructured the nil check to return early with an empty inventory when hr.Status.History.Latest() returns nil. This prevents the nil pointer dereference by ensuring we never attempt to access latest.Name or latest.Version when the history is empty.

Reproduce

Dummy HelmRelease

---
apiVersion: source.toolkit.fluxcd.io/v1
kind: HelmRepository
metadata:
  name: test-repo
  namespace: default
spec:
  interval: 5m
  url: https://example.com/nonexistent
---
apiVersion: helm.toolkit.fluxcd.io/v2
kind: HelmRelease
metadata:
  name: test-release
  namespace: default
spec:
  interval: 5m
  chart:
    spec:
      chart: nginx
      version: "1.0.0"
      sourceRef:
        kind: HelmRepository
        name: test-repo

apply the above config and then run the following (before fix applied):

./sylvactl inventory HelmRelease/default/test-release
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x1698dd7]

goroutine 1 [running]:
gitlab.com/sylva-projects/sylva-elements/sylvactl/internal/inventory.getHelmReleaseInventory({0x1f95720, 0x1770e5346d80}, 0x1770e550b188, 0x1770e52f4248)
        /builds/sylva-projects/sylva-elements/sylvactl/internal/inventory/inventory.go:254 +0x2b7
gitlab.com/sylva-projects/sylva-elements/sylvactl/internal/inventory.getFluxInventory({0x1f95720, 0x1770e5346d80}, 0x1770e54aa408, {0x1f9f420, 0x1770e549a720}, 0x1770e52f4248)
        /builds/sylva-projects/sylva-elements/sylvactl/internal/inventory/inventory.go:76 +0x4ef
gitlab.com/sylva-projects/sylva-elements/sylvactl/internal/inventory.formatResourceStatus({0x1f95720, 0x1770e5346d80}, {0x88?}, {0x1fb3638, 0x1770e538b888}, 0x1770e52f4248)
        /builds/sylva-projects/sylva-elements/sylvactl/internal/inventory/printer.go:95 +0x74c
gitlab.com/sylva-projects/sylva-elements/sylvactl/internal/inventory.PrintResourcesStatus({0x1f95720, 0x1770e5346d80}, {0x80?}, 0x1770e52f4248, {0x1770e5453c50, 0x1, 0x0?}, {0x1770e5453c60, 0x1, 0x1})
        /builds/sylva-projects/sylva-elements/sylvactl/internal/inventory/printer.go:45 +0x14f
main.inventoryCmdRun(0x1770e4f47500?, {0x1770e51f1800?, 0x4?, 0x1c3cbfe?})
        /builds/sylva-projects/sylva-elements/sylvactl/cmd/inventory.go:95 +0x4c9
github.com/spf13/cobra.(*Command).execute(0x30b7240, {0x1770e51f17c0, 0x1, 0x1})
        /go/pkg/mod/github.com/spf13/cobra@v1.10.2/command.go:1015 +0xb14
github.com/spf13/cobra.(*Command).ExecuteC(0x30b69a0)
        /go/pkg/mod/github.com/spf13/cobra@v1.10.2/command.go:1148 +0x465
github.com/spf13/cobra.(*Command).Execute(...)
        /go/pkg/mod/github.com/spf13/cobra@v1.10.2/command.go:1071
main.main()
        /builds/sylva-projects/sylva-elements/sylvactl/cmd/main.go:59 +0x4b

After fix applied

./sylvactl inventory HelmRelease/default/test-release
IDENTIFIER                       STATUS     REASON         MESSAGE
HelmRelease/default/test-release InProgress                HelmRelease generation is 1, but latest observed generation is -1
╰┄╴┬┄┄[Conditions]
   ├┄╴Reconciling                True       Progressing    Fulfilling prerequisites
   ╰┄╴Ready                      False      SourceNotReady HelmChart 'default/default-test-release' is not ready: no artifact available for HelmRepository source 'test-repo'
Edited by Nitin Sharma

Merge request reports

Loading
Loading