Skip to content

Commit

Permalink
CFE-857 : Apply user defined tags on created gcp resources
Browse files Browse the repository at this point in the history
  • Loading branch information
bharath-b-rh committed Dec 20, 2023
1 parent 833a867 commit 99b9d60
Show file tree
Hide file tree
Showing 12 changed files with 781 additions and 253 deletions.
25 changes: 21 additions & 4 deletions data/data/gcp/bootstrap/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ resource "google_storage_bucket" "ignition" {
labels = var.gcp_extra_labels
}

resource "google_tags_location_tag_binding" "user_tag_binding_bucket" {
for_each = var.gcp_extra_tags

parent = format("//storage.googleapis.com/projects/_/buckets/%s",
google_storage_bucket.ignition.name,
)
tag_value = each.value
location = var.gcp_region

depends_on = [google_storage_bucket.ignition]
}

resource "google_storage_bucket_object" "ignition" {
bucket = google_storage_bucket.ignition.name
name = "bootstrap.ign"
Expand Down Expand Up @@ -88,10 +100,11 @@ resource "google_compute_instance" "bootstrap" {

boot_disk {
initialize_params {
type = var.gcp_master_root_volume_type
size = var.gcp_master_root_volume_size
image = var.compute_image
labels = var.gcp_extra_labels
type = var.gcp_master_root_volume_type
size = var.gcp_master_root_volume_size
image = var.compute_image
labels = var.gcp_extra_labels
resource_manager_tags = var.gcp_extra_tags
}
kms_key_self_link = var.gcp_root_volume_kms_key_link
}
Expand Down Expand Up @@ -138,6 +151,10 @@ resource "google_compute_instance" "bootstrap" {

labels = var.gcp_extra_labels

params {
resource_manager_tags = var.gcp_extra_tags
}

lifecycle {
# In GCP TF apply is run a second time to remove bootstrap node from LB.
# If machine_type = n2-standard series, install will error as TF tries to
Expand Down
2 changes: 1 addition & 1 deletion data/data/gcp/cluster/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module "master" {
confidential_compute = var.gcp_master_confidential_compute
on_host_maintenance = var.gcp_master_on_host_maintenance
gcp_extra_labels = var.gcp_extra_labels
gcp_extra_tags = var.gcp_extra_tags

tags = var.gcp_control_plane_tags
}
Expand Down Expand Up @@ -82,4 +83,3 @@ module "dns" {
project_id = var.gcp_project_id
gcp_extra_labels = var.gcp_extra_labels
}

13 changes: 9 additions & 4 deletions data/data/gcp/cluster/master/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ resource "google_compute_instance" "master" {

boot_disk {
initialize_params {
type = var.root_volume_type
size = var.root_volume_size
image = var.image
labels = var.gcp_extra_labels
type = var.root_volume_type
size = var.root_volume_size
image = var.image
labels = var.gcp_extra_labels
resource_manager_tags = var.gcp_extra_tags
}
kms_key_self_link = var.root_volume_kms_key_link
}
Expand Down Expand Up @@ -97,6 +98,10 @@ resource "google_compute_instance" "master" {
scopes = ["https://www.googleapis.com/auth/cloud-platform"]
}

params {
resource_manager_tags = var.gcp_extra_tags
}

lifecycle {
# In GCP TF apply is run a second time to remove bootstrap node from LB.
# If machine_type = n2-standard series, install will error as TF tries to
Expand Down
9 changes: 9 additions & 0 deletions data/data/gcp/cluster/master/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,12 @@ variable "on_host_maintenance" {
description = "The behavior when a maintenance event occurs."
default = ""
}

variable "gcp_extra_tags" {
type = map(string)
description = <<EOF
(optional) Extra GCP tags to be applied to the created resources.
Example: `{ "tagKeys/123" = "tagValues/456", "tagKeys/456" = "tagValues/789" }`
EOF
default = {}
}
11 changes: 10 additions & 1 deletion data/data/gcp/variables-gcp.tf
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,13 @@ variable "gcp_user_provisioned_dns" {
description = <<EOF
When true the user has selected to configure their own dns solution, and no dns records will be created.
EOF
}
}

variable "gcp_extra_tags" {
type = map(string)
description = <<EOF
(optional) Extra GCP tags to be applied to the created resources.
Example: `{ "tagKeys/123" = "tagValues/456", "tagKeys/456" = "tagValues/789" }`
EOF
default = {}
}
6 changes: 6 additions & 0 deletions pkg/asset/cluster/tfvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,11 @@ func (t *TerraformVariables) Generate(parents asset.Parents) error {
return fmt.Errorf("%s: No GCP build found", st.FormatPrefix(archName))
}

tags, err := gcpconfig.GetUserTags(gcpconfig.NewTagManager(client), installConfig.Config)
if err != nil {
return errors.Wrapf(err, "failed to fetch user-defined tags")
}

data, err := gcptfvars.TFVars(
gcptfvars.TFVarsSources{
Auth: auth,
Expand All @@ -537,6 +542,7 @@ func (t *TerraformVariables) Generate(parents asset.Parents) error {
PublishStrategy: installConfig.Config.Publish,
InfrastructureName: clusterID.InfraID,
UserProvisionedDNS: installConfig.Config.GCP.UserProvisionedDNS == gcp.UserProvisionedDNSEnabled,
UserTags: tags,
},
)
if err != nil {
Expand Down

0 comments on commit 99b9d60

Please sign in to comment.