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

62 lines
1.7 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"
"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
)
// 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 {
2017-11-05 01:18:28 +00:00
res, err := http.Get(fmt.Sprintf("http://0.0.0.0:%v%v", n.cfg.ListenPorts.Status, ngxHealthPath))
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return fmt.Errorf("ingress controller is not healthy")
}
// check the nginx master process is running
fs, err := proc.NewFS("/proc")
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
}
2017-11-06 22:34:30 +00:00
f, err := n.fileSystem.ReadFile("/run/nginx.pid")
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 /run/nginx.pid")
2017-11-05 01:18:28 +00:00
}
pid, err := strconv.Atoi(strings.TrimRight(string(f), "\r\n"))
if err != nil {
2017-11-06 22:34:30 +00:00
return errors.Wrap(err, "unexpected error reading the PID from /run/nginx.pid")
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
}