Skip to content

Commit

Permalink
Add path to externalURLs (#2208)
Browse files Browse the repository at this point in the history
  • Loading branch information
rprin authored and Alexander Matyushentsev committed Aug 23, 2019
1 parent 459402b commit 16883df
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 5 deletions.
15 changes: 12 additions & 3 deletions controller/cache/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,23 @@ func populateIngressInfo(un *unstructured.Unstructured, node *node) {
stringPort = fmt.Sprintf("%v", port)
}

var externalURL string
switch stringPort {
case "80", "http":
urlsSet[fmt.Sprintf("http://%s", host)] = true
externalURL = fmt.Sprintf("http://%s", host)
case "443", "https":
urlsSet[fmt.Sprintf("https://%s", host)] = true
externalURL = fmt.Sprintf("https://%s", host)
default:
urlsSet[fmt.Sprintf("http://%s:%s", host, stringPort)] = true
externalURL = fmt.Sprintf("http://%s:%s", host, stringPort)
}

subPath := ""
if nestedPath, ok, err := unstructured.NestedString(path, "path"); ok && err == nil {
subPath = nestedPath
}

externalURL += subPath
urlsSet[externalURL] = true
}
}
}
Expand Down
92 changes: 90 additions & 2 deletions controller/cache/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestGetIngressInfo(t *testing.T) {
Kind: kube.ServiceKind,
Name: "helm-guestbook",
}},
ExternalURLs: []string{"https://helm-guestbook.com"},
ExternalURLs: []string{"https://helm-guestbook.com/"},
}, node.networkingInfo)
}

Expand Down Expand Up @@ -103,6 +103,94 @@ func TestGetIngressInfoNoHost(t *testing.T) {
Kind: kube.ServiceKind,
Name: "helm-guestbook",
}},
ExternalURLs: []string{"https://107.178.210.11"},
ExternalURLs: []string{"https://107.178.210.11/"},
}, node.networkingInfo)
}
func TestExternalUrlWithSubPath(t *testing.T) {
ingress := strToUnstructured(`
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: helm-guestbook
namespace: default
spec:
rules:
- http:
paths:
- backend:
serviceName: helm-guestbook
servicePort: 443
path: /my/sub/path/
status:
loadBalancer:
ingress:
- ip: 107.178.210.11`)

node := &node{}
populateNodeInfo(ingress, node)

expectedExternalUrls := []string{"https://107.178.210.11/my/sub/path/"}
assert.Equal(t, expectedExternalUrls, node.networkingInfo.ExternalURLs)
}
func TestExternalUrlWithMultipleSubPaths(t *testing.T) {
ingress := strToUnstructured(`
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: helm-guestbook
namespace: default
spec:
rules:
- host: helm-guestbook.com
http:
paths:
- backend:
serviceName: helm-guestbook
servicePort: 443
path: /my/sub/path/
- backend:
serviceName: helm-guestbook-2
servicePort: 443
path: /my/sub/path/2
- backend:
serviceName: helm-guestbook-3
servicePort: 443
status:
loadBalancer:
ingress:
- ip: 107.178.210.11`)

node := &node{}
populateNodeInfo(ingress, node)

expectedExternalUrls := []string{"https://helm-guestbook.com/my/sub/path/", "https://helm-guestbook.com/my/sub/path/2", "https://helm-guestbook.com"}
actualURLs := node.networkingInfo.ExternalURLs
sort.Strings(expectedExternalUrls)
sort.Strings(actualURLs)
assert.Equal(t, expectedExternalUrls, actualURLs)
}
func TestExternalUrlWithNoSubPath(t *testing.T) {
ingress := strToUnstructured(`
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: helm-guestbook
namespace: default
spec:
rules:
- http:
paths:
- backend:
serviceName: helm-guestbook
servicePort: 443
status:
loadBalancer:
ingress:
- ip: 107.178.210.11`)

node := &node{}
populateNodeInfo(ingress, node)

expectedExternalUrls := []string{"https://107.178.210.11"}
assert.Equal(t, expectedExternalUrls, node.networkingInfo.ExternalURLs)
}

0 comments on commit 16883df

Please sign in to comment.