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

refactor(google): Generate guest accelerator name/desc prefix programmatically #2883

Merged
merged 3 commits into from
Feb 16, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
├─ Standard provisioned storage (pd-standard) 10 GB $0.40
└─ NVIDIA Tesla K80 (on-demand) 2,920 hours $919.80

google_compute_instance.gpu_l4
├─ Instance usage (Linux/UNIX, on-demand, g2-standard-4) 730 hours $515.99
├─ Standard provisioned storage (pd-standard) 10 GB $0.40
└─ NVIDIA L4 (on-demand) 730 hours $286.18

google_compute_instance.gpu_with_hours
├─ Instance usage (Linux/UNIX, on-demand, n1-standard-16) 100 hours $76.00
├─ Standard provisioned storage (pd-standard) 10 GB $0.40
Expand Down Expand Up @@ -81,17 +86,17 @@
├─ Instance usage (Linux/UNIX, on-demand, f1-micro) 100 hours $0.76
└─ Standard provisioned storage (pd-standard) 10 GB $0.40

OVERALL TOTAL $9,447.04
OVERALL TOTAL $10,249.60
──────────────────────────────────
18 cloud resources were detected:
17 were estimated, all of which include usage-based costs, see https://infracost.io/usage-file
19 cloud resources were detected:
18 were estimated, all of which include usage-based costs, see https://infracost.io/usage-file
∙ 1 is not supported yet, see https://infracost.io/requested-resources:
∙ 1 x google_compute_instance

┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┓
┃ Project ┃ Monthly cost ┃
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━┫
┃ TestComputeInstanceGoldenFile ┃ $9,447
┃ TestComputeInstanceGoldenFile ┃ $10,250
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━┛
Logs:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,27 @@ resource "google_compute_instance" "gpu" {
}
}

resource "google_compute_instance" "gpu_l4" {
name = "gpu_l4"
machine_type = "g2-standard-4"
zone = "us-central1-a"

boot_disk {
initialize_params {
image = "centos-cloud/centos-7"
}
}

guest_accelerator {
type = "nvidia-l4"
count = 1
}

network_interface {
network = "default"
}
}

resource "google_compute_instance" "preemptible_gpu" {
name = "preemptible_gpu"
machine_type = "n1-standard-16"
Expand Down
39 changes: 10 additions & 29 deletions internal/resources/google/compute_cost_component_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"strings"

"github.com/shopspring/decimal"
"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/infracost/infracost/internal/logging"
"github.com/infracost/infracost/internal/schema"
)

Expand Down Expand Up @@ -388,34 +389,14 @@ func computeDiskIOPSCostComponent(region string, diskType string, diskSize float
// guestAcceleratorCostComponent returns a cost component for Guest Accelerator usage for Compute resources.
// Callers should be aware guestAcceleratorCostComponent returns nil if the provided guestAcceleratorType is not supported.
func guestAcceleratorCostComponent(region string, purchaseOption string, guestAcceleratorType string, guestAcceleratorCount int64, instanceCount int64, monthlyHours *float64) *schema.CostComponent {
var (
name string
descPrefix string
)

switch guestAcceleratorType {
case "nvidia-tesla-t4":
name = "NVIDIA Tesla T4"
descPrefix = "Nvidia Tesla T4 GPU"
case "nvidia-tesla-p4":
name = "NVIDIA Tesla P4"
descPrefix = "Nvidia Tesla P4 GPU"
case "nvidia-tesla-v100":
name = "NVIDIA Tesla V100"
descPrefix = "Nvidia Tesla V100 GPU"
case "nvidia-tesla-p100":
name = "NVIDIA Tesla P100"
descPrefix = "Nvidia Tesla P100 GPU"
case "nvidia-tesla-k80":
name = "NVIDIA Tesla K80"
descPrefix = "Nvidia Tesla K80 GPU"
case "nvidia-tesla-a100":
name = "NVIDIA Tesla A100"
descPrefix = "Nvidia Tesla A100 GPU"
default:
logging.Logger.Debug().Msgf("skipping cost component because guest_accelerator.type '%s' is not supported", guestAcceleratorType)
return nil
}
// From strings in format 'nvidia-tesla-a100' create:
// - name: 'NVIDIA Tesla A100'
// - descPrefix: 'Nvidia Tesla A100 GPU'
parts := strings.Split(guestAcceleratorType, "-")
rest := cases.Title(language.English).String(strings.Join(parts[1:], " "))
vdmgolub marked this conversation as resolved.
Show resolved Hide resolved

name := fmt.Sprintf("%s %s", strings.ToUpper(parts[0]), rest)
descPrefix := fmt.Sprintf("%s %s GPU", cases.Title(language.English).String(parts[0]), rest)
vdmgolub marked this conversation as resolved.
Show resolved Hide resolved

descRegex := fmt.Sprintf("/^%s running/", descPrefix)
if strings.ToLower(purchaseOption) == "preemptible" {
Expand Down