Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

container: include the architecture of the image in container.Spec #585

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pkg/arch/arch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import (
type Arch uint64

const ( // architecture enum
ARCH_AARCH64 Arch = iota
ARCH_UNSET Arch = iota
ARCH_AARCH64
ARCH_PPC64LE
ARCH_S390X
ARCH_X86_64
)

func (a Arch) String() string {
switch a {
case ARCH_UNSET:
return "unset"
case ARCH_AARCH64:
return "aarch64"
case ARCH_PPC64LE:
Expand Down
18 changes: 16 additions & 2 deletions pkg/container/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
_ "github.com/containers/image/v5/oci/layout"
"golang.org/x/sys/unix"

"github.com/osbuild/images/internal/common"

"github.com/containers/common/pkg/retry"
"github.com/containers/image/v5/copy"
"github.com/containers/image/v5/docker"
Expand All @@ -30,6 +28,9 @@ import (
"github.com/opencontainers/go-digest"

imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"

"github.com/osbuild/images/internal/common"
"github.com/osbuild/images/pkg/arch"
)

const (
Expand Down Expand Up @@ -334,6 +335,7 @@ func (cl *Client) UploadImage(ctx context.Context, from, tag string) (digest.Dig
type RawManifest struct {
Data []byte
MimeType string
Arch arch.Arch
}

// Digest computes the digest from the raw manifest data
Expand Down Expand Up @@ -368,6 +370,17 @@ func (cl *Client) GetManifest(ctx context.Context, digest digest.Digest, local b
if err != nil {
return
}
img, err := ref.NewImage(ctx, cl.sysCtx)
if err != nil {
return r, err
}
defer img.Close()

info, err := img.Inspect(ctx)
if err != nil {
return r, err
}
r.Arch = arch.FromString(info.Architecture)

src, err := ref.NewImageSource(ctx, cl.sysCtx)
if err != nil {
Expand Down Expand Up @@ -515,6 +528,7 @@ func (cl *Client) Resolve(ctx context.Context, name string, local bool) (Spec, e
name,
local,
)
spec.Arch = raw.Arch

return spec, nil
}
6 changes: 5 additions & 1 deletion pkg/container/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"testing"
"time"

"github.com/osbuild/images/pkg/container"
"github.com/stretchr/testify/assert"

"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/container"
)

//
Expand Down Expand Up @@ -45,6 +47,7 @@ func TestClientResolve(t *testing.T) {
TLSVerify: client.GetTLSVerify(),
LocalName: client.Target.String(),
ListDigest: listDigest,
Arch: arch.ARCH_X86_64,
}, spec)

client.SetArchitectureChoice("ppc64le")
Expand All @@ -58,6 +61,7 @@ func TestClientResolve(t *testing.T) {
TLSVerify: client.GetTLSVerify(),
LocalName: client.Target.String(),
ListDigest: listDigest,
Arch: arch.ARCH_PPC64LE,
}, spec)

// don't have that architecture
Expand Down
12 changes: 7 additions & 5 deletions pkg/container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import (

"github.com/opencontainers/go-digest"

"github.com/osbuild/images/internal/common"
"github.com/osbuild/images/pkg/container"

"github.com/containers/image/v5/docker/reference"
"github.com/containers/image/v5/manifest"

"github.com/osbuild/images/internal/common"
"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/container"
)

const rootLayer = `H4sIAAAJbogA/+SWUYqDMBCG53lP4V5g9x8dzRX2Bvtc0VIhEIhKe/wSKxgU6ktjC/O9hMzAQDL8
Expand Down Expand Up @@ -308,7 +309,7 @@ func (reg *Registry) GetRef(repo string) string {
return fmt.Sprintf("%s/%s", reg.server.Listener.Addr().String(), repo)
}

func (reg *Registry) Resolve(target, arch string) (container.Spec, error) {
func (reg *Registry) Resolve(target string, imgArch arch.Arch) (container.Spec, error) {

ref, err := reference.ParseNormalizedNamed(target)
if err != nil {
Expand Down Expand Up @@ -354,7 +355,7 @@ func (reg *Registry) Resolve(target, arch string) (container.Spec, error) {
checksum = ""

for _, m := range lst.Manifests {
if m.Platform.Architecture == arch {
if arch.FromString(m.Platform.Architecture) == imgArch {
checksum = m.Digest.String()
break
}
Expand All @@ -377,6 +378,7 @@ func (reg *Registry) Resolve(target, arch string) (container.Spec, error) {
LocalName: target,
TLSVerify: common.ToPtr(false),
ListDigest: listDigest,
Arch: imgArch,
}, nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/container/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type SourceSpec struct {
Local bool
}

// XXX: use arch.Arch here?
func NewResolver(arch string) *Resolver {
return &Resolver{
ctx: context.Background(),
Expand Down
4 changes: 2 additions & 2 deletions pkg/container/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"github.com/stretchr/testify/assert"

"github.com/osbuild/images/internal/common"
"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/container"
)

func TestResolver(t *testing.T) {

registry := NewTestRegistry()
defer registry.Close()

repo := registry.AddRepo("library/osbuild")
ref := registry.GetRef("library/osbuild")

Expand Down Expand Up @@ -52,7 +52,7 @@ func TestResolver(t *testing.T) {

want := make([]container.Spec, len(refs))
for i, r := range refs {
spec, err := registry.Resolve(r, "amd64")
spec, err := registry.Resolve(r, arch.ARCH_X86_64)
assert.NoError(t, err)
want[i] = spec
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/container/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package container
import (
"github.com/containers/image/v5/docker/reference"
"github.com/opencontainers/go-digest"

"github.com/osbuild/images/pkg/arch"
)

// A Spec is the specification of how to get a specific
Expand All @@ -18,6 +20,8 @@ type Spec struct {
LocalName string // name to use inside the image
ListDigest string // digest of the list manifest at the Source (optional)
LocalStorage bool

Arch arch.Arch // the architecture of the image
}

// NewSpec creates a new Spec from the essential information.
Expand Down
Loading