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 26, 2024
1 parent adba148 commit 9963951
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
16 changes: 16 additions & 0 deletions cmd/medius/images/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func (b *buildAndPublish) Do(entry *common.Entry, timestamp time.Time) ([]string
defer os.Remove(file)

b.Log.Info("Building containerdisk ...")
// TODO: Fix arguments
containerDisk, err := build.ContainerDisk(file, build.ContainerDiskConfig(artifactInfo.SHA256Sum, metadata.AdditionalLabels))
if err != nil {
return nil, fmt.Errorf("error creating the containerdisk : %v", err)
Expand All @@ -144,6 +145,7 @@ func (b *buildAndPublish) Do(entry *common.Entry, timestamp time.Time) ([]string

names := prepareTags(timestamp, b.Options.PublishImagesOptions.TargetRegistry, entry, artifactInfo)
for _, name := range names {
// TODO: Use b.pushImageIndex
if err := b.pushImage(containerDisk, name); err != nil {
return nil, err
}
Expand Down Expand Up @@ -261,6 +263,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
34 changes: 30 additions & 4 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ 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"
LabelShaSum = "shasum"
ImageOS = "linux"
)

func ContainerDiskConfig(checksum string, additionalLabels map[string]string) v1.Config {
Expand All @@ -34,7 +35,7 @@ func ContainerDiskConfig(checksum string, additionalLabels map[string]string) v1
return v1.Config{Labels: labels, Env: env}
}

func ContainerDisk(imgPath string, config v1.Config) (v1.Image, error) {
func ContainerDisk(imgPath, imageArchitecture string, config v1.Config) (v1.Image, error) {
img := empty.Image
layer, err := tarball.LayerFromOpener(StreamLayerOpener(imgPath))
if err != nil {
Expand All @@ -52,7 +53,8 @@ func ContainerDisk(imgPath string, config v1.Config) (v1.Image, error) {
}

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

img, err = mutate.ConfigFile(img, cf)
Expand All @@ -62,3 +64,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 9963951

Please sign in to comment.