Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add metric for subnet info #3932

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ func (c *Controller) startWorkers(ctx context.Context) {
}

go wait.Until(c.resyncProviderNetworkStatus, 30*time.Second, ctx.Done())
go wait.Until(c.resyncSubnetMetrics, 30*time.Second, ctx.Done())
go wait.Until(c.exportSubnetMetrics, 30*time.Second, ctx.Done())
go wait.Until(c.CheckGatewayReady, 5*time.Second, ctx.Done())

go wait.Until(c.runAddOvnEipWorker, time.Second, ctx.Done())
Expand Down
80 changes: 73 additions & 7 deletions pkg/controller/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package controller

import (
"math"
"strconv"
"strings"
"sync"

"k8s.io/apimachinery/pkg/labels"
"k8s.io/klog/v2"

kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
"github.com/kubeovn/kube-ovn/pkg/ovsdb/ovnnb"
)

var registerMetricsOnce sync.Once
Expand All @@ -19,23 +22,32 @@ func (c *Controller) registerSubnetMetrics() {
})
}

// resyncSubnetMetrics start to update subnet metrics
func (c *Controller) resyncSubnetMetrics() {
c.exportSubnetMetrics()
func resetSubnetMetrics() {
metricSubnetAvailableIPs.Reset()
metricSubnetUsedIPs.Reset()
metricCentralSubnetInfo.Reset()
metricSubnetIPAMInfo.Reset()
metricSubnetIPAssignedInfo.Reset()
}

func (c *Controller) exportSubnetMetrics() bool {
func (c *Controller) exportSubnetMetrics() {
subnets, err := c.subnetsLister.List(labels.Everything())
if err != nil {
klog.Errorf("failed to list subnet, %v", err)
return false
return
}

resetSubnetMetrics()
for _, subnet := range subnets {
c.exportSubnetAvailableIPsGauge(subnet)
c.exportSubnetUsedIPsGauge(subnet)
}
c.exportSubnetIPAMInfo(subnet)
c.exportSubnetIPAssignedInfo(subnet)

return true
if subnet.Spec.GatewayType == kubeovnv1.GWCentralizedType {
c.exportCentralizedSubnetInfo(subnet)
}
}
}

func (c *Controller) exportSubnetAvailableIPsGauge(subnet *kubeovnv1.Subnet) {
Expand All @@ -60,3 +72,57 @@ func (c *Controller) exportSubnetUsedIPsGauge(subnet *kubeovnv1.Subnet) {
}
metricSubnetUsedIPs.WithLabelValues(subnet.Name, subnet.Spec.Protocol, subnet.Spec.CIDRBlock).Set(usingIPs)
}

func (c *Controller) exportCentralizedSubnetInfo(subnet *kubeovnv1.Subnet) {
lrPolicyList, err := c.OVNNbClient.GetLogicalRouterPoliciesByExtID(c.config.ClusterRouter, "subnet", subnet.Name)
if err != nil {
klog.Errorf("failed to list lr policy for subnet %s: %v", subnet.Name, err)
return
}

for _, lrPolicy := range lrPolicyList {
if lrPolicy.Action == ovnnb.LogicalRouterPolicyActionReroute {
metricCentralSubnetInfo.WithLabelValues(subnet.Name, strconv.FormatBool(subnet.Spec.EnableEcmp), subnet.Spec.GatewayNode, subnet.Status.ActivateGateway, lrPolicy.Match, strings.Join(lrPolicy.Nexthops, ",")).Set(1)
break
}
}
}

func (c *Controller) exportSubnetIPAMInfo(subnet *kubeovnv1.Subnet) {
ipamSubnet, ok := c.ipam.Subnets[subnet.Name]
if !ok {
klog.Errorf("failed to get subnet %s in ipam", subnet.Name)
return
}

switch subnet.Spec.Protocol {
case kubeovnv1.ProtocolIPv4:
metricSubnetIPAMInfo.WithLabelValues(subnet.Name, subnet.Spec.CIDRBlock, ipamSubnet.V4Free.String(), ipamSubnet.V4Reserved.String(), ipamSubnet.V4Available.String(), ipamSubnet.V4Using.String()).Set(1)

case kubeovnv1.ProtocolIPv6:
metricSubnetIPAMInfo.WithLabelValues(subnet.Name, subnet.Spec.CIDRBlock, ipamSubnet.V6Free.String(), ipamSubnet.V6Reserved.String(), ipamSubnet.V6Available.String(), ipamSubnet.V6Using.String()).Set(1)

case kubeovnv1.ProtocolDual:
metricSubnetIPAMInfo.WithLabelValues(subnet.Name, subnet.Spec.CIDRBlock, ipamSubnet.V4Free.String(), ipamSubnet.V4Reserved.String(), ipamSubnet.V4Available.String(), ipamSubnet.V4Using.String()).Set(1)
metricSubnetIPAMInfo.WithLabelValues(subnet.Name, subnet.Spec.CIDRBlock, ipamSubnet.V6Free.String(), ipamSubnet.V6Reserved.String(), ipamSubnet.V6Available.String(), ipamSubnet.V6Using.String()).Set(1)
}
}

func (c *Controller) exportSubnetIPAssignedInfo(subnet *kubeovnv1.Subnet) {
ipamSubnet, ok := c.ipam.Subnets[subnet.Name]
if !ok {
klog.Errorf("failed to get subnet %s in ipam", subnet.Name)
return
}

if subnet.Spec.Protocol == kubeovnv1.ProtocolIPv4 || subnet.Spec.Protocol == kubeovnv1.ProtocolDual {
for ip, pod := range ipamSubnet.V4IPToPod {
metricSubnetIPAssignedInfo.WithLabelValues(subnet.Name, ip, pod).Set(1)
}
}
if subnet.Spec.Protocol == kubeovnv1.ProtocolIPv6 || subnet.Spec.Protocol == kubeovnv1.ProtocolDual {
for ip, pod := range ipamSubnet.V6IPToPod {
metricSubnetIPAssignedInfo.WithLabelValues(subnet.Name, ip, pod).Set(1)
}
}
}
42 changes: 42 additions & 0 deletions pkg/controller/net_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,51 @@ var (
"protocol",
"subnet_cidr",
})

metricCentralSubnetInfo = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "centralized_subnet_info",
Help: "Provide information for centralized subnet.",
},
[]string{
"subnet_name",
"enable_ecmp",
"gateway_node",
"active_gateway",
"match",
"nexthops",
})

metricSubnetIPAMInfo = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "subnet_ipam_info",
Help: "Provide information for subnet ip address management.",
},
[]string{
"subnet_name",
"cidr",
"free",
"reserved",
"available",
"using",
})

metricSubnetIPAssignedInfo = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "subnet_ip_assign_info",
Help: "Provide information for subnet ip address assigned info.",
},
[]string{
"subnet_name",
"ip",
"pod_name",
})
)

func registerMetrics() {
prometheus.MustRegister(metricSubnetAvailableIPs)
prometheus.MustRegister(metricSubnetUsedIPs)
prometheus.MustRegister(metricCentralSubnetInfo)
prometheus.MustRegister(metricSubnetIPAMInfo)
prometheus.MustRegister(metricSubnetIPAssignedInfo)
}
Loading