From c448c54b47988fcf68af6ddb6dbf8d192742d5f7 Mon Sep 17 00:00:00 2001 From: Olivier Bierlaire Date: Tue, 12 Sep 2023 14:11:46 +0200 Subject: [PATCH] Make replicas and counts clearer (#90) --- README.md | 27 +-- internal/estimate/estimate.go | 7 +- internal/estimate/estimate/Energy.go | 2 +- internal/estimate/estimate/Resource.go | 9 +- internal/estimate/estimate_test.go | 70 +++---- internal/estimate/estimation/estimation.go | 2 +- internal/output/text.go | 10 +- internal/plan/resources.go | 9 +- internal/plan/test/resources_aws_rds_test.go | 66 +++---- internal/plan/test/resources_aws_test.go | 41 +++-- .../plan/test/resources_childmodules_test.go | 68 +++---- internal/plan/test/resources_gcp_gke_test.go | 163 ++++++++-------- internal/plan/test/resources_gcp_test.go | 174 +++++++++--------- internal/resources/compute.go | 26 +-- pkg/estimate/estimate.go | 13 +- pkg/resources/ressource.go | 13 +- test/outputs/nothing.txt | 12 +- 17 files changed, 369 insertions(+), 343 deletions(-) diff --git a/README.md b/README.md index 37930ea..fecc531 100644 --- a/README.md +++ b/README.md @@ -95,18 +95,21 @@ Download the latest release from [releases page](https://github.com/carboniferio ```bash $ carbonifer plan - ------------------------------ ---------------- ------- ------------------------ - resource type name count emissions per instance - ------------------------------ ---------------- ------- ------------------------ - google_sql_database_instance instance 1 2.1716 gCO2eq/h - google_compute_disk first 1 0.0449 gCO2eq/h - google_compute_instance first 1 43.2803 gCO2eq/h - google_compute_instance second 1 0.4489 gCO2eq/h - google_compute_region_disk regional-first 1 0.0898 gCO2eq/h - google_compute_subnetwork first unsupported - google_compute_network vpc_network unsupported - ------------------------------ ---------------- ------- ------------------------ - Total 5 46.0356 gCO2eq/h + Average estimation of CO2 emissions per instance: + + ------------------------------------------- ------- ---------- ------------------------ + resource count replicas emissions per instance + ------------------------------------------- ------- ---------- ------------------------ + google_compute_disk.first 1 1 0.0422 gCO2eq/h + google_compute_instance.first 1 1 33.5977 gCO2eq/h + google_compute_instance.second 1 1 0.4248 gCO2eq/h + google_compute_region_disk.regional-first 1 2 0.0844 gCO2eq/h + google_sql_database_instance.instance 1 2 2.0550 gCO2eq/h + google_compute_subnetwork.first unsupported + google_compute_network.vpc_network unsupported + ------------------------------------------- ------- ---------- ------------------------ + Total 7 38.3433 gCO2eq/h + ------------------------------------------- ------- ---------- ------------------------ ``` diff --git a/internal/estimate/estimate.go b/internal/estimate/estimate.go index dcbba0e..82c4660 100644 --- a/internal/estimate/estimate.go +++ b/internal/estimate/estimate.go @@ -37,9 +37,9 @@ func EstimateResources(resourceList map[string]resources.Resource) estimation.Es unsupportedResources = append(unsupportedResources, resource) } - estimationTotal.Power = estimationTotal.Power.Add(estimationResource.Power.Mul(estimationResource.Count)) - estimationTotal.CarbonEmissions = estimationTotal.CarbonEmissions.Add(estimationResource.CarbonEmissions.Mul(estimationResource.Count)) - estimationTotal.ResourcesCount = estimationTotal.ResourcesCount.Add(estimationResource.Count) + estimationTotal.Power = estimationTotal.Power.Add(estimationResource.Power.Mul(estimationResource.TotalCount)) + estimationTotal.CarbonEmissions = estimationTotal.CarbonEmissions.Add(estimationResource.CarbonEmissions.Mul(estimationResource.TotalCount)) + estimationTotal.ResourcesCount = estimationTotal.ResourcesCount.Add(estimationResource.TotalCount) } return estimation.EstimationReport{ @@ -94,5 +94,6 @@ func estimateNotSupported(resource resources.UnsupportedResource) *estimation.Es Power: decimal.Zero, CarbonEmissions: decimal.Zero, AverageCPUUsage: decimal.Zero, + TotalCount: decimal.Zero, } } diff --git a/internal/estimate/estimate/Energy.go b/internal/estimate/estimate/Energy.go index 9f109e7..0aaa679 100644 --- a/internal/estimate/estimate/Energy.go +++ b/internal/estimate/estimate/Energy.go @@ -26,7 +26,7 @@ func estimateWattHour(resource *resources.ComputeResource) decimal.Decimal { storageInWh, gpuEstimationInWh, ) - replicationFactor := resource.Specs.ReplicationFactor + replicationFactor := resource.Identification.ReplicationFactor if replicationFactor == 0 { replicationFactor = 1 } diff --git a/internal/estimate/estimate/Resource.go b/internal/estimate/estimate/Resource.go index 473e4a4..ba8c867 100644 --- a/internal/estimate/estimate/Resource.go +++ b/internal/estimate/estimate/Resource.go @@ -68,12 +68,19 @@ func EstimateSupportedResource(resource resources.Resource) *estimation.Estimati resource.GetIdentification().Count, ) + if resource.GetIdentification().Name == "my_cluster_autoscaled" { + log.Println("my_cluster_autoscaled") + } + + count := int64(computeResource.Identification.Count) + replicationFactor := int64(computeResource.Identification.ReplicationFactor) + est := &estimation.EstimationResource{ Resource: &computeResource, Power: avgWatt.RoundFloor(10), CarbonEmissions: carbonEmissionPerTime.RoundFloor(10), AverageCPUUsage: decimal.NewFromFloat(viper.GetFloat64("provider.gcp.avg_cpu_use")).RoundFloor(10), - Count: decimal.NewFromInt(int64(computeResource.Identification.Count)), + TotalCount: decimal.NewFromInt(count * replicationFactor), } return est } diff --git a/internal/estimate/estimate_test.go b/internal/estimate/estimate_test.go index bf28572..5694802 100644 --- a/internal/estimate/estimate_test.go +++ b/internal/estimate/estimate_test.go @@ -15,12 +15,13 @@ import ( var resourceGCPComputeBasic = resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Address: "google_compute_instance.machine-name-1", - Name: "machine-name-1", - ResourceType: "type-1", - Provider: providers.GCP, - Region: "europe-west9", - Count: 1, + Address: "google_compute_instance.machine-name-1", + Name: "machine-name-1", + ResourceType: "type-1", + Provider: providers.GCP, + Region: "europe-west9", + ReplicationFactor: 1, + Count: 1, }, Specs: &resources.ComputeResourceSpecs{ VCPUs: 2, @@ -30,12 +31,13 @@ var resourceGCPComputeBasic = resources.ComputeResource{ var resourceGCPComputeCPUType = resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Address: "google_compute_instance.machine-name-2", - Name: "machine-name-2", - ResourceType: "type-1", - Provider: providers.GCP, - Region: "europe-west9", - Count: 1, + Address: "google_compute_instance.machine-name-2", + Name: "machine-name-2", + ResourceType: "type-1", + Provider: providers.GCP, + Region: "europe-west9", + ReplicationFactor: 1, + Count: 1, }, Specs: &resources.ComputeResourceSpecs{ VCPUs: 2, @@ -48,12 +50,13 @@ var resourceGCPComputeCPUType = resources.ComputeResource{ var resourceUnsupportedComputeBasic = resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Address: "unsupported.machine-name-3", - Name: "machine-name-3", - ResourceType: "type-1", - Provider: providers.AZURE, - Region: "europe-west9", - Count: 1, + Address: "unsupported.machine-name-3", + Name: "machine-name-3", + ResourceType: "type-1", + Provider: providers.AZURE, + Region: "europe-west9", + ReplicationFactor: 1, + Count: 1, }, Specs: &resources.ComputeResourceSpecs{ VCPUs: 2, @@ -63,12 +66,13 @@ var resourceUnsupportedComputeBasic = resources.ComputeResource{ var resourceGCPInstanceGroup = resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Address: "google_compute_instance_group.machine-group-1", - Name: "machine-group-1", - ResourceType: "type-1", - Provider: providers.GCP, - Region: "europe-west9", - Count: 3, + Address: "google_compute_instance_group.machine-group-1", + Name: "machine-group-1", + ResourceType: "type-1", + Provider: providers.GCP, + Region: "europe-west9", + ReplicationFactor: 1, + Count: 3, }, Specs: &resources.ComputeResourceSpecs{ VCPUs: 2, @@ -94,7 +98,7 @@ func TestEstimateResource(t *testing.T) { Power: decimal.NewFromFloat(7.600784000).RoundFloor(10), CarbonEmissions: decimal.NewFromFloat(0.448446256).RoundFloor(10), AverageCPUUsage: decimal.NewFromFloat(avgCPUUse), - Count: decimal.NewFromInt(1), + TotalCount: decimal.NewFromInt(1), }, }, { @@ -105,7 +109,7 @@ func TestEstimateResource(t *testing.T) { Power: decimal.NewFromFloat(9.5565660741), CarbonEmissions: decimal.NewFromFloat(0.5638373983), AverageCPUUsage: decimal.NewFromFloat(avgCPUUse), - Count: decimal.NewFromInt(1), + TotalCount: decimal.NewFromInt(1), }, }, { @@ -116,7 +120,7 @@ func TestEstimateResource(t *testing.T) { Power: decimal.NewFromFloat(7.600784000).RoundFloor(10), CarbonEmissions: decimal.NewFromFloat(0.448446256).RoundFloor(10), AverageCPUUsage: decimal.NewFromFloat(avgCPUUse), - Count: decimal.NewFromInt(3), + TotalCount: decimal.NewFromInt(3), }, }, } @@ -148,7 +152,7 @@ func TestEstimateResourceKilo(t *testing.T) { Power: decimal.NewFromFloat(5472.56448).RoundFloor(10), CarbonEmissions: decimal.NewFromFloat(232.4745391104).RoundFloor(10), AverageCPUUsage: decimal.NewFromFloat(avgCPUUse), - Count: decimal.NewFromInt(1), + TotalCount: decimal.NewFromInt(1), }, }, { @@ -159,7 +163,7 @@ func TestEstimateResourceKilo(t *testing.T) { Power: decimal.NewFromFloat(6880.7275733647).RoundFloor(10), CarbonEmissions: decimal.NewFromFloat(292.2933073165).RoundFloor(10), AverageCPUUsage: decimal.NewFromFloat(avgCPUUse), - Count: decimal.NewFromInt(1), + TotalCount: decimal.NewFromInt(1), }, }, } @@ -202,7 +206,7 @@ func EqualsEstimationResource(t *testing.T, expected *estimation.EstimationResou assert.Equal(t, expected.Power.String(), actual.Power.String()) assert.Equal(t, expected.CarbonEmissions.String(), actual.CarbonEmissions.String()) assert.Equal(t, expected.AverageCPUUsage.String(), actual.AverageCPUUsage.String()) - assert.Equal(t, expected.Count.String(), actual.Count.String()) + assert.Equal(t, expected.TotalCount.String(), actual.TotalCount.String()) } @@ -227,21 +231,21 @@ func TestEstimateResources(t *testing.T) { Power: decimal.NewFromFloat(7.600784).Round(10), CarbonEmissions: decimal.NewFromFloat(0.448446256).Round(10), AverageCPUUsage: decimal.NewFromFloat(avgCPUUse), - Count: decimal.NewFromInt(1), + TotalCount: decimal.NewFromInt(1), }, { Resource: &resourceGCPComputeCPUType, Power: decimal.NewFromFloat(9.5565660741), CarbonEmissions: decimal.NewFromFloat(0.5638373983), AverageCPUUsage: decimal.NewFromFloat(avgCPUUse), - Count: decimal.NewFromInt(1), + TotalCount: decimal.NewFromInt(1), }, { Resource: &resourceGCPInstanceGroup, Power: decimal.NewFromFloat(7.600784).Round(10), CarbonEmissions: decimal.NewFromFloat(0.448446256).Round(10), AverageCPUUsage: decimal.NewFromFloat(avgCPUUse), - Count: decimal.NewFromInt(3), + TotalCount: decimal.NewFromInt(3), }, } SortEstimations(&expectedResources) diff --git a/internal/estimate/estimation/estimation.go b/internal/estimate/estimation/estimation.go index af79d7a..98fbde8 100644 --- a/internal/estimate/estimation/estimation.go +++ b/internal/estimate/estimation/estimation.go @@ -22,7 +22,7 @@ type EstimationResource struct { Power decimal.Decimal `json:"PowerPerInstance"` CarbonEmissions decimal.Decimal `json:"CarbonEmissionsPerInstance"` AverageCPUUsage decimal.Decimal - Count decimal.Decimal + TotalCount decimal.Decimal `json:"TotalCount"` // Count * ReplicationFactor } // EstimationTotal is the struct that contains the total estimation diff --git a/internal/output/text.go b/internal/output/text.go index 7077943..6863801 100644 --- a/internal/output/text.go +++ b/internal/output/text.go @@ -17,7 +17,7 @@ func GenerateReportText(report estimation.EstimationReport) string { tableString.WriteString("\n Average estimation of CO2 emissions per instance: \n\n") table := tablewriter.NewWriter(tableString) - table.SetHeader([]string{"resource type", "address", "count", "emissions per instance"}) + table.SetHeader([]string{"resource", "count", "replicas", "emissions per instance"}) // Default sort estimations := report.Resources @@ -25,23 +25,23 @@ func GenerateReportText(report estimation.EstimationReport) string { for _, resource := range report.Resources { table.Append([]string{ - resource.Resource.GetIdentification().ResourceType, resource.Resource.GetAddress(), - fmt.Sprintf("%v", resource.Count), + fmt.Sprintf("%v", resource.Resource.GetIdentification().Count), + fmt.Sprintf("%v", resource.Resource.GetIdentification().ReplicationFactor), fmt.Sprintf(" %v %v", resource.CarbonEmissions.StringFixed(4), report.Info.UnitCarbonEmissionsTime), }) } for _, resource := range report.UnsupportedResources { table.Append([]string{ - resource.GetIdentification().ResourceType, resource.GetIdentification().Address, "", + "", "unsupported", }) } - table.SetFooter([]string{"", "Total", report.Total.ResourcesCount.String(), fmt.Sprintf(" %v %v", report.Total.CarbonEmissions.StringFixed(4), report.Info.UnitCarbonEmissionsTime)}) + table.SetFooter([]string{"Total", report.Total.ResourcesCount.String(), "", fmt.Sprintf(" %v %v", report.Total.CarbonEmissions.StringFixed(4), report.Info.UnitCarbonEmissionsTime)}) // Format table.SetAutoFormatHeaders(false) diff --git a/internal/plan/resources.go b/internal/plan/resources.go index 457f5be..2a23fb5 100644 --- a/internal/plan/resources.go +++ b/internal/plan/resources.go @@ -194,9 +194,8 @@ func GetComputeResource(resourceI interface{}, resourceMapping *ResourceMapping, Address: resourceAddress, }, Specs: &resources.ComputeResourceSpecs{ - HddStorage: decimal.Zero, - SsdStorage: decimal.Zero, - ReplicationFactor: 1, + HddStorage: decimal.Zero, + SsdStorage: decimal.Zero, }, } @@ -278,7 +277,9 @@ func GetComputeResource(resourceI interface{}, resourceMapping *ResourceMapping, if err != nil { return nil, errors.Wrapf(err, "Cannot parse replication factor for %v", resourceAddress) } - computeResource.Specs.ReplicationFactor = int32(intValue) + computeResource.Identification.ReplicationFactor = int32(intValue) + } else { + computeResource.Identification.ReplicationFactor = 1 } // Add count (case of autoscaling group) diff --git a/internal/plan/test/resources_aws_rds_test.go b/internal/plan/test/resources_aws_rds_test.go index 715ec37..0f2d1a9 100644 --- a/internal/plan/test/resources_aws_rds_test.go +++ b/internal/plan/test/resources_aws_rds_test.go @@ -27,53 +27,53 @@ func TestGetResource_RDS(t *testing.T) { wantResources := map[string]resources.Resource{ "aws_db_instance.first": resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Address: "aws_db_instance.first", - Name: "first", - ResourceType: "aws_db_instance", - Provider: providers.AWS, - Region: "eu-west-3", - Count: 1, + Address: "aws_db_instance.first", + Name: "first", + ResourceType: "aws_db_instance", + Provider: providers.AWS, + Region: "eu-west-3", + Count: 1, + ReplicationFactor: 2, }, Specs: &resources.ComputeResourceSpecs{ - VCPUs: int32(2), - MemoryMb: int32(8192), - ReplicationFactor: 2, - HddStorage: decimal.Zero, - SsdStorage: decimal.NewFromInt(300), + VCPUs: int32(2), + MemoryMb: int32(8192), + HddStorage: decimal.Zero, + SsdStorage: decimal.NewFromInt(300), }, }, "aws_db_instance.second": resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Address: "aws_db_instance.second", - Name: "second", - ResourceType: "aws_db_instance", - Provider: providers.AWS, - Region: "eu-west-3", - Count: 1, + Address: "aws_db_instance.second", + Name: "second", + ResourceType: "aws_db_instance", + Provider: providers.AWS, + Region: "eu-west-3", + Count: 1, + ReplicationFactor: 1, }, Specs: &resources.ComputeResourceSpecs{ - VCPUs: int32(2), - MemoryMb: int32(8192), - ReplicationFactor: 1, - HddStorage: decimal.Zero, - SsdStorage: decimal.NewFromInt(200), + VCPUs: int32(2), + MemoryMb: int32(8192), + HddStorage: decimal.Zero, + SsdStorage: decimal.NewFromInt(200), }, }, "aws_db_instance.third": resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Address: "aws_db_instance.third", - Name: "third", - ResourceType: "aws_db_instance", - Provider: providers.AWS, - Region: "eu-west-3", - Count: 1, + Address: "aws_db_instance.third", + Name: "third", + ResourceType: "aws_db_instance", + Provider: providers.AWS, + Region: "eu-west-3", + Count: 1, + ReplicationFactor: 1, }, Specs: &resources.ComputeResourceSpecs{ - VCPUs: int32(2), - MemoryMb: int32(8192), - ReplicationFactor: 1, - HddStorage: decimal.Zero, - SsdStorage: decimal.NewFromInt(300), + VCPUs: int32(2), + MemoryMb: int32(8192), + HddStorage: decimal.Zero, + SsdStorage: decimal.NewFromInt(300), }, }, } diff --git a/internal/plan/test/resources_aws_test.go b/internal/plan/test/resources_aws_test.go index e642324..6a15e4b 100644 --- a/internal/plan/test/resources_aws_test.go +++ b/internal/plan/test/resources_aws_test.go @@ -27,34 +27,35 @@ func TestGetResource_DiskFromAMI(t *testing.T) { wantResources := map[string]resources.Resource{ "aws_instance.foo": resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Name: "foo", - Address: "aws_instance.foo", - ResourceType: "aws_instance", - Provider: providers.AWS, - Region: "eu-west-3", - Count: 1, + Name: "foo", + Address: "aws_instance.foo", + ResourceType: "aws_instance", + Provider: providers.AWS, + Region: "eu-west-3", + Count: 1, + ReplicationFactor: 1, }, Specs: &resources.ComputeResourceSpecs{ - VCPUs: int32(2), - MemoryMb: int32(8192), - ReplicationFactor: 1, - HddStorage: decimal.NewFromInt(80), - SsdStorage: decimal.NewFromInt(30), + VCPUs: int32(2), + MemoryMb: int32(8192), + + HddStorage: decimal.NewFromInt(80), + SsdStorage: decimal.NewFromInt(30), }, }, "aws_ebs_volume.ebs_volume": resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Address: "aws_ebs_volume.ebs_volume", - Name: "ebs_volume", - ResourceType: "aws_ebs_volume", - Provider: providers.AWS, - Region: "eu-west-3", - Count: 1, + Address: "aws_ebs_volume.ebs_volume", + Name: "ebs_volume", + ResourceType: "aws_ebs_volume", + Provider: providers.AWS, + Region: "eu-west-3", + Count: 1, + ReplicationFactor: 1, }, Specs: &resources.ComputeResourceSpecs{ - ReplicationFactor: 1, - HddStorage: decimal.Zero, - SsdStorage: decimal.NewFromInt(100), + HddStorage: decimal.Zero, + SsdStorage: decimal.NewFromInt(100), }, }, "aws_network_interface.foo": resources.UnsupportedResource{ diff --git a/internal/plan/test/resources_childmodules_test.go b/internal/plan/test/resources_childmodules_test.go index 42df816..a38e411 100644 --- a/internal/plan/test/resources_childmodules_test.go +++ b/internal/plan/test/resources_childmodules_test.go @@ -28,53 +28,55 @@ func TestGetResource_ChildModules(t *testing.T) { wantResources := map[string]resources.Resource{ "module.backend.module.db.google_sql_database_instance.instance": resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Name: "instance", - ResourceType: "google_sql_database_instance", - Provider: providers.GCP, - Region: "europe-west9", - Count: 1, - Address: "module.backend.module.db.google_sql_database_instance.instance", + Name: "instance", + ResourceType: "google_sql_database_instance", + Provider: providers.GCP, + Region: "europe-west9", + Count: 1, + ReplicationFactor: 2, + Address: "module.backend.module.db.google_sql_database_instance.instance", }, Specs: &resources.ComputeResourceSpecs{ - VCPUs: int32(1), - MemoryMb: int32(1740), - ReplicationFactor: 2, - HddStorage: decimal.Zero, - SsdStorage: decimal.NewFromInt(10), + VCPUs: int32(1), + MemoryMb: int32(1740), + HddStorage: decimal.Zero, + SsdStorage: decimal.NewFromInt(10), }, }, "module.backend.module.middleware.module.api_ms.google_compute_instance.cbf-test-vm": resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Name: "cbf-test-vm", - ResourceType: "google_compute_instance", - Provider: providers.GCP, - Region: "europe-west9", - Count: 1, - Address: "module.backend.module.middleware.module.api_ms.google_compute_instance.cbf-test-vm", + Name: "cbf-test-vm", + ResourceType: "google_compute_instance", + Provider: providers.GCP, + Region: "europe-west9", + Count: 1, + ReplicationFactor: 1, + Address: "module.backend.module.middleware.module.api_ms.google_compute_instance.cbf-test-vm", }, Specs: &resources.ComputeResourceSpecs{ - VCPUs: int32(12), - MemoryMb: int32(87040), - ReplicationFactor: 1, - HddStorage: decimal.NewFromInt(10), - SsdStorage: decimal.Zero, + VCPUs: int32(12), + MemoryMb: int32(87040), + + HddStorage: decimal.NewFromInt(10), + SsdStorage: decimal.Zero, }, }, "module.backend.module.middleware.module.users_ms.google_compute_instance.cbf-test-vm": resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Name: "cbf-test-vm", - ResourceType: "google_compute_instance", - Provider: providers.GCP, - Region: "europe-west9", - Count: 1, - Address: "module.backend.module.middleware.module.users_ms.google_compute_instance.cbf-test-vm", + Name: "cbf-test-vm", + ResourceType: "google_compute_instance", + Provider: providers.GCP, + Region: "europe-west9", + Count: 1, + ReplicationFactor: 1, + Address: "module.backend.module.middleware.module.users_ms.google_compute_instance.cbf-test-vm", }, Specs: &resources.ComputeResourceSpecs{ - VCPUs: int32(2), - MemoryMb: int32(7680), - ReplicationFactor: 1, - HddStorage: decimal.NewFromInt(10), - SsdStorage: decimal.Zero, + VCPUs: int32(2), + MemoryMb: int32(7680), + + HddStorage: decimal.NewFromInt(10), + SsdStorage: decimal.Zero, }, }, "module.network.google_compute_network.vpc_network": resources.UnsupportedResource{ diff --git a/internal/plan/test/resources_gcp_gke_test.go b/internal/plan/test/resources_gcp_gke_test.go index 408f818..04a8459 100644 --- a/internal/plan/test/resources_gcp_gke_test.go +++ b/internal/plan/test/resources_gcp_gke_test.go @@ -28,122 +28,129 @@ func TestGetResource_GCP_GKE(t *testing.T) { wantResources := map[string]resources.Resource{ "google_container_cluster.my_cluster": resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Name: "my_cluster", - ResourceType: "google_container_cluster", - Provider: providers.GCP, - Region: "europe-west9", - Count: 5, - Address: "google_container_cluster.my_cluster", + Name: "my_cluster", + ResourceType: "google_container_cluster", + Provider: providers.GCP, + Region: "europe-west9", + Count: 5, + ReplicationFactor: 3, + Address: "google_container_cluster.my_cluster", }, Specs: &resources.ComputeResourceSpecs{ - VCPUs: int32(2), - MemoryMb: int32(7680), - ReplicationFactor: 3, - HddStorage: decimal.Zero, - SsdStorage: decimal.NewFromInt(2725), + VCPUs: int32(2), + MemoryMb: int32(7680), + + HddStorage: decimal.Zero, + SsdStorage: decimal.NewFromInt(2725), }, }, "google_container_cluster.my_cluster_no_pool": resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Name: "my_cluster_no_pool", - ResourceType: "google_container_cluster", - Provider: providers.GCP, - Region: "europe-west9", - Count: 4, - Address: "google_container_cluster.my_cluster_no_pool", + Name: "my_cluster_no_pool", + ResourceType: "google_container_cluster", + Provider: providers.GCP, + Region: "europe-west9", + Count: 4, + ReplicationFactor: 3, + Address: "google_container_cluster.my_cluster_no_pool", }, Specs: &resources.ComputeResourceSpecs{ - VCPUs: int32(2), - MemoryMb: int32(7680), - ReplicationFactor: 3, - HddStorage: decimal.Zero, - SsdStorage: decimal.NewFromInt(950), + VCPUs: int32(2), + MemoryMb: int32(7680), + + HddStorage: decimal.Zero, + SsdStorage: decimal.NewFromInt(950), }, }, "google_container_cluster.auto_provisioned": resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Name: "auto_provisioned", - ResourceType: "google_container_cluster", - Provider: providers.GCP, - Region: "europe-west9", - Count: 1, - Address: "google_container_cluster.auto_provisioned", + Name: "auto_provisioned", + ResourceType: "google_container_cluster", + Provider: providers.GCP, + Region: "europe-west9", + Count: 1, + ReplicationFactor: 3, + Address: "google_container_cluster.auto_provisioned", }, Specs: &resources.ComputeResourceSpecs{ - VCPUs: int32(5), - MemoryMb: int32(10240), - ReplicationFactor: 3, - HddStorage: decimal.Zero, - SsdStorage: decimal.NewFromInt(300), - GpuTypes: []string{"nvidia-tesla-k80", "nvidia-tesla-k80", "nvidia-tesla-k80"}, + VCPUs: int32(5), + MemoryMb: int32(10240), + + HddStorage: decimal.Zero, + SsdStorage: decimal.NewFromInt(300), + GpuTypes: []string{"nvidia-tesla-k80", "nvidia-tesla-k80", "nvidia-tesla-k80"}, }, }, "google_container_cluster.my_cluster_sub_pool": resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Name: "my_cluster_sub_pool", - ResourceType: "google_container_cluster", - Provider: providers.GCP, - Region: "europe-west9", - Count: 4, - Address: "google_container_cluster.my_cluster_sub_pool", + Name: "my_cluster_sub_pool", + ResourceType: "google_container_cluster", + Provider: providers.GCP, + Region: "europe-west9", + Count: 4, + ReplicationFactor: 3, + Address: "google_container_cluster.my_cluster_sub_pool", }, Specs: &resources.ComputeResourceSpecs{ - VCPUs: int32(2), - MemoryMb: int32(7680), - ReplicationFactor: 3, - HddStorage: decimal.Zero, - SsdStorage: decimal.NewFromInt(950), + VCPUs: int32(2), + MemoryMb: int32(7680), + + HddStorage: decimal.Zero, + SsdStorage: decimal.NewFromInt(950), }, }, "google_container_cluster.my_cluster_autoscaled": resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Name: "my_cluster_autoscaled", - ResourceType: "google_container_cluster", - Provider: providers.GCP, - Region: "europe-west9", - Count: 12, - Address: "google_container_cluster.my_cluster_autoscaled", + Name: "my_cluster_autoscaled", + ResourceType: "google_container_cluster", + Provider: providers.GCP, + Region: "europe-west9", + Count: 12, + ReplicationFactor: 3, + Address: "google_container_cluster.my_cluster_autoscaled", }, Specs: &resources.ComputeResourceSpecs{ - VCPUs: int32(2), - MemoryMb: int32(7680), - ReplicationFactor: 3, - HddStorage: decimal.Zero, - SsdStorage: decimal.NewFromInt(150), + VCPUs: int32(2), + MemoryMb: int32(7680), + + HddStorage: decimal.Zero, + SsdStorage: decimal.NewFromInt(150), }, }, "google_container_cluster.my_cluster_autoscaled_monozone": resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Name: "my_cluster_autoscaled_monozone", - ResourceType: "google_container_cluster", - Provider: providers.GCP, - Region: "europe-west9", - Count: 12, - Address: "google_container_cluster.my_cluster_autoscaled_monozone", + Name: "my_cluster_autoscaled_monozone", + ResourceType: "google_container_cluster", + Provider: providers.GCP, + Region: "europe-west9", + Count: 12, + ReplicationFactor: 1, + Address: "google_container_cluster.my_cluster_autoscaled_monozone", }, Specs: &resources.ComputeResourceSpecs{ - VCPUs: int32(2), - MemoryMb: int32(7680), - ReplicationFactor: 1, - HddStorage: decimal.Zero, - SsdStorage: decimal.NewFromInt(150), + VCPUs: int32(2), + MemoryMb: int32(7680), + + HddStorage: decimal.Zero, + SsdStorage: decimal.NewFromInt(150), }, }, "google_container_cluster.my_cluster_autoscaled_total": resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Name: "my_cluster_autoscaled_total", - ResourceType: "google_container_cluster", - Provider: providers.GCP, - Region: "europe-west9", - Count: 70, - Address: "google_container_cluster.my_cluster_autoscaled_total", + Name: "my_cluster_autoscaled_total", + ResourceType: "google_container_cluster", + Provider: providers.GCP, + Region: "europe-west9", + Count: 70, + ReplicationFactor: 1, + Address: "google_container_cluster.my_cluster_autoscaled_total", }, Specs: &resources.ComputeResourceSpecs{ - VCPUs: int32(2), - MemoryMb: int32(7680), - ReplicationFactor: 1, - HddStorage: decimal.Zero, - SsdStorage: decimal.NewFromInt(150), + VCPUs: int32(2), + MemoryMb: int32(7680), + + HddStorage: decimal.Zero, + SsdStorage: decimal.NewFromInt(150), }, }, } diff --git a/internal/plan/test/resources_gcp_test.go b/internal/plan/test/resources_gcp_test.go index 9e920dd..4b1480e 100644 --- a/internal/plan/test/resources_gcp_test.go +++ b/internal/plan/test/resources_gcp_test.go @@ -107,17 +107,17 @@ func TestGetResource(t *testing.T) { }, want: resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Address: "google_compute_disk.disk1", - Name: "disk1", - ResourceType: "google_compute_disk", - Provider: providers.GCP, - Region: "europe-west9", - Count: 1, + Address: "google_compute_disk.disk1", + Name: "disk1", + ResourceType: "google_compute_disk", + Provider: providers.GCP, + Region: "europe-west9", + Count: 1, + ReplicationFactor: 1, }, Specs: &resources.ComputeResourceSpecs{ - HddStorage: decimal.New(1024, 0), - SsdStorage: decimal.Zero, - ReplicationFactor: 1, + HddStorage: decimal.New(1024, 0), + SsdStorage: decimal.Zero, }, }, }, @@ -129,17 +129,17 @@ func TestGetResource(t *testing.T) { }, want: resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Address: "google_compute_disk.disk2", - Name: "disk2", - ResourceType: "google_compute_disk", - Provider: providers.GCP, - Region: "europe-west9", - Count: 1, + Address: "google_compute_disk.disk2", + Name: "disk2", + ResourceType: "google_compute_disk", + Provider: providers.GCP, + Region: "europe-west9", + Count: 1, + ReplicationFactor: 1, }, Specs: &resources.ComputeResourceSpecs{ - HddStorage: decimal.New(10, 0), - SsdStorage: decimal.Zero, - ReplicationFactor: 1, + HddStorage: decimal.New(10, 0), + SsdStorage: decimal.Zero, }, }, }, @@ -151,17 +151,17 @@ func TestGetResource(t *testing.T) { }, want: resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Address: "google_compute_region_disk.diskr", - Name: "diskr", - ResourceType: "google_compute_region_disk", - Provider: providers.GCP, - Region: "europe-west9", - Count: 1, + Address: "google_compute_region_disk.diskr", + Name: "diskr", + ResourceType: "google_compute_region_disk", + Provider: providers.GCP, + Region: "europe-west9", + Count: 1, + ReplicationFactor: 2, }, Specs: &resources.ComputeResourceSpecs{ - HddStorage: decimal.Zero, - SsdStorage: decimal.NewFromInt(1024), - ReplicationFactor: 2, + HddStorage: decimal.Zero, + SsdStorage: decimal.NewFromInt(1024), }, }, }, @@ -173,12 +173,13 @@ func TestGetResource(t *testing.T) { }, want: resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Address: "google_compute_instance.attachedgpu", - Name: "attachedgpu", - ResourceType: "google_compute_instance", - Provider: providers.GCP, - Region: "europe-west9", - Count: 1, + Address: "google_compute_instance.attachedgpu", + Name: "attachedgpu", + ResourceType: "google_compute_instance", + Provider: providers.GCP, + Region: "europe-west9", + Count: 1, + ReplicationFactor: 1, }, Specs: &resources.ComputeResourceSpecs{ VCPUs: int32(2), @@ -187,9 +188,8 @@ func TestGetResource(t *testing.T) { "nvidia-tesla-k80", "nvidia-tesla-k80", }, - HddStorage: decimal.Zero, - SsdStorage: decimal.Zero, - ReplicationFactor: 1, + HddStorage: decimal.Zero, + SsdStorage: decimal.Zero, }, }, }, @@ -201,20 +201,20 @@ func TestGetResource(t *testing.T) { }, want: resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Address: "google_compute_instance.defaultgpu", - Name: "defaultgpu", - ResourceType: "google_compute_instance", - Provider: providers.GCP, - Region: "europe-west9", - Count: 1, + Address: "google_compute_instance.defaultgpu", + Name: "defaultgpu", + ResourceType: "google_compute_instance", + Provider: providers.GCP, + Region: "europe-west9", + Count: 1, + ReplicationFactor: 1, }, Specs: &resources.ComputeResourceSpecs{ - GpuTypes: nil, - VCPUs: int32(12), - MemoryMb: int32(87040), - HddStorage: decimal.Zero, - SsdStorage: decimal.Zero, - ReplicationFactor: 1, + GpuTypes: nil, + VCPUs: int32(12), + MemoryMb: int32(87040), + HddStorage: decimal.Zero, + SsdStorage: decimal.Zero, }, }, }, @@ -246,21 +246,21 @@ func TestGetResources_DiskImage(t *testing.T) { wantResources := map[string]resources.Resource{ "google_compute_disk.diskImage": resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Address: "google_compute_disk.diskImage", - Name: "diskImage", - ResourceType: "google_compute_disk", - Provider: providers.GCP, - Region: "europe-west9", - Count: 1, + Address: "google_compute_disk.diskImage", + Name: "diskImage", + ResourceType: "google_compute_disk", + Provider: providers.GCP, + Region: "europe-west9", + Count: 1, + ReplicationFactor: 1, }, Specs: &resources.ComputeResourceSpecs{ - GpuTypes: nil, - HddStorage: decimal.New(int64(10), 0), - SsdStorage: decimal.Zero, - MemoryMb: 0, - VCPUs: 0, - CPUType: "", - ReplicationFactor: 1, + GpuTypes: nil, + HddStorage: decimal.New(int64(10), 0), + SsdStorage: decimal.Zero, + MemoryMb: 0, + VCPUs: 0, + CPUType: "", }, }, } @@ -307,21 +307,21 @@ func TestGetResources_GroupInstance(t *testing.T) { }, "google_compute_instance_group_manager.my-group-manager": resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Address: "google_compute_instance_group_manager.my-group-manager", - Name: "my-group-manager", - ResourceType: "google_compute_instance_group_manager", - Provider: providers.GCP, - Region: "europe-west9", - Count: 3, + Address: "google_compute_instance_group_manager.my-group-manager", + Name: "my-group-manager", + ResourceType: "google_compute_instance_group_manager", + Provider: providers.GCP, + Region: "europe-west9", + Count: 3, + ReplicationFactor: 1, }, Specs: &resources.ComputeResourceSpecs{ - GpuTypes: nil, - HddStorage: decimal.New(20, 0), - SsdStorage: decimal.Zero, - MemoryMb: 8192, - VCPUs: 2, - CPUType: "", - ReplicationFactor: 1, + GpuTypes: nil, + HddStorage: decimal.New(20, 0), + SsdStorage: decimal.Zero, + MemoryMb: 8192, + VCPUs: 2, + CPUType: "", }, }, } @@ -367,21 +367,21 @@ func TestGetResources_InstanceFromTemplate(t *testing.T) { }, "google_compute_instance_from_template.ifromtpl": resources.ComputeResource{ Identification: &resources.ResourceIdentification{ - Address: "google_compute_instance_from_template.ifromtpl", - Name: "ifromtpl", - ResourceType: "google_compute_instance_from_template", - Provider: providers.GCP, - Region: "europe-west9", - Count: 1, + Address: "google_compute_instance_from_template.ifromtpl", + Name: "ifromtpl", + ResourceType: "google_compute_instance_from_template", + Provider: providers.GCP, + Region: "europe-west9", + Count: 1, + ReplicationFactor: 1, }, Specs: &resources.ComputeResourceSpecs{ - GpuTypes: nil, - HddStorage: decimal.New(20, 0), - SsdStorage: decimal.Zero, - MemoryMb: 8192, - VCPUs: 2, - CPUType: "", - ReplicationFactor: 1, + GpuTypes: nil, + HddStorage: decimal.New(20, 0), + SsdStorage: decimal.Zero, + MemoryMb: 8192, + VCPUs: 2, + CPUType: "", }, }, } diff --git a/internal/resources/compute.go b/internal/resources/compute.go index 155a1e2..faa58ac 100644 --- a/internal/resources/compute.go +++ b/internal/resources/compute.go @@ -7,24 +7,24 @@ import ( // ComputeResourceSpecs is the struct that contains the specs of a compute resource type ComputeResourceSpecs struct { - GpuTypes []string - HddStorage decimal.Decimal - SsdStorage decimal.Decimal - MemoryMb int32 - VCPUs int32 - CPUType string - ReplicationFactor int32 + GpuTypes []string + HddStorage decimal.Decimal + SsdStorage decimal.Decimal + MemoryMb int32 + VCPUs int32 + CPUType string } // ResourceIdentification is the struct that contains the identification of a resource type ResourceIdentification struct { // Indentification - Name string - ResourceType string - Provider providers.Provider - Region string - Count int64 - Address string + Name string + ResourceType string + Provider providers.Provider + Region string + Count int64 + ReplicationFactor int32 + Address string } // ComputeResource is the struct that contains the info of a compute resource diff --git a/pkg/estimate/estimate.go b/pkg/estimate/estimate.go index 8c2ba86..cf721fc 100644 --- a/pkg/estimate/estimate.go +++ b/pkg/estimate/estimate.go @@ -33,7 +33,7 @@ func GetEstimation(resource resources.GenericResource) (EstimationReport, error) Power: estimation.Power.Truncate(10), CarbonEmissions: estimation.CarbonEmissions.Truncate(10), AverageCPUUsage: estimation.AverageCPUUsage.Truncate(10), - Count: estimation.Count.Truncate(10), + Count: estimation.TotalCount.Truncate(10), }, nil } @@ -56,12 +56,11 @@ func toInternalComputeResource(resource resources.GenericResource) internalResou 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, - ReplicationFactor: resource.ReplicationFactor, + GpuTypes: resource.GPUTypes, + HddStorage: resource.Storage.HddStorage, + SsdStorage: resource.Storage.SsdStorage, + MemoryMb: resource.MemoryMb, + VCPUs: resource.VCPUs, }, } } diff --git a/pkg/resources/ressource.go b/pkg/resources/ressource.go index 1b0c20f..73e2b91 100644 --- a/pkg/resources/ressource.go +++ b/pkg/resources/ressource.go @@ -39,12 +39,13 @@ func (g GenericResource) IsSupported() bool { // GetIdentification returns the identification of the resource func (g GenericResource) GetIdentification() *resources.ResourceIdentification { return &resources.ResourceIdentification{ - Name: g.Name, - ResourceType: "compute", - Provider: internalProvider.Provider(g.Provider), - Region: g.Region, - Count: 1, - Address: g.Address, + Name: g.Name, + ResourceType: "compute", + Provider: internalProvider.Provider(g.Provider), + Region: g.Region, + Count: 1, + ReplicationFactor: 1, + Address: g.Address, } } diff --git a/test/outputs/nothing.txt b/test/outputs/nothing.txt index 84ba7fa..f4dce8b 100644 --- a/test/outputs/nothing.txt +++ b/test/outputs/nothing.txt @@ -1,9 +1,9 @@ Average estimation of CO2 emissions per instance: - --------------- --------- ------- ------------------------ - resource type address count emissions per instance - --------------- --------- ------- ------------------------ - --------------- --------- ------- ------------------------ - Total 0 0.0000 gCO2eq/h - --------------- --------- ------- ------------------------ \ No newline at end of file + ---------- ------- ---------- ------------------------ + resource count replicas emissions per instance + ---------- ------- ---------- ------------------------ + ---------- ------- ---------- ------------------------ + Total 0 0.0000 gCO2eq/h + ---------- ------- ---------- ------------------------