Skip to content

fix: Ignore non-numeric characters in kubectl minor version string

Matt Smiley requested to merge use_numeric_cmp_for_kube_minor_version_check into master

Problem

Our glsh kube script has a trivial bug due to a safety check assuming that kubectl version --client will reasonably return a numeric value. However, for some builds (like mine), it includes a + suffix (see example below). Since we only aim to ensure a minimum version requirement, we can ignore that suffix.

$ glsh kube use-cluster gprd-us-east1-b
/data/src/git/gitlab/gitlab-com/runbooks/bin/kube: line 122: ((: 26+ < 24: syntax error: operand expected (error token is "< 24")
...

$ kubectl version --client -o json | jq '.clientVersion.minor'
"26+"

$ kubectl version --client -o json | jq '.clientVersion'
{
  "major": "1",
  "minor": "26+",
  "gitVersion": "v1.26.13-dispatcher",
  "gitCommit": "eb237aa977a4a0cf4fcec65fc730b4d96af37ccf",
  "gitTreeState": "clean",
  "buildDate": "2024-02-08T00:21:15Z",
  "goVersion": "go1.20.13",
  "compiler": "gc",
  "platform": "linux/amd64"
}

Fix: Ignore non-numeric characters in kubectl minor version string.

Our kube script ensures that the installed kubectl is at least minor version 24:

kubectl version --client -o json | jq -r '.clientVersion.minor'

It uses a numeric comparison for this check.

However, some builds of kubectl include a + suffix on their minor version string:

$ kubectl version --client -o json | jq '.clientVersion.minor'
"26+"

This change strips off that unwanted suffix by matching only the first numeric sequence:

$ kubectl version --client -o json | jq '.clientVersion.minor | match("\\d+").string'
"26"

More generally:

$ echo '{ "foo" : "abc123def456" }' | jq '.foo | match("\\d+").string'
"123"

Merge request reports