Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added handling empty path in Ingress resource for NGINX Plus Controller #17

Merged
merged 1 commit into from
Apr 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}