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 Feb 17, 2024
1 parent 905abb4 commit 1363d39
Show file tree
Hide file tree
Showing 14 changed files with 689 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 = {}
}
9 changes: 9 additions & 0 deletions pkg/asset/cluster/tfvars/tfvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,14 @@ func (t *TerraformVariables) Generate(parents asset.Parents) error {
return fmt.Errorf("%s: No GCP build found", st.FormatPrefix(archName))
}

tags, err := gcpconfig.GetUserTags(ctx,
gcpconfig.NewTagManager(client),
installConfig.Config.Platform.GCP.ProjectID,
installConfig.Config.Platform.GCP.UserTags)
if err != nil {
return fmt.Errorf("failed to fetch user-defined tags: %w", err)
}

data, err := gcptfvars.TFVars(
gcptfvars.TFVarsSources{
Auth: auth,
Expand All @@ -541,6 +549,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
83 changes: 83 additions & 0 deletions pkg/asset/installconfig/gcp/mock/usertags_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1363d39

Please sign in to comment.