From c3d5dac4a89d81da57eb9561824d08f0e12db5da Mon Sep 17 00:00:00 2001 From: Max Jonas Werner Date: Fri, 22 Apr 2022 10:16:36 +0200 Subject: [PATCH] fix panic when HelmRepository's artifact size is nil This fixes the immediate issue of the nil pointer dereference but we still haven't isolated the actual cause of the size being nil to begin with. This is ongoing work and as soon as we have boiled that down to the simplest case we will provide a regression test for that case. closes #680 Signed-off-by: Max Jonas Werner Co-authored-by: Hidde Beydals --- controllers/helmrepository_controller.go | 7 +++++-- controllers/helmrepository_controller_test.go | 9 +++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/controllers/helmrepository_controller.go b/controllers/helmrepository_controller.go index 725a3079e..f8c7fd210 100644 --- a/controllers/helmrepository_controller.go +++ b/controllers/helmrepository_controller.go @@ -255,14 +255,17 @@ func (r *HelmRepositoryReconciler) notify(oldObj, newObj *sourcev1.HelmRepositor sourcev1.GroupVersion.Group + "/checksum": newObj.Status.Artifact.Checksum, } - size := units.HumanSize(float64(*newObj.Status.Artifact.Size)) + humanReadableSize := "unknown size" + if size := newObj.Status.Artifact.Size; size != nil { + humanReadableSize = fmt.Sprintf("size %s", units.HumanSize(float64(*size))) + } var oldChecksum string if oldObj.GetArtifact() != nil { oldChecksum = oldObj.GetArtifact().Checksum } - message := fmt.Sprintf("stored fetched index of size %s from '%s'", size, chartRepo.URL) + message := fmt.Sprintf("stored fetched index of %s from '%s'", humanReadableSize, chartRepo.URL) // Notify on new artifact and failure recovery. if oldChecksum != newObj.GetArtifact().Checksum { diff --git a/controllers/helmrepository_controller_test.go b/controllers/helmrepository_controller_test.go index 54171b1cf..7a1e5d593 100644 --- a/controllers/helmrepository_controller_test.go +++ b/controllers/helmrepository_controller_test.go @@ -859,6 +859,15 @@ func TestHelmRepositoryReconciler_notify(t *testing.T) { res: sreconcile.ResultEmpty, resErr: errors.New("some error"), }, + { + name: "new artifact with nil size", + res: sreconcile.ResultSuccess, + resErr: nil, + newObjBeforeFunc: func(obj *sourcev1.HelmRepository) { + obj.Status.Artifact = &sourcev1.Artifact{Revision: "xxx", Checksum: "yyy", Size: nil} + }, + wantEvent: "Normal NewArtifact stored fetched index of unknown size", + }, { name: "new artifact", res: sreconcile.ResultSuccess,