Skip to content

Commit

Permalink
Merge pull request #99 from grafana/paula/fix-gcp-disk-size-conversion
Browse files Browse the repository at this point in the history
Fix GCP disk size conversion
  • Loading branch information
paulajulve authored May 24, 2024
2 parents 8e72cb9 + d9252a7 commit a251b71
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 11 deletions.
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
14 changes: 13 additions & 1 deletion 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 @@ -22,7 +24,9 @@ func TestDisk(t *testing.T) {
Name: compute.StandardSSDLRS,
},
DiskProperties: &compute.DiskProperties{
TimeCreated: &date.Time{Time: createdAt},
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 }

0 comments on commit a251b71

Please sign in to comment.