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

pkg/arch: split arch from pkg/platform #275

Merged
merged 2 commits into from
Nov 27, 2023
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
4 changes: 2 additions & 2 deletions cmd/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"path/filepath"
"strings"

"github.com/osbuild/images/internal/common"
"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/blueprint"
"github.com/osbuild/images/pkg/container"
"github.com/osbuild/images/pkg/distro"
Expand Down Expand Up @@ -341,7 +341,7 @@ func main() {
fail(fmt.Sprintf("invalid or unsupported distribution: %q", distroName))
}

archName := common.CurrentArch()
archName := arch.Current().String()
arch, err := distribution.GetArch(archName)
if err != nil {
fail(fmt.Sprintf("invalid arch name %q for distro %q: %s\n", archName, distroName, err.Error()))
Expand Down
16 changes: 8 additions & 8 deletions cmd/osbuild-dnf-json-tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/blueprint"
"github.com/osbuild/images/pkg/distro"
rhel "github.com/osbuild/images/pkg/distro/rhel8"
"github.com/osbuild/images/pkg/dnfjson"
"github.com/osbuild/images/pkg/ostree"
"github.com/osbuild/images/pkg/platform"
"github.com/osbuild/images/pkg/rpmmd"
)

Expand Down Expand Up @@ -85,22 +85,22 @@ func TestDepsolvePackageSets(t *testing.T) {

// Set up temporary directory for rpm/dnf cache
dir := t.TempDir()
solver := dnfjson.NewSolver(cs9.ModulePlatformID(), cs9.Releasever(), platform.ARCH_X86_64.String(), cs9.Name(), dir)
solver := dnfjson.NewSolver(cs9.ModulePlatformID(), cs9.Releasever(), arch.ARCH_X86_64.String(), cs9.Name(), dir)

repos, err := rpmmd.LoadRepositories([]string{repoDir}, cs9.Name())
require.NoErrorf(t, err, "Failed to LoadRepositories %v", cs9.Name())
x86Repos, ok := repos[platform.ARCH_X86_64.String()]
require.Truef(t, ok, "failed to get %q repos for %q", platform.ARCH_X86_64.String(), cs9.Name())
x86Repos, ok := repos[arch.ARCH_X86_64.String()]
require.Truef(t, ok, "failed to get %q repos for %q", arch.ARCH_X86_64.String(), cs9.Name())

x86Arch, err := cs9.GetArch(platform.ARCH_X86_64.String())
require.Nilf(t, err, "failed to get %q arch of %q distro", platform.ARCH_X86_64.String(), cs9.Name())
x86Arch, err := cs9.GetArch(arch.ARCH_X86_64.String())
require.Nilf(t, err, "failed to get %q arch of %q distro", arch.ARCH_X86_64.String(), cs9.Name())

qcow2ImageTypeName := "qcow2"
qcow2Image, err := x86Arch.GetImageType(qcow2ImageTypeName)
require.Nilf(t, err, "failed to get %q image type of %q/%q distro/arch", qcow2ImageTypeName, cs9.Name(), platform.ARCH_X86_64.String())
require.Nilf(t, err, "failed to get %q image type of %q/%q distro/arch", qcow2ImageTypeName, cs9.Name(), arch.ARCH_X86_64.String())

manifestSource, _, err := qcow2Image.Manifest(&blueprint.Blueprint{Packages: []blueprint.Package{{Name: "bind"}}}, distro.ImageOptions{}, x86Repos, 0)
require.Nilf(t, err, "failed to initialise manifest for %q image type of %q/%q distro/arch", qcow2ImageTypeName, cs9.Name(), platform.ARCH_X86_64.String())
require.Nilf(t, err, "failed to initialise manifest for %q image type of %q/%q distro/arch", qcow2ImageTypeName, cs9.Name(), arch.ARCH_X86_64.String())
imagePkgSets := manifestSource.GetPackageSetChains()

gotPackageSpecsSets := make(map[string][]rpmmd.PackageSpec, len(imagePkgSets))
Expand Down
4 changes: 2 additions & 2 deletions cmd/osbuild-playground/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os"
"path"

"github.com/osbuild/images/internal/common"
"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/distro"
"github.com/osbuild/images/pkg/distroregistry"
"github.com/osbuild/images/pkg/image"
Expand Down Expand Up @@ -42,7 +42,7 @@ func main() {
var distroArg string
flag.StringVar(&distroArg, "distro", "host", "distro to build from")
var archArg string
flag.StringVar(&archArg, "arch", common.CurrentArch(), "architecture to build for")
flag.StringVar(&archArg, "arch", arch.Current().String(), "architecture to build for")
var imageTypeArg string
flag.StringVar(&imageTypeArg, "type", "my-container", "image type to build")
flag.Parse()
Expand Down
4 changes: 2 additions & 2 deletions internal/boot/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"

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

type awsCredentials struct {
Expand Down Expand Up @@ -97,7 +97,7 @@ func UploadImageToAWS(c *awsCredentials, imagePath string, imageName string) err
if err != nil {
return fmt.Errorf("cannot upload the image: %v", err)
}
_, _, err = uploader.Register(imageName, c.Bucket, imageName, nil, common.CurrentArch(), nil)
_, _, err = uploader.Register(imageName, c.Bucket, imageName, nil, arch.Current().String(), nil)
if err != nil {
return fmt.Errorf("cannot register the image: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/boot/context-managers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/osbuild/images/cmd/osbuild-image-tests/constants"
"github.com/osbuild/images/internal/common"
"github.com/osbuild/images/pkg/platform"
"github.com/osbuild/images/pkg/arch"
)

// WithNetworkNamespace provides the function f with a new network namespace
Expand Down Expand Up @@ -111,7 +111,7 @@ func WithBootedQemuImage(image string, ns NetNS, f func() error) error {
}

var qemuCmd *exec.Cmd
if common.CurrentArch() == "x86_64" {
if arch.IsX86_64() {
hostDistroName, _, _, err := common.GetHostDistroName()
if err != nil {
return fmt.Errorf("cannot determing the current distro: %v", err)
Expand All @@ -136,7 +136,7 @@ func WithBootedQemuImage(image string, ns NetNS, f func() error) error {
"-nographic",
image,
)
} else if common.CurrentArch() == platform.ARCH_AARCH64.String() {
} else if arch.IsAarch64() {
// This command does not use KVM as I was unable to make it work in Beaker,
// once we have machines that can use KVM, enable it to make it faster
qemuCmd = ns.NamespacedCommand(
Expand Down
17 changes: 0 additions & 17 deletions internal/common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,11 @@ import (
"fmt"
"io"
"regexp"
"runtime"
"sort"
"strconv"
"strings"
)

var RuntimeGOARCH = runtime.GOARCH

func CurrentArch() string {
if RuntimeGOARCH == "amd64" {
return "x86_64"
} else if RuntimeGOARCH == "arm64" {
return "aarch64"
} else if RuntimeGOARCH == "ppc64le" {
return "ppc64le"
} else if RuntimeGOARCH == "s390x" {
return "s390x"
} else {
panic("unsupported architecture")
}
}

func PanicOnError(err error) {
if err != nil {
panic(err)
Expand Down
35 changes: 0 additions & 35 deletions internal/common/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestCurrentArchAMD64(t *testing.T) {
origRuntimeGOARCH := RuntimeGOARCH
defer func() { RuntimeGOARCH = origRuntimeGOARCH }()
RuntimeGOARCH = "amd64"
assert.Equal(t, "x86_64", CurrentArch())
}

func TestCurrentArchARM64(t *testing.T) {
origRuntimeGOARCH := RuntimeGOARCH
defer func() { RuntimeGOARCH = origRuntimeGOARCH }()
RuntimeGOARCH = "arm64"
assert.Equal(t, "aarch64", CurrentArch())
}

func TestCurrentArchPPC64LE(t *testing.T) {
origRuntimeGOARCH := RuntimeGOARCH
defer func() { RuntimeGOARCH = origRuntimeGOARCH }()
RuntimeGOARCH = "ppc64le"
assert.Equal(t, "ppc64le", CurrentArch())
}

func TestCurrentArchS390X(t *testing.T) {
origRuntimeGOARCH := RuntimeGOARCH
defer func() { RuntimeGOARCH = origRuntimeGOARCH }()
RuntimeGOARCH = "s390x"
assert.Equal(t, "s390x", CurrentArch())
}

func TestCurrentArchUnsupported(t *testing.T) {
origRuntimeGOARCH := RuntimeGOARCH
defer func() { RuntimeGOARCH = origRuntimeGOARCH }()
RuntimeGOARCH = "UKNOWN"
assert.PanicsWithValue(t, "unsupported architecture", func() { CurrentArch() })
}

func TestPanicOnError(t *testing.T) {
err := errors.New("Error message")
assert.PanicsWithValue(t, err, func() { PanicOnError(err) })
Expand Down
4 changes: 2 additions & 2 deletions internal/test/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/BurntSushi/toml"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/osbuild/images/internal/common"
"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/distro"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -244,7 +244,7 @@ func GenerateCIArtifactName(prefix string) (string, error) {
return "", fmt.Errorf("The environment variables must specify BRANCH_NAME, BUILD_ID, and DISTRO_CODE")
}

arch := common.CurrentArch()
arch := arch.Current().String()

return fmt.Sprintf("%s%s-%s-%s-%s", prefix, distroCode, arch, branchName, buildId), nil
}
62 changes: 62 additions & 0 deletions pkg/arch/arch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package arch

import (
"runtime"
)

type Arch uint64

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

func (a Arch) String() string {
switch a {
case ARCH_AARCH64:
return "aarch64"
case ARCH_PPC64LE:
return "ppc64le"
case ARCH_S390X:
return "s390x"
case ARCH_X86_64:
return "x86_64"
default:
panic("invalid architecture")
}
}

var runtimeGOARCH = runtime.GOARCH

func Current() Arch {
switch runtimeGOARCH {
case "amd64":
return ARCH_X86_64
case "arm64":
return ARCH_AARCH64
case "ppc64le":
return ARCH_PPC64LE
case "s390x":
return ARCH_S390X
default:
panic("unsupported architecture")
}
}

func IsX86_64() bool {
return Current() == ARCH_X86_64
}

func IsAarch64() bool {
return Current() == ARCH_AARCH64
}

func IsPPC() bool {
return Current() == ARCH_PPC64LE
}

func IsS390x() bool {
return Current() == ARCH_S390X
}
46 changes: 46 additions & 0 deletions pkg/arch/arch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package arch

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCurrentArchAMD64(t *testing.T) {
origRuntimeGOARCH := runtimeGOARCH
defer func() { runtimeGOARCH = origRuntimeGOARCH }()
runtimeGOARCH = "amd64"
assert.Equal(t, "x86_64", Current().String())
assert.True(t, IsX86_64())
}

func TestCurrentArchARM64(t *testing.T) {
origRuntimeGOARCH := runtimeGOARCH
defer func() { runtimeGOARCH = origRuntimeGOARCH }()
runtimeGOARCH = "arm64"
assert.Equal(t, "aarch64", Current().String())
assert.True(t, IsAarch64())
}

func TestCurrentArchPPC64LE(t *testing.T) {
origRuntimeGOARCH := runtimeGOARCH
defer func() { runtimeGOARCH = origRuntimeGOARCH }()
runtimeGOARCH = "ppc64le"
assert.Equal(t, "ppc64le", Current().String())
assert.True(t, IsPPC())
}

func TestCurrentArchS390X(t *testing.T) {
origRuntimeGOARCH := runtimeGOARCH
defer func() { runtimeGOARCH = origRuntimeGOARCH }()
runtimeGOARCH = "s390x"
assert.Equal(t, "s390x", Current().String())
assert.True(t, IsS390x())
}

func TestCurrentArchUnsupported(t *testing.T) {
origRuntimeGOARCH := runtimeGOARCH
defer func() { runtimeGOARCH = origRuntimeGOARCH }()
runtimeGOARCH = "UKNOWN"
assert.PanicsWithValue(t, "unsupported architecture", func() { Current() })
}
9 changes: 5 additions & 4 deletions pkg/distro/fedora/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/osbuild/images/internal/environment"
"github.com/osbuild/images/internal/fsnode"
"github.com/osbuild/images/internal/oscap"
"github.com/osbuild/images/pkg/arch"
"github.com/osbuild/images/pkg/distro"
"github.com/osbuild/images/pkg/osbuild"
"github.com/osbuild/images/pkg/platform"
Expand Down Expand Up @@ -532,23 +533,23 @@ func newDistro(version int) distro.Distro {

// Architecture definitions
x86_64 := architecture{
name: platform.ARCH_X86_64.String(),
name: arch.ARCH_X86_64.String(),
distro: &rd,
}

aarch64 := architecture{
name: platform.ARCH_AARCH64.String(),
name: arch.ARCH_AARCH64.String(),
distro: &rd,
}

ppc64le := architecture{
distro: &rd,
name: platform.ARCH_PPC64LE.String(),
name: arch.ARCH_PPC64LE.String(),
}

s390x := architecture{
distro: &rd,
name: platform.ARCH_S390X.String(),
name: arch.ARCH_S390X.String(),
}

ociImgType := qcow2ImgType
Expand Down
6 changes: 3 additions & 3 deletions pkg/distro/fedora/package_sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strconv"

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

Expand Down Expand Up @@ -364,7 +364,7 @@ func anacondaPackageSet(t *imageType) rpmmd.PackageSet {
}

switch t.Arch().Name() {
case platform.ARCH_X86_64.String():
case arch.ARCH_X86_64.String():
ps = ps.Append(rpmmd.PackageSet{
Include: []string{
"biosdevname",
Expand All @@ -374,7 +374,7 @@ func anacondaPackageSet(t *imageType) rpmmd.PackageSet {
},
})

case platform.ARCH_AARCH64.String():
case arch.ARCH_AARCH64.String():
ps = ps.Append(rpmmd.PackageSet{
Include: []string{
"dmidecode",
Expand Down
Loading
Loading