forked from nginx/kubernetes-ingress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginx.go
311 lines (263 loc) · 8.06 KB
/
nginx.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package nginx
import (
"bytes"
"fmt"
"os"
"os/exec"
"path"
"text/template"
"github.com/golang/glog"
)
const dhparamFilename = "dhparam.pem"
// NginxController Updates NGINX configuration, starts and reloads NGINX
type NginxController struct {
nginxConfdPath string
nginxCertsPath string
local bool
}
// IngressNginxConfig describes an NGINX configuration
type IngressNginxConfig struct {
Upstreams []Upstream
Servers []Server
}
// Upstream describes an NGINX upstream
type Upstream struct {
Name string
UpstreamServers []UpstreamServer
}
// UpstreamServer describes a server in an NGINX upstream
type UpstreamServer struct {
Address string
Port string
}
// Server describes an NGINX server
type Server struct {
ServerSnippets []string
Name string
ServerTokens bool
Locations []Location
SSL bool
SSLCertificate string
SSLCertificateKey string
HTTP2 bool
RedirectToHTTPS bool
ProxyProtocol bool
HSTS bool
HSTSMaxAge int64
HSTSIncludeSubdomains bool
ProxyHideHeaders []string
ProxyPassHeaders []string
CorsEnabled bool
CorsDomains string
// http://nginx.org/en/docs/http/ngx_http_realip_module.html
RealIPHeader string
SetRealIPFrom []string
RealIPRecursive bool
}
// Location describes an NGINX location
type Location struct {
LocationSnippets []string
Path string
Upstream Upstream
ProxyConnectTimeout string
ProxyReadTimeout string
ClientMaxBodySize string
Websocket bool
Rewrite string
SSL bool
ProxyBuffering bool
ProxyBuffers string
ProxyBufferSize string
ProxyMaxTempFileSize string
}
// NginxMainConfig describe the main NGINX configuration file
type NginxMainConfig struct {
ServerNamesHashBucketSize string
ServerNamesHashMaxSize string
LogFormat string
HealthStatus bool
HTTPSnippets []string
// http://nginx.org/en/docs/http/ngx_http_ssl_module.html
SSLProtocols string
SSLPreferServerCiphers bool
SSLCiphers string
SSLDHParam string
}
// NewUpstreamWithDefaultServer creates an upstream with the default server.
// proxy_pass to an upstream with the default server returns 502.
// We use it for services that have no endpoints
func NewUpstreamWithDefaultServer(name string) Upstream {
return Upstream{
Name: name,
UpstreamServers: []UpstreamServer{UpstreamServer{Address: "127.0.0.1", Port: "8181"}},
}
}
// NewNginxController creates a NGINX controller
func NewNginxController(nginxConfPath string, local bool, healthStatus bool) (*NginxController, error) {
ngxc := NginxController{
nginxConfdPath: path.Join(nginxConfPath, "conf.d"),
nginxCertsPath: path.Join(nginxConfPath, "ssl"),
local: local,
}
if !local {
createDir(ngxc.nginxCertsPath)
}
cfg := &NginxMainConfig{ServerNamesHashMaxSize: NewDefaultConfig().MainServerNamesHashMaxSize, HealthStatus: healthStatus}
ngxc.UpdateMainConfigFile(cfg)
return &ngxc, nil
}
// DeleteIngress deletes the configuration file, which corresponds for the
// specified ingress from NGINX conf directory
func (nginx *NginxController) DeleteIngress(name string) {
filename := nginx.getIngressNginxConfigFileName(name)
glog.V(3).Infof("deleting %v", filename)
if !nginx.local {
if err := os.Remove(filename); err != nil {
glog.Warningf("Failed to delete %v: %v", filename, err)
}
}
}
// AddOrUpdateIngress creates or updates a file with
// the specified configuration for the specified ingress
func (nginx *NginxController) AddOrUpdateIngress(name string, config IngressNginxConfig) {
glog.V(3).Infof("Updating NGINX configuration")
filename := nginx.getIngressNginxConfigFileName(name)
nginx.templateIt(config, filename)
}
// AddOrUpdateDHParam creates the servers dhparam.pem file
func (nginx *NginxController) AddOrUpdateDHParam(dhparam string) (string, error) {
fileName := nginx.nginxCertsPath + "/" + dhparamFilename
if !nginx.local {
pem, err := os.Create(fileName)
if err != nil {
return fileName, fmt.Errorf("Couldn't create file %v: %v", fileName, err)
}
defer pem.Close()
_, err = pem.WriteString(dhparam)
if err != nil {
return fileName, fmt.Errorf("Couldn't write to pem file %v: %v", fileName, err)
}
}
return fileName, nil
}
// AddOrUpdateCertAndKey creates a .pem file wth the cert and the key with the
// specified name
func (nginx *NginxController) AddOrUpdateCertAndKey(name string, cert string, key string) string {
pemFileName := nginx.nginxCertsPath + "/" + name + ".pem"
if !nginx.local {
pem, err := os.Create(pemFileName)
if err != nil {
glog.Fatalf("Couldn't create pem file %v: %v", pemFileName, err)
}
defer pem.Close()
_, err = pem.WriteString(key)
if err != nil {
glog.Fatalf("Couldn't write to pem file %v: %v", pemFileName, err)
}
_, err = pem.WriteString("\n")
if err != nil {
glog.Fatalf("Couldn't write to pem file %v: %v", pemFileName, err)
}
_, err = pem.WriteString(cert)
if err != nil {
glog.Fatalf("Couldn't write to pem file %v: %v", pemFileName, err)
}
}
return pemFileName
}
func (nginx *NginxController) getIngressNginxConfigFileName(name string) string {
return path.Join(nginx.nginxConfdPath, name+".conf")
}
func (nginx *NginxController) templateIt(config IngressNginxConfig, filename string) {
tmpl, err := template.New("ingress.tmpl").ParseFiles("ingress.tmpl")
if err != nil {
glog.Fatal("Failed to parse template file")
}
glog.V(3).Infof("Writing NGINX conf to %v", filename)
if glog.V(3) {
tmpl.Execute(os.Stdout, config)
}
if !nginx.local {
w, err := os.Create(filename)
if err != nil {
glog.Fatalf("Failed to open %v: %v", filename, err)
}
defer w.Close()
if err := tmpl.Execute(w, config); err != nil {
glog.Fatalf("Failed to write template %v", err)
}
} else {
// print conf to stdout here
}
glog.V(3).Infof("NGINX configuration file had been updated")
}
// Reload reloads NGINX
func (nginx *NginxController) Reload() error {
if !nginx.local {
if err := shellOut("nginx -t"); err != nil {
return fmt.Errorf("Invalid nginx configuration detected, not reloading: %s", err)
}
if err := shellOut("nginx -s reload"); err != nil {
return fmt.Errorf("Reloading NGINX failed: %s", err)
}
} else {
glog.V(3).Info("Reloading nginx")
}
return nil
}
// Start starts NGINX
func (nginx *NginxController) Start() {
if !nginx.local {
if err := shellOut("nginx"); err != nil {
glog.Fatalf("Failed to start nginx: %v", err)
}
} else {
glog.V(3).Info("Starting nginx")
}
}
func createDir(path string) {
if err := os.Mkdir(path, os.ModeDir); err != nil {
glog.Fatalf("Couldn't create directory %v: %v", path, err)
}
}
func shellOut(cmd string) (err error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
glog.V(3).Infof("executing %s", cmd)
command := exec.Command("sh", "-c", cmd)
command.Stdout = &stdout
command.Stderr = &stderr
err = command.Start()
if err != nil {
return fmt.Errorf("Failed to execute %v, err: %v", cmd, err)
}
err = command.Wait()
if err != nil {
return fmt.Errorf("Command %v stdout: %q\nstderr: %q\nfinished with error: %v", cmd,
stdout.String(), stderr.String(), err)
}
return nil
}
// UpdateMainConfigFile update the main NGINX configuration file
func (nginx *NginxController) UpdateMainConfigFile(cfg *NginxMainConfig) {
tmpl, err := template.New("nginx.conf.tmpl").ParseFiles("nginx.conf.tmpl")
if err != nil {
glog.Fatalf("Failed to parse the main config template file: %v", err)
}
filename := "/etc/nginx/nginx.conf"
glog.V(3).Infof("Writing NGINX conf to %v", filename)
if glog.V(3) {
tmpl.Execute(os.Stdout, cfg)
}
if !nginx.local {
w, err := os.Create(filename)
if err != nil {
glog.Fatalf("Failed to open %v: %v", filename, err)
}
defer w.Close()
if err := tmpl.Execute(w, cfg); err != nil {
glog.Fatalf("Failed to write template %v", err)
}
}
glog.V(3).Infof("The main NGINX configuration file had been updated")
}