Enable network policy controller metrics

Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
(cherry picked from commit ab8d2f55b9)
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
pull/9216/head
Brad Davidson 2024-01-10 00:10:28 +00:00 committed by Brad Davidson
parent bda4b73493
commit b04e18c4a0
1 changed files with 44 additions and 7 deletions

View File

@ -17,12 +17,12 @@ import (
"k8s.io/apimachinery/pkg/util/wait" "k8s.io/apimachinery/pkg/util/wait"
cloudproviderapi "k8s.io/cloud-provider/api" cloudproviderapi "k8s.io/cloud-provider/api"
"github.com/cloudnativelabs/kube-router/v2/pkg/version"
"github.com/cloudnativelabs/kube-router/v2/pkg/controllers/netpol" "github.com/cloudnativelabs/kube-router/v2/pkg/controllers/netpol"
"github.com/cloudnativelabs/kube-router/v2/pkg/healthcheck" "github.com/cloudnativelabs/kube-router/v2/pkg/healthcheck"
"github.com/cloudnativelabs/kube-router/v2/pkg/metrics"
"github.com/cloudnativelabs/kube-router/v2/pkg/options" "github.com/cloudnativelabs/kube-router/v2/pkg/options"
"github.com/cloudnativelabs/kube-router/v2/pkg/utils" "github.com/cloudnativelabs/kube-router/v2/pkg/utils"
"github.com/cloudnativelabs/kube-router/v2/pkg/version"
"github.com/coreos/go-iptables/iptables" "github.com/coreos/go-iptables/iptables"
"github.com/k3s-io/k3s/pkg/daemons/config" "github.com/k3s-io/k3s/pkg/daemons/config"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -31,8 +31,15 @@ import (
"k8s.io/client-go/informers" "k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd"
"k8s.io/component-base/metrics/legacyregistry"
) )
func init() {
// ensure that kube-router exposes metrics through the same registry used by Kubernetes components
metrics.DefaultRegisterer = legacyregistry.Registerer()
metrics.DefaultGatherer = legacyregistry.DefaultGatherer
}
// Run creates and starts a new instance of the kube-router network policy controller // Run creates and starts a new instance of the kube-router network policy controller
// The code in this function is cribbed from the upstream controller at: // The code in this function is cribbed from the upstream controller at:
// https://github.com/cloudnativelabs/kube-router/blob/ee9f6d890d10609284098229fa1e283ab5d83b93/pkg/cmd/kube-router.go#L78 // https://github.com/cloudnativelabs/kube-router/blob/ee9f6d890d10609284098229fa1e283ab5d83b93/pkg/cmd/kube-router.go#L78
@ -92,7 +99,7 @@ func Run(ctx context.Context, nodeConfig *config.Node) error {
krConfig.EnableIPv6 = nodeConfig.AgentConfig.EnableIPv6 krConfig.EnableIPv6 = nodeConfig.AgentConfig.EnableIPv6
krConfig.NodePortRange = strings.ReplaceAll(nodeConfig.AgentConfig.ServiceNodePortRange.String(), "-", ":") krConfig.NodePortRange = strings.ReplaceAll(nodeConfig.AgentConfig.ServiceNodePortRange.String(), "-", ":")
krConfig.HostnameOverride = nodeConfig.AgentConfig.NodeName krConfig.HostnameOverride = nodeConfig.AgentConfig.NodeName
krConfig.MetricsEnabled = false krConfig.MetricsEnabled = true
krConfig.RunFirewall = true krConfig.RunFirewall = true
krConfig.RunRouter = false krConfig.RunRouter = false
krConfig.RunServiceProxy = false krConfig.RunServiceProxy = false
@ -141,22 +148,31 @@ func Run(ctx context.Context, nodeConfig *config.Node) error {
ipSetHandlers[v1core.IPv6Protocol] = ipset ipSetHandlers[v1core.IPv6Protocol] = ipset
} }
// Start kube-router healthcheck server. Netpol requires it // Start kube-router healthcheck controller; netpol requires it
hc, err := healthcheck.NewHealthController(krConfig) hc, err := healthcheck.NewHealthController(krConfig)
if err != nil { if err != nil {
return err return err
} }
// Initialize all healthcheck timers. Otherwise, the system reports incorrect heartbeat missing messages // Start kube-router metrics controller to avoid complaints about metrics heartbeat missing
mc, err := metrics.NewMetricsController(krConfig)
if err != nil {
return nil
}
// Initialize all healthcheck timers. Otherwise, the system reports heartbeat missing messages
hc.SetAlive() hc.SetAlive()
wg.Add(1) wg.Add(1)
go hc.RunCheck(healthCh, stopCh, &wg) go hc.RunCheck(healthCh, stopCh, &wg)
wg.Add(1)
go metricsRunCheck(mc, healthCh, stopCh, &wg)
npc, err := netpol.NewNetworkPolicyController(client, krConfig, podInformer, npInformer, nsInformer, &sync.Mutex{}, npc, err := netpol.NewNetworkPolicyController(client, krConfig, podInformer, npInformer, nsInformer, &sync.Mutex{},
iptablesCmdHandlers, ipSetHandlers) iptablesCmdHandlers, ipSetHandlers)
if err != nil { if err != nil {
return errors.Wrap(err, "unable to initialize Network Policy Controller") return errors.Wrap(err, "unable to initialize network policy controller")
} }
podInformer.AddEventHandler(npc.PodEventHandler) podInformer.AddEventHandler(npc.PodEventHandler)
@ -164,8 +180,29 @@ func Run(ctx context.Context, nodeConfig *config.Node) error {
npInformer.AddEventHandler(npc.NetworkPolicyEventHandler) npInformer.AddEventHandler(npc.NetworkPolicyEventHandler)
wg.Add(1) wg.Add(1)
logrus.Infof("Starting the netpol controller version %s, built on %s, %s", version.Version, version.BuildDate, runtime.Version()) logrus.Infof("Starting network policy controller version %s, built on %s, %s", version.Version, version.BuildDate, runtime.Version())
go npc.Run(healthCh, stopCh, &wg) go npc.Run(healthCh, stopCh, &wg)
return nil return nil
} }
// metricsRunCheck is a stub version of mc.Run() that doesn't start up a dedicated http server.
func metricsRunCheck(mc *metrics.Controller, healthChan chan<- *healthcheck.ControllerHeartbeat, stopCh <-chan struct{}, wg *sync.WaitGroup) {
t := time.NewTicker(3 * time.Second)
defer wg.Done()
// register metrics for this controller
metrics.BuildInfo.WithLabelValues(runtime.Version(), version.Version).Set(1)
metrics.DefaultRegisterer.MustRegister(metrics.BuildInfo)
for {
healthcheck.SendHeartBeat(healthChan, "MC")
select {
case <-stopCh:
t.Stop()
return
case <-t.C:
logrus.Debugf("Kube-router network policy controller metrics tick")
}
}
}