Skip to content

Commit c16c7b2

Browse files
committed
Refactoring in gke persistent module
1 parent c9394b0 commit c16c7b2

7 files changed

Lines changed: 92 additions & 115 deletions

File tree

modules/compute/gke-job-template/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ No modules.
114114
| <a name="input_node_count"></a> [node\_count](#input\_node\_count) | How many nodes the job should run in parallel. | `number` | `1` | no |
115115
| <a name="input_node_pool_names"></a> [node\_pool\_names](#input\_node\_pool\_names) | A list of node pool names on which to run the job. Can be populated via `use` field. | `list(string)` | `[]` | no |
116116
| <a name="input_node_selectors"></a> [node\_selectors](#input\_node\_selectors) | A list of node selectors to use to place the job. | <pre>list(object({<br/> key = string<br/> value = string<br/> }))</pre> | `[]` | no |
117-
| <a name="input_persistent_volume_claims"></a> [persistent\_volume\_claims](#input\_persistent\_volume\_claims) | A list of objects that describes a k8s PVC that is to be used and mounted on the job. Generally supplied by the gke-persistent-volume module. | <pre>list(object({<br/> name = string<br/> mount_path = string<br/> mount_options = string<br/> is_gcs = bool<br/> }))</pre> | `[]` | no |
117+
| <a name="input_persistent_volume_claims"></a> [persistent\_volume\_claims](#input\_persistent\_volume\_claims) | A list of objects that describes a k8s PVC that is to be used and mounted on the job. Generally supplied by the gke-persistent-volume module. | <pre>list(object({<br/> name = string<br/> namespace = string<br/> mount_path = string<br/> mount_options = string<br/> storage_type = string<br/> }))</pre> | `[]` | no |
118118
| <a name="input_random_name_sufix"></a> [random\_name\_sufix](#input\_random\_name\_sufix) | Appends a random suffix to the job name to avoid clashes. | `bool` | `true` | no |
119119
| <a name="input_requested_cpu_per_pod"></a> [requested\_cpu\_per\_pod](#input\_requested\_cpu\_per\_pod) | The requested cpu per pod. If null, allocatable\_cpu\_per\_node will be used to claim whole nodes. If provided will override allocatable\_cpu\_per\_node. | `number` | `-1` | no |
120120
| <a name="input_requested_gpu_per_pod"></a> [requested\_gpu\_per\_pod](#input\_requested\_gpu\_per\_pod) | The requested gpu per pod. If null, allocatable\_gpu\_per\_node will be used to claim whole nodes. If provided will override allocatable\_gpu\_per\_node. | `number` | `-1` | no |

modules/compute/gke-job-template/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ locals {
110110
node_selectors = concat(local.machine_family_node_selector, local.local_ssd_node_selector, var.node_selectors)
111111

112112
any_gcs = anytrue([for pvc in var.persistent_volume_claims :
113-
pvc.is_gcs
113+
pvc.storage_type == "gcs"
114114
])
115115

116116
job_template_contents = templatefile(

modules/compute/gke-job-template/variables.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,10 @@ variable "persistent_volume_claims" {
150150
description = "A list of objects that describes a k8s PVC that is to be used and mounted on the job. Generally supplied by the gke-persistent-volume module."
151151
type = list(object({
152152
name = string
153+
namespace = string
153154
mount_path = string
154155
mount_options = string
155-
is_gcs = bool
156+
storage_type = string
156157
}))
157158
default = []
158159
}

modules/file-system/gke-persistent-volume/README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,13 @@ limitations under the License.
123123
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
124124
| <a name="requirement_google"></a> [google](#requirement\_google) | >= 4.42 |
125125
| <a name="requirement_kubectl"></a> [kubectl](#requirement\_kubectl) | >= 1.7.0 |
126-
| <a name="requirement_local"></a> [local](#requirement\_local) | >= 2.0.0 |
127126

128127
## Providers
129128

130129
| Name | Version |
131130
|------|---------|
132131
| <a name="provider_google"></a> [google](#provider\_google) | >= 4.42 |
133132
| <a name="provider_kubectl"></a> [kubectl](#provider\_kubectl) | >= 1.7.0 |
134-
| <a name="provider_local"></a> [local](#provider\_local) | >= 2.0.0 |
135133

136134
## Modules
137135

@@ -144,7 +142,6 @@ No modules.
144142
| [kubectl_manifest.pv](https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/resources/manifest) | resource |
145143
| [kubectl_manifest.pvc](https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/resources/manifest) | resource |
146144
| [kubectl_manifest.pvc_namespace](https://registry.terraform.io/providers/gavinbunney/kubectl/latest/docs/resources/manifest) | resource |
147-
| [local_file.debug_file](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file) | resource |
148145
| [google_client_config.default](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/client_config) | data source |
149146
| [google_container_cluster.gke_cluster](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/container_cluster) | data source |
150147

@@ -167,6 +164,6 @@ No modules.
167164

168165
| Name | Description |
169166
|------|-------------|
170-
| <a name="output_persistent_volume_claims"></a> [persistent\_volume\_claims](#output\_persistent\_volume\_claims) | An object that describes a k8s PVC created by this module. |
171-
| <a name="output_pvc_name"></a> [pvc\_name](#output\_pvc\_name) | The name for k8s PVC created by this module. |
167+
| <a name="output_persistent_volume_claims"></a> [persistent\_volume\_claims](#output\_persistent\_volume\_claims) | An object describing the Kubernetes PersistentVolumeClaim created by this module. |
168+
| <a name="output_pvc_name"></a> [pvc\_name](#output\_pvc\_name) | The name of the Kubernetes PVC created by this module. |
172169
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

modules/file-system/gke-persistent-volume/main.tf

Lines changed: 82 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -20,119 +20,102 @@ locals {
2020
}
2121

2222
locals {
23-
is_gcs = (var.gcs_bucket_name != null)
24-
is_lustre = (var.lustre_id != null)
23+
# Flags indicating which storage type is active based on input variables.
24+
storage_type_active = {
25+
gcs = var.gcs_bucket_name != null
26+
lustre = var.lustre_id != null
27+
filestore = var.filestore_id != null
28+
}
2529

26-
filestore_id = (
27-
!local.is_gcs && !local.is_lustre ? # If not using gcs or lustre
28-
var.filestore_id : # Then filestore_id must be provided
29-
"projects/empty/locations/empty/instances/empty" # Otherwise use something arbitrary as it will not be used
30-
)
30+
# Determine the active storage type name.
31+
active_types = [for type, is_active in local.storage_type_active : type if is_active]
3132

32-
lustre_id = (
33-
local.is_lustre ?
34-
var.lustre_id :
35-
"projects/empty/locations/empty/instances/empty"
36-
)
37-
location = local.is_lustre ? split("/", local.lustre_id)[3] : split("/", local.filestore_id)[3]
38-
filestore_name = split("/", local.filestore_id)[5]
39-
filestore_share_name = trimprefix(var.network_storage.remote_mount, "/")
40-
41-
# Determine the base_name based on which storage type is used
42-
base_name = local.is_gcs ? var.gcs_bucket_name : (local.is_lustre ? split("/", local.lustre_id)[5] : local.filestore_name)
43-
pv_name = var.pv_name != null ? var.pv_name : "${local.base_name}-pv"
44-
pvc_name = var.pvc_name != null ? var.pvc_name : "${local.base_name}-pvc"
45-
list_mount_options = split(",", var.network_storage.mount_options)
46-
47-
filestore_pv_contents = templatefile(
48-
"${path.module}/templates/filestore-pv.yaml.tftpl",
49-
{
50-
pv_name = local.pv_name
51-
capacity = "${var.capacity_gib}Gi"
52-
location = local.location
53-
filestore_name = local.filestore_name
54-
share_name = local.filestore_share_name
55-
ip_address = var.network_storage.server_ip
56-
labels = local.labels
57-
pvc_name = local.pvc_name
58-
namespace = var.namespace
59-
}
60-
)
33+
# The precondition in kubectl_manifest.pv ensures exactly one type is active.
34+
storage_type = length(local.active_types) > 0 ? local.active_types[0] : "unknown"
6135

62-
filestore_pvc_contents = templatefile(
63-
"${path.module}/templates/filestore-pvc.yaml.tftpl",
64-
{
65-
pv_name = local.pv_name
66-
capacity = "${var.capacity_gib}Gi"
67-
pvc_name = local.pvc_name
68-
labels = local.labels
69-
namespace = var.namespace
70-
}
71-
)
36+
# Map containing the base name derivation logic for each storage type.
37+
base_name_map = {
38+
gcs = var.gcs_bucket_name
39+
lustre = var.lustre_id != null ? split("/", var.lustre_id)[5] : null
40+
filestore = var.filestore_id != null ? split("/", var.filestore_id)[5] : null
41+
}
42+
# Retrieve the base name for the active storage type.
43+
base_name = local.base_name_map[local.storage_type]
44+
45+
# PV and PVC names
46+
pv_name = var.pv_name != null ? var.pv_name : "${local.base_name}-pv"
47+
pvc_name = var.pvc_name != null ? var.pvc_name : "${local.base_name}-pvc"
48+
49+
# Template file paths
50+
pv_templates = {
51+
gcs = "${path.module}/templates/gcs-pv.yaml.tftpl"
52+
lustre = "${path.module}/templates/managed-lustre-pv.yaml.tftpl"
53+
filestore = "${path.module}/templates/filestore-pv.yaml.tftpl"
54+
}
55+
pvc_templates = {
56+
gcs = "${path.module}/templates/gcs-pvc.yaml.tftpl"
57+
lustre = "${path.module}/templates/managed-lustre-pvc.yaml.tftpl"
58+
filestore = "${path.module}/templates/filestore-pvc.yaml.tftpl"
59+
}
7260

73-
gcs_pv_contents = templatefile(
74-
"${path.module}/templates/gcs-pv.yaml.tftpl",
75-
{
76-
pv_name = local.pv_name
77-
capacity = "${var.capacity_gib}Gi"
78-
labels = local.labels
79-
mount_options = local.is_gcs ? local.list_mount_options : null
80-
bucket_name = local.is_gcs ? var.gcs_bucket_name : ""
81-
namespace = var.namespace
82-
pvc_name = local.pvc_name
83-
}
84-
)
61+
# Common variables for all PVC templates
62+
common_pvc_vars = {
63+
pv_name = local.pv_name
64+
pvc_name = local.pvc_name
65+
labels = local.labels
66+
capacity = "${var.capacity_gib}Gi"
67+
namespace = var.namespace
68+
}
8569

86-
gcs_pvc_contents = templatefile(
87-
"${path.module}/templates/gcs-pvc.yaml.tftpl",
88-
{
89-
pv_name = local.pv_name
90-
pvc_name = local.pvc_name
91-
labels = local.labels
92-
capacity = "${var.capacity_gib}Gi"
93-
namespace = var.namespace
94-
}
95-
)
70+
# Common variables for all PV templates
71+
common_pv_vars = {
72+
pv_name = local.pv_name
73+
capacity = "${var.capacity_gib}Gi"
74+
labels = local.labels
75+
}
9676

97-
lustre_pv_contents = templatefile(
98-
"${path.module}/templates/managed-lustre-pv.yaml.tftpl",
99-
{
100-
pv_name = local.pv_name
101-
capacity = "${var.capacity_gib}Gi"
102-
location = local.location
77+
# Variables for PV templates, merging common vars with type-specific ones.
78+
pv_template_vars = {
79+
gcs = merge(local.common_pv_vars, {
80+
mount_options = var.gcs_bucket_name != null ? split(",", var.network_storage.mount_options) : []
81+
bucket_name = var.gcs_bucket_name
82+
namespace = var.namespace
83+
pvc_name = local.pvc_name
84+
})
85+
lustre = merge(local.common_pv_vars, {
86+
location = var.lustre_id != null ? split("/", var.lustre_id)[3] : null
10387
project = split("/", var.cluster_id)[1]
104-
labels = local.labels
10588
instance_name = local.base_name
106-
server_ip = split("@", var.network_storage.server_ip)[0]
89+
server_ip = var.lustre_id != null ? split("@", var.network_storage.server_ip)[0] : null
10790
filesystem_name = var.network_storage.remote_mount
10891
pvc_name = local.pvc_name
10992
namespace = var.namespace
110-
}
111-
)
93+
})
94+
filestore = merge(local.common_pv_vars, {
95+
location = var.filestore_id != null ? split("/", var.filestore_id)[3] : null
96+
filestore_name = local.base_name
97+
share_name = trimprefix(var.network_storage.remote_mount, "/")
98+
ip_address = var.network_storage.server_ip
99+
pvc_name = local.pvc_name
100+
namespace = var.namespace
101+
})
102+
}
112103

113-
lustre_pvc_contents = templatefile(
114-
"${path.module}/templates/managed-lustre-pvc.yaml.tftpl",
115-
{
116-
pv_name = local.pv_name
117-
pvc_name = local.pvc_name
118-
labels = local.labels
119-
capacity = "${var.capacity_gib}Gi"
120-
namespace = var.namespace
121-
}
104+
# Rendered YAML contents
105+
pv_content = templatefile(
106+
local.pv_templates[local.storage_type],
107+
local.pv_template_vars[local.storage_type]
108+
)
109+
pvc_content = templatefile(
110+
local.pvc_templates[local.storage_type],
111+
local.common_pvc_vars
122112
)
123113

114+
# GKE Cluster details
124115
cluster_name = split("/", var.cluster_id)[5]
125116
cluster_location = split("/", var.cluster_id)[3]
126117
}
127118

128-
resource "local_file" "debug_file" {
129-
content = <<-EOF
130-
${local.filestore_pv_contents}
131-
${local.filestore_pvc_contents}
132-
EOF
133-
filename = "${path.root}/pv-pvc-debug-file-${local.filestore_name}.yaml"
134-
}
135-
136119
data "google_container_cluster" "gke_cluster" {
137120
name = local.cluster_name
138121
location = local.cluster_location
@@ -156,17 +139,17 @@ resource "kubectl_manifest" "pvc_namespace" {
156139
}
157140

158141
resource "kubectl_manifest" "pv" {
159-
yaml_body = local.is_gcs ? local.gcs_pv_contents : (local.is_lustre ? local.lustre_pv_contents : local.filestore_pv_contents)
142+
yaml_body = local.pv_content
160143

161144
lifecycle {
162145
precondition {
163-
condition = ((var.gcs_bucket_name != null ? 1 : 0) + (var.filestore_id != null ? 1 : 0) + (var.lustre_id != null ? 1 : 0)) == 1
164-
error_message = "Either gcs_bucket_name, filestore_id, or lustre_id must be set."
146+
condition = length(local.active_types) == 1
147+
error_message = "Exactly one of gcs_bucket_name, filestore_id, or lustre_id must be set."
165148
}
166149
}
167150
}
168151

169152
resource "kubectl_manifest" "pvc" {
170-
yaml_body = local.is_gcs ? local.gcs_pvc_contents : (local.is_lustre ? local.lustre_pvc_contents : local.filestore_pvc_contents)
153+
yaml_body = local.pvc_content
171154
depends_on = [kubectl_manifest.pv, kubectl_manifest.pvc_namespace]
172155
}

modules/file-system/gke-persistent-volume/outputs.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
*/
1616

1717
output "persistent_volume_claims" {
18-
description = "An object that describes a k8s PVC created by this module."
18+
description = "An object describing the Kubernetes PersistentVolumeClaim created by this module."
1919
value = {
2020
name = local.pvc_name
21+
namespace = var.namespace
2122
mount_path = var.network_storage.local_mount
2223
mount_options = var.network_storage.mount_options
23-
is_gcs = local.is_gcs
24+
storage_type = local.storage_type
2425
}
25-
depends_on = [kubectl_manifest.pvc]
2626
}
2727

2828
output "pvc_name" {
29-
description = "The name for k8s PVC created by this module."
29+
description = "The name of the Kubernetes PVC created by this module."
3030
value = local.pvc_name
3131
}

modules/file-system/gke-persistent-volume/versions.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ terraform {
2323
source = "gavinbunney/kubectl"
2424
version = ">= 1.7.0"
2525
}
26-
local = {
27-
source = "hashicorp/local"
28-
version = ">= 2.0.0"
29-
}
3026
}
3127
provider_meta "google" {
3228
module_name = "blueprints/terraform/hpc-toolkit:gke-persistent-volume/v1.65.0"

0 commit comments

Comments
 (0)