@@ -25,7 +25,6 @@ import (
2525
2626 "github.com/ncabatoff/process-exporter/proc"
2727 "github.com/pkg/errors"
28- "k8s.io/klog"
2928
3029 "k8s.io/ingress-nginx/internal/nginx"
3130)
@@ -37,41 +36,48 @@ func (n NGINXController) Name() string {
3736
3837// Check returns if the nginx healthz endpoint is returning ok (status code 200)
3938func (n * NGINXController ) Check (_ * http.Request ) error {
40- statusCode , _ , err := nginx .NewGetStatusRequest (nginx .HealthPath )
41- if err != nil {
42- klog .Errorf ("healthcheck error: %v" , err )
43- return err
39+ if n .isShuttingDown {
40+ return fmt .Errorf ("the ingress controller is shutting down" )
4441 }
4542
46- if statusCode != 200 {
47- klog .Errorf ("healthcheck error: %v" , statusCode )
48- return fmt .Errorf ("ingress controller is not healthy" )
43+ // check the nginx master process is running
44+ fs , err := proc .NewFS ("/proc" , false )
45+ if err != nil {
46+ return errors .Wrap (err , "reading /proc directory" )
4947 }
5048
51- statusCode , _ , err = nginx . NewGetStatusRequest ( "/is-dynamic-lb-initialized" )
49+ f , err := ioutil . ReadFile ( nginx . PID )
5250 if err != nil {
53- klog .Errorf ("healthcheck error: %v" , err )
54- return err
51+ return errors .Wrapf (err , "reading %v" , nginx .PID )
5552 }
5653
57- if statusCode != 200 {
58- klog . Errorf ( "healthcheck error: %v" , statusCode )
59- return fmt . Errorf ( "dynamic load balancer not started" )
54+ pid , err := strconv . Atoi ( strings . TrimRight ( string ( f ), " \r \n " ))
55+ if err != nil {
56+ return errors . Wrapf ( err , "reading NGINX PID from file %v" , nginx . PID )
6057 }
6158
62- // check the nginx master process is running
63- fs , err := proc .NewFS ("/proc" , false )
59+ _ , err = fs .NewProc (pid )
6460 if err != nil {
65- return errors .Wrap (err , "unexpected error reading /proc directory" )
61+ return errors .Wrapf (err , "checking for NGINX process with PID %v" , pid )
6662 }
67- f , err := ioutil .ReadFile (nginx .PID )
63+
64+ statusCode , _ , err := nginx .NewGetStatusRequest (nginx .HealthPath )
6865 if err != nil {
69- return errors .Wrapf (err , "unexpected error reading %v" , nginx . PID )
66+ return errors .Wrapf (err , "checking if NGINX is running" )
7067 }
71- pid , err := strconv .Atoi (strings .TrimRight (string (f ), "\r \n " ))
68+
69+ if statusCode != 200 {
70+ return fmt .Errorf ("ingress controller is not healthy (%v)" , statusCode )
71+ }
72+
73+ statusCode , _ , err = nginx .NewGetStatusRequest ("/is-dynamic-lb-initialized" )
7274 if err != nil {
73- return errors .Wrapf (err , "unexpected error reading the nginx PID from %v" , nginx . PID )
75+ return errors .Wrapf (err , "checking if the dynamic load balancer started" )
7476 }
75- _ , err = fs .NewProc (pid )
76- return err
77+
78+ if statusCode != 200 {
79+ return fmt .Errorf ("dynamic load balancer not started" )
80+ }
81+
82+ return nil
7783}
0 commit comments