-
Notifications
You must be signed in to change notification settings - Fork 8.4k
/
Copy pathmain.go
177 lines (139 loc) · 4.62 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
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"
"io"
"net/http"
"os"
"os/exec"
"strings"
"time"
ps "github.com/mitchellh/go-ps"
klog "k8s.io/klog/v2"
)
// 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
// ProfilerAddress IP address used by the ingress controller to expose the Go Profiler when it is enabled.
var ProfilerAddress = "127.0.0.1"
// TemplatePath path of the NGINX template
var TemplatePath = "/etc/nginx/template/nginx.tmpl"
// PID defines the location of the pid file used by NGINX
var PID = "/tmp/nginx/nginx.pid"
// StatusPort port used by NGINX for the status server
var StatusPort = 10246
// HealthPath defines the path used to define the health check location in NGINX
var HealthPath = "/healthz"
// HealthCheckTimeout defines the time limit in seconds for a probe to health-check-path to succeed
var HealthCheckTimeout = 10 * time.Second
// 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"
// StreamPort defines the port used by NGINX for the NGINX stream configuration socket
var StreamPort = 10247
// NewGetStatusRequest creates a new GET request to the internal NGINX status server
func NewGetStatusRequest(path string) (statusCode int, data []byte, err error) {
url := fmt.Sprintf("http://127.0.0.1:%v%v", StatusPort, path)
client := http.Client{}
res, err := client.Get(url)
if err != nil {
return 0, nil, err
}
defer res.Body.Close()
data, err = io.ReadAll(res.Body)
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{}) (statusCode int, body []byte, err error) {
url := fmt.Sprintf("http://127.0.0.1:%v%v", StatusPort, path)
buf, err := json.Marshal(data)
if err != nil {
return 0, nil, err
}
client := http.Client{}
res, err := client.Post(url, contentType, bytes.NewReader(buf))
if err != nil {
return 0, nil, err
}
defer res.Body.Close()
body, err = io.ReadAll(res.Body)
if err != nil {
return 0, nil, err
}
return res.StatusCode, body, nil
}
// GetServerBlock takes an nginx.conf file and a host and tries to find the server block for that host
func GetServerBlock(conf, host string) (string, error) {
startMsg := fmt.Sprintf("## start server %v\n", host)
endMsg := fmt.Sprintf("## end server %v", host)
blockStart := strings.Index(conf, startMsg)
if blockStart < 0 {
return "", fmt.Errorf("host %v was not found in the controller's nginx.conf", host)
}
blockStart += len(startMsg)
blockEnd := strings.Index(conf, endMsg)
if blockEnd < 0 {
return "", fmt.Errorf("the end of the host server block could not be found, but the beginning was")
}
return conf[blockStart:blockEnd], nil
}
// ReadNginxConf reads the nginx configuration file into a string
func ReadNginxConf() (string, error) {
return readFileToString("/etc/nginx/nginx.conf")
}
// readFileToString reads any file into a string
func readFileToString(path string) (string, error) {
f, err := os.Open(path)
if err != nil {
return "", err
}
defer f.Close()
contents, err := io.ReadAll(f)
if err != nil {
return "", err
}
return string(contents), nil
}
// Version return details about NGINX
func Version() string {
flag := "-v"
if klog.V(2).Enabled() {
flag = "-V"
}
cmd := exec.Command("nginx", flag)
out, err := cmd.CombinedOutput()
if err != nil {
klog.ErrorS(err, "unexpected error obtaining NGINX version")
return "N/A"
}
return string(out)
}
// IsRunning returns true if a process with the name 'nginx' is found
func IsRunning() bool {
processes, err := ps.Processes()
if err != nil {
klog.ErrorS(err, "unexpected error obtaining process list")
}
for _, p := range processes {
if p.Executable() == "nginx" {
return true
}
}
return false
}