From ed67b42e2b0aab26dcc28eced10958838ba03715 Mon Sep 17 00:00:00 2001 From: Michael Bridgen Date: Tue, 11 Oct 2022 10:39:43 +0100 Subject: [PATCH] Standardise source artifact field access This standardises how the .status.artifact.X fields are accessed, so that (for the sake of a few extra lines) there's less repeated code, and errors are consistent. Signed-off-by: Michael Bridgen --- pkg/controller/stack/flux.go | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/pkg/controller/stack/flux.go b/pkg/controller/stack/flux.go index 0eaab4ec..4cbe1b83 100644 --- a/pkg/controller/stack/flux.go +++ b/pkg/controller/stack/flux.go @@ -7,7 +7,6 @@ import ( "context" "crypto/sha1" "crypto/sha256" - "errors" "fmt" "io" "net/http" @@ -36,19 +35,26 @@ func (sess *reconcileStackSession) SetupWorkdirFromFluxSource(ctx context.Contex // this source artifact fetching code is based closely on // https://github.com/fluxcd/kustomize-controller/blob/db3c321163522259595894ca6c19ed44a876976d/controllers/kustomization_controller.go#L529 - artifactURL, ok, err := unstructured.NestedString(source.Object, "status", "artifact", "url") - if !ok || err != nil { - return "", errors.New("expected source to have .status.artifact.url, but it did not") - } - revision, ok, err := unstructured.NestedString(source.Object, "status", "artifact", "revision") - if !ok || err != nil { - return "", errors.New("did not find revision in .status.artifact") + getField := func(field string) (string, error) { + value, ok, err := unstructured.NestedString(source.Object, "status", "artifact", field) + if !ok || err != nil || value == "" { + return "", fmt.Errorf("expected a non-empty string in .status.artifact.%s", field) + } + return value, nil } - checksum, ok, err := unstructured.NestedString(source.Object, "status", "artifact", "checksum") - if !ok || err != nil { - return "", errors.New("did not find revision in .status.artifact") + artifactURL, err := getField("url") + if err != nil { + return "", err + } + revision, err := getField("revision") + if err != nil { + return "", err + } + checksum, err := getField("checksum") + if err != nil { + return "", err } req, err := http.NewRequestWithContext(ctx, http.MethodGet, artifactURL, nil)