Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update find-images output to contain the images' digest #2327

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/pkg/packager/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func (p *Packager) FindImages() (imgMap map[string][]string, err error) {
erroredCharts := []string{}
erroredCosignLookups := []string{}
whyResources := []string{}
erroredShaLookups := []string{}

cwd, err := os.Getwd()
if err != nil {
Expand Down Expand Up @@ -232,7 +233,14 @@ func (p *Packager) FindImages() (imgMap map[string][]string, err error) {
for _, image := range sortedImages {
// Use print because we want this dumped to stdout
imagesMap[component.Name] = append(imagesMap[component.Name], image)
componentDefinition += fmt.Sprintf(" - %s\n", image)
imageWithSha, err := utils.GetImageWithSha(image)
if err != nil {
message.WarnErrf(err, "Problem getting image with sha for %s: %s", image, err.Error())
erroredShaLookups = append(erroredShaLookups, image)
continue
}
message.Debugf("Image with sha: %s", imageWithSha)
componentDefinition += fmt.Sprintf(" - %s\n", imageWithSha)
}
}

Expand Down Expand Up @@ -303,7 +311,7 @@ func (p *Packager) FindImages() (imgMap map[string][]string, err error) {
return nil, err
}

if len(erroredCharts) > 0 || len(erroredCosignLookups) > 0 {
if len(erroredCharts) > 0 || len(erroredCosignLookups) > 0 || len(erroredShaLookups) > 0 {
errMsg := ""
if len(erroredCharts) > 0 {
errMsg = fmt.Sprintf("the following charts had errors: %s", erroredCharts)
Expand All @@ -314,6 +322,12 @@ func (p *Packager) FindImages() (imgMap map[string][]string, err error) {
}
errMsg += fmt.Sprintf("the following images errored on cosign lookups: %s", erroredCosignLookups)
}
if len(erroredShaLookups) > 0 {
if errMsg != "" {
errMsg += "\n"
}
errMsg += fmt.Sprintf("the following images errored on sha lookups: %s", erroredShaLookups)
}
return imagesMap, fmt.Errorf(errMsg)
}

Expand Down
17 changes: 17 additions & 0 deletions src/pkg/utils/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ package utils
import (
"encoding/json"
"fmt"
"github.com/google/go-containerregistry/pkg/crane"
"os"
"path/filepath"
"regexp"

"github.com/defenseunicorns/zarf/src/pkg/transform"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
Expand All @@ -17,6 +19,21 @@ import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

// GetImageWithSha returns the image reference and the sha256 digest of the image.
func GetImageWithSha(imgSrc string) (string, error) {
regex := regexp.MustCompile(`^.*@sha256:[a-f0-9]{64}$`)
if regex.MatchString(imgSrc) {
return imgSrc, nil
}

imgDescriptor, err := crane.Head(imgSrc)
if err != nil {
return "", err
}

return fmt.Sprintf("%s@%s", imgSrc, imgDescriptor.Digest), nil
}

// LoadOCIImage returns a v1.Image with the image ref specified from a location provided, or an error if the image cannot be found.
func LoadOCIImage(imgPath string, refInfo transform.Image) (v1.Image, error) {
// Use the manifest within the index.json to load the specific image we want
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ components:

- name: baseline
images:
- defenseunicorns/zarf-game:multi-tile-dark
- defenseunicorns/zarf-game:multi-tile-dark@sha256:0b694ca1c33afae97b7471488e07968599f1d2470c629f76af67145ca64428af
# Cosign artifacts for images - dos-games - baseline
- index.docker.io/defenseunicorns/zarf-game:sha256-0b694ca1c33afae97b7471488e07968599f1d2470c629f76af67145ca64428af.sig
Loading