Skip to content

Commit

Permalink
Added handling empty path in Ingress resource for NGINX Plus
Browse files Browse the repository at this point in the history
Also added logging output of failed shellout commands
  • Loading branch information
pleshakov committed Apr 19, 2016
1 parent f8e60b3 commit d76fbd5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
5 changes: 4 additions & 1 deletion nginx-plus-controller/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ PREFIX = gcr.io/nginx-ingress/nginx-plus-ingress
nginx-plus-ingress:
CGO_ENABLED=0 GOOS=linux godep go build -a -installsuffix cgo -ldflags '-w' -o nginx-plus-ingress *.go

container: nginx-plus-ingress
test:
godep go test ./...

container: nginx-plus-ingress test
docker build -t $(PREFIX):$(TAG) .

push: container
Expand Down
9 changes: 8 additions & 1 deletion nginx-plus-controller/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (lbc *LoadBalancerController) updateNGINX(name string, ing *extensions.Ingr
var locations []nginx.Location

for _, path := range rule.HTTP.Paths {
loc := nginx.Location{Path: path.Path}
loc := nginx.Location{Path: pathOrDefault(path.Path)}
upsName := getNameForUpstream(ing, rule.Host, path.Backend.ServiceName)

if ups, ok := upstreams[upsName]; ok {
Expand All @@ -207,6 +207,13 @@ func (lbc *LoadBalancerController) updateNGINX(name string, ing *extensions.Ingr
lbc.nginx.AddOrUpdateIngress(name, nginx.IngressNGINXConfig{Upstreams: upstreamMapToSlice(upstreams), Servers: servers})
}

func pathOrDefault(path string) string {
if path == "" {
return "/"
}
return path
}

func getNameForUpstream(ing *extensions.Ingress, host string, service string) string {
return fmt.Sprintf("%v-%v-%v-%v", ing.Namespace, ing.Name, host, service)
}
Expand Down
20 changes: 20 additions & 0 deletions nginx-plus-controller/controller/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package controller

import (
"testing"
)

func TestPathOrDefaultReturnDefault(t *testing.T) {
path := ""
expected := "/"
if pathOrDefault(path) != expected {
t.Errorf("pathOrDefault(%q) should return %q", path, expected)
}
}

func TestPathOrDefaultReturnActual(t *testing.T) {
path := "/path/to/resource"
if pathOrDefault(path) != path {
t.Errorf("pathOrDefault(%q) should return %q", path, path)
}
}
9 changes: 9 additions & 0 deletions nginx-plus-controller/nginx/nginx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nginx

import (
"bytes"
"html/template"
"os"
"os/exec"
Expand Down Expand Up @@ -219,16 +220,24 @@ func (nginx *NGINXController) createCertsDir() {
}

func shellOut(cmd string) {
var stdout bytes.Buffer
var stderr bytes.Buffer

glog.Infof("executing %s", cmd)

command := exec.Command("sh", "-c", cmd)
command.Stdout = &stdout
command.Stderr = &stderr

err := command.Start()
if err != nil {
glog.Fatalf("Failed to execute %v, err: %v", cmd, err)
}

err = command.Wait()
if err != nil {
glog.Errorf("Command %v stdout: %q", cmd, stdout.String())
glog.Errorf("Command %v stderr: %q", cmd, stderr.String())
glog.Fatalf("Command %v finished with error: %v", cmd, err)
}
}

0 comments on commit d76fbd5

Please sign in to comment.