Skip to content

Commit

Permalink
fix: iterate over existing digests in index
Browse files Browse the repository at this point in the history
Signed-off-by: Mauren Berti <[email protected]>
  • Loading branch information
Mauren Berti committed Oct 3, 2023
1 parent 93a6742 commit 989ff65
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions internal/provider/structure_test_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,38 @@ func (d *StructureTestDataSource) Read(ctx context.Context, req datasource.ReadR
}
}

img, err := desc.Image()
if err != nil {
resp.Diagnostics.AddError("Unable to load image", fmt.Sprintf("Unable to load image for ref %s, got error: %s", data.Digest.ValueString(), err))
return
}
if desc.MediaType.IsIndex() {
index, err := desc.ImageIndex()
if err != nil {
resp.Diagnostics.AddError("Unable to read image index", fmt.Sprintf("Unable to read image index for ref %s, got error: %s", data.Digest.ValueString(), err))
return
}

if err := conds.Check(img); err != nil {
data.TestedRef = basetypes.NewStringValue("")
data.Id = basetypes.NewStringValue("")
resp.Diagnostics.AddError("Image does not match rules", fmt.Sprintf("Image does not match rules:\n%s", err))
return
indexManifest, err := index.IndexManifest()
if err != nil {
resp.Diagnostics.AddError("Unable to read image index manifest", fmt.Sprintf("Unable to read image index manifest for ref %s, got error: %s", data.Digest.ValueString(), err))
return
}

for _, mDesc := range indexManifest.Manifests {
if !mDesc.MediaType.IsImage() {
// ignore if not image
continue
}

img, err := index.Image(mDesc.Digest)
if err != nil {
resp.Diagnostics.AddError("Unable to load image", fmt.Sprintf("Unable to load image for ref %s, got error: %s", data.Digest.ValueString(), err))
return
}

if err := conds.Check(img); err != nil {
data.TestedRef = basetypes.NewStringValue("")
data.Id = basetypes.NewStringValue("")
resp.Diagnostics.AddError("Image does not match rules", fmt.Sprintf("Image does not match rules:\n%s", err))
return
}
}
}

data.TestedRef = data.Digest
Expand Down

0 comments on commit 989ff65

Please sign in to comment.