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

Fix GCP disk size conversion #99

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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: 1 addition & 3 deletions aws/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"github.com/grafana/unused"
)

const GiBbytes = 1073741824

var _ unused.Disk = &Disk{}

// Disk holds information about an AWS EC2 volume.
Expand Down Expand Up @@ -49,7 +47,7 @@ func (d *Disk) Meta() unused.Meta { return d.meta }
func (d *Disk) SizeGB() int { return int(*d.Volume.Size) }

// SizeBytes returns the size of this AWS EC2 volume in bytes.
func (d *Disk) SizeBytes() float64 { return float64(*d.Volume.Size) * GiBbytes }
func (d *Disk) SizeBytes() float64 { return float64(*d.Volume.Size) * unused.GiBbytes }

// LastUsedAt returns a zero [time.Time] value, as AWS does not
// provide this information.
Expand Down
10 changes: 10 additions & 0 deletions aws/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

func TestDisk(t *testing.T) {
createdAt := time.Date(2021, 7, 16, 5, 55, 00, 0, time.UTC)
size := int32(10)

for _, keyName := range []string{"Name", "CSIVolumeName"} {
t.Run(keyName, func(t *testing.T) {
Expand All @@ -24,6 +25,7 @@ func TestDisk(t *testing.T) {
Value: aws.String("my-disk"),
},
},
Size: &size,
},
nil,
nil,
Expand All @@ -44,6 +46,14 @@ func TestDisk(t *testing.T) {
if !createdAt.Equal(d.CreatedAt()) {
t.Errorf("expecting CreatedAt() %v, got %v", createdAt, d.CreatedAt())
}

if exp, got := int(size), d.SizeGB(); exp != got {
t.Errorf("expecting SizeGB() %d, got %d", exp, got)
}

if exp, got := float64(size) * unused.GiBbytes, d.SizeBytes(); exp != got {
t.Errorf("expecting SizeBytes() %f, got %f", exp, got)
}
})
}
}
2 changes: 2 additions & 0 deletions azure/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ func (d *Disk) Provider() unused.Provider { return d.provider }
func (d *Disk) Name() string { return *d.Disk.Name }

// SizeGB returns the size of this Azure compute disk in GB.
// Note that Azure uses binary GB, aka, GiB
// https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.management.compute.models.datadisk.disksizegb?view=azure-dotnet-legacy
func (d *Disk) SizeGB() int { return int(*d.Disk.DiskSizeGB) }

// SizeBytes returns the size of this Azure compute disk in bytes.
Expand Down
12 changes: 12 additions & 0 deletions azure/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ func TestDisk(t *testing.T) {
createdAt := time.Date(2021, 7, 16, 5, 55, 00, 0, time.UTC)
name := "my-disk"
id := "my-disk-id"
sizeGB := int32(10)
sizeBytes := int64(10_737_418_240)

var d unused.Disk = &Disk{
compute.Disk{
Expand All @@ -23,6 +25,8 @@ func TestDisk(t *testing.T) {
},
DiskProperties: &compute.DiskProperties{
TimeCreated: &date.Time{Time: createdAt},
DiskSizeGB: &sizeGB,
DiskSizeBytes: &sizeBytes,
},
},
nil,
Expand All @@ -49,6 +53,14 @@ func TestDisk(t *testing.T) {
t.Errorf("expecting CreatedAt() %v, got %v", createdAt, d.CreatedAt())
}

if exp, got := int(sizeGB), d.SizeGB(); exp != got {
t.Errorf("expecting SizeGB() %d, got %d", exp, got)
}

if exp, got := float64(sizeBytes), d.SizeBytes(); exp != got {
t.Errorf("expecting SizeBytes() %f, got %f", exp, got)
}

if !d.LastUsedAt().IsZero() {
t.Errorf("Azure doesn't provide a last usage timestamp for disks, got %v", d.LastUsedAt())
}
Expand Down
2 changes: 2 additions & 0 deletions disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ const (
HDD DiskType = "hdd"
Unknown DiskType = "unknown"
)

const GiBbytes = 1_073_741_824 // 2^30
9 changes: 5 additions & 4 deletions gcp/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
compute "google.golang.org/api/compute/v1"
)

const GBbytes = 1_000_000_000

// ensure we are properly defining the interface
var _ unused.Disk = &Disk{}

Expand Down Expand Up @@ -50,11 +48,14 @@ func (d *Disk) LastUsedAt() time.Time {
return t
}

// SizeGB returns the size of the GCP compute disk in GB.
// SizeGB returns the size of the GCP compute disk in binary GB (aka GiB).
// GCP Storage docs: https://cloud.google.com/compute/docs/disks
// GCP pricing docs: https://cloud.google.com/compute/disks-image-pricing
// Note that it specifies the use of JEDEC binary gigabytes for the disk size.
func (d *Disk) SizeGB() int { return int(d.Disk.SizeGb) }

// SizeBytes returns the size of the GCP compute disk in bytes.
func (d *Disk) SizeBytes() float64 { return float64(d.Disk.SizeGb) * GBbytes }
func (d *Disk) SizeBytes() float64 { return float64(d.Disk.SizeGb) * unused.GiBbytes }

// DiskType Type returns the type of the GCP compute disk.
func (d *Disk) DiskType() unused.DiskType {
Expand Down
10 changes: 10 additions & 0 deletions gcp/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import (
func TestDisk(t *testing.T) {
createdAt := time.Date(2021, 7, 16, 5, 55, 00, 0, time.UTC)
detachedAt := createdAt.Add(1 * time.Hour)
size := 10

var d unused.Disk = &Disk{
&compute.Disk{
Id: 1234,
Name: "my-disk",
CreationTimestamp: createdAt.Format(time.RFC3339),
LastDetachTimestamp: detachedAt.Format(time.RFC3339),
SizeGb: int64(size),
},
nil,
unused.Meta{"foo": "bar"},
Expand All @@ -44,6 +46,14 @@ func TestDisk(t *testing.T) {
t.Errorf("expecting LastUsedAt() %v, got %v", detachedAt, d.LastUsedAt())
}

if exp, got := size, d.SizeGB(); exp != got {
t.Errorf("expecting SizeGB() %d, got %d", exp, got)
}

if exp, got := float64(size) * unused.GiBbytes, d.SizeBytes(); exp != got {
t.Errorf("expecting SizeBytes() %f, got %f", exp, got)
}

err := unusedtest.AssertEqualMeta(unused.Meta{"foo": "bar"}, d.Meta())
if err != nil {
t.Fatalf("metadata doesn't match: %v", err)
Expand Down
4 changes: 1 addition & 3 deletions unusedtest/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (

var _ unused.Disk = Disk{}

const GBbytes = 1_000_000_000

// Disk implements [unused.Disk] for testing purposes.
type Disk struct {
id, name string
Expand All @@ -32,5 +30,5 @@ func (d Disk) CreatedAt() time.Time { return d.createdAt }
func (d Disk) Meta() unused.Meta { return d.meta }
func (d Disk) LastUsedAt() time.Time { return d.createdAt.Add(1 * time.Minute) }
func (d Disk) SizeGB() int { return d.size }
func (d Disk) SizeBytes() float64 { return float64(d.size) * GBbytes }
func (d Disk) SizeBytes() float64 { return float64(d.size) * unused.GiBbytes }
func (d Disk) DiskType() unused.DiskType { return d.diskType }
Loading