Skip to content

Commit

Permalink
cmdbuilder: add WithVersion() that reports version metrics of binary
Browse files Browse the repository at this point in the history
  • Loading branch information
mfojtik committed Apr 14, 2020
1 parent d3cb092 commit 114736d
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pkg/controller/controllercmd/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"
"sync"
"time"

"k8s.io/component-base/metrics"
"k8s.io/component-base/metrics/legacyregistry"
"k8s.io/klog"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/version"
genericapiserver "k8s.io/apiserver/pkg/server"
"k8s.io/apiserver/pkg/server/healthz"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -72,6 +76,8 @@ type ControllerBuilder struct {
authorizationConfig *operatorv1alpha1.DelegatedAuthorization
healthChecks []healthz.HealthChecker

versionInfo *version.Info

// nonZeroExitFn takes a function that exit the process with non-zero code.
// This stub exists for unit test where we can check if the graceful termination work properly.
// Default function will klog.Warning(args) and os.Exit(1).
Expand Down Expand Up @@ -134,6 +140,12 @@ func (b *ControllerBuilder) WithLeaderElection(leaderElection configv1.LeaderEle
return b
}

// WithVersion accepts a getting that provide binary version information that is used to report build_info information to prometheus
func (b *ControllerBuilder) WithVersion(info version.Info) *ControllerBuilder {
b.versionInfo = &info
return b
}

// WithServer adds a server that provides metrics and healthz
func (b *ControllerBuilder) WithServer(servingInfo configv1.HTTPServingInfo, authenticationConfig operatorv1alpha1.DelegatedAuthentication, authorizationConfig operatorv1alpha1.DelegatedAuthorization) *ControllerBuilder {
b.servingInfo = servingInfo.DeepCopy()
Expand Down Expand Up @@ -194,6 +206,23 @@ func (b *ControllerBuilder) Run(ctx context.Context, config *unstructured.Unstru
}
}

// report the binary version metrics to prometheus
if b.versionInfo != nil {
buildInfo := metrics.NewGaugeVec(
&metrics.GaugeOpts{
Name: strings.Replace(b.componentNamespace, "-", "_", -1) + "_build_info",
Help: "A metric with a constant '1' value labeled by major, minor, git version, git commit, git tree state, build date, Go version, " +
"and compiler from which " + b.componentName + " was built, and platform on which it is running.",
StabilityLevel: metrics.ALPHA,
},
[]string{"major", "minor", "gitVersion", "gitCommit", "gitTreeState", "buildDate", "goVersion", "compiler", "platform"},
)
legacyregistry.MustRegister(buildInfo)
buildInfo.WithLabelValues(b.versionInfo.Major, b.versionInfo.Minor, b.versionInfo.GitVersion, b.versionInfo.GitCommit, b.versionInfo.GitTreeState, b.versionInfo.BuildDate, b.versionInfo.GoVersion,
b.versionInfo.Compiler, b.versionInfo.Platform).Set(1)
klog.Infof("%s version %s-%s", b.componentName, b.versionInfo.GitVersion, b.versionInfo.GitCommit)
}

kubeConfig := ""
if b.kubeAPIServerConfigFile != nil {
kubeConfig = *b.kubeAPIServerConfigFile
Expand Down

0 comments on commit 114736d

Please sign in to comment.