Search code examples
kubernetesopenshiftgoogle-kubernetes-enginesemantic-versioningopenshift-3

How to get Kubernetes Version as integer from API


I am trying to get the current Kubernetes version via the API, because depending on the version some things are not supported (e.g. readyz endpoint is not available before 1.16, so I want to check healthz endpoint instead).

I found the following endpoint: /version

Example result:

{
  "major": "1",
  "minor": "11+",
  "gitVersion": "v1.11.0+d4cacc0",
  "gitCommit": "d4cacc0",
  "gitTreeState": "clean",
  "buildDate": "2021-05-12T14:13:55Z",
  "goVersion": "go1.10.8",
  "compiler": "gc",
  "platform": "linux/amd64"
}

Is this the correct approach to get the version via API? I thaught Kubernetes uses semantic versioning, why is the "+" in the minor String? Is this configurable? Where can I find a specification of this API/fields?

Ideally this should work for standalone and managed Kubernetes flavors (Openshift, GKE...).


Solution

  • GitVersion/gitVersion is the semantic versioning field.

    This is the version of the kube-api-server component specifically.

    The parsing is done by regex in The k8s.io/apimachinery package:

    ^\s*v?([0-9]+(?:\.[0-9]+)*)(.*)*$`
    

    The /version endpoint is part of the documented API spec (operationId: "getCode") so should be pretty stable. It's what kubectl version uses so I doubt it will change easily.