Fix kine metrics registration

Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
This commit is contained in:
Brad Davidson 2026-05-28 06:40:17 +00:00 committed by Brad Davidson
parent fb4c6d1fb3
commit ad2fa9df0c
3 changed files with 17 additions and 4 deletions

View file

@ -84,6 +84,9 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont
// If exiting due to an error, ensure that contexts are cancelled so that the
// WaitGroup exits. Otherwise, wait for something else to initiate shutdown.
defer func() {
if r := recover(); r != nil {
rerr = fmt.Errorf("server panicked: %v", r)
}
if rerr != nil {
// do not need to pass the error in here, it will be reported by the CLI error handler
signals.RequestShutdown(nil)

View file

@ -182,11 +182,17 @@ func (c *Cluster) startStorage(ctx context.Context, bootstrap bool) error {
}()
c.storageRunning = true
if !bootstrap {
// only register metrics when not bootstrapping, to prevent
// multiple datastore metrics from being registered.
if !c.config.KineTLS {
// if TLS is not enabled for kine, storage will only be started once,
// so go ahead and register metrics now
c.config.Datastore.MetricsRegisterer = metrics.DefaultRegisterer
// set the tls config for the kine storage
}
if !bootstrap {
// only register metrics after boostrapping is done, to prevent
// multiple datastore metrics from being registered
c.config.Datastore.MetricsRegisterer = metrics.DefaultRegisterer
// set the tls config for the kine storage, once bootstrapped
c.config.Datastore.ServerTLSConfig.CAFile = c.config.Runtime.ETCDServerCA
c.config.Datastore.ServerTLSConfig.CertFile = c.config.Runtime.ServerETCDCert
c.config.Datastore.ServerTLSConfig.KeyFile = c.config.Runtime.ServerETCDKey

View file

@ -7,6 +7,8 @@ import (
"github.com/k3s-io/k3s/pkg/daemons/config"
"github.com/k3s-io/k3s/pkg/daemons/control/deps"
"github.com/k3s-io/k3s/pkg/metrics"
"github.com/prometheus/client_golang/prometheus"
)
// GenerateDataDir creates a temporary directory at "/tmp/k3s/<RANDOM_STRING>/".
@ -43,6 +45,8 @@ func CleanupDataDir(cnf *config.Control) {
// GenerateRuntime creates a temporary data dir and configures
// config.ControlRuntime with all the appropriate certificate keys.
func GenerateRuntime(cnf *config.Control) error {
// reset metrics registerer to avoid panicing if test cover metrics registration
metrics.DefaultRegisterer = prometheus.NewRegistry()
// reuse ready channel from existing runtime if set
cnf.Runtime = config.NewRuntime()
if err := GenerateDataDir(cnf); err != nil {