Skip to content

Commit

Permalink
feat: arm containerdisks
Browse files Browse the repository at this point in the history
Build and push multiple containerdisks with
different CPU architectures (x64 or aarch64).

Jira-Url: https://issues.redhat.com/browse/CNV-38597
Signed-off-by: Ben Oukhanov <[email protected]>
  • Loading branch information
codingben committed Feb 28, 2024
1 parent adba148 commit a4f4ac8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
22 changes: 21 additions & 1 deletion cmd/medius/images/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,15 @@ func (b *buildAndPublish) Do(entry *common.Entry, timestamp time.Time) ([]string
return nil, b.Ctx.Err()
}

b.Log.Info("Building containerdisk index ...")
containerDiskIndex, err := build.ContainerDiskIndex([]v1.Image{containerDisk})
if err != nil {
return nil, fmt.Errorf("error creating the containerdisk index : %v", err)
}

names := prepareTags(timestamp, b.Options.PublishImagesOptions.TargetRegistry, entry, artifactInfo)
for _, name := range names {
if err := b.pushImage(containerDisk, name); err != nil {
if err := b.pushImageIndex(containerDiskIndex, name); err != nil {
return nil, err
}
if errors.Is(b.Ctx.Err(), context.Canceled) {
Expand Down Expand Up @@ -261,6 +267,20 @@ func (b *buildAndPublish) pushImage(containerDisk v1.Image, name string) error {
return nil
}

func (b *buildAndPublish) pushImageIndex(containerDiskIndex v1.ImageIndex, name string) error {
if !b.Options.DryRun {
b.Log.Infof("Pushing %s image index", name)
if err := b.Repo.PushImageIndex(b.Ctx, containerDiskIndex, name); err != nil {
b.Log.WithError(err).Error("Failed to push image image")
return err
}
} else {
b.Log.Infof("Dry run enabled, not pushing %s image index", name)
}

return nil
}

func prepareTags(timestamp time.Time, registry string, entry *common.Entry, artifactDetails *api.ArtifactDetails) []string {
metadata := entry.Artifact.Metadata()
imageName := path.Join(registry, metadata.Describe())
Expand Down
27 changes: 27 additions & 0 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/partial"
"github.com/google/go-containerregistry/pkg/v1/tarball"
)

const (
LabelShaSum = "shasum"
ImageArchitecture = "amd64"
ImageOS = "linux"
)

func ContainerDiskConfig(checksum string, additionalLabels map[string]string) v1.Config {
Expand Down Expand Up @@ -53,6 +55,7 @@ func ContainerDisk(imgPath string, config v1.Config) (v1.Image, error) {

// Modify the config file
cf.Architecture = ImageArchitecture
cf.OS = ImageOS
cf.Config = config

img, err = mutate.ConfigFile(img, cf)
Expand All @@ -62,3 +65,27 @@ func ContainerDisk(imgPath string, config v1.Config) (v1.Image, error) {

return img, nil
}

func ContainerDiskIndex(images []v1.Image) (v1.ImageIndex, error) {
indexAddendum := make([]mutate.IndexAddendum, 0, len(images))

for _, image := range images {
configFile, err := image.ConfigFile()
if err != nil {
return nil, err
}

descriptor, err := partial.Descriptor(image)
if err != nil {
return nil, err
}
descriptor.Platform = configFile.Platform()

indexAddendum = append(indexAddendum, mutate.IndexAddendum{
Add: image,
Descriptor: *descriptor,
})
}

return mutate.AppendManifests(empty.Index, indexAddendum...), nil
}
12 changes: 12 additions & 0 deletions pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"github.com/docker/distribution/registry/api/errcode"
v2 "github.com/docker/distribution/registry/api/v2"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/pkg/errors"
)

Expand All @@ -30,6 +32,7 @@ type ImageInfo struct {
type Repository interface {
ImageMetadata(imgRef string, insecure bool) (*ImageInfo, error)
PushImage(ctx context.Context, img v1.Image, imgRef string) error
PushImageIndex(ctx context.Context, img v1.ImageIndex, imgRef string) error
CopyImage(ctx context.Context, srcRef, dstRef string, insecure bool) error
}

Expand Down Expand Up @@ -82,6 +85,15 @@ func (r RepositoryImpl) PushImage(ctx context.Context, img v1.Image, imgRef stri
return crane.Push(img, imgRef, crane.WithContext(ctx))
}

func (r RepositoryImpl) PushImageIndex(ctx context.Context, imageIndex v1.ImageIndex, imageRef string) error {
ref, err := name.ParseReference(imageRef)
if err != nil {
return err
}

return remote.WriteIndex(ref, imageIndex, remote.WithContext(ctx))
}

func (r RepositoryImpl) CopyImage(ctx context.Context, srcRef, dstRef string, insecure bool) error {
options := []crane.Option{
crane.WithContext(ctx),
Expand Down

0 comments on commit a4f4ac8

Please sign in to comment.