Refactor metric prometheus leader helper
This commit is contained in:
parent
7c6ffeaeac
commit
ccd88f625c
3 changed files with 28 additions and 21 deletions
|
@ -124,10 +124,7 @@ func (n *NGINXController) syncIngress(interface{}) error {
|
|||
ings := n.store.ListIngresses(nil)
|
||||
hosts, servers, pcfg := n.getConfiguration(ings)
|
||||
|
||||
if n.isLeader() {
|
||||
klog.V(2).Infof("Updating ssl expiration metrics.")
|
||||
n.metricCollector.SetSSLExpireTime(servers)
|
||||
}
|
||||
n.metricCollector.SetSSLExpireTime(servers)
|
||||
|
||||
if n.runningConfig.Equal(pcfg) {
|
||||
klog.V(3).Infof("No configuration change detected, skipping backend reload.")
|
||||
|
|
|
@ -31,7 +31,6 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"text/template"
|
||||
"time"
|
||||
|
@ -268,8 +267,6 @@ type NGINXController struct {
|
|||
|
||||
metricCollector metric.Collector
|
||||
|
||||
currentLeader uint32
|
||||
|
||||
validationWebhookServer *http.Server
|
||||
|
||||
command NginxExecTester
|
||||
|
@ -296,14 +293,12 @@ func (n *NGINXController) Start() {
|
|||
go n.syncStatus.Run(stopCh)
|
||||
}
|
||||
|
||||
n.setLeader(true)
|
||||
n.metricCollector.OnStartedLeading(electionID)
|
||||
// manually update SSL expiration metrics
|
||||
// (to not wait for a reload)
|
||||
n.metricCollector.SetSSLExpireTime(n.runningConfig.Servers)
|
||||
},
|
||||
OnStoppedLeading: func() {
|
||||
n.setLeader(false)
|
||||
n.metricCollector.OnStoppedLeading(electionID)
|
||||
},
|
||||
PodName: n.podInfo.Name,
|
||||
|
@ -1192,15 +1187,3 @@ func buildRedirects(servers []*ingress.Server) []*redirect {
|
|||
|
||||
return redirectServers
|
||||
}
|
||||
|
||||
func (n *NGINXController) setLeader(leader bool) {
|
||||
var i uint32
|
||||
if leader {
|
||||
i = 1
|
||||
}
|
||||
atomic.StoreUint32(&n.currentLeader, i)
|
||||
}
|
||||
|
||||
func (n *NGINXController) isLeader() bool {
|
||||
return atomic.LoadUint32(&n.currentLeader) != 0
|
||||
}
|
||||
|
|
|
@ -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(¤tLeader, i)
|
||||
}
|
||||
|
||||
func isLeader() bool {
|
||||
return atomic.LoadUint32(¤tLeader) != 0
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue