diff --git a/__snapshots__/example-version.js b/__snapshots__/example-version.js new file mode 100644 index 000000000..b857940a2 --- /dev/null +++ b/__snapshots__/example-version.js @@ -0,0 +1,50 @@ +exports['example.tf updateContent updates version in example.tf 1'] = ` +/** + * Copyright 2024-2025 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. + */ + +module "gke" { + source = "terraform-google-modules/kubernetes-engine/google//modules/beta-autopilot-private-cluster" + version = "~> 2.1" + + project_id = var.project_id + name = "\${local.cluster_type}-cluster" + regional = true + region = var.region + network = module.gcp-network.network_name + subnetwork = local.subnet_names[index(module.gcp-network.subnets_names, local.subnet_name)] + ip_range_pods = local.pods_range_name + ip_range_services = local.svc_range_name + release_channel = "REGULAR" + enable_vertical_pod_autoscaling = true + enable_private_endpoint = true + enable_private_nodes = true + master_ipv4_cidr_block = "172.16.0.0/28" + add_cluster_firewall_rules = true + add_master_webhook_firewall_rules = true + add_shadow_firewall_rules = true + network_tags = ["allow-google-apis"] + deletion_protection = false + enable_binary_authorization = true + + master_authorized_networks = [ + { + cidr_block = "10.60.0.0/17" + display_name = "VPC" + }, + ] +} + +` diff --git a/src/strategies/terraform-module.ts b/src/strategies/terraform-module.ts index 71287fcd2..63fd9d5fe 100644 --- a/src/strategies/terraform-module.ts +++ b/src/strategies/terraform-module.ts @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// Copyright 2020-2025 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -18,6 +18,7 @@ import {Changelog} from '../updaters/changelog'; import {ReadMe} from '../updaters/terraform/readme'; import {ModuleVersion} from '../updaters/terraform/module-version'; import {MetadataVersion} from '../updaters/terraform/metadata-version'; +import {ExampleVersion} from '../updaters/terraform/example-version'; import {BaseStrategy, BuildUpdatesOptions} from './base'; import {Update} from '../update'; import {Version} from '../version'; @@ -106,6 +107,24 @@ export class TerraformModule extends BaseStrategy { }), }); }); + + // Update module examples to current candidate version. + const exampleFiles = await this.github.findFilesByExtensionAndRef( + 'tf', + this.targetBranch, + this.path + 'examples/' + ); + + exampleFiles.forEach(path => { + updates.push({ + path: this.addPath(path), + createIfMissing: false, + updater: new ExampleVersion({ + version, + }), + }); + }); + return updates; } diff --git a/src/updaters/terraform/example-version.ts b/src/updaters/terraform/example-version.ts new file mode 100644 index 000000000..a27639530 --- /dev/null +++ b/src/updaters/terraform/example-version.ts @@ -0,0 +1,32 @@ +// Copyright 2020-2025 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. + +import {DefaultUpdater} from '../default'; + +/** + * Updates a Terraform module's examples versions. + */ +export class ExampleVersion extends DefaultUpdater { + /** + * Given initial file contents, return updated contents. + * @param {string} content The initial content + * @returns {string} The updated content + */ + updateContent(content: string): string { + return content.replace( + /version = "~> [\d]+.[\d]+"/, + `version = "~> ${this.version.major}.${this.version.minor}"` + ); + } +} diff --git a/test/updaters/example-version.ts b/test/updaters/example-version.ts new file mode 100644 index 000000000..4d01f3d31 --- /dev/null +++ b/test/updaters/example-version.ts @@ -0,0 +1,38 @@ +// Copyright 2020-2025 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. + +import {readFileSync} from 'fs'; +import {resolve} from 'path'; +import * as snapshot from 'snap-shot-it'; +import {describe, it} from 'mocha'; +import {ExampleVersion} from '../../src/updaters/terraform/example-version'; +import {Version} from '../../src/version'; + +const fixturesPath = './test/updaters/fixtures'; + +describe('example.tf', () => { + describe('updateContent', () => { + it('updates version in example.tf', async () => { + const oldContent = readFileSync( + resolve(fixturesPath, './terraform-module/example.tf'), + 'utf8' + ).replace(/\r\n/g, '\n'); + const version = new ExampleVersion({ + version: Version.parse('2.1.0'), + }); + const newContent = version.updateContent(oldContent); + snapshot(newContent); + }); + }); +}); diff --git a/test/updaters/fixtures/terraform-module/example.tf b/test/updaters/fixtures/terraform-module/example.tf new file mode 100644 index 000000000..16ad4b534 --- /dev/null +++ b/test/updaters/fixtures/terraform-module/example.tf @@ -0,0 +1,47 @@ +/** + * Copyright 2024-2025 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. + */ + +module "gke" { + source = "terraform-google-modules/kubernetes-engine/google//modules/beta-autopilot-private-cluster" + version = "~> 1.0" + + project_id = var.project_id + name = "${local.cluster_type}-cluster" + regional = true + region = var.region + network = module.gcp-network.network_name + subnetwork = local.subnet_names[index(module.gcp-network.subnets_names, local.subnet_name)] + ip_range_pods = local.pods_range_name + ip_range_services = local.svc_range_name + release_channel = "REGULAR" + enable_vertical_pod_autoscaling = true + enable_private_endpoint = true + enable_private_nodes = true + master_ipv4_cidr_block = "172.16.0.0/28" + add_cluster_firewall_rules = true + add_master_webhook_firewall_rules = true + add_shadow_firewall_rules = true + network_tags = ["allow-google-apis"] + deletion_protection = false + enable_binary_authorization = true + + master_authorized_networks = [ + { + cidr_block = "10.60.0.0/17" + display_name = "VPC" + }, + ] +}