Skip to content

Commit

Permalink
Make some carbonifer methods public
Browse files Browse the repository at this point in the history
Signed-off-by: Mathis Joffre <mathis.joffre@ovhcloud.com>
  • Loading branch information
Mathis Joffre committed Feb 24, 2023
1 parent c6fbba3 commit 58a5d4e
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 21 deletions.
26 changes: 25 additions & 1 deletion pkg/estimate/estimate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package estimate

import (
"github.com/carboniferio/carbonifer/internal/estimate"
internalResources "github.com/carboniferio/carbonifer/internal/resources"
"github.com/carboniferio/carbonifer/pkg/providers"
"github.com/carboniferio/carbonifer/pkg/resources"
"github.com/shopspring/decimal"
"github.com/spf13/viper"
)

type EstimationReport struct {
Expand All @@ -16,7 +18,7 @@ type EstimationReport struct {
}

func GetEstimation(resource resources.GenericResource) (EstimationReport, error) {
estimation, err := estimate.EstimateResource(resource)
estimation, err := estimate.EstimateResource(toInternalComputeResource(resource))
if err != nil {
return EstimationReport{}, err
}
Expand All @@ -40,3 +42,25 @@ func GetEstimationFromInstanceType(instanceType string, zone string, provider pr
}
return estimation, nil
}

func toInternalComputeResource(resource resources.GenericResource) internalResources.ComputeResource {
return internalResources.ComputeResource{
Identification: resource.GetIdentification(),
Specs: &internalResources.ComputeResourceSpecs{
GpuTypes: resource.GPUTypes,
HddStorage: resource.Storage.HddStorage,
SsdStorage: resource.Storage.SsdStorage,
MemoryMb: resource.MemoryMb,
VCPUs: resource.VCPUs,
CPUType: resource.CPUTypes[0], // TODO: Support multiple CPU types
ReplicationFactor: resource.ReplicationFactor,
},
}
}

func init() {
viper.Set("data.path", "../../data")
viper.Set("unit.power", "")
viper.Set("unit.time", "")
viper.Set("unit.carbon", "")
}
78 changes: 78 additions & 0 deletions pkg/estimate/estimate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package estimate

import (
"github.com/carboniferio/carbonifer/pkg/providers"
"github.com/carboniferio/carbonifer/pkg/resources"
"github.com/shopspring/decimal"
"reflect"
"testing"
)

func TestGetEstimation(t *testing.T) {
type args struct {
resource resources.GenericResource
}
tests := []struct {
name string
args args
want EstimationReport
wantErr bool
}{
{
name: "e2-standard-2",
args: args{
resource: resources.GenericResource{
Name: "e2-standard-2",
Region: "europe-west4",
Provider: providers.GCP,
CPUTypes: []string{
"Skylake",
"Broadwell",
"Haswell",
"AMD EPYC Rome",
"AMD EPYC Milan",
},
VCPUs: 2,
MemoryMb: 8192,
Storage: resources.Storage{},
ReplicationFactor: 0,
},
},
want: EstimationReport{
Resource: resources.GenericResource{
Name: "e2-standard-2",
Region: "europe-west4",
Provider: providers.GCP,
CPUTypes: []string{
"Skylake",
"Broadwell",
"Haswell",
"AMD EPYC Rome",
"AMD EPYC Milan",
},
MemoryMb: 8192,
VCPUs: 2,
Storage: resources.Storage{},
ReplicationFactor: 0,
},
Power: decimal.NewFromFloat(4.8677297799),
CarbonEmissions: decimal.NewFromFloat(1377.5675277218),
AverageCPUUsage: decimal.NewFromInt(0),
Count: decimal.NewFromInt(1),
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetEstimation(tt.args.resource)
if (err != nil) != tt.wantErr {
t.Errorf("GetEstimation() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetEstimation() got = %v, want %v", got, tt.want)
}
})
}
}
45 changes: 27 additions & 18 deletions pkg/resources/ressource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@ import (
)

type GenericResource struct {
Name string
Region string
Provider providers.Provider
GPUTypes []string
CPUTypes []string
MemoryMb int32
Storage Storage
Name string
Region string
Provider providers.Provider
GPUTypes []string
CPUTypes []string
VCPUs int32
MemoryMb int32
Storage Storage
ReplicationFactor int32
}

// IsSupported returns true if the resource is supported by carbonifer. At the moment, only GCP is supported.
func (g GenericResource) IsSupported() bool {
if g.Provider == providers.GCP {
// Use a switch to make it easier to add new providers
switch g.Provider {
case providers.GCP:
return true
default:
return false
}
return false
}

func (g GenericResource) GetIdentification() *resources.ResourceIdentification {
Expand All @@ -48,8 +53,6 @@ type Storage struct {
}

func GetResource(instanceType string, zone string, provider providers.Provider) (GenericResource, error) {
// TODO: remove this hardcoded path and viper.Set
viper.Set("data.path", "../../data")
switch provider {
case providers.GCP:
return fromGCPMachineTypeToResource(zone, gcp.GetGCPMachineType(instanceType, zone)), nil
Expand All @@ -60,12 +63,18 @@ func GetResource(instanceType string, zone string, provider providers.Provider)

func fromGCPMachineTypeToResource(region string, machineType gcp.MachineType) GenericResource {
return GenericResource{
Name: machineType.Name,
Region: region,
Provider: providers.GCP,
GPUTypes: machineType.GPUTypes,
MemoryMb: machineType.MemoryMb,
CPUTypes: machineType.CpuTypes,
Storage: Storage{},
Name: machineType.Name,
Region: region,
Provider: providers.GCP,
GPUTypes: machineType.GPUTypes,
MemoryMb: machineType.MemoryMb,
CPUTypes: machineType.CpuTypes,
VCPUs: machineType.Vcpus,
Storage: Storage{},
ReplicationFactor: 0,
}
}

func init() {
viper.Set("data.path", "../../data")
}
6 changes: 4 additions & 2 deletions pkg/resources/ressource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ func TestGetResource(t *testing.T) {
"AMD EPYC Rome",
"AMD EPYC Milan",
},
MemoryMb: 8192,
Storage: Storage{},
VCPUs: 2,
MemoryMb: 8192,
Storage: Storage{},
ReplicationFactor: 0,
},
wantErr: false,
},
Expand Down

0 comments on commit 58a5d4e

Please sign in to comment.