Refactor metric prometheus leader helper

This commit is contained in:
Manuel Alejandro de Brito Fontes 2019-06-28 18:29:58 -04:00
parent 7c6ffeaeac
commit ccd88f625c
No known key found for this signature in database
GPG key ID: 786136016A8BA02A
3 changed files with 28 additions and 21 deletions

View file

@ -18,9 +18,12 @@ package metric
import (
"os"
"sync/atomic"
"time"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/klog"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/ingress-nginx/internal/ingress"
"k8s.io/ingress-nginx/internal/ingress/annotations/class"
@ -153,6 +156,11 @@ func (c *collector) Stop() {
}
func (c *collector) SetSSLExpireTime(servers []*ingress.Server) {
if !isLeader() {
return
}
klog.V(2).Infof("Updating ssl expiration metrics.")
c.ingressController.SetSSLExpireTime(servers)
}
@ -162,11 +170,30 @@ func (c *collector) SetHosts(hosts sets.String) {
// OnStartedLeading indicates the pod was elected as the leader
func (c *collector) OnStartedLeading(electionID string) {
setLeader(true)
c.ingressController.OnStartedLeading(electionID)
}
// OnStoppedLeading indicates the pod stopped being the leader
func (c *collector) OnStoppedLeading(electionID string) {
setLeader(false)
c.ingressController.OnStoppedLeading(electionID)
c.ingressController.RemoveAllSSLExpireMetrics(c.registry)
}
var (
currentLeader uint32
)
func setLeader(leader bool) {
var i uint32
if leader {
i = 1
}
atomic.StoreUint32(&currentLeader, i)
}
func isLeader() bool {
return atomic.LoadUint32(&currentLeader) != 0
}