Skip to content

Commit

Permalink
Add reservation affinity for dataproc cluster. (#7070) (#13393)
Browse files Browse the repository at this point in the history
fixes #12848

Signed-off-by: Modular Magician <magic-modules@google.com>

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Jan 5, 2023
1 parent 9e336b1 commit 6883cd0
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/7070.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
dataproc: added support for `reservation_affinity` in `google_dataproc_cluster`
```
65 changes: 65 additions & 0 deletions google/resource_dataproc_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ var (
"cluster_config.0.gce_cluster_config.0.internal_ip_only",
"cluster_config.0.gce_cluster_config.0.shielded_instance_config",
"cluster_config.0.gce_cluster_config.0.metadata",
"cluster_config.0.gce_cluster_config.0.reservation_affinity",
}

schieldedInstanceConfigKeys = []string{
Expand All @@ -64,6 +65,12 @@ var (
"cluster_config.0.gce_cluster_config.0.shielded_instance_config.0.enable_integrity_monitoring",
}

reservationAffinityKeys = []string{
"cluster_config.0.gce_cluster_config.0.reservation_affinity.0.consume_reservation_type",
"cluster_config.0.gce_cluster_config.0.reservation_affinity.0.key",
"cluster_config.0.gce_cluster_config.0.reservation_affinity.0.values",
}

preemptibleWorkerDiskConfigKeys = []string{
"cluster_config.0.preemptible_worker_config.0.disk_config.0.num_local_ssds",
"cluster_config.0.preemptible_worker_config.0.disk_config.0.boot_disk_size_gb",
Expand Down Expand Up @@ -627,6 +634,42 @@ func resourceDataprocCluster() *schema.Resource {
},
},
},

"reservation_affinity": {
Type: schema.TypeList,
Optional: true,
AtLeastOneOf: gceClusterConfigKeys,
Computed: true,
MaxItems: 1,
Description: `Reservation Affinity for consuming Zonal reservation.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"consume_reservation_type": {
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: reservationAffinityKeys,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"NO_RESERVATION", "ANY_RESERVATION", "SPECIFIC_RESERVATION"}, false),
Description: `Type of reservation to consume.`,
},
"key": {
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: reservationAffinityKeys,
ForceNew: true,
Description: `Corresponds to the label key of reservation resource.`,
},
"values": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
AtLeastOneOf: reservationAffinityKeys,
ForceNew: true,
Description: `Corresponds to the label values of reservation resource.`,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -1558,6 +1601,19 @@ func expandGceClusterConfig(d *schema.ResourceData, config *Config) (*dataproc.G
conf.ShieldedInstanceConfig.EnableVtpm = v.(bool)
}
}
if v, ok := d.GetOk("cluster_config.0.gce_cluster_config.0.reservation_affinity"); ok {
cfgRa := v.([]interface{})[0].(map[string]interface{})
conf.ReservationAffinity = &dataproc.ReservationAffinity{}
if v, ok := cfgRa["consume_reservation_type"]; ok {
conf.ReservationAffinity.ConsumeReservationType = v.(string)
}
if v, ok := cfgRa["key"]; ok {
conf.ReservationAffinity.Key = v.(string)
}
if v, ok := cfgRa["values"]; ok {
conf.ReservationAffinity.Values = convertStringSet(v.(*schema.Set))
}
}
return conf, nil
}

Expand Down Expand Up @@ -2268,6 +2324,15 @@ func flattenGceClusterConfig(d *schema.ResourceData, gcc *dataproc.GceClusterCon
},
}
}
if gcc.ReservationAffinity != nil {
gceConfig["reservation_affinity"] = []map[string]interface{}{
{
"consume_reservation_type": gcc.ReservationAffinity.ConsumeReservationType,
"key": gcc.ReservationAffinity.Key,
"values": gcc.ReservationAffinity.Values,
},
}
}

return []map[string]interface{}{gceConfig}
}
Expand Down
67 changes: 67 additions & 0 deletions google/resource_dataproc_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,30 @@ func TestAccDataprocCluster_withMetadataAndTags(t *testing.T) {
})
}

func TestAccDataprocCluster_withReservationAffinity(t *testing.T) {
t.Parallel()

var cluster dataproc.Cluster
rnd := randString(t, 10)
vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDataprocClusterDestroy(t),
Steps: []resource.TestStep{
{
Config: testAccDataprocCluster_withReservationAffinity(rnd),
Check: resource.ComposeTestCheckFunc(
testAccCheckDataprocClusterExists(t, "google_dataproc_cluster.basic", &cluster),

resource.TestCheckResourceAttr("google_dataproc_cluster.basic", "cluster_config.0.gce_cluster_config.0.reservation_affinity.0.consume_reservation_type", "SPECIFIC_RESERVATION"),
resource.TestCheckResourceAttr("google_dataproc_cluster.basic", "cluster_config.0.gce_cluster_config.0.reservation_affinity.0.key", "compute.googleapis.com/reservation-name"),
resource.TestCheckResourceAttr("google_dataproc_cluster.basic", "cluster_config.0.gce_cluster_config.0.reservation_affinity.0.values.#", "1"),
),
},
},
})
}

func TestAccDataprocCluster_singleNodeCluster(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -1339,6 +1363,49 @@ resource "google_dataproc_cluster" "basic" {
`, rnd)
}

func testAccDataprocCluster_withReservationAffinity(rnd string) string {
return fmt.Sprintf(`
resource "google_compute_reservation" "reservation" {
name = "tf-test-dproc-reservation-%s"
zone = "us-central1-f"
specific_reservation {
count = 10
instance_properties {
machine_type = "n1-standard-2"
}
}
specific_reservation_required = true
}
resource "google_dataproc_cluster" "basic" {
name = "tf-test-dproc-%s"
region = "us-central1"
cluster_config {
master_config {
machine_type = "n1-standard-2"
}
worker_config {
machine_type = "n1-standard-2"
}
gce_cluster_config {
zone = "us-central1-f"
reservation_affinity {
consume_reservation_type = "SPECIFIC_RESERVATION"
key = "compute.googleapis.com/reservation-name"
values = [google_compute_reservation.reservation.name]
}
}
}
}
`, rnd, rnd)
}

func testAccDataprocCluster_singleNodeCluster(rnd string) string {
return fmt.Sprintf(`
resource "google_dataproc_cluster" "single_node_cluster" {
Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/dataproc_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,11 @@ resource "google_dataproc_cluster" "accelerated_cluster" {
* `metadata` - (Optional) A map of the Compute Engine metadata entries to add to all instances
(see [Project and instance metadata](https://cloud.google.com/compute/docs/storing-retrieving-metadata#project_and_instance_metadata)).

* `reservation_affinity` - (Optional) Reservation Affinity for consuming zonal reservation.
* `consume_reservation_type` - (Optional) Corresponds to the type of reservation consumption.
* `key` - (Optional) Corresponds to the label key of reservation resource.
* `values` - (Optional) Corresponds to the label values of reservation resource.

* `shielded_instance_config` (Optional) Shielded Instance Config for clusters using [Compute Engine Shielded VMs](https://cloud.google.com/security/shielded-cloud/shielded-vm).

- - -
Expand Down

0 comments on commit 6883cd0

Please sign in to comment.