Skip to content

Commit

Permalink
Using separate Client to refresh Runtime metrics on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
koala7659 committed Oct 25, 2024
1 parent 8264618 commit cc0e9f1
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"encoding/json"
"flag"
"fmt"
"github.com/go-logr/logr"
"io"
"k8s.io/client-go/rest"
"os"
"time"

Expand Down Expand Up @@ -106,7 +108,9 @@ func main() {
logger := zap.New(zap.UseFlagOptions(&opts))
ctrl.SetLogger(logger)

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
restConfig := ctrl.GetConfigOrDie()

mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
Metrics: metricsserver.Options{
BindAddress: metricsAddr,
},
Expand Down Expand Up @@ -220,14 +224,7 @@ func main() {
os.Exit(1)
}

logger.Info("Refreshing runtime CR metrics")
metrics.ResetRuntimeMetrics()
var RuntimeList infrastructuremanagerv1.RuntimeList
if err = mgr.GetClient().List(context.TODO(), &RuntimeList); err == nil {
for _, rt := range RuntimeList.Items {
metrics.SetRuntimeStates(rt)
}
}
refreshRuntimeMetrics(restConfig, logger, metrics)

setupLog.Info("Starting Manager", "kubeconfigExpirationTime", expirationTime, "kubeconfigRotationPeriod", rotationPeriod)

Expand Down Expand Up @@ -313,3 +310,29 @@ func validateAuditLogDataMap(data map[string]map[string]auditlogging.AuditLogDat

return nil
}

func refreshRuntimeMetrics(restConfig *rest.Config, logger logr.Logger, metrics metrics.Metrics) {
k8sClient, err := client.New(restConfig, client.Options{})
if err != nil {
setupLog.Error(err, "Unable to set up client for refreshing runtime CR metrics")
os.Exit(1)
}

err = infrastructuremanagerv1.AddToScheme(k8sClient.Scheme())
if err != nil {
setupLog.Error(err, "unable to set up client")
os.Exit(1)
}

logger.Info("Refreshing runtime CR metrics")
metrics.ResetRuntimeMetrics()
rl := infrastructuremanagerv1.RuntimeList{}
if err = k8sClient.List(context.Background(), &rl, &client.ListOptions{Namespace: "kcp-system"}); err != nil {
setupLog.Error(err, "error while listing unable to list runtimes")
os.Exit(1)
}

for _, rt := range rl.Items {
metrics.SetRuntimeStates(rt)
}
}

0 comments on commit cc0e9f1

Please sign in to comment.