Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add examples to terraform-module #2462

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions __snapshots__/example-version.js
Original file line number Diff line number Diff line change
@@ -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"
},
]
}

`
21 changes: 20 additions & 1 deletion src/strategies/terraform-module.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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';
Expand Down Expand Up @@ -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;
}

Expand Down
32 changes: 32 additions & 0 deletions src/updaters/terraform/example-version.ts
Original file line number Diff line number Diff line change
@@ -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}"`
);
}
}
38 changes: 38 additions & 0 deletions test/updaters/example-version.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
47 changes: 47 additions & 0 deletions test/updaters/fixtures/terraform-module/example.tf
Original file line number Diff line number Diff line change
@@ -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"
},
]
}