2019-01-21 14:29:36 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 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 nginx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-08-06 14:18:17 +00:00
|
|
|
"io"
|
2019-01-21 14:29:36 +00:00
|
|
|
"net/http"
|
2019-02-08 16:25:04 +00:00
|
|
|
"os"
|
2019-08-13 17:46:16 +00:00
|
|
|
"os/exec"
|
2019-02-25 16:29:52 +00:00
|
|
|
"strings"
|
2019-01-21 14:29:36 +00:00
|
|
|
"time"
|
|
|
|
|
2019-08-31 00:18:11 +00:00
|
|
|
ps "github.com/mitchellh/go-ps"
|
2020-08-08 23:31:02 +00:00
|
|
|
"k8s.io/klog/v2"
|
2019-01-21 14:29:36 +00:00
|
|
|
)
|
|
|
|
|
2019-09-28 20:30:57 +00:00
|
|
|
// TODO: Check https://github.com/kubernetes/kubernetes/blob/master/pkg/master/ports/ports.go for ports already being used
|
|
|
|
|
|
|
|
// ProfilerPort port used by the ingress controller to expose the Go Profiler when it is enabled.
|
|
|
|
var ProfilerPort = 10245
|
|
|
|
|
2019-08-13 21:14:55 +00:00
|
|
|
// TemplatePath path of the NGINX template
|
|
|
|
var TemplatePath = "/etc/nginx/template/nginx.tmpl"
|
|
|
|
|
2019-01-21 14:29:36 +00:00
|
|
|
// PID defines the location of the pid file used by NGINX
|
|
|
|
var PID = "/tmp/nginx.pid"
|
|
|
|
|
2019-09-01 18:21:24 +00:00
|
|
|
// StatusPort port used by NGINX for the status server
|
2019-09-28 20:30:57 +00:00
|
|
|
var StatusPort = 10246
|
2019-01-21 14:29:36 +00:00
|
|
|
|
|
|
|
// HealthPath defines the path used to define the health check location in NGINX
|
|
|
|
var HealthPath = "/healthz"
|
|
|
|
|
2019-06-11 02:32:17 +00:00
|
|
|
// HealthCheckTimeout defines the time limit in seconds for a probe to health-check-path to succeed
|
|
|
|
var HealthCheckTimeout = 10 * time.Second
|
|
|
|
|
2019-01-21 14:29:36 +00:00
|
|
|
// StatusPath defines the path used to expose the NGINX status page
|
|
|
|
// http://nginx.org/en/docs/http/ngx_http_stub_status_module.html
|
|
|
|
var StatusPath = "/nginx_status"
|
|
|
|
|
2019-09-08 21:14:54 +00:00
|
|
|
// StreamPort defines the port used by NGINX for the NGINX stream configuration socket
|
2019-09-28 20:30:57 +00:00
|
|
|
var StreamPort = 10247
|
2019-01-21 14:29:36 +00:00
|
|
|
|
|
|
|
// NewGetStatusRequest creates a new GET request to the internal NGINX status server
|
|
|
|
func NewGetStatusRequest(path string) (int, []byte, error) {
|
2019-09-01 18:21:24 +00:00
|
|
|
url := fmt.Sprintf("http://127.0.0.1:%v%v", StatusPort, path)
|
2019-06-11 02:32:17 +00:00
|
|
|
|
2019-09-01 18:21:24 +00:00
|
|
|
client := http.Client{}
|
|
|
|
res, err := client.Get(url)
|
2019-01-21 14:29:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
2021-08-06 14:18:17 +00:00
|
|
|
data, err := io.ReadAll(res.Body)
|
2019-01-21 14:29:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.StatusCode, data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPostStatusRequest creates a new POST request to the internal NGINX status server
|
|
|
|
func NewPostStatusRequest(path, contentType string, data interface{}) (int, []byte, error) {
|
2019-09-01 18:21:24 +00:00
|
|
|
url := fmt.Sprintf("http://127.0.0.1:%v%v", StatusPort, path)
|
2019-01-21 14:29:36 +00:00
|
|
|
|
|
|
|
buf, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
2019-09-01 18:21:24 +00:00
|
|
|
client := http.Client{}
|
|
|
|
res, err := client.Post(url, contentType, bytes.NewReader(buf))
|
2019-01-21 14:29:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
2021-08-06 14:18:17 +00:00
|
|
|
body, err := io.ReadAll(res.Body)
|
2019-01-21 14:29:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.StatusCode, body, nil
|
|
|
|
}
|
|
|
|
|
2019-02-25 16:29:52 +00:00
|
|
|
// GetServerBlock takes an nginx.conf file and a host and tries to find the server block for that host
|
|
|
|
func GetServerBlock(conf string, host string) (string, error) {
|
2019-03-12 16:52:23 +00:00
|
|
|
startMsg := fmt.Sprintf("## start server %v\n", host)
|
2019-02-25 16:29:52 +00:00
|
|
|
endMsg := fmt.Sprintf("## end server %v", host)
|
|
|
|
|
|
|
|
blockStart := strings.Index(conf, startMsg)
|
|
|
|
if blockStart < 0 {
|
2019-07-08 20:10:38 +00:00
|
|
|
return "", fmt.Errorf("host %v was not found in the controller's nginx.conf", host)
|
2019-02-25 16:29:52 +00:00
|
|
|
}
|
|
|
|
blockStart = blockStart + len(startMsg)
|
|
|
|
|
|
|
|
blockEnd := strings.Index(conf, endMsg)
|
|
|
|
if blockEnd < 0 {
|
2019-07-08 20:10:38 +00:00
|
|
|
return "", fmt.Errorf("the end of the host server block could not be found, but the beginning was")
|
2019-02-25 16:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return conf[blockStart:blockEnd], nil
|
|
|
|
}
|
|
|
|
|
2019-02-08 16:25:04 +00:00
|
|
|
// ReadNginxConf reads the nginx configuration file into a string
|
|
|
|
func ReadNginxConf() (string, error) {
|
2019-07-19 14:42:44 +00:00
|
|
|
return readFileToString("/etc/nginx/nginx.conf")
|
2019-02-25 16:29:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-19 14:42:44 +00:00
|
|
|
// readFileToString reads any file into a string
|
|
|
|
func readFileToString(path string) (string, error) {
|
2019-02-25 16:29:52 +00:00
|
|
|
f, err := os.Open(path)
|
2019-02-08 16:25:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-02-25 16:29:52 +00:00
|
|
|
defer f.Close()
|
2019-02-08 16:25:04 +00:00
|
|
|
|
2021-08-06 14:18:17 +00:00
|
|
|
contents, err := io.ReadAll(f)
|
2019-02-08 16:25:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(contents), nil
|
|
|
|
}
|
|
|
|
|
2019-08-13 17:46:16 +00:00
|
|
|
// Version return details about NGINX
|
|
|
|
func Version() string {
|
|
|
|
flag := "-v"
|
|
|
|
|
2020-08-08 23:31:02 +00:00
|
|
|
if klog.V(2).Enabled() {
|
2019-08-13 17:46:16 +00:00
|
|
|
flag = "-V"
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command("nginx", flag)
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2020-09-27 20:32:40 +00:00
|
|
|
klog.ErrorS(err, "unexpected error obtaining NGINX version")
|
2019-08-13 17:46:16 +00:00
|
|
|
return "N/A"
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(out)
|
|
|
|
}
|
2019-08-31 00:18:11 +00:00
|
|
|
|
|
|
|
// IsRunning returns true if a process with the name 'nginx' is found
|
|
|
|
func IsRunning() bool {
|
|
|
|
processes, _ := ps.Processes()
|
|
|
|
for _, p := range processes {
|
|
|
|
if p.Executable() == "nginx" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|