Skip to content

Commit

Permalink
Fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
aledbf committed Mar 12, 2017
1 parent 7ba389c commit e702c55
Show file tree
Hide file tree
Showing 10 changed files with 441 additions and 300 deletions.
2 changes: 1 addition & 1 deletion controllers/nginx/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ build: clean
-ldflags "-s -w -X ${PKG}/pkg/version.RELEASE=${RELEASE} -X ${PKG}/pkg/version.COMMIT=${COMMIT} -X ${PKG}/pkg/version.REPO=${REPO_INFO}" \
-o rootfs/nginx-ingress-controller ${PKG}/pkg/cmd/controller

container: build
container:
$(DOCKER) build --pull -t $(PREFIX):$(RELEASE) rootfs

push: container
Expand Down
58 changes: 50 additions & 8 deletions controllers/nginx/pkg/cmd/controller/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,73 @@ import (
"k8s.io/ingress/controllers/nginx/pkg/metric/collector"
)

const (
ngxStatusPath = "/internal_nginx_status"
ngxVtsPath = "/nginx_status/format/json"
)

func (n *NGINXController) setupMonitor(sm statusModule) {
csm := n.statusModule
if csm != sm {
prometheus
glog.Infof("changing prometheus collector from %v to %v", csm, sm)
n.stats.stop(csm)
n.stats.start(sm)
n.statusModule = sm
}
}

type statsCollector struct {
process prometheus.Collector
basic prometheus.Collector
vts prometheus.Collector
basic collector.Stopable
vts collector.Stopable

namespace string
watchClass string
}

func (s *statsCollector) stop(sm statusModule) {
switch sm {
case defaultStatusModule:
s.basic.Stop()
prometheus.Unregister(s.basic)
break
case vtsStatusModule:
s.vts.Stop()
prometheus.Unregister(s.vts)
break
}
}

func newStatsCollector() (*statsCollector, error) {
pc, err := collector.NewNamedProcess(true, collector.BinaryNameMatcher{"nginx", n.cmdArgs})
func (s *statsCollector) start(sm statusModule) {
switch sm {
case defaultStatusModule:
s.basic = collector.NewNginxStatus(s.namespace, s.watchClass, ngxHealthPort, ngxStatusPath)
prometheus.Register(s.basic)
break
case vtsStatusModule:
s.vts = collector.NewNGINXVTSCollector(s.namespace, s.watchClass, ngxHealthPort, ngxVtsPath)
prometheus.Register(s.vts)
break
}
}

func newStatsCollector(ns, class, binary string) *statsCollector {
glog.Infof("starting new nginx stats collector for Ingress controller running in namespace %v (class %v)", ns, class)
pc, err := collector.NewNamedProcess(true, collector.BinaryNameMatcher{
Name: "nginx",
Binary: binary,
})
if err != nil {
return nil, err
glog.Fatalf("unexpected error registering nginx collector: %v", err)
}
err = prometheus.Register(pc)
if err != nil {
glog.Fatalf("unexpected error registering nginx collector: %v", err)
}

return nil, &statsCollector{
process: pc,
return &statsCollector{
namespace: ns,
watchClass: class,
process: pc,
}
}
45 changes: 25 additions & 20 deletions controllers/nginx/pkg/cmd/controller/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ type statusModule string
const (
ngxHealthPort = 18080
ngxHealthPath = "/healthz"
ngxStatusPath = "/internal_nginx_status"
ngxVtsPath = "/nginx_status/format/json"

defaultStatusModule statusModule = "default"
vtsStatusModule statusModule = "vts"
Expand All @@ -70,7 +68,7 @@ func newNGINXController() ingress.Controller {
if ngx == "" {
ngx = binary
}
n := NGINXController{
n := &NGINXController{
binary: ngx,
configmap: &api.ConfigMap{},
}
Expand Down Expand Up @@ -102,7 +100,7 @@ Error loading new template : %v

go n.Start()

return ingress.Controller(&n)
return ingress.Controller(n)
}

// NGINXController ...
Expand All @@ -117,11 +115,15 @@ type NGINXController struct {

cmdArgs []string

watchClass string
namespace string

stats *statsCollector
statusModule statusModule
}

// Start start a new NGINX master process running in foreground.
func (n NGINXController) Start() {
func (n *NGINXController) Start() {
glog.Info("starting NGINX process...")

done := make(chan error, 1)
Expand Down Expand Up @@ -170,15 +172,6 @@ func (n *NGINXController) start(cmd *exec.Cmd, done chan error) {

n.cmdArgs = cmd.Args

cfg := ngx_template.ReadConfig(n.configmap.Data)
n.statusModule = defaultStatusModule
if cfg.EnableVtsStatus {
n.statusModule = vtsStatusModule
n.setupMonitor(vtsStatusModule)
} else {
n.setupMonitor(defaultStatusModule)
}

go func() {
done <- cmd.Wait()
}()
Expand Down Expand Up @@ -264,12 +257,20 @@ func (n NGINXController) Info() *ingress.BackendInfo {
}

// OverrideFlags customize NGINX controller flags
func (n NGINXController) OverrideFlags(flags *pflag.FlagSet) {
ig, err := flags.GetString("ingress-class")
if err == nil && ig != "" && ig != defIngressClass {
glog.Warningf("only Ingress with class %v will be processed by this ingress controller", ig)
func (n *NGINXController) OverrideFlags(flags *pflag.FlagSet) {
ic, _ := flags.GetString("ingress-class")
wc, _ := flags.GetString("watch-namespace")

if ic == "" {
ic = defIngressClass
}
flags.Set("ingress-class", defIngressClass)

if ic != defIngressClass {
glog.Warningf("only Ingress with class %v will be processed by this ingress controller", ic)
}

flags.Set("ingress-class", ic)
n.stats = newStatsCollector(ic, wc, n.binary)
}

// DefaultIngressClass just return the default ingress class
Expand Down Expand Up @@ -336,7 +337,11 @@ func (n *NGINXController) OnUpdate(ingressCfg ingress.Configuration) ([]byte, er
cfg := ngx_template.ReadConfig(n.configmap.Data)

// we need to check if the status module configuration changed
n.setupMonitor()
if cfg.EnableVtsStatus {
n.setupMonitor(vtsStatusModule)
} else {
n.setupMonitor(defaultStatusModule)
}

// NGINX cannot resize the has tables used to store server names.
// For this reason we check if the defined size defined is correct
Expand Down
144 changes: 87 additions & 57 deletions controllers/nginx/pkg/metric/collector/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,72 +17,103 @@ limitations under the License.
package collector

import (
"fmt"

"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
)

var (
activeDesc = prometheus.NewDesc(
"nginx_active_connections",
"total number of active connections",
nil, nil)

acceptedDesc = prometheus.NewDesc(
"nginx_accepted_connections",
"total number of accepted client connections",
nil, nil)

handledDesc = prometheus.NewDesc(
"nginx_handled_connections",
"total number of handled connections",
nil, nil)

requestsDesc = prometheus.NewDesc(
"nginx_total_requests",
"total number of client requests",
nil, nil)

readingDesc = prometheus.NewDesc(
"nginx_current_reading_connections",
"current number of connections where nginx is reading the request header",
nil, nil)

writingDesc = prometheus.NewDesc(
"nginx_current_writing_connections",
"current number of connections where nginx is writing the response back to the client",
nil, nil)

waitingDesc = prometheus.NewDesc(
"nginx_current_waiting_connections",
"current number of idle client connections waiting for a request",
nil, nil)
)

type (
nginxStatusCollector struct {
scrapeChan chan scrapeRequest
scrapeChan chan scrapeRequest
ngxHealthPort int
ngxVtsPath string
data *nginxStatusData
}

nginxStatusData struct {
active *prometheus.Desc
accepted *prometheus.Desc
handled *prometheus.Desc
requests *prometheus.Desc
reading *prometheus.Desc
writing *prometheus.Desc
waiting *prometheus.Desc
}
)

func NewNginxStatus() (prometheus.Collector, error) {
func buildNS(namespace, class string) string {
if namespace == "" {
namespace = "all"
}
if class == "" {
class = "all"
}

return fmt.Sprintf("%v_%v", namespace, class)
}

// NewNginxStatus returns a new prometheus collector the default nginx status module
func NewNginxStatus(namespace, class string, ngxHealthPort int, ngxVtsPath string) Stopable {
p := nginxStatusCollector{
scrapeChan: make(chan scrapeRequest),
scrapeChan: make(chan scrapeRequest),
ngxHealthPort: ngxHealthPort,
ngxVtsPath: ngxVtsPath,
}

ns := buildNS(namespace, class)

p.data = &nginxStatusData{
active: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "active_connections"),
"total number of active connections",
nil, nil),

accepted: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "accepted_connections"),
"total number of accepted client connections",
nil, nil),

handled: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "handled_connections"),
"total number of handled connections",
nil, nil),

requests: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "total_requests"),
"total number of client requests",
nil, nil),

reading: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "current_reading_connections"),
"current number of connections where nginx is reading the request header",
nil, nil),

writing: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "current_writing_connections"),
"current number of connections where nginx is writing the response back to the client",
nil, nil),

waiting: prometheus.NewDesc(
prometheus.BuildFQName(system, ns, "current_waiting_connections"),
"current number of idle client connections waiting for a request",
nil, nil),
}

go p.start()

return p, nil
return p
}

// Describe implements prometheus.Collector.
func (p nginxStatusCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- activeDesc
ch <- acceptedDesc
ch <- handledDesc
ch <- requestsDesc
ch <- readingDesc
ch <- writingDesc
ch <- waitingDesc
ch <- p.data.active
ch <- p.data.accepted
ch <- p.data.handled
ch <- p.data.requests
ch <- p.data.reading
ch <- p.data.writing
ch <- p.data.waiting
}

// Collect implements prometheus.Collector.
Expand All @@ -106,25 +137,24 @@ func (p nginxStatusCollector) Stop() {

// nginxStatusCollector scrap the nginx status
func (p nginxStatusCollector) scrape(ch chan<- prometheus.Metric) {
s, err := getNginxStatus()
s, err := getNginxStatus(p.ngxHealthPort, p.ngxVtsPath)
if err != nil {
glog.Warningf("unexpected error obtaining nginx status info: %v", err)
return
}

ch <- prometheus.MustNewConstMetric(activeDesc,
ch <- prometheus.MustNewConstMetric(p.data.active,
prometheus.GaugeValue, float64(s.Active))
ch <- prometheus.MustNewConstMetric(acceptedDesc,
ch <- prometheus.MustNewConstMetric(p.data.accepted,
prometheus.GaugeValue, float64(s.Accepted))
ch <- prometheus.MustNewConstMetric(handledDesc,
ch <- prometheus.MustNewConstMetric(p.data.handled,
prometheus.GaugeValue, float64(s.Handled))
ch <- prometheus.MustNewConstMetric(requestsDesc,
ch <- prometheus.MustNewConstMetric(p.data.requests,
prometheus.GaugeValue, float64(s.Requests))
ch <- prometheus.MustNewConstMetric(readingDesc,
ch <- prometheus.MustNewConstMetric(p.data.reading,
prometheus.GaugeValue, float64(s.Reading))
ch <- prometheus.MustNewConstMetric(writingDesc,
ch <- prometheus.MustNewConstMetric(p.data.writing,
prometheus.GaugeValue, float64(s.Writing))
ch <- prometheus.MustNewConstMetric(waitingDesc,
ch <- prometheus.MustNewConstMetric(p.data.waiting,
prometheus.GaugeValue, float64(s.Waiting))

}
Loading

0 comments on commit e702c55

Please sign in to comment.