forked from infracost/infracost
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for Google Cloud Run resources (infracost#3067)
* feat: generate cloudrun service resource files * feat: add cpu throttling cost component * feat: add region tier mapping * feat: add number of requests cost component * chore: usage and region change * feat: add memory usage * chore: memory cpu seconds * feat: add free cloudrun resources * chore: move region tier mapping to utils * feat: add cloud run job * feat: add cloud run v2 service * chore: fetch min instance count from tf code * feat(google): add cloud run resources usage example * chore: format cloud_run resources files * chore: stylistic changes to make consistent with other resources * chore: update labels to fix golden tests * chore: update golden files tests --------- Co-authored-by: Alistair Scott <[email protected]>
- Loading branch information
1 parent
f45e8fa
commit bd801a6
Showing
26 changed files
with
978 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package google | ||
|
||
import ( | ||
"github.com/infracost/infracost/internal/resources/google" | ||
"github.com/infracost/infracost/internal/schema" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
) | ||
|
||
func getCloudRunServiceRegistryItem() *schema.RegistryItem { | ||
return &schema.RegistryItem{ | ||
Name: "google_cloud_run_service", | ||
RFunc: newCloudRunService, | ||
} | ||
} | ||
|
||
func newCloudRunService(d *schema.ResourceData, u *schema.UsageData) *schema.Resource { | ||
region := d.Get("location").String() | ||
cpuThrottling := true | ||
minScale := float64(0.5) | ||
annotations := d.Get("metadata.0.annotations").Map() | ||
limits := d.Get("template.0.spec.0.containers.0.resources.0.limits").Map() | ||
if val, ok := annotations["run.googleapis.com/cpu-throttling"]; ok { | ||
cpuThrottling = val.Bool() | ||
} | ||
if val, ok := annotations["autoscaling.knative.dev/minScale"]; ok { | ||
minScale = val.Float() | ||
} | ||
|
||
cpu := int64(1) | ||
if val, ok := limits["cpu"]; ok { | ||
cpu = int64(val.Float()) | ||
} | ||
|
||
memory := int64(536870912) // 512 MiB | ||
if val, ok := limits["memory"]; ok { | ||
parseMemory, err := resource.ParseQuantity(val.String()) | ||
if err == nil { | ||
memory = parseMemory.Value() // bytes | ||
} | ||
} | ||
r := &google.CloudRunService{ | ||
Address: d.Address, | ||
Region: region, | ||
CpuLimit: cpu, | ||
CpuMinScale: minScale, | ||
CpuThrottlingEnabled: cpuThrottling, | ||
MemoryLimit: memory, | ||
} | ||
r.PopulateUsage(u) | ||
return r.BuildResource() | ||
} |
16 changes: 16 additions & 0 deletions
16
internal/providers/terraform/google/cloud_run_service_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package google_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/infracost/infracost/internal/providers/terraform/tftest" | ||
) | ||
|
||
func TestCloudRunService(t *testing.T) { | ||
t.Parallel() | ||
if testing.Short() { | ||
t.Skip("skipping test in short mode") | ||
} | ||
|
||
tftest.GoldenFileResourceTests(t, "cloud_run_service_test") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package google | ||
|
||
import ( | ||
"github.com/infracost/infracost/internal/resources/google" | ||
"github.com/infracost/infracost/internal/schema" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
) | ||
|
||
func getCloudRunV2JobRegistryItem() *schema.RegistryItem { | ||
return &schema.RegistryItem{ | ||
Name: "google_cloud_run_v2_job", | ||
CoreRFunc: newCloudRunV2Job, | ||
} | ||
} | ||
|
||
func newCloudRunV2Job(d *schema.ResourceData) schema.CoreResource { | ||
region := d.Get("location").String() | ||
limits := d.Get("template.0.template.0.containers.0.resources.0.limits").Map() | ||
taskCount := int64(1) | ||
if !d.IsEmpty("template.0.task_count") { | ||
taskCount = int64(d.Get("template.0.task_count").Int()) | ||
} | ||
|
||
cpu := int64(1) | ||
if val, ok := limits["cpu"]; ok { | ||
cpu = int64(val.Float()) | ||
} | ||
memory := int64(536870912) // 512 MiB | ||
if val, ok := limits["memory"]; ok { | ||
parseMemory, err := resource.ParseQuantity(val.String()) | ||
if err == nil { | ||
memory = parseMemory.Value() // bytes | ||
} | ||
} | ||
return &google.CloudRunV2Job{ | ||
Address: d.Address, | ||
Region: region, | ||
CpuLimit: cpu, | ||
MemoryLimit: memory, | ||
TaskCount: taskCount, | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
internal/providers/terraform/google/cloud_run_v2_job_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package google_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/infracost/infracost/internal/providers/terraform/tftest" | ||
) | ||
|
||
func TestCloudRunV2Job(t *testing.T) { | ||
t.Parallel() | ||
if testing.Short() { | ||
t.Skip("skipping test in short mode") | ||
} | ||
|
||
tftest.GoldenFileResourceTests(t, "cloud_run_v2_job_test") | ||
} |
48 changes: 48 additions & 0 deletions
48
internal/providers/terraform/google/cloud_run_v2_service.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package google | ||
|
||
import ( | ||
"github.com/infracost/infracost/internal/resources/google" | ||
"github.com/infracost/infracost/internal/schema" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
) | ||
|
||
func getCloudRunV2ServiceRegistryItem() *schema.RegistryItem { | ||
return &schema.RegistryItem{ | ||
Name: "google_cloud_run_v2_service", | ||
RFunc: newCloudRunV2Service, | ||
} | ||
} | ||
|
||
func newCloudRunV2Service(d *schema.ResourceData, u *schema.UsageData) *schema.Resource { | ||
region := d.Get("location").String() | ||
limits := d.Get("template.0.containers.0.resources.0.limits").Map() | ||
cpu := int64(1) | ||
if val, ok := limits["cpu"]; ok { | ||
cpu = int64(val.Float()) | ||
} | ||
memory := int64(536870912) // 512 MiB | ||
if val, ok := limits["memory"]; ok { | ||
parseMemory, err := resource.ParseQuantity(val.String()) | ||
if err == nil { | ||
memory = parseMemory.Value() // bytes | ||
} | ||
} | ||
isCpuIdle := true | ||
if !d.IsEmpty("template.0.containers.0.resources.0.cpu_idle") { | ||
isCpuIdle = d.Get("template.0.containers.0.resources.0.cpu_idle").Bool() | ||
} | ||
minInstanceCount := float64(0.5) | ||
if !d.IsEmpty("template.0.scaling.0.min_instance_count") { | ||
minInstanceCount = d.Get("template.0.scaling.0.min_instance_count").Float() | ||
} | ||
r := &google.CloudRunV2Service{ | ||
Address: d.Address, | ||
Region: region, | ||
CpuLimit: cpu, | ||
MemoryLimit: memory, | ||
IsThrottlingEnabled: isCpuIdle, | ||
MinInstanceCount: minInstanceCount, | ||
} | ||
r.PopulateUsage(u) | ||
return r.BuildResource() | ||
} |
16 changes: 16 additions & 0 deletions
16
internal/providers/terraform/google/cloud_run_v2_service_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package google_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/infracost/infracost/internal/providers/terraform/tftest" | ||
) | ||
|
||
func TestCloudRunV2Service(t *testing.T) { | ||
t.Parallel() | ||
if testing.Short() { | ||
t.Skip("skipping test in short mode") | ||
} | ||
|
||
tftest.GoldenFileResourceTests(t, "cloud_run_v2_service_test") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
.../providers/terraform/google/testdata/cloud_run_service_test/cloud_run_service_test.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
Name Monthly Qty Unit Monthly Cost | ||
|
||
google_cloud_run_service.throttling_enabled | ||
├─ CPU allocation time (tier 2) 1,800,000 vCPU-seconds $60.48 | ||
├─ Memory allocation time (tier 2) 900,000 GiB-seconds $3.15 | ||
└─ Number of requests 500,000 requests $0.20 | ||
|
||
google_cloud_run_service.throttling_disabled | ||
├─ CPU allocation time (always-on) 1,314,000 vCPU-seconds $28.38 | ||
└─ Memory allocation time (always-on) 657,000 GiB-seconds $1.58 | ||
|
||
OVERALL TOTAL $93.79 | ||
|
||
*Usage costs can be estimated by updating Infracost Cloud settings, see docs for other options. | ||
|
||
────────────────────────────────── | ||
2 cloud resources were detected: | ||
∙ 2 were estimated | ||
|
||
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━┓ | ||
┃ Project ┃ Baseline cost ┃ Usage cost* ┃ Total cost ┃ | ||
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━╋━━━━━━━━━━━━┫ | ||
┃ main ┃ $94 ┃ $0.00 ┃ $94 ┃ | ||
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┻━━━━━━━━━━━━┛ |
Oops, something went wrong.