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)
|
ings := n.store.ListIngresses(nil)
|
||||||
hosts, servers, pcfg := n.getConfiguration(ings)
|
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) {
|
if n.runningConfig.Equal(pcfg) {
|
||||||
klog.V(3).Infof("No configuration change detected, skipping backend reload.")
|
klog.V(3).Infof("No configuration change detected, skipping backend reload.")
|
||||||
|
|
|
@ -31,7 +31,6 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
@ -268,8 +267,6 @@ type NGINXController struct {
|
||||||
|
|
||||||
metricCollector metric.Collector
|
metricCollector metric.Collector
|
||||||
|
|
||||||
currentLeader uint32
|
|
||||||
|
|
||||||
validationWebhookServer *http.Server
|
validationWebhookServer *http.Server
|
||||||
|
|
||||||
command NginxExecTester
|
command NginxExecTester
|
||||||
|
@ -296,14 +293,12 @@ func (n *NGINXController) Start() {
|
||||||
go n.syncStatus.Run(stopCh)
|
go n.syncStatus.Run(stopCh)
|
||||||
}
|
}
|
||||||
|
|
||||||
n.setLeader(true)
|
|
||||||
n.metricCollector.OnStartedLeading(electionID)
|
n.metricCollector.OnStartedLeading(electionID)
|
||||||
// manually update SSL expiration metrics
|
// manually update SSL expiration metrics
|
||||||
// (to not wait for a reload)
|
// (to not wait for a reload)
|
||||||
n.metricCollector.SetSSLExpireTime(n.runningConfig.Servers)
|
n.metricCollector.SetSSLExpireTime(n.runningConfig.Servers)
|
||||||
},
|
},
|
||||||
OnStoppedLeading: func() {
|
OnStoppedLeading: func() {
|
||||||
n.setLeader(false)
|
|
||||||
n.metricCollector.OnStoppedLeading(electionID)
|
n.metricCollector.OnStoppedLeading(electionID)
|
||||||
},
|
},
|
||||||
PodName: n.podInfo.Name,
|
PodName: n.podInfo.Name,
|
||||||
|
@ -1192,15 +1187,3 @@ func buildRedirects(servers []*ingress.Server) []*redirect {
|
||||||
|
|
||||||
return redirectServers
|
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 (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"k8s.io/klog"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
"k8s.io/ingress-nginx/internal/ingress"
|
"k8s.io/ingress-nginx/internal/ingress"
|
||||||
"k8s.io/ingress-nginx/internal/ingress/annotations/class"
|
"k8s.io/ingress-nginx/internal/ingress/annotations/class"
|
||||||
|
@ -153,6 +156,11 @@ func (c *collector) Stop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *collector) SetSSLExpireTime(servers []*ingress.Server) {
|
func (c *collector) SetSSLExpireTime(servers []*ingress.Server) {
|
||||||
|
if !isLeader() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
klog.V(2).Infof("Updating ssl expiration metrics.")
|
||||||
c.ingressController.SetSSLExpireTime(servers)
|
c.ingressController.SetSSLExpireTime(servers)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,11 +170,30 @@ func (c *collector) SetHosts(hosts sets.String) {
|
||||||
|
|
||||||
// OnStartedLeading indicates the pod was elected as the leader
|
// OnStartedLeading indicates the pod was elected as the leader
|
||||||
func (c *collector) OnStartedLeading(electionID string) {
|
func (c *collector) OnStartedLeading(electionID string) {
|
||||||
|
setLeader(true)
|
||||||
c.ingressController.OnStartedLeading(electionID)
|
c.ingressController.OnStartedLeading(electionID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// OnStoppedLeading indicates the pod stopped being the leader
|
// OnStoppedLeading indicates the pod stopped being the leader
|
||||||
func (c *collector) OnStoppedLeading(electionID string) {
|
func (c *collector) OnStoppedLeading(electionID string) {
|
||||||
|
setLeader(false)
|
||||||
c.ingressController.OnStoppedLeading(electionID)
|
c.ingressController.OnStoppedLeading(electionID)
|
||||||
c.ingressController.RemoveAllSSLExpireMetrics(c.registry)
|
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