Skip to content

Commit

Permalink
many: use common.{Ki,Mi,Gi}B instead of manually doing them
Browse files Browse the repository at this point in the history
This commit uses `common.{Ki,Mi,Gi}B` in places that used to
write `10 * 1024 * 1024` etc. It's not really important but
feels quicker to scan when having the constants.
  • Loading branch information
mvo5 authored and thozza committed Dec 6, 2023
1 parent c305a0a commit 82de94a
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/osbuild-pipeline/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func main() {
// Set cache size to 3 GiB
// osbuild-pipeline is often used to generate a lot of manifests in a row
// let the cache grow to fit much more repository metadata than we usually allow
solver.SetMaxCacheSize(3 * 1024 * 1024 * 1024)
solver.SetMaxCacheSize(3 * common.GiB)

manifest, _, err := imageType.Manifest(&composeRequest.Blueprint, options, repos, seedArg)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions cmd/osbuild-playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path"

"github.com/osbuild/images/internal/common"
"github.com/osbuild/images/pkg/distro"
"github.com/osbuild/images/pkg/dnfjson"
"github.com/osbuild/images/pkg/image"
Expand All @@ -20,8 +21,8 @@ func RunPlayground(img image.ImageKind, d distro.Distro, arch distro.Arch, repos
solver := dnfjson.NewSolver(d.ModulePlatformID(), d.Releasever(), arch.Name(), d.Name(), path.Join(state_dir, "rpmmd"))
solver.SetDNFJSONPath(findDnfJsonBin())

// Set cache size to 3 GiB
solver.SetMaxCacheSize(1 * 1024 * 1024 * 1024)
// Set cache size to 1 GiB
solver.SetMaxCacheSize(1 * common.GiB)

manifest := manifest.New()

Expand Down
8 changes: 8 additions & 0 deletions internal/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ const (
GibiByte = 1024 * 1024 * 1024 // GiB
TeraByte = 1000 * 1000 * 1000 * 1000 // TB
TebiByte = 1024 * 1024 * 1024 * 1024 // TiB

// shorthands
KiB = KibiByte
MB = MegaByte
MiB = MebiByte
GB = GigaByte
GiB = GibiByte
TiB = TebiByte
)

// These constants are set during buildtime using additional
Expand Down
6 changes: 4 additions & 2 deletions pkg/blueprint/blueprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/BurntSushi/toml"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

func TestBlueprintParse(t *testing.T) {
Expand Down Expand Up @@ -35,7 +37,7 @@ size = "20 GB"
assert.Equal(t, "/var", bp.Customizations.Filesystem[0].Mountpoint)
assert.Equal(t, uint64(2147483648), bp.Customizations.Filesystem[0].MinSize)
assert.Equal(t, "/opt", bp.Customizations.Filesystem[1].Mountpoint)
assert.Equal(t, uint64(20*1000*1000*1000), bp.Customizations.Filesystem[1].MinSize)
assert.Equal(t, uint64(20*common.GB), bp.Customizations.Filesystem[1].MinSize)

blueprint = `{
"name": "test",
Expand All @@ -50,7 +52,7 @@ size = "20 GB"
require.Nil(t, err)
assert.Equal(t, bp.Name, "test")
assert.Equal(t, "/opt", bp.Customizations.Filesystem[0].Mountpoint)
assert.Equal(t, uint64(20*1024*1024*1024), bp.Customizations.Filesystem[0].MinSize)
assert.Equal(t, uint64(20*common.GiB), bp.Customizations.Filesystem[0].MinSize)
}

func TestGetPackages(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions pkg/disk/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

const (
KiB = 1024
MiB = 1024 * KiB
GiB = 1024 * MiB
KiB = common.KiB
MiB = common.MiB
GiB = common.GiB
)

func TestDisk_AlignUp(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion pkg/disk/luks.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"math/rand"

"github.com/google/uuid"

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

type Argon2id struct {
Expand Down Expand Up @@ -96,5 +98,5 @@ func (lc *LUKSContainer) MetadataSize() uint64 {
}

// 16 MiB is the default size for the LUKS2 header
return 16 * 1024 * 1024
return 16 * common.MiB
}
2 changes: 1 addition & 1 deletion pkg/disk/lvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (vg *LVMVolumeGroup) MetadataSize() uint64 {
// of the metadata and its location and thus the start of the physical
// extent. For now we assume the default which results in a start of
// the physical extent 1 MiB
return 1024 * 1024
return 1 * common.MiB
}

type LVMLogicalVolume struct {
Expand Down
3 changes: 2 additions & 1 deletion pkg/disk/partition_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/google/uuid"

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

Expand Down Expand Up @@ -630,7 +631,7 @@ func (pt *PartitionTable) ensureLVM() error {
// we need a /boot partition to boot LVM, ensure one exists
bootPath := entityPath(pt, "/boot")
if bootPath == nil {
_, err := pt.CreateMountpoint("/boot", 512*1024*1024)
_, err := pt.CreateMountpoint("/boot", 512*common.MiB)

if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions pkg/distro/rhel9/edge.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func edgeBasePartitionTables(t *imageType) (disk.PartitionTable, bool) {
Description: "built with lvm2 and osbuild",
LogicalVolumes: []disk.LVMLogicalVolume{
{
Size: 9 * 1024 * 1024 * 1024, // 9 GB
Size: 9 * common.GiB, // 9 GiB
Name: "rootlv",
Payload: &disk.Filesystem{
Type: "xfs",
Expand Down Expand Up @@ -471,7 +471,7 @@ func edgeBasePartitionTables(t *imageType) (disk.PartitionTable, bool) {
Description: "built with lvm2 and osbuild",
LogicalVolumes: []disk.LVMLogicalVolume{
{
Size: 9 * 1024 * 1024 * 1024, // 9 GB
Size: 9 * common.GiB, // 9 GiB
Name: "rootlv",
Payload: &disk.Filesystem{
Type: "xfs",
Expand Down
2 changes: 1 addition & 1 deletion pkg/upload/azure/azurestorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const DefaultUploadThreads = 16

// PageBlobMaxUploadPagesBytes defines how much bytes can we upload in a single UploadPages call.
// See https://learn.microsoft.com/en-us/rest/api/storageservices/put-page
const PageBlobMaxUploadPagesBytes = 4 * 1024 * 1024
const PageBlobMaxUploadPagesBytes = 4 * common.MiB

// allZerosSlice returns true if all values in the slice are equal to 0
func allZerosSlice(slice []byte) bool {
Expand Down
3 changes: 2 additions & 1 deletion pkg/upload/koji/koji.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/ubccr/kerby/khttp"

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

Expand Down Expand Up @@ -383,7 +384,7 @@ func (k *Koji) uploadChunk(chunk []byte, filepath, filename string, offset uint6
// Upload uploads file to the temporary filepath on the kojiserver under the name filename
// The md5sum and size of the file is returned on success.
func (k *Koji) Upload(file io.Reader, filepath, filename string) (string, uint64, error) {
chunk := make([]byte, 1024*1024) // upload a megabyte at a time
chunk := make([]byte, common.MiB) // upload a mebiByte at a time
offset := uint64(0)
// Koji uses MD5 hashes
/* #nosec G401 */
Expand Down

0 comments on commit 82de94a

Please sign in to comment.