Skip to content

Commit

Permalink
Add CheckImageArchitectures
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Dec 7, 2024
1 parent 7784488 commit 7009a01
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions pkg/lib/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ limitations under the License.
package lib

import (
"errors"
"fmt"
"os"
"strings"

v1 "github.com/google/go-containerregistry/pkg/v1"
"k8s.io/apimachinery/pkg/util/sets"
"sigs.k8s.io/yaml"
)
Expand Down Expand Up @@ -51,6 +53,69 @@ func CheckImageExists(files []string) error {
return nil
}

var desiredArchs = sets.New("amd64", "arm64")

func CheckImageArchitectures(files []string, archSkipList []string) error {
archSkipSet := sets.NewString(archSkipList...)

images, err := LoadImageList(files)
if err != nil {
return err
}

var missing []string
missingArchs := map[string][]string{}
for _, img := range images {
obj, found, err := ImageManifest(img)
if err != nil || !found {
missing = append(missing, img)
continue
}
switch mf := obj.(type) {
case *v1.IndexManifest:
var archs []string
for _, d := range mf.Manifests {
if d.Platform != nil && d.Platform.Architecture != "" {
archs = append(archs, d.Platform.Architecture)
}
}
if missing := sets.List(desiredArchs.Difference(sets.New[string](archs...))); len(missing) > 0 {
missingArchs[img] = missing
} else {
fmt.Println("✔ " + img)
}
case *v1.Manifest:
missingArchs[img] = []string{"arm64"}
default:
missingArchs[img] = []string{"amd64", "arm64"}
}
}

var fail bool
if len(missing) > 0 {
fmt.Println("----------------------------------------")
fmt.Println("Missing Images:")
fmt.Println(strings.Join(missing, "\n"))
fail = true
}

if len(missingArchs) > 0 {
fmt.Println("----------------------------------------")
fmt.Println("Missing Architectures:")
for img, archs := range missingArchs {
fmt.Printf("%s: %v\n", img, archs)
if !archSkipSet.Has(img) {
fail = true
}
}
}

if fail {
return errors.New("missing images and/or architectures")
}
return nil
}

func LoadImageList(files []string) ([]string, error) {
result := sets.New[string]()
for _, filename := range files {
Expand Down

0 comments on commit 7009a01

Please sign in to comment.