Skip to content

Commit cc9dbd7

Browse files
committed
finish updates to find-images, update unit tests, begin work on find-images --update
1 parent 9534ea6 commit cc9dbd7

File tree

9 files changed

+123
-12
lines changed

9 files changed

+123
-12
lines changed

src/cmd/dev.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,16 @@ func (o *devFindImagesOptions) run(cmd *cobra.Command, args []string) error {
801801
componentDefinition += fmt.Sprintf(" - %s\n", cosignArtifact)
802802
}
803803
}
804+
if len(finding.ArchiveImages) > 0 {
805+
componentDefinition += fmt.Sprintf(" # Archive images - %s\n", finding.ComponentName)
806+
componentDefinition += fmt.Sprintf(" - name: %s\n", finding.ComponentName)
807+
componentDefinition += " imageArchives:\n"
808+
componentDefinition += fmt.Sprintf(" - path: %s\n", finding.Path)
809+
componentDefinition += " images:\n"
810+
for _, image := range finding.ArchiveImages {
811+
componentDefinition += fmt.Sprintf(" - %s\n", image)
812+
}
813+
}
804814
}
805815
fmt.Println(componentDefinition)
806816

src/pkg/images/unpack.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ func GetManifestsFromArchive(ctx context.Context, imageArchive string) ([]ocispe
5959
return manifests, err
6060
}
6161

62-
// FindImagesInManifests takes a list of OCI Descriptors and returns image References
63-
func FindImagesInManifests(manifests []ocispec.Descriptor) ([]string, error) {
62+
// FindImagesInOCIManifests takes a list of OCI Descriptors and returns image References
63+
func FindImagesInOCIManifests(manifests []ocispec.Descriptor) ([]string, error) {
6464
var foundImages []string
6565
for _, manifestDesc := range manifests {
6666
imageName := getRefFromManifest(manifestDesc)

src/pkg/images/unpack_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestGetRefFromManifest(t *testing.T) {
7272
}
7373
}
7474

75-
func TestFindImagesInManifests(t *testing.T) {
75+
func TestFindImagesInOCIManifests(t *testing.T) {
7676
t.Parallel()
7777

7878
testCases := []struct {
@@ -131,7 +131,7 @@ func TestFindImagesInManifests(t *testing.T) {
131131
for _, tc := range testCases {
132132
t.Run(tc.name, func(t *testing.T) {
133133
t.Parallel()
134-
images, err := FindImagesInManifests(tc.manifests)
134+
images, err := FindImagesInOCIManifests(tc.manifests)
135135
if tc.expectErr != nil {
136136
require.ErrorContains(t, err, tc.expectErr.Error())
137137
return

src/pkg/packager/find_images.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import (
88
"errors"
99
"fmt"
1010
"os"
11+
"path"
1112
"path/filepath"
1213
"regexp"
14+
"slices"
1315
"sort"
1416
"strings"
1517
"time"
@@ -69,10 +71,14 @@ type FindImagesOptions struct {
6971
type ComponentImageScan struct {
7072
// ComponentName is the name of the component where the images were found
7173
ComponentName string
74+
// ArchivePath is the path to the imageArchive
75+
Path string
7276
// Matches contains definitively identified container images, such as those in an image: field
7377
Matches []string
7478
// PotentialMatches contains potential container images found by a regex
7579
PotentialMatches []string
80+
// ArchiveImages contains images that are included in Matches, but are provided with an imageArchive
81+
ArchiveImages []string
7682
// CosignArtifacts contains found cosign artifacts for images
7783
CosignArtifacts []string
7884
// WhyResources contains the resources where specific images were found (when Why option is used)
@@ -231,18 +237,16 @@ func FindImages(ctx context.Context, packagePath string, opts FindImagesOptions)
231237
if err != nil {
232238
return nil, err
233239
}
234-
archivePath := pkgPath.BaseDir + archive.Path
240+
archivePath := path.Join(pkgPath.BaseDir, archive.Path)
235241
imageManifests, err := images.GetManifestsFromArchive(ctx, archivePath)
236242
if err != nil {
237243
return nil, fmt.Errorf("failed to retrieve image manifests from archive %s: %w", archive.Path, err)
238244
}
239-
archiveImages, err := images.FindImagesInManifests(imageManifests)
245+
scan.ArchiveImages, err = images.FindImagesInOCIManifests(imageManifests)
240246
if err != nil {
241247
return nil, fmt.Errorf("failed to unpack image archive %s: %w", archive.Path, err)
242248
}
243-
for _, image := range archiveImages {
244-
matchedImages[image] = true
245-
}
249+
scan.Path = archivePath
246250
}
247251

248252
imgCompStart := time.Now()
@@ -255,6 +259,7 @@ func FindImages(ctx context.Context, packagePath string, opts FindImagesOptions)
255259
}
256260

257261
sortedMatchedImages, sortedExpectedImages := getSortedImages(matchedImages, maybeImages)
262+
258263
scan.Matches = sortedMatchedImages
259264

260265
// Handle the "maybes"
@@ -308,6 +313,19 @@ func FindImages(ctx context.Context, packagePath string, opts FindImagesOptions)
308313
componentImageScans = append(componentImageScans, scan)
309314
}
310315

316+
// Create slice of all archive images
317+
var allArchiveImages []string
318+
for _, scan := range componentImageScans {
319+
allArchiveImages = append(allArchiveImages, scan.ArchiveImages...)
320+
}
321+
322+
// Remove images matches if those images are present in any image imageArchives
323+
for i := range componentImageScans {
324+
componentImageScans[i].Matches = slices.DeleteFunc(componentImageScans[i].Matches, func(s string) bool {
325+
return slices.Contains(allArchiveImages, s)
326+
})
327+
}
328+
311329
if opts.Why != "" {
312330
var foundWhyResource bool
313331
for _, componentImageScan := range componentImageScans {

src/pkg/packager/find_images_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,17 @@ func TestFindImages(t *testing.T) {
205205
},
206206
expectedImages: []ComponentImageScan{
207207
{
208-
ComponentName: "image-archive-component",
208+
ComponentName: "manifest-referencing-image-in-archive",
209+
},
210+
{
211+
ComponentName: "manifest-referencing-image-not-in-archive",
209212
Matches: []string{
213+
"docker.io/library/alpine:latest",
214+
},
215+
},
216+
{
217+
ComponentName: "image-archive-component",
218+
ArchiveImages: []string{
210219
"docker.io/library/scratch:latest",
211220
},
212221
},
@@ -224,6 +233,7 @@ func TestFindImages(t *testing.T) {
224233
require.Len(t, tt.expectedImages, len(imagesScans))
225234
for i, expected := range tt.expectedImages {
226235
require.Equal(t, expected.ComponentName, imagesScans[i].ComponentName)
236+
require.ElementsMatch(t, expected.ArchiveImages, imagesScans[i].ArchiveImages)
227237
require.ElementsMatch(t, expected.Matches, imagesScans[i].Matches)
228238
require.ElementsMatch(t, expected.PotentialMatches, imagesScans[i].PotentialMatches)
229239
require.ElementsMatch(t, expected.CosignArtifacts, imagesScans[i].CosignArtifacts)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: alpine
5+
spec:
6+
selector:
7+
matchLabels:
8+
app: alpine
9+
template:
10+
metadata:
11+
labels:
12+
app: alpine
13+
spec:
14+
containers:
15+
- name: alpine
16+
image: docker.io/library/alpine:latest
17+
- name: second-alpine
18+
image: docker.io/library/alpine:latest
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: scrach
5+
spec:
6+
selector:
7+
matchLabels:
8+
app: scratch
9+
template:
10+
metadata:
11+
labels:
12+
app: scratch
13+
spec:
14+
containers:
15+
- name: scratch
16+
image: docker.io/library/scratch:latest

src/pkg/packager/testdata/find-images/image-archives/zarf.yaml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ metadata:
33
name: image-archive
44
description: image archive
55
components:
6+
- name: manifest-referencing-image-in-archive
7+
required: true
8+
manifests:
9+
- name: scratch-manifest
10+
namespace: default
11+
files:
12+
- deployment.yaml
13+
- name: manifest-referencing-image-not-in-archive
14+
required: true
15+
manifests:
16+
- name: alpine-manifest
17+
namespace: default
18+
files:
19+
- deployment-alpine.yaml
620
- name: image-archive-component
721
imageArchives:
8-
- path: ./scratch.tar
22+
- path: scratch.tar

src/pkg/packager/update.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ import (
1818
"github.com/zarf-dev/zarf/src/pkg/packager/layout"
1919
)
2020

21+
type componentImagesPatch struct {
22+
Images ImagesPatch `yaml:"images"`
23+
ImageArchives ImageArchivesPatch `yaml:"imageArchives"`
24+
}
25+
26+
type ImagesPatch struct {
27+
Images []string `yaml:"images"`
28+
}
29+
30+
type ImageArchivesPatch struct {
31+
ImageArchives ImagesPatch `yaml:"imageArchives"`
32+
}
33+
2134
// UpdateImages updates the images field for components in a zarf.yaml
2235
func UpdateImages(ctx context.Context, packagePath string, imagesScans []ComponentImageScan) error {
2336
l := logger.From(ctx)
@@ -68,7 +81,7 @@ func createUpdate(zarfPackage v1alpha1.ZarfPackage, imagesScans []ComponentImage
6881
}
6982

7083
for _, scan := range imagesScans {
71-
if len(scan.Matches)+len(scan.PotentialMatches)+len(scan.CosignArtifacts) == 0 {
84+
if len(scan.Matches)+len(scan.PotentialMatches)+len(scan.CosignArtifacts)+len(scan.ArchiveImages) == 0 {
7285
continue
7386
}
7487

@@ -82,6 +95,18 @@ func createUpdate(zarfPackage v1alpha1.ZarfPackage, imagesScans []ComponentImage
8295
componentMerge := map[string]any{
8396
"images": combined,
8497
}
98+
99+
// componentMerge := ImagesPatch{
100+
// Images: combined,
101+
// }
102+
103+
// if len(scan.ArchiveImages) > 0 {
104+
// patch = componentImagesPatch{
105+
// Images: patch,
106+
// ImageArchives: ImagesPatch{scan.ImageArchives},
107+
// }
108+
// }
109+
85110
componentNode, err := yaml.ValueToNode(componentMerge, yaml.IndentSequence(true))
86111
if err != nil {
87112
return "", fmt.Errorf("failed to create YAML node for component %s: %w", scan.ComponentName, err)

0 commit comments

Comments
 (0)