The worker pool size can be retrieved from the Compute Engine API by getting the instance group associated with the node pool.
const { google } = require('googleapis')
const Compute = require('@google-cloud/compute')
const container = google.container('v1')
const compute = new Compute()
const projectId = 'project-12345'
const zone = 'us-central1-a'
const nodePoolId = 'worker-pool'
const clusterId = 'cluster-name'
async function authorize() {
const auth = new google.auth.GoogleAuth({
scopes: [ 'https://www.googleapis.com/auth/cloud-platform' ],
})
return auth.getClient()
}
const getNodePoolSize = async () => {
const auth = await authorize()
const clusterName = `projects/${projectId}/zones/${zone}/clusters/${clusterId}`
const request = { name: clusterName, auth }
const response = await container.projects.locations.clusters.get(request)
const nodePool = response.data.nodePools.find(({ name }) => name === nodePoolId)
const igName = nodePool.instanceGroupUrls[0].match(/.*\/instanceGroupManagers\/([a-z0-9-]*)$/)[1]
const instanceGroup = await compute.zone(zone).instanceGroup(igName).get()
return instanceGroup[1 /* 0 is config, 1 is instance */].size
}
Note that this is using two different Node API mechanisms. We could use google.compute
instead of @google-cloud/compute
. Also, the two APIs are authenticated differently. The former uses the authorize()
method to get a client, while the latter is authenticated via the default account set in environment variables.
Cluster
object returned byget
for theCluster
resource. It seems to expose acurrentNodeCount
value. However, it is also a deprecated field and suggestion is to directly use the K8s API. In that case, if you use the node library, this information is exposed by theGetCluster
call. – Shabirmeancontainer.projects.locations.clusters.get
. I have not directly tried thegetCluster
call, but I'd assume it returns the same results. The Cluster resource does include the Instance Group IDs for all the node pools. I'll try checking the GCE instance pool next. – Daniel Wexler