-
Notifications
You must be signed in to change notification settings - Fork 854
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1190 from GoogleCloudPlatform/lcaggio/dataproc
Dataproc Module
- Loading branch information
Showing
6 changed files
with
807 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# Google Cloud Dataproc | ||
|
||
This module Manages a Google ['Cloud Dataproc'](https://cloud.google.com/dataproc) cluster resource, including IAM. | ||
|
||
## TODO | ||
|
||
- [ ] Add support for Cloud Dataproc ['autoscaling policy'](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/dataproc_autoscaling_policy_iam). | ||
|
||
## Examples | ||
|
||
### Simple | ||
|
||
```hcl | ||
module "processing-dp-cluster-2" { | ||
source = "./fabric/modules/dataproc" | ||
project_id = "my-project" | ||
name = "my-cluster" | ||
region = "europe-west1" | ||
} | ||
# tftest modules=1 resources=1 | ||
``` | ||
|
||
### Cluster configuration | ||
|
||
To set cluster configuration use the 'dataproc_config.cluster_config' variable. | ||
|
||
```hcl | ||
module "processing-dp-cluster" { | ||
source = "./fabric/modules/dataproc" | ||
project_id = "my-project" | ||
name = "my-cluster" | ||
region = "europe-west1" | ||
prefix = "prefix" | ||
dataproc_config = { | ||
cluster_config = { | ||
gce_cluster_config = { | ||
subnetwork = "https://www.googleapis.com/compute/v1/projects/PROJECT/regions/europe-west1/subnetworks/SUBNET" | ||
zone = "europe-west1-b" | ||
service_account = "" | ||
service_account_scopes = ["cloud-platform"] | ||
internal_ip_only = true | ||
} | ||
} | ||
} | ||
} | ||
# tftest modules=1 resources=1 | ||
``` | ||
|
||
## IAM Examples | ||
|
||
IAM is managed via several variables that implement different levels of control: | ||
|
||
- `group_iam` and `iam` configure authoritative bindings that manage individual roles exclusively, mapping to the `google_dataproc_cluster_iam_binding` resource | ||
- `iam_additive` configure additive bindings that only manage individual role/member pairs, mapping to the `google_dataproc_cluster_iam_member` resource | ||
|
||
### Authorative IAM | ||
|
||
The iam variable is based on role keys and is typically used for service accounts, or where member values can be dynamic and would create potential problems in the underlying for_each cycle. | ||
|
||
```hcl | ||
module "processing-dp-cluster" { | ||
source = "./fabric/modules/dataproc" | ||
project_id = "my-project" | ||
name = "my-cluster" | ||
region = "europe-west1" | ||
prefix = "prefix" | ||
iam = { | ||
"roles/dataproc.viewer" = [ | ||
"serviceAccount:service-account@PROJECT_ID.iam.gserviceaccount.com" | ||
] | ||
} | ||
} | ||
# tftest modules=1 resources=2 | ||
``` | ||
|
||
The group_iam variable uses group email addresses as keys and is a convenient way to assign roles to humans following Google's best practices. The end result is readable code that also serves as documentation. | ||
|
||
```hcl | ||
module "processing-dp-cluster" { | ||
source = "./fabric/modules/dataproc" | ||
project_id = "my-project" | ||
name = "my-cluster" | ||
region = "europe-west1" | ||
prefix = "prefix" | ||
group_iam = { | ||
"gcp-data-engineers@example.net" = [ | ||
"roles/dataproc.viewer" | ||
] | ||
} | ||
} | ||
# tftest modules=1 resources=2 | ||
``` | ||
|
||
### Additive IAM | ||
|
||
Additive IAM is typically used where bindings for specific roles are controlled by different modules or in different Terraform stages. One example is when the cluster is created by one team but a different team manages access. | ||
|
||
```hcl | ||
module "processing-dp-cluster" { | ||
source = "./fabric/modules/dataproc" | ||
project_id = "my-project" | ||
name = "my-cluster" | ||
region = "europe-west1" | ||
prefix = "prefix" | ||
iam_additive = { | ||
"roles/dataproc.viewer" = [ | ||
"serviceAccount:service-account@PROJECT_ID.iam.gserviceaccount.com" | ||
] | ||
} | ||
} | ||
# tftest modules=1 resources=2 | ||
``` | ||
|
||
<!-- BEGIN TFDOC --> | ||
|
||
## Variables | ||
|
||
| name | description | type | required | default | | ||
|---|---|:---:|:---:|:---:| | ||
| [name](variables.tf#L211) | Cluster name. | <code>string</code> | ✓ | | | ||
| [project_id](variables.tf#L226) | Project ID. | <code>string</code> | ✓ | | | ||
| [region](variables.tf#L231) | Dataproc region. | <code>string</code> | ✓ | | | ||
| [dataproc_config](variables.tf#L17) | Dataproc cluster config. | <code title="object({ graceful_decommission_timeout = optional(string, null) cluster_config = optional(object({ staging_bucket = optional(string, null) temp_bucket = optional(string, null) gce_cluster_config = optional(object({ zone = optional(string, null) network = optional(string, null) subnetwork = optional(string, null) service_account = optional(string, null) service_account_scopes = optional(list(string), null) tags = optional(list(string), []) internal_ip_only = optional(bool, null) metadata = optional(map(string), {}) reservation_affinity = optional(object({ consume_reservation_type = string key = string values = string }), null) node_group_affinity = optional(object({ node_group_uri = string }), null) shielded_instance_config = optional(object({ enable_secure_boot = bool enable_vtpm = bool enable_integrity_monitoring = bool }), null) }), null) master_config = optional(object({ num_instances = number machine_type = string min_cpu_platform = string disk_config = optional(object({ boot_disk_type = string boot_disk_size_gb = number num_local_ssds = number }), null) accelerators = optional(object({ accelerator_type = string accelerator_count = number }), null) }), null) worker_config = optional(object({ num_instances = number machine_type = string min_cpu_platform = string disk_config = optional(object({ boot_disk_type = string boot_disk_size_gb = number num_local_ssds = number }), null) image_uri = string accelerators = optional(object({ accelerator_type = string accelerator_count = number }), null) }), null) preemptible_worker_config = optional(object({ num_instances = number preemptibility = string disk_config = optional(object({ boot_disk_type = string boot_disk_size_gb = number num_local_ssds = number }), null) }), null) software_config = optional(object({ image_version = string override_properties = list(map(string)) optional_components = list(string) }), null) security_config = optional(object({ kerberos_config = object({ cross_realm_trust_admin_server = optional(string, null) cross_realm_trust_kdc = optional(string, null) cross_realm_trust_realm = optional(string, null) cross_realm_trust_shared_password_uri = optional(string, null) enable_kerberos = optional(string, null) kdc_db_key_uri = optional(string, null) key_password_uri = optional(string, null) keystore_uri = optional(string, null) keystore_password_uri = optional(string, null) kms_key_uri = string realm = optional(string, null) root_principal_password_uri = string tgt_lifetime_hours = optional(string, null) truststore_password_uri = optional(string, null) truststore_uri = optional(string, null) }) }), null) autoscaling_config = optional(object({ policy_uri = string }), null) initialization_action = optional(object({ script = string timeout_sec = optional(string, null) }), null) encryption_config = optional(object({ kms_key_name = string }), null) lifecycle_config = optional(object({ idle_delete_ttl = optional(string, null) auto_delete_time = optional(string, null) }), null) endpoint_config = optional(object({ enable_http_port_access = string }), null) dataproc_metric_config = optional(object({ metrics = list(object({ metric_source = string metric_overrides = optional(string, null) })) }), null) metastore_config = optional(object({ dataproc_metastore_service = string }), null) }), null) virtual_cluster_config = optional(object({ staging_bucket = optional(string, null) auxiliary_services_config = optional(object({ metastore_config = optional(object({ dataproc_metastore_service = string }), null) spark_history_server_config = optional(object({ dataproc_cluster = string }), null) }), null) kubernetes_cluster_config = object({ kubernetes_namespace = optional(string, null) kubernetes_software_config = object({ component_version = list(map(string)) properties = optional(list(map(string)), null) }) gke_cluster_config = object({ gke_cluster_target = optional(string, null) node_pool_target = optional(object({ node_pool = string roles = list(string) node_pool_config = optional(object({ autoscaling = optional(object({ min_node_count = optional(number, null) max_node_count = optional(number, null) }), null) config = object({ machine_type = optional(string, null) preemptible = optional(bool, null) local_ssd_count = optional(number, null) min_cpu_platform = optional(string, null) spot = optional(bool, null) }) locations = optional(list(string), null) }), null) }), null) }) }) }), null) })">object({…})</code> | | <code>{}</code> | | ||
| [group_iam](variables.tf#L184) | Authoritative IAM binding for organization groups, in {GROUP_EMAIL => [ROLES]} format. Group emails need to be static. Can be used in combination with the `iam` variable. | <code>map(list(string))</code> | | <code>{}</code> | | ||
| [iam](variables.tf#L191) | IAM bindings in {ROLE => [MEMBERS]} format. | <code>map(list(string))</code> | | <code>{}</code> | | ||
| [iam_additive](variables.tf#L198) | IAM additive bindings in {ROLE => [MEMBERS]} format. | <code>map(list(string))</code> | | <code>{}</code> | | ||
| [labels](variables.tf#L205) | The resource labels for instance to use to annotate any related underlying resources, such as Compute Engine VMs. | <code>map(string)</code> | | <code>{}</code> | | ||
| [prefix](variables.tf#L216) | Optional prefix used to generate project id and name. | <code>string</code> | | <code>null</code> | | ||
| [service_account](variables.tf#L236) | Service account to set on the Dataproc cluster. | <code>string</code> | | <code>null</code> | | ||
|
||
<!-- END TFDOC --> |
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,67 @@ | ||
/** | ||
* Copyright 2022 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
# # tfdoc:file:description Generic IAM bindings and roles. | ||
|
||
locals { | ||
_group_iam_roles = distinct(flatten(values(var.group_iam))) | ||
_group_iam = { | ||
for r in local._group_iam_roles : r => [ | ||
for k, v in var.group_iam : "group:${k}" if try(index(v, r), null) != null | ||
] | ||
} | ||
_iam_additive_pairs = flatten([ | ||
for role, members in var.iam_additive : [ | ||
for member in members : { role = role, member = member } | ||
] | ||
]) | ||
iam = { | ||
for role in distinct(concat(keys(var.iam), keys(local._group_iam))) : | ||
role => concat( | ||
try(var.iam[role], []), | ||
try(local._group_iam[role], []) | ||
) | ||
} | ||
iam_additive = { | ||
for pair in local._iam_additive_pairs : | ||
"${pair.role}-${pair.member}" => { | ||
role = pair.role | ||
member = pair.member | ||
} | ||
} | ||
} | ||
|
||
resource "google_dataproc_cluster_iam_binding" "authoritative" { | ||
for_each = local.iam | ||
project = var.project_id | ||
cluster = google_dataproc_cluster.cluster.name | ||
region = var.region | ||
role = each.key | ||
members = each.value | ||
} | ||
|
||
resource "google_dataproc_cluster_iam_member" "additive" { | ||
for_each = ( | ||
length(var.iam_additive) > 0 | ||
? local.iam_additive | ||
: {} | ||
) | ||
project = var.project_id | ||
cluster = google_dataproc_cluster.cluster.name | ||
region = var.region | ||
role = each.value.role | ||
member = each.value.member | ||
} |
Oops, something went wrong.