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"
|
2021-08-06 14:18:17 +00:00
|
|
|
"os"
|
2017-11-05 01:18:28 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ncabatoff/process-exporter/proc"
|
|
|
|
|
2019-01-21 14:29:36 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/nginx"
|
|
|
|
)
|
2018-06-11 02:30:37 +00:00
|
|
|
|
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 {
|
2019-09-01 18:21:24 +00:00
|
|
|
if n.isShuttingDown {
|
|
|
|
return fmt.Errorf("the ingress controller is shutting down")
|
2017-11-05 01:18:28 +00:00
|
|
|
}
|
2018-10-15 19:29:32 +00:00
|
|
|
|
2019-09-01 18:21:24 +00:00
|
|
|
// check the nginx master process is running
|
|
|
|
fs, err := proc.NewFS("/proc", false)
|
|
|
|
if err != nil {
|
2022-01-10 00:29:12 +00:00
|
|
|
return fmt.Errorf("reading /proc directory: %w", err)
|
2017-11-05 01:18:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 14:18:17 +00:00
|
|
|
f, err := os.ReadFile(nginx.PID)
|
2018-10-09 22:36:10 +00:00
|
|
|
if err != nil {
|
2022-01-10 00:29:12 +00:00
|
|
|
return fmt.Errorf("reading %v: %w", nginx.PID, err)
|
2018-10-09 22:36:10 +00:00
|
|
|
}
|
2018-10-15 19:29:32 +00:00
|
|
|
|
2019-09-01 18:21:24 +00:00
|
|
|
pid, err := strconv.Atoi(strings.TrimRight(string(f), "\r\n"))
|
|
|
|
if err != nil {
|
2022-01-10 00:29:12 +00:00
|
|
|
return fmt.Errorf("reading NGINX PID from file %v: %w", nginx.PID, err)
|
2018-04-08 18:24:37 +00:00
|
|
|
}
|
|
|
|
|
2020-12-28 01:26:51 +00:00
|
|
|
_, err = fs.Proc(pid)
|
2017-11-05 01:18:28 +00:00
|
|
|
if err != nil {
|
2022-01-10 00:29:12 +00:00
|
|
|
return fmt.Errorf("checking for NGINX process with PID %v: %w", pid, err)
|
2017-11-05 01:18:28 +00:00
|
|
|
}
|
2019-09-01 18:21:24 +00:00
|
|
|
|
2020-07-01 02:53:31 +00:00
|
|
|
statusCode, _, err := nginx.NewGetStatusRequest("/is-dynamic-lb-initialized")
|
2017-11-05 01:18:28 +00:00
|
|
|
if err != nil {
|
2022-01-10 00:29:12 +00:00
|
|
|
return fmt.Errorf("checking if the dynamic load balancer started: %w", err)
|
2017-11-05 01:18:28 +00:00
|
|
|
}
|
2019-09-01 18:21:24 +00:00
|
|
|
|
|
|
|
if statusCode != 200 {
|
|
|
|
return fmt.Errorf("dynamic load balancer not started")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-11-05 01:18:28 +00:00
|
|
|
}
|