Skip to content

Commit

Permalink
Refactor metric prometheus leader helper
Browse files Browse the repository at this point in the history
  • Loading branch information
aledbf committed Jun 29, 2019
1 parent 7c6ffea commit ccd88f6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 21 deletions.
5 changes: 1 addition & 4 deletions internal/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down
17 changes: 0 additions & 17 deletions internal/ingress/controller/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"syscall"
"text/template"
"time"
Expand Down Expand Up @@ -268,8 +267,6 @@ type NGINXController struct {

metricCollector metric.Collector

currentLeader uint32

validationWebhookServer *http.Server

command NginxExecTester
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
27 changes: 27 additions & 0 deletions internal/ingress/metric/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand All @@ -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
}

0 comments on commit ccd88f6

Please sign in to comment.