ingress-nginx-helm/internal/ingress/controller/checker.go

98 lines
2.4 KiB
Go
Raw Normal View History

2017-11-05 01:18:28 +00:00
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"fmt"
"net/http"
"strconv"
"strings"
2018-10-15 19:29:32 +00:00
"time"
2017-11-05 01:18:28 +00:00
"github.com/ncabatoff/process-exporter/proc"
2017-11-06 22:34:30 +00:00
"github.com/pkg/errors"
2017-11-05 01:18:28 +00:00
)
2018-06-11 02:30:37 +00:00
const nginxPID = "/tmp/nginx.pid"
2017-11-05 01:18:28 +00:00
// Name returns the healthcheck name
func (n NGINXController) Name() string {
2017-11-06 22:34:30 +00:00
return "nginx-ingress-controller"
2017-11-05 01:18:28 +00:00
}
// Check returns if the nginx healthz endpoint is returning ok (status code 200)
2017-11-06 22:34:30 +00:00
func (n *NGINXController) Check(_ *http.Request) error {
2018-10-15 19:29:32 +00:00
url := fmt.Sprintf("http://127.0.0.1:%v%v", n.cfg.ListenPorts.Status, ngxHealthPath)
2018-10-24 20:02:28 +00:00
timeout := n.cfg.HealthCheckTimeout
statusCode, err := simpleGet(url, timeout)
2017-11-05 01:18:28 +00:00
if err != nil {
return err
}
2018-10-15 19:29:32 +00:00
if statusCode != 200 {
2017-11-05 01:18:28 +00:00
return fmt.Errorf("ingress controller is not healthy")
}
2018-10-15 19:29:32 +00:00
url = fmt.Sprintf("http://127.0.0.1:%v/is-dynamic-lb-initialized", n.cfg.ListenPorts.Status)
2018-10-24 20:02:28 +00:00
statusCode, err = simpleGet(url, timeout)
2018-10-09 22:36:10 +00:00
if err != nil {
return err
}
2018-10-15 19:29:32 +00:00
if statusCode != 200 {
2018-10-09 22:36:10 +00:00
return fmt.Errorf("dynamic load balancer not started")
}
2017-11-05 01:18:28 +00:00
// check the nginx master process is running
2018-12-05 16:27:55 +00:00
fs, err := proc.NewFS("/proc", false)
2017-11-05 01:18:28 +00:00
if err != nil {
2017-11-06 22:34:30 +00:00
return errors.Wrap(err, "unexpected error reading /proc directory")
2017-11-05 01:18:28 +00:00
}
2018-06-11 02:30:37 +00:00
f, err := n.fileSystem.ReadFile(nginxPID)
2017-11-05 01:18:28 +00:00
if err != nil {
2018-06-11 02:30:37 +00:00
return errors.Wrapf(err, "unexpected error reading %v", nginxPID)
2017-11-05 01:18:28 +00:00
}
pid, err := strconv.Atoi(strings.TrimRight(string(f), "\r\n"))
if err != nil {
2018-06-11 02:30:37 +00:00
return errors.Wrapf(err, "unexpected error reading the nginx PID from %v", nginxPID)
2017-11-05 01:18:28 +00:00
}
_, err = fs.NewProc(pid)
2017-11-06 22:34:30 +00:00
return err
2017-11-05 01:18:28 +00:00
}
2018-10-15 19:29:32 +00:00
2018-10-24 20:02:28 +00:00
func simpleGet(url string, timeout time.Duration) (int, error) {
2018-10-15 19:29:32 +00:00
client := &http.Client{
2018-10-24 20:02:28 +00:00
Timeout: timeout * time.Second,
2018-10-15 19:29:32 +00:00
Transport: &http.Transport{DisableKeepAlives: true},
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return -1, err
}
res, err := client.Do(req)
if err != nil {
return -1, err
}
defer res.Body.Close()
return res.StatusCode, nil
}