in src/main/java/com/google/cloud/run/kafkascaler/clients/CloudRunClientWrapper.java [237:272]
public void updateServiceMinInstances(String serviceName, int instances) throws IOException {
GoogleCloudRunV2Service service = getService(serviceName);
GoogleCloudRunV2ServiceScaling oldScaling = service.getScaling();
GoogleCloudRunV2ServiceScaling newScaling = new GoogleCloudRunV2ServiceScaling();
if (oldScaling != null && oldScaling.getMaxInstanceCount() != null) {
newScaling.setMaxInstanceCount(oldScaling.getMaxInstanceCount());
}
newScaling.setMinInstanceCount(instances);
newScaling.setScalingMode(AUTOMATIC_SCALING_MODE);
service.setScaling(newScaling);
GoogleLongrunningOperation operation =
this.cloudRun
.projects()
.locations()
.services()
.patch(
String.format(
"projects/%s/locations/%s/services/%s",
this.projectId, this.region, serviceName),
service)
.setUpdateMask(SCALING_UPDATE_MASK)
.execute();
if (operation.getError() != null) {
throw new IOException(
"Request failed to Cloud Run to update service instances: " + operation.getError());
} else {
logger.atInfo().log(
"Sent update service request to set instances to %d for service %s",
instances, serviceName);
}
}