From 1685ab30014c0fbf4d534319d722c7102b5509eb Mon Sep 17 00:00:00 2001 From: Hang Yan Date: Sun, 23 Feb 2020 19:25:43 +0800 Subject: [PATCH] Support ingress path for kubernetes (#1245) * Support ingress path for kubernetes --- docs/user-guide.md | 2 +- pkg/transformer/kubernetes/kubernetes.go | 2 ++ pkg/transformer/utils.go | 10 ++++++++++ .../docker-compose-expose-multiple-hostname.yml | 2 +- .../kubernetes-expose-multiple-hostname.json | 7 ++++--- 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/docs/user-guide.md b/docs/user-guide.md index 38e0ee7b..6cf83ee9 100644 --- a/docs/user-guide.md +++ b/docs/user-guide.md @@ -332,7 +332,7 @@ services: ``` - `kompose.service.expose` defines if the service needs to be made accessible from outside the cluster or not. If the value is set to "true", the provider sets the endpoint automatically, and for any other value, the value is set as the hostname. If multiple ports are defined in a service, the first one is chosen to be the exposed. - - For the Kubernetes provider, an ingress resource is created and it is assumed that an ingress controller has already been configured. If the value is set to a comma sepatated list, multiple hostnames are supported. + - For the Kubernetes provider, an ingress resource is created and it is assumed that an ingress controller has already been configured. If the value is set to a comma sepatated list, multiple hostnames are supported.Hostname with path is also supported. - For the OpenShift provider, a route is created. - `kompose.service.nodeport.port` defines the port value when service type is `nodeport`, this label should only be set when the service only contains 1 port. Usually kubernetes define a port range for node port values, kompose will not validate this. - `kompose.service.expose.tls-secret` provides the name of the TLS secret to use with the Kubernetes ingress controller. This requires kompose.service.expose to be set. diff --git a/pkg/transformer/kubernetes/kubernetes.go b/pkg/transformer/kubernetes/kubernetes.go index e3d1820d..f673d645 100755 --- a/pkg/transformer/kubernetes/kubernetes.go +++ b/pkg/transformer/kubernetes/kubernetes.go @@ -462,11 +462,13 @@ func (k *Kubernetes) initIngress(name string, service kobject.ServiceConfig, por } for i, host := range hosts { + host, p := transformer.ParseIngressPath(host) ingress.Spec.Rules[i] = extensions.IngressRule{ IngressRuleValue: extensions.IngressRuleValue{ HTTP: &extensions.HTTPIngressRuleValue{ Paths: []extensions.HTTPIngressPath{ { + Path: p, Backend: extensions.IngressBackend{ ServiceName: name, ServicePort: intstr.IntOrString{ diff --git a/pkg/transformer/utils.go b/pkg/transformer/utils.go index 51820422..c3898c0e 100644 --- a/pkg/transformer/utils.go +++ b/pkg/transformer/utils.go @@ -103,6 +103,16 @@ func ParseVolume(volume string) (name, host, container, mode string, err error) return } +// ParseIngressPath parse path for ingress. +// eg. example.com/org -> example.com org +func ParseIngressPath(url string) (string, string) { + if strings.Contains(url, "/") { + splits := strings.Split(url, "/") + return splits[0], "/" + splits[1] + } + return url, "" +} + func isPath(substring string) bool { return strings.Contains(substring, "/") || substring == "." } diff --git a/script/test/fixtures/expose-service/compose-files/docker-compose-expose-multiple-hostname.yml b/script/test/fixtures/expose-service/compose-files/docker-compose-expose-multiple-hostname.yml index 697d2f33..66d32f4c 100644 --- a/script/test/fixtures/expose-service/compose-files/docker-compose-expose-multiple-hostname.yml +++ b/script/test/fixtures/expose-service/compose-files/docker-compose-expose-multiple-hostname.yml @@ -5,7 +5,7 @@ web: links: - redis labels: - kompose.service.expose: " batman.example.com ,, batwoman.example.com " + kompose.service.expose: " batman.example.com/home ,, batwoman.example.com " redis: image: redis:3.0 ports: diff --git a/script/test/fixtures/expose-service/provider-files/kubernetes-expose-multiple-hostname.json b/script/test/fixtures/expose-service/provider-files/kubernetes-expose-multiple-hostname.json index 245bc76a..1ad9b514 100644 --- a/script/test/fixtures/expose-service/provider-files/kubernetes-expose-multiple-hostname.json +++ b/script/test/fixtures/expose-service/provider-files/kubernetes-expose-multiple-hostname.json @@ -44,7 +44,7 @@ }, "annotations": { "kompose.cmd": "%CMD%", - "kompose.service.expose": " batman.example.com ,, batwoman.example.com ", + "kompose.service.expose": " batman.example.com/home ,, batwoman.example.com ", "kompose.version": "%VERSION%" } }, @@ -125,7 +125,7 @@ "metadata": { "annotations": { "kompose.cmd": "%CMD%", - "kompose.service.expose": " batman.example.com ,, batwoman.example.com ", + "kompose.service.expose": " batman.example.com/home ,, batwoman.example.com ", "kompose.version": "%VERSION%" }, "creationTimestamp": null, @@ -146,7 +146,7 @@ "metadata": { "annotations": { "kompose.cmd": "%CMD%", - "kompose.service.expose": " batman.example.com ,, batwoman.example.com ", + "kompose.service.expose": " batman.example.com/home ,, batwoman.example.com ", "kompose.version": "%VERSION%" }, "creationTimestamp": null, @@ -193,6 +193,7 @@ "http": { "paths": [ { + "path": "/home", "backend": { "serviceName": "web", "servicePort": 5000