Skip to content

Commit cb91d9c

Browse files
authored
feat: consume reservations samples (#3877)
* feat: consume reservations samples * clean up resources before tests
1 parent 232f480 commit cb91d9c

9 files changed

+666
-21
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
async function main(instanceName) {
20+
// [START compute_consume_any_matching_reservation]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
const compute = computeLib.protos.google.cloud.compute.v1;
24+
25+
// Instantiate a reservationsClient
26+
const instancesClient = new computeLib.InstancesClient();
27+
// Instantiate a zoneOperationsClient
28+
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
29+
30+
/**
31+
* TODO(developer): Update/uncomment these variables before running the sample.
32+
*/
33+
// The ID of the project where you want to create instance.
34+
const projectId = await instancesClient.getProjectId();
35+
// The zone in which to create instance.
36+
const zone = 'us-central1-a';
37+
// The name of the instance to create.
38+
// const instanceName = 'instance-01';
39+
// Machine type to use for VM.
40+
const machineType = 'n1-standard-4';
41+
42+
// Create instance to consume reservation if their properties match the VM properties
43+
async function callCreateInstanceToConsumeAnyReservation() {
44+
// Describe the size and source image of the boot disk to attach to the instance.
45+
const disk = new compute.Disk({
46+
boot: true,
47+
autoDelete: true,
48+
type: 'PERSISTENT',
49+
initializeParams: {
50+
diskSizeGb: '10',
51+
sourceImage: 'projects/debian-cloud/global/images/family/debian-12',
52+
},
53+
});
54+
55+
// Define networkInterface
56+
const networkInterface = new compute.NetworkInterface({
57+
name: 'global/networks/default',
58+
});
59+
60+
// Define reservationAffinity
61+
const reservationAffinity = new compute.ReservationAffinity({
62+
consumeReservationType: 'ANY_RESERVATION',
63+
});
64+
65+
// Create an instance
66+
const instance = new compute.Instance({
67+
name: instanceName,
68+
machineType: `zones/${zone}/machineTypes/${machineType}`,
69+
minCpuPlatform: 'Intel Skylake',
70+
disks: [disk],
71+
networkInterfaces: [networkInterface],
72+
reservationAffinity,
73+
});
74+
75+
const [response] = await instancesClient.insert({
76+
project: projectId,
77+
instanceResource: instance,
78+
zone,
79+
});
80+
81+
let operation = response.latestResponse;
82+
83+
// Wait for the create instance operation to complete.
84+
while (operation.status !== 'DONE') {
85+
[operation] = await zoneOperationsClient.wait({
86+
operation: operation.name,
87+
project: projectId,
88+
zone: operation.zone.split('/').pop(),
89+
});
90+
}
91+
92+
console.log(`Instance ${instanceName} created.`);
93+
}
94+
95+
await callCreateInstanceToConsumeAnyReservation();
96+
// [END compute_consume_any_matching_reservation]
97+
}
98+
99+
main(...process.argv.slice(2)).catch(err => {
100+
console.error(err);
101+
process.exitCode = 1;
102+
});
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
async function main(instanceName, reservationName) {
20+
// [START compute_consume_single_project_reservation]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
const compute = computeLib.protos.google.cloud.compute.v1;
24+
25+
// Instantiate a reservationsClient
26+
const instancesClient = new computeLib.InstancesClient();
27+
// Instantiate a zoneOperationsClient
28+
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
29+
30+
/**
31+
* TODO(developer): Update/uncomment these variables before running the sample.
32+
*/
33+
// The ID of the project where you want to create instance.
34+
const projectId = await instancesClient.getProjectId();
35+
// The zone in which to create instance.
36+
const zone = 'us-central1-a';
37+
// The name of the instance to create.
38+
// const instanceName = 'instance-01';
39+
// The name of the reservation to consume.
40+
// Ensure that the specificReservationRequired field in reservation properties is set to true.
41+
// const reservationName = 'reservation-01';
42+
// Machine type to use for VM.
43+
const machineType = 'n1-standard-4';
44+
45+
// Create instance to consume a specific single-project reservation
46+
async function callCreateInstanceToConsumeSingleProjectReservation() {
47+
// Describe the size and source image of the boot disk to attach to the instance.
48+
// Ensure that the VM's properties match the reservation's VM properties,
49+
// including the zone, machine type (machine family, vCPUs, and memory),
50+
// minimum CPU platform, GPU amount and type, and local SSD interface and size
51+
const disk = new compute.Disk({
52+
boot: true,
53+
autoDelete: true,
54+
type: 'PERSISTENT',
55+
initializeParams: {
56+
diskSizeGb: '10',
57+
sourceImage: 'projects/debian-cloud/global/images/family/debian-12',
58+
},
59+
});
60+
61+
// Define networkInterface
62+
const networkInterface = new compute.NetworkInterface({
63+
name: 'global/networks/default',
64+
});
65+
66+
// Define reservationAffinity
67+
const reservationAffinity = new compute.ReservationAffinity({
68+
consumeReservationType: 'SPECIFIC_RESERVATION',
69+
key: 'compute.googleapis.com/reservation-name',
70+
values: [reservationName],
71+
});
72+
73+
// Create an instance
74+
const instance = new compute.Instance({
75+
name: instanceName,
76+
machineType: `zones/${zone}/machineTypes/${machineType}`,
77+
minCpuPlatform: 'Intel Skylake',
78+
disks: [disk],
79+
networkInterfaces: [networkInterface],
80+
reservationAffinity,
81+
});
82+
83+
const [response] = await instancesClient.insert({
84+
project: projectId,
85+
instanceResource: instance,
86+
zone,
87+
});
88+
89+
let operation = response.latestResponse;
90+
91+
// Wait for the create instance operation to complete.
92+
while (operation.status !== 'DONE') {
93+
[operation] = await zoneOperationsClient.wait({
94+
operation: operation.name,
95+
project: projectId,
96+
zone: operation.zone.split('/').pop(),
97+
});
98+
}
99+
100+
console.log(`Instance ${instanceName} created.`);
101+
}
102+
103+
await callCreateInstanceToConsumeSingleProjectReservation();
104+
// [END compute_consume_single_project_reservation]
105+
}
106+
107+
main(...process.argv.slice(2)).catch(err => {
108+
console.error(err);
109+
process.exitCode = 1;
110+
});
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
async function main(instanceName) {
20+
// [START compute_instance_not_consume_reservation]
21+
// Import the Compute library
22+
const computeLib = require('@google-cloud/compute');
23+
const compute = computeLib.protos.google.cloud.compute.v1;
24+
25+
// Instantiate a reservationsClient
26+
const instancesClient = new computeLib.InstancesClient();
27+
// Instantiate a zoneOperationsClient
28+
const zoneOperationsClient = new computeLib.ZoneOperationsClient();
29+
30+
/**
31+
* TODO(developer): Update/uncomment these variables before running the sample.
32+
*/
33+
// The ID of the project where you want to create instance.
34+
const projectId = await instancesClient.getProjectId();
35+
// The zone in which to create instance.
36+
const zone = 'us-central1-a';
37+
// The name of the instance to create.
38+
// const instanceName = 'instance-01';
39+
// Machine type to use for VM.
40+
const machineType = 'n1-standard-4';
41+
42+
// Create a VM that explicitly doesn't consume reservations
43+
async function callCreateInstanceToNotConsumeReservation() {
44+
// Describe the size and source image of the boot disk to attach to the instance.
45+
const disk = new compute.Disk({
46+
boot: true,
47+
autoDelete: true,
48+
type: 'PERSISTENT',
49+
initializeParams: {
50+
diskSizeGb: '10',
51+
sourceImage: 'projects/debian-cloud/global/images/family/debian-12',
52+
},
53+
});
54+
55+
// Define networkInterface
56+
const networkInterface = new compute.NetworkInterface({
57+
name: 'global/networks/default',
58+
});
59+
60+
// Define reservationAffinity
61+
const reservationAffinity = new compute.ReservationAffinity({
62+
consumeReservationType: 'NO_RESERVATION',
63+
});
64+
65+
// Create an instance
66+
const instance = new compute.Instance({
67+
name: instanceName,
68+
machineType: `zones/${zone}/machineTypes/${machineType}`,
69+
minCpuPlatform: 'Intel Skylake',
70+
disks: [disk],
71+
networkInterfaces: [networkInterface],
72+
reservationAffinity,
73+
});
74+
75+
const [response] = await instancesClient.insert({
76+
project: projectId,
77+
instanceResource: instance,
78+
zone,
79+
});
80+
81+
let operation = response.latestResponse;
82+
83+
// Wait for the create instance operation to complete.
84+
while (operation.status !== 'DONE') {
85+
[operation] = await zoneOperationsClient.wait({
86+
operation: operation.name,
87+
project: projectId,
88+
zone: operation.zone.split('/').pop(),
89+
});
90+
}
91+
92+
console.log(`Instance ${instanceName} created.`);
93+
}
94+
95+
await callCreateInstanceToNotConsumeReservation();
96+
// [END compute_instance_not_consume_reservation]
97+
}
98+
99+
main(...process.argv.slice(2)).catch(err => {
100+
console.error(err);
101+
process.exitCode = 1;
102+
});

compute/reservations/createReservationFromProperties.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,25 @@ async function main(reservationName) {
5050
// To have the reserved VMs use a specific minimum CPU platform instead of the zone's default CPU platform.
5151
minCpuPlatform: 'Intel Skylake',
5252
// If you want to attach GPUs to your reserved N1 VMs, update and uncomment guestAccelerators if needed.
53-
guestAccelerators: [
54-
{
55-
// The number of GPUs to add per reserved VM.
56-
acceleratorCount: 1,
57-
// Supported GPU model for N1 VMs. Ensure that your chosen GPU model is available in the zone,
58-
// where you want to reserve resources.
59-
acceleratorType: 'nvidia-tesla-t4',
60-
},
61-
],
53+
// guestAccelerators: [
54+
// {
55+
// // The number of GPUs to add per reserved VM.
56+
// acceleratorCount: 1,
57+
// // Supported GPU model for N1 VMs. Ensure that your chosen GPU model is available in the zone,
58+
// // where you want to reserve resources.
59+
// acceleratorType: 'nvidia-tesla-t4',
60+
// },
61+
// ],
6262
// If you want to add local SSD disks to each reserved VM, update and uncomment localSsds if needed.
6363
// You can specify up to 24 Local SSD disks. Each Local SSD disk is 375 GB.
64-
localSsds: [
65-
{
66-
diskSizeGb: 375,
67-
// The type of interface you want each Local SSD disk to use. Specify one of the following values: NVME or SCSI.
68-
// Make sure that the machine type you specify for the reserved VMs supports the chosen disk interfaces.
69-
interface: 'NVME',
70-
},
71-
],
64+
// localSsds: [
65+
// {
66+
// diskSizeGb: 375,
67+
// // The type of interface you want each Local SSD disk to use. Specify one of the following values: NVME or SCSI.
68+
// // Make sure that the machine type you specify for the reserved VMs supports the chosen disk interfaces.
69+
// interface: 'NVME',
70+
// },
71+
// ],
7272
},
7373
});
7474

@@ -77,6 +77,9 @@ async function main(reservationName) {
7777
name: reservationName,
7878
zone,
7979
specificReservation,
80+
// To specify that only VMs that specifically target this reservation can consume it,
81+
// set specificReservationRequired field to true.
82+
specificReservationRequired: true,
8083
});
8184

8285
const [response] = await reservationsClient.insert({

compute/reservations/createReservationInstanceTemplate.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ async function main(reservationName, location, instanceTemplateName) {
6868
const reservation = new compute.Reservation({
6969
name: reservationName,
7070
specificReservation,
71+
// To specify that only VMs that specifically target this reservation can consume it,
72+
// set specificReservationRequired field to true.
73+
specificReservationRequired: true,
7174
});
7275

7376
const [response] = await reservationsClient.insert({

0 commit comments

Comments
 (0)