Skip to content

Commit 14ba6c4

Browse files
authored
Merge pull request #48 from nginxinc/propagete-changes-to-plus
Propagate commits for NGINX controller to NGINX Plus controller
2 parents 8727844 + 5dc0ca1 commit 14ba6c4

File tree

4 files changed

+34
-20
lines changed

4 files changed

+34
-20
lines changed

nginx-controller/nginx/nginx.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,10 @@ func (nginx *NginxController) templateIt(config IngressNginxConfig, filename str
178178
func (nginx *NginxController) Reload() error {
179179
if !nginx.local {
180180
if err := shellOut("nginx -t"); err != nil {
181-
return fmt.Errorf("Invalid nginx configuration detected, not reloading", err)
181+
return fmt.Errorf("Invalid nginx configuration detected, not reloading: %s", err)
182182
}
183183
if err := shellOut("nginx -s reload"); err != nil {
184-
return fmt.Errorf("nginx -s failed: %s", err)
184+
return fmt.Errorf("Reloading NGINX failed: %s", err)
185185
}
186186
} else {
187187
glog.V(3).Info("Reloading nginx")

nginx-plus-controller/controller/controller.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ func (lbc *LoadBalancerController) syncEndp(key string) {
267267

268268
for _, ing := range ings {
269269
ingEx := lbc.createIngress(&ing)
270-
glog.V(3).Infof("Updating Endponits for %v/%v", ing.Name, ing.Namespace)
270+
glog.V(3).Infof("Updating Endpoints for %v/%v", ing.Name, ing.Namespace)
271271
name := ing.Namespace + "-" + ing.Name
272272
lbc.cnf.UpdateEndpoints(name, &ingEx)
273273
}
@@ -378,7 +378,7 @@ func (lbc *LoadBalancerController) createIngress(ing *extensions.Ingress) nginx.
378378
secretName := tls.SecretName
379379
secret, err := lbc.client.Secrets(ing.Namespace).Get(secretName)
380380
if err != nil {
381-
glog.Warningf("Error retriveing secret %v for ing %v: %v", secretName, ing.Name, err)
381+
glog.Warningf("Error retrieving secret %v for Ingress %v: %v", secretName, ing.Name, err)
382382
continue
383383
}
384384
ingEx.Secrets[secretName] = secret
@@ -388,7 +388,7 @@ func (lbc *LoadBalancerController) createIngress(ing *extensions.Ingress) nginx.
388388
if ing.Spec.Backend != nil {
389389
endps, err := lbc.getEndpointsForIngressBackend(ing.Spec.Backend, ing.Namespace)
390390
if err != nil {
391-
glog.V(3).Infof("Error retriviend endpoints for the services %v: %v", ing.Spec.Backend.ServiceName, err)
391+
glog.V(3).Infof("Error retrieving endpoints for the services %v: %v", ing.Spec.Backend.ServiceName, err)
392392
} else {
393393
ingEx.Endpoints[ing.Spec.Backend.ServiceName] = endps
394394
}
@@ -402,7 +402,7 @@ func (lbc *LoadBalancerController) createIngress(ing *extensions.Ingress) nginx.
402402
for _, path := range rule.HTTP.Paths {
403403
endps, err := lbc.getEndpointsForIngressBackend(&path.Backend, ing.Namespace)
404404
if err != nil {
405-
glog.V(3).Infof("Error retriviend endpoints for the services %v: %v", path.Backend.ServiceName, err)
405+
glog.V(3).Infof("Error retrieving endpoints for the services %v: %v", path.Backend.ServiceName, err)
406406
} else {
407407
ingEx.Endpoints[path.Backend.ServiceName] = endps
408408
}
@@ -430,7 +430,7 @@ func (lbc *LoadBalancerController) getEndpointsForIngressBackend(backend *extens
430430
return &endps, nil
431431
}
432432

433-
return nil, fmt.Errorf("Svc %s doesn't exists", svcKey)
433+
return nil, fmt.Errorf("service %s doesn't exists", svcKey)
434434
}
435435

436436
func parseNginxConfigMaps(nginxConfigMaps string) (string, string, error) {

nginx-plus-controller/nginx/configurator.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ func (cnf *Configurator) AddOrUpdateIngress(name string, ingEx *IngressEx) {
4040
pems := cnf.updateCertificates(ingEx)
4141
nginxCfg := cnf.generateNginxCfg(ingEx, pems)
4242
cnf.nginx.AddOrUpdateIngress(name, nginxCfg)
43-
cnf.nginx.Reload()
44-
time.Sleep(500 * time.Millisecond)
45-
cnf.updateEndpoints(name, ingEx)
43+
if err := cnf.nginx.Reload(); err != nil {
44+
glog.Errorf("Error when adding or updating ingress %q: %q", name, err)
45+
} else {
46+
time.Sleep(500 * time.Millisecond)
47+
cnf.updateEndpoints(name, ingEx)
48+
}
4649
}
4750

4851
func (cnf *Configurator) updateCertificates(ingEx *IngressEx) map[string]string {
@@ -257,7 +260,9 @@ func (cnf *Configurator) DeleteIngress(name string) {
257260
defer cnf.lock.Unlock()
258261

259262
cnf.nginx.DeleteIngress(name)
260-
cnf.nginx.Reload()
263+
if err := cnf.nginx.Reload(); err != nil {
264+
glog.Errorf("Error when removing ingress %q: %q", name, err)
265+
}
261266
}
262267

263268
// UpdateEndpoints updates endpoints in NGINX configuration for an Ingress resource

nginx-plus-controller/nginx/nginx.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package nginx
22

33
import (
44
"bytes"
5+
"fmt"
56
"html/template"
67
"os"
78
"os/exec"
@@ -200,18 +201,26 @@ func (nginx *NginxController) templateIt(config IngressNginxConfig, filename str
200201
}
201202

202203
// Reload reloads NGINX
203-
func (nginx *NginxController) Reload() {
204+
func (nginx *NginxController) Reload() error {
204205
if !nginx.local {
205-
shellOut("nginx -s reload")
206+
if err := shellOut("nginx -t"); err != nil {
207+
return fmt.Errorf("Invalid nginx configuration detected, not reloading: %s", err)
208+
}
209+
if err := shellOut("nginx -s reload"); err != nil {
210+
return fmt.Errorf("Reloading NGINX failed: %s", err)
211+
}
206212
} else {
207213
glog.V(3).Info("Reloading nginx")
208214
}
215+
return nil
209216
}
210217

211218
// Start starts NGINX
212219
func (nginx *NginxController) Start() {
213220
if !nginx.local {
214-
shellOut("nginx")
221+
if err := shellOut("nginx"); err != nil {
222+
glog.Fatalf("Failed to start nginx")
223+
}
215224
} else {
216225
glog.V(3).Info("Starting nginx")
217226
}
@@ -223,7 +232,7 @@ func (nginx *NginxController) createCertsDir() {
223232
}
224233
}
225234

226-
func shellOut(cmd string) {
235+
func shellOut(cmd string) (err error) {
227236
var stdout bytes.Buffer
228237
var stderr bytes.Buffer
229238

@@ -233,17 +242,17 @@ func shellOut(cmd string) {
233242
command.Stdout = &stdout
234243
command.Stderr = &stderr
235244

236-
err := command.Start()
245+
err = command.Start()
237246
if err != nil {
238-
glog.Fatalf("Failed to execute %v, err: %v", cmd, err)
247+
return fmt.Errorf("Failed to execute %v, err: %v", cmd, err)
239248
}
240249

241250
err = command.Wait()
242251
if err != nil {
243-
glog.Errorf("Command %v stdout: %q", cmd, stdout.String())
244-
glog.Errorf("Command %v stderr: %q", cmd, stderr.String())
245-
glog.Fatalf("Command %v finished with error: %v", cmd, err)
252+
return fmt.Errorf("Command %v stdout: %q\nstderr: %q\nfinished with error: %v", cmd,
253+
stdout.String(), stderr.String(), err)
246254
}
255+
return nil
247256
}
248257

249258
// UpdateMainConfigFile update the main NGINX configuration file

0 commit comments

Comments
 (0)