gcloud container get-server-config
gcloud container clusters create is a complex command. The --cluster-version flag, often combined with --release-channel in order to have Google maintain the master and node versions, take values that are provided by gcloud container get-server-config.
The available Kubernetes versions differs by region and by zone:
gcloud container get-server-config \
--project=${PROJECT} \
--zone=us-west2-c
Yields:
channels:
- channel: RAPID
defaultVersion: 1.21.3-gke.2001
validVersions:
- 1.21.4-gke.301
- 1.21.3-gke.2001
Whereas:
gcloud container get-server-config \
--project=${PROJECT} \
--region=us-west2
Yields
channels:
- channel: RAPID
defaultVersion: 1.21.4-gke.301
validVersions:
- 1.21.4-gke.1801
- 1.21.4-gke.301
I assume the divergence results from a controlled rollout of Kubernetes versions across regions and zones.
NOTE The command does not appear to validate regions and zones and you can use e.g.
--region=us-west2-cand--zone=us-west2which is easily done and could be problematic
It’s possible to filter the rather unwieldy results from this command using gcloud.
For example to filter by channel:
CHANNEL="RAPID"
gcloud container get-server-config \
--project=${PROJECT} \
--zone=${ZONE} \
--flatten=containers \
--filter=channels.channel=${CHANNEL}
gcloud filters are often combined with --flatten as in this case --flatten=channels. Adding --flatten=channels has the effect of turning a single result into multiple results, one for each item in the flattened list. In this case, channels has 3 items (RAPID,REGULAR and STABLE) and so 3 (YAML) documents are returned.
The addition of --filter limits the results to only the document containing channel: RAPID but we otherwise receive e.g. validImageTypes, validMasterVersions and validNodeVersions which, in this case, we don’t want.
Yields:
channels:
channel: RAPID
defaultVersion: 1.21.3-gke.2001
validVersions:
- 1.21.4-gke.301
- 1.21.3-gke.2001
defaultClusterVersion: 1.20.8-gke.2101
defaultImageType: COS
validImageTypes:
- ...
validMasterVersions:
- ...
validNodeVersions:
- ...
NOTE The use of
--flattenmeans that we now have achannelsdictionary (containing e.g. a keychannel) whereas without--flatten,channelswas a list (of 3 items).
We can address this by formatting the results to only include channels:
CHANNEL="RAPID"
gcloud container get-server-config \
--project=${PROJECT} \
--zone=${ZONE} \
--flatten=channels \
--filter=channels.channel=${CHANNEL} \
--format="yaml(channels)"
Yields:
channels:
channel: RAPID
defaultVersion: 1.21.3-gke.2001
validVersions:
- 1.21.4-gke.301
- 1.21.3-gke.2001
Assuming (!) that validVersions are listed in decreasing order, the latest version is the first (0th item of the list). So:
CHANNEL="RAPID"
gcloud container get-server-config \
--project=${PROJECT} \
--zone=${ZONE} \
--flatten=channels \
--filter=channels.channel=${CHANNEL} \
--format="value(channels.validVersions[0])"
Yields:
1.21.4-gke.301
The command (somewhat annoyingly) outputs e.g. Fetching server config for ... but this is sent to stderr (not stdout) and is not included if, for example, you assign the command to a variable CLUSTER_VERSION=$(gcloud container get-server-config ...) using the previous command will only contain 1.21.4-gke.301
So, you can:
CHANNEL="RAPID"
CLUSTER_VERSION=$(\
gcloud container get-server-config \
--project=${PROJECT} \
--zone=${ZONE} \
--flatten=channels \
--filter=channels.channel=${CHANNEL} \
--format="value(channels.validVersions[0])")
gcloud beta container clusters create ... \
--cluster-version=${CLUSTER_VERSION} ...