Skip to content

Commit

Permalink
Standardise source artifact field access
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
squaremo committed Oct 11, 2022
1 parent c467b4d commit ed67b42
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions pkg/controller/stack/flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"
"crypto/sha1"
"crypto/sha256"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ed67b42

Please sign in to comment.