Skip to content

Commit

Permalink
Add IP Num Alert
Browse files Browse the repository at this point in the history
  • Loading branch information
hongzhen-ma committed Dec 7, 2020
1 parent b073398 commit ecbd01a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ func (c *Controller) Run(stopCh <-chan struct{}) {
klog.Fatalf("gc failed %v", err)
}

c.registerSubnetMetrics()

// start workers to do all the network operations
c.startWorkers(stopCh)
<-stopCh
Expand Down Expand Up @@ -439,4 +441,6 @@ func (c *Controller) startWorkers(stopCh <-chan struct{}) {
klog.Errorf("gc lsp error %v", err)
}
}, 30*time.Second, stopCh)

go wait.Until(c.resyncSubnetMetrics, 30*time.Second, stopCh)
}
47 changes: 47 additions & 0 deletions pkg/controller/exporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package controller

import (
"sync"

kubeovnv1 "github.com/alauda/kube-ovn/pkg/apis/kubeovn/v1"

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

var registerMetricsOnce sync.Once

// registerSubnetMetrics register subnet metrics
func (c *Controller) registerSubnetMetrics() {
registerMetricsOnce.Do(func() {
registerMetrics()
})
}

// resyncSubnetMetrics start to update subnet metrics
func (c *Controller) resyncSubnetMetrics() {
for c.exportSubnetMetrics() {
}
}

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

return true
}

func (c *Controller) exportSubnetAvailableIPsGauge(subnet *kubeovnv1.Subnet) {
metricSubnetAvailableIPs.WithLabelValues(subnet.Name, subnet.Spec.Protocol, subnet.Spec.CIDRBlock).Set(subnet.Status.AvailableIPs)
}

func (c *Controller) exportSubnetUsedIPsGauge(subnet *kubeovnv1.Subnet) {
metricSubnetUsedIPs.WithLabelValues(subnet.Name, subnet.Spec.Protocol, subnet.Spec.CIDRBlock).Set(subnet.Status.UsingIPs)
}
32 changes: 32 additions & 0 deletions pkg/controller/net_metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package controller

import "github.com/prometheus/client_golang/prometheus"

var (
metricSubnetAvailableIPs = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "subnet_available_ip_count",
Help: "The available num of ip address in subnet.",
},
[]string{
"subnet_name",
"protocol",
"subnet_cidr",
})

metricSubnetUsedIPs = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "subnet_used_ip_count",
Help: "The used num of ip address in subnet.",
},
[]string{
"subnet_name",
"protocol",
"subnet_cidr",
})
)

func registerMetrics() {
prometheus.MustRegister(metricSubnetAvailableIPs)
prometheus.MustRegister(metricSubnetUsedIPs)
}

0 comments on commit ecbd01a

Please sign in to comment.