Skip to content

Commit f49bc36

Browse files
committed
feat(gobgp): add kube_router_bgp_peer_info metric
Replace the misleading kube_router_controller_bgp_peers gauge which only counts 'cluster nodes'. with a new per peer metric kube_router_bgp_peer_info with 'GaugeVec' that exposes actual BGP session state from gobgp. labels include peer address, asn, and session state. metric value is 1 if established and 0 otherwise. Closes: #848 Signed-off-by: Roman Kuzmitskii <roman@damex.org>
1 parent da98050 commit f49bc36

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

pkg/controllers/routing/bgp_peers.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ func (nrc *NetworkRoutingController) syncInternalPeers() {
8888
// get the current list of the nodes from API server
8989
nodes := nrc.nodeLister.List()
9090

91-
if nrc.MetricsEnabled {
92-
metrics.ControllerBPGpeers.Set(float64(len(nodes)))
93-
}
9491
// establish peer and add Pod CIDRs with current set of nodes
9592
currentNodes := make([]string, 0)
9693
for _, obj := range nodes {

pkg/controllers/routing/network_routes_controller.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,10 @@ func (nrc *NetworkRoutingController) Run(
436436
nrc.syncInternalPeers()
437437
}
438438

439+
if nrc.MetricsEnabled {
440+
nrc.updateBGPPeerMetrics()
441+
}
442+
439443
if err == nil {
440444
healthcheck.SendHeartBeat(healthChan, healthcheck.NetworkRoutesController)
441445
} else {
@@ -729,6 +733,47 @@ func (nrc *NetworkRoutingController) injectRoute(path *apiutil.Path) error {
729733
return nrc.routeSyncer.SyncLocalRouteTable()
730734
}
731735

736+
func bgpSessionStateString(state gobgpapi.PeerState_SessionState) string {
737+
switch state {
738+
case gobgpapi.PeerState_SESSION_STATE_UNSPECIFIED:
739+
return "unspecified"
740+
case gobgpapi.PeerState_SESSION_STATE_IDLE:
741+
return "idle"
742+
case gobgpapi.PeerState_SESSION_STATE_CONNECT:
743+
return "connect"
744+
case gobgpapi.PeerState_SESSION_STATE_ACTIVE:
745+
return "active"
746+
case gobgpapi.PeerState_SESSION_STATE_OPENSENT:
747+
return "opensent"
748+
case gobgpapi.PeerState_SESSION_STATE_OPENCONFIRM:
749+
return "openconfirm"
750+
case gobgpapi.PeerState_SESSION_STATE_ESTABLISHED:
751+
return "established"
752+
default:
753+
return "unknown"
754+
}
755+
}
756+
757+
func (nrc *NetworkRoutingController) updateBGPPeerMetrics() {
758+
metrics.ControllerBGPPeerInfo.Reset()
759+
err := nrc.bgpServer.ListPeer(context.Background(), &gobgpapi.ListPeerRequest{}, func(peer *gobgpapi.Peer) {
760+
if peer.Conf == nil || peer.State == nil {
761+
return
762+
}
763+
addr := peer.Conf.NeighborAddress
764+
asn := strconv.FormatUint(uint64(peer.Conf.PeerAsn), 10)
765+
state := bgpSessionStateString(peer.State.SessionState)
766+
var value float64
767+
if peer.State.SessionState == gobgpapi.PeerState_SESSION_STATE_ESTABLISHED {
768+
value = 1
769+
}
770+
metrics.ControllerBGPPeerInfo.WithLabelValues(addr, asn, state).Set(value)
771+
})
772+
if err != nil {
773+
klog.Errorf("error updating BGP peer metrics: %v", err)
774+
}
775+
}
776+
732777
func (nrc *NetworkRoutingController) isPeerEstablished(peerIP string) (bool, error) {
733778
var peerConnected bool
734779
peerFunc := func(peer *gobgpapi.Peer) {
@@ -1227,7 +1272,7 @@ func NewNetworkRoutingController(clientset kubernetes.Interface,
12271272
metrics.DefaultRegisterer.MustRegister(metrics.ControllerBGPadvertisementsReceived)
12281273
metrics.DefaultRegisterer.MustRegister(metrics.ControllerBGPadvertisementsSent)
12291274
metrics.DefaultRegisterer.MustRegister(metrics.ControllerBGPInternalPeersSyncTime)
1230-
metrics.DefaultRegisterer.MustRegister(metrics.ControllerBPGpeers)
1275+
metrics.DefaultRegisterer.MustRegister(metrics.ControllerBGPPeerInfo)
12311276
metrics.DefaultRegisterer.MustRegister(metrics.ControllerRoutesSyncTime)
12321277
nrc.MetricsEnabled = true
12331278
}

pkg/metrics/metrics_controller.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,12 @@ var (
158158
Name: "controller_routes_sync_time",
159159
Help: "Time it took for controller to sync routes",
160160
})
161-
// ControllerBPGpeers BGP peers in the runtime configuration
162-
ControllerBPGpeers = prometheus.NewGauge(prometheus.GaugeOpts{
161+
// ControllerBGPPeerInfo BGP peer information
162+
ControllerBGPPeerInfo = prometheus.NewGaugeVec(prometheus.GaugeOpts{
163163
Namespace: namespace,
164-
Name: "controller_bgp_peers",
165-
Help: "BGP peers in the runtime configuration",
166-
})
164+
Name: "bgp_peer_info",
165+
Help: "BGP peer information",
166+
}, []string{"peer", "asn", "state"})
167167
// ControllerBGPInternalPeersSyncTime Time it took to sync internal bgp peers
168168
ControllerBGPInternalPeersSyncTime = prometheus.NewHistogram(prometheus.HistogramOpts{
169169
Namespace: namespace,

0 commit comments

Comments
 (0)