-
Notifications
You must be signed in to change notification settings - Fork 429
/
exporter.go
128 lines (109 loc) · 4.33 KB
/
exporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
// registerSubnetMetrics register subnet metrics
func (c *Controller) registerSubnetMetrics() {
registerMetricsOnce.Do(func() {
registerMetrics()
})
}
func resetSubnetMetrics() {
metricSubnetAvailableIPs.Reset()
metricSubnetUsedIPs.Reset()
metricCentralSubnetInfo.Reset()
metricSubnetIPAMInfo.Reset()
metricSubnetIPAssignedInfo.Reset()
}
func (c *Controller) exportSubnetMetrics() {
subnets, err := c.subnetsLister.List(labels.Everything())
if err != nil {
klog.Errorf("failed to list subnet, %v", err)
return
}
resetSubnetMetrics()
for _, subnet := range subnets {
c.exportSubnetAvailableIPsGauge(subnet)
c.exportSubnetUsedIPsGauge(subnet)
c.exportSubnetIPAMInfo(subnet)
c.exportSubnetIPAssignedInfo(subnet)
if subnet.Spec.GatewayType == kubeovnv1.GWCentralizedType {
c.exportCentralizedSubnetInfo(subnet)
}
}
}
func (c *Controller) exportSubnetAvailableIPsGauge(subnet *kubeovnv1.Subnet) {
var availableIPs float64
switch subnet.Spec.Protocol {
case kubeovnv1.ProtocolIPv4:
availableIPs = subnet.Status.V4AvailableIPs
case kubeovnv1.ProtocolIPv6:
availableIPs = subnet.Status.V6AvailableIPs
default:
availableIPs = math.Min(subnet.Status.V4AvailableIPs, subnet.Status.V6AvailableIPs)
}
metricSubnetAvailableIPs.WithLabelValues(subnet.Name, subnet.Spec.Protocol, subnet.Spec.CIDRBlock).Set(availableIPs)
}
func (c *Controller) exportSubnetUsedIPsGauge(subnet *kubeovnv1.Subnet) {
var usingIPs float64
if subnet.Spec.Protocol == kubeovnv1.ProtocolIPv6 {
usingIPs = subnet.Status.V6UsingIPs
} else {
usingIPs = subnet.Status.V4UsingIPs
}
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)
}
}
}