From 66373c2eb91dc372e84520568cb58264942fcc82 Mon Sep 17 00:00:00 2001 From: fpetkovski Date: Tue, 19 Aug 2025 12:46:48 +0200 Subject: [PATCH] Add info metric for client-go version KSM supports a range of Kubernetes versions which makes it important to keep up with new versions as clusters upgrade. In order to allow for alerting on unsupported cluster versions, this commit adds an info metric for the client-go version used by KSM. This way users can set up alerts if the cluster in which KSM is running is outside of the supported range. Signed-off-by: fpetkovski --- Makefile | 6 ++++-- pkg/app/server.go | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index fbbe669a7..1d77deb45 100644 --- a/Makefile +++ b/Makefile @@ -20,10 +20,12 @@ MULTI_ARCH_IMG = $(IMAGE)-$(ARCH) USER ?= $(shell id -u -n) HOST ?= $(shell hostname) MARKDOWNLINT_CLI2_VERSION = 0.18.1 +CLIENT_GO_VERSION = $(shell go list -m -f '{{.Version}}' k8s.io/client-go) +KSM_MODULE = $(shell go list -m) DOCKER_CLI ?= docker PROMTOOL_CLI ?= promtool -GOMPLATE_CLI ?= go tool github.com/hairyhenderson/gomplate/v4/cmd/gomplate +GOMPLATE_CLI ?= go tool github.com/hairyhenderson/gomplate/v4/cmd/gomplate GOJSONTOYAML_CLI ?= go tool github.com/brancz/gojsontoyaml EMBEDMD_CLI ?= go tool github.com/campoy/embedmd JSONNET_CLI ?= go tool github.com/google/go-jsonnet/cmd/jsonnet @@ -69,7 +71,7 @@ doccheck: generate validate-template @echo OK build-local: - GOOS=$(OS) GOARCH=$(ARCH) CGO_ENABLED=0 go build -ldflags "-s -w -X ${PKG}/version.Version=${TAG} -X ${PKG}/version.Revision=${GIT_COMMIT} -X ${PKG}/version.Branch=${BRANCH} -X ${PKG}/version.BuildUser=${USER}@${HOST} -X ${PKG}/version.BuildDate=${BUILD_DATE}" -o kube-state-metrics + GOOS=$(OS) GOARCH=$(ARCH) CGO_ENABLED=0 go build -ldflags "-s -w -X ${PKG}/version.Version=${TAG} -X ${PKG}/version.Revision=${GIT_COMMIT} -X ${PKG}/version.Branch=${BRANCH} -X ${PKG}/version.BuildUser=${USER}@${HOST} -X ${PKG}/version.BuildDate=${BUILD_DATE} -X ${PKG}/version.BuildDate=${BUILD_DATE} -X ${KSM_MODULE}/pkg/app.ClientGoVersion=${CLIENT_GO_VERSION}" -o kube-state-metrics build: kube-state-metrics diff --git a/pkg/app/server.go b/pkg/app/server.go index 31831ef73..a33ddf8e2 100644 --- a/pkg/app/server.go +++ b/pkg/app/server.go @@ -63,6 +63,10 @@ import ( "k8s.io/kube-state-metrics/v2/pkg/util/proc" ) +// ClientGoVersion is the version for the client-go library used by KSM. This +// value is set at build time using go build flags. +var ClientGoVersion = "unknown" + const ( metricsPath = "/metrics" healthzPath = "/healthz" @@ -86,6 +90,16 @@ func RunKubeStateMetricsWrapper(ctx context.Context, opts *options.Options) erro func RunKubeStateMetrics(ctx context.Context, opts *options.Options) error { ksmMetricsRegistry := prometheus.NewRegistry() ksmMetricsRegistry.MustRegister(versionCollector.NewCollector("kube_state_metrics")) + + clientGoVersion := promauto.With(ksmMetricsRegistry).NewGaugeVec( + prometheus.GaugeOpts{ + Name: "kube_state_metrics_client_go_info", + Help: "An info metric for the client-go version used by kube-state-metrics", + }, + []string{"version"}, + ) + clientGoVersion.WithLabelValues(ClientGoVersion).Set(1) + durationVec := promauto.With(ksmMetricsRegistry).NewHistogramVec( prometheus.HistogramOpts{ Name: "http_request_duration_seconds",