implement expose service, add tests, fix #140
Implements a kompose specific docker compose label "kompose.service.expose" which can be used to expose the specified services externally. The accepted values are of type string. 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. Unit tests, functional tests, glide updates and docs have also been added in this commit for the related feature.
This commit is contained in:
@@ -68,6 +68,7 @@ type ServiceConfig struct {
|
||||
VolumesFrom []string `compose:"volumes_from",bundle:""`
|
||||
ServiceType string `compose:"kompose.service.type",bundle:""`
|
||||
Build string `compose:"build",bundle:""`
|
||||
ExposeService string `compose:"kompose.service.expose",bundle:""`
|
||||
}
|
||||
|
||||
// EnvVar holds the environment variable struct of a container
|
||||
|
||||
@@ -302,6 +302,8 @@ func (c *Compose) LoadFile(file string) kobject.KomposeObject {
|
||||
switch key {
|
||||
case "kompose.service.type":
|
||||
serviceConfig.ServiceType = handleServiceType(value)
|
||||
case "kompose.service.expose":
|
||||
serviceConfig.ExposeService = strings.ToLower(value)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,6 +39,7 @@ import (
|
||||
|
||||
deployapi "github.com/openshift/origin/pkg/deploy/api"
|
||||
imageapi "github.com/openshift/origin/pkg/image/api"
|
||||
routeapi "github.com/openshift/origin/pkg/route/api"
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -201,7 +202,6 @@ func PrintList(objects []runtime.Object, opt kobject.ConvertOptions) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch t := v.(type) {
|
||||
case *api.ReplicationController:
|
||||
file = transformer.Print(t.Name, dirName, strings.ToLower(t.Kind), data, opt.ToStdout, opt.GenerateYaml, f)
|
||||
@@ -219,6 +219,10 @@ func PrintList(objects []runtime.Object, opt kobject.ConvertOptions) error {
|
||||
file = transformer.Print(t.Name, dirName, strings.ToLower(t.Kind), data, opt.ToStdout, opt.GenerateYaml, f)
|
||||
case *api.Pod:
|
||||
file = transformer.Print(t.Name, dirName, strings.ToLower(t.Kind), data, opt.ToStdout, opt.GenerateYaml, f)
|
||||
case *routeapi.Route:
|
||||
file = transformer.Print(t.Name, dirName, strings.ToLower(t.Kind), data, opt.ToStdout, opt.GenerateYaml, f)
|
||||
case *extensions.Ingress:
|
||||
file = transformer.Print(t.Name, dirName, strings.ToLower(t.Kind), data, opt.ToStdout, opt.GenerateYaml, f)
|
||||
}
|
||||
files = append(files, file)
|
||||
}
|
||||
|
||||
@@ -201,6 +201,45 @@ func (k *Kubernetes) InitDS(name string, service kobject.ServiceConfig) *extensi
|
||||
return ds
|
||||
}
|
||||
|
||||
func (k *Kubernetes) initIngress(name string, service kobject.ServiceConfig, port int32) *extensions.Ingress {
|
||||
|
||||
ingress := &extensions.Ingress{
|
||||
TypeMeta: unversioned.TypeMeta{
|
||||
Kind: "Ingress",
|
||||
APIVersion: "extensions/v1beta1",
|
||||
},
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: name,
|
||||
},
|
||||
Spec: extensions.IngressSpec{
|
||||
Rules: []extensions.IngressRule{
|
||||
{
|
||||
IngressRuleValue: extensions.IngressRuleValue{
|
||||
HTTP: &extensions.HTTPIngressRuleValue{
|
||||
Paths: []extensions.HTTPIngressPath{
|
||||
{
|
||||
Backend: extensions.IngressBackend{
|
||||
ServiceName: name,
|
||||
ServicePort: intstr.IntOrString{
|
||||
IntVal: port,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if service.ExposeService != "true" {
|
||||
ingress.Spec.Rules[0].Host = service.ExposeService
|
||||
}
|
||||
|
||||
return ingress
|
||||
}
|
||||
|
||||
// Initialize PersistentVolumeClaim
|
||||
func (k *Kubernetes) CreatePVC(name string, mode string) *api.PersistentVolumeClaim {
|
||||
size, err := resource.ParseQuantity("100Mi")
|
||||
@@ -429,6 +468,10 @@ func (k *Kubernetes) Transform(komposeObject kobject.KomposeObject, opt kobject.
|
||||
if k.PortsExist(name, service) {
|
||||
svc := k.CreateService(name, service, objects)
|
||||
objects = append(objects, svc)
|
||||
|
||||
if service.ExposeService != "" {
|
||||
objects = append(objects, k.initIngress(name, service, svc.Spec.Ports[0].Port))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -527,6 +570,12 @@ func (k *Kubernetes) Deploy(komposeObject kobject.KomposeObject, opt kobject.Con
|
||||
return err
|
||||
}
|
||||
logrus.Infof("Successfully created PersistentVolumeClaim: %s", t.Name)
|
||||
case *extensions.Ingress:
|
||||
_, err := client.Ingress(namespace).Create(t)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Infof("Successfully created Ingress: %s", t.Name)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -585,8 +634,21 @@ func (k *Kubernetes) Undeploy(komposeObject kobject.KomposeObject, opt kobject.C
|
||||
} else {
|
||||
logrus.Infof("Successfully deleted PersistentVolumeClaim: %s", t.Name)
|
||||
}
|
||||
case *extensions.Ingress:
|
||||
// delete ingress
|
||||
ingDeleteOptions := &api.DeleteOptions{
|
||||
TypeMeta: unversioned.TypeMeta{
|
||||
Kind: "Ingress",
|
||||
APIVersion: "extensions/v1beta1",
|
||||
},
|
||||
}
|
||||
err = client.Ingress(namespace).Delete(t.Name, ingDeleteOptions)
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
logrus.Infof("Successfully deleted Ingress: %s", t.Name)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -197,6 +197,62 @@ func checkMeta(config kobject.ServiceConfig, meta api.ObjectMeta, expectedName s
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestKomposeConvertIngress(t *testing.T) {
|
||||
|
||||
testCases := map[string]struct {
|
||||
komposeObject kobject.KomposeObject
|
||||
opt kobject.ConvertOptions
|
||||
labelValue string
|
||||
}{
|
||||
"Convert to Ingress: label set to true": {newKomposeObject(), kobject.ConvertOptions{CreateD: true}, "true"},
|
||||
"Convert to Ingress: label set to example.com": {newKomposeObject(), kobject.ConvertOptions{CreateD: true}, "example.com"},
|
||||
}
|
||||
|
||||
for name, test := range testCases {
|
||||
|
||||
var expectedHost string
|
||||
|
||||
t.Log("Test case:", name)
|
||||
k := Kubernetes{}
|
||||
|
||||
appName := "app"
|
||||
|
||||
// Setting value for ExposeService in ServiceConfig
|
||||
config := test.komposeObject.ServiceConfigs[appName]
|
||||
config.ExposeService = test.labelValue
|
||||
test.komposeObject.ServiceConfigs[appName] = config
|
||||
|
||||
switch test.labelValue {
|
||||
case "true":
|
||||
expectedHost = ""
|
||||
default:
|
||||
expectedHost = test.labelValue
|
||||
}
|
||||
|
||||
// Run Transform
|
||||
objs := k.Transform(test.komposeObject, test.opt)
|
||||
|
||||
// Check results
|
||||
for _, obj := range objs {
|
||||
if ing, ok := obj.(*extensions.Ingress); ok {
|
||||
if ing.ObjectMeta.Name != appName {
|
||||
t.Errorf("Expected ObjectMeta.Name to be %s, got %s instead", appName, ing.ObjectMeta.Name)
|
||||
}
|
||||
if ing.Spec.Rules[0].IngressRuleValue.HTTP.Paths[0].Backend.ServiceName != appName {
|
||||
t.Errorf("Expected Backend.ServiceName to be %s, got %s instead", appName, ing.Spec.Rules[0].IngressRuleValue.HTTP.Paths[0].Backend.ServiceName)
|
||||
}
|
||||
if ing.Spec.Rules[0].IngressRuleValue.HTTP.Paths[0].Backend.ServicePort.IntVal != config.Port[0].HostPort {
|
||||
t.Errorf("Expected Backend.ServicePort to be %d, got %v instead", config.Port[0].HostPort, ing.Spec.Rules[0].IngressRuleValue.HTTP.Paths[0].Backend.ServicePort.IntVal)
|
||||
}
|
||||
if ing.Spec.Rules[0].Host != expectedHost {
|
||||
t.Errorf("Expected Rules[0].Host to be %s, got %s instead", expectedHost, ing.Spec.Rules[0].Host)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestKomposeConvert(t *testing.T) {
|
||||
replicas := 3
|
||||
testCases := map[string]struct {
|
||||
|
||||
@@ -38,7 +38,9 @@ import (
|
||||
deployapi "github.com/openshift/origin/pkg/deploy/api"
|
||||
deploymentconfigreaper "github.com/openshift/origin/pkg/deploy/cmd"
|
||||
imageapi "github.com/openshift/origin/pkg/image/api"
|
||||
routeapi "github.com/openshift/origin/pkg/route/api"
|
||||
"k8s.io/kubernetes/pkg/kubectl"
|
||||
"k8s.io/kubernetes/pkg/util/intstr"
|
||||
)
|
||||
|
||||
type OpenShift struct {
|
||||
@@ -156,6 +158,34 @@ func (o *OpenShift) initDeploymentConfig(name string, service kobject.ServiceCon
|
||||
return dc
|
||||
}
|
||||
|
||||
func (o *OpenShift) initRoute(name string, service kobject.ServiceConfig, port int32) *routeapi.Route {
|
||||
route := &routeapi.Route{
|
||||
TypeMeta: unversioned.TypeMeta{
|
||||
Kind: "Route",
|
||||
APIVersion: "v1",
|
||||
},
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Name: name,
|
||||
},
|
||||
Spec: routeapi.RouteSpec{
|
||||
Port: &routeapi.RoutePort{
|
||||
TargetPort: intstr.IntOrString{
|
||||
IntVal: port,
|
||||
},
|
||||
},
|
||||
To: routeapi.RouteTargetReference{
|
||||
Kind: "Service",
|
||||
Name: name,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if service.ExposeService != "true" {
|
||||
route.Spec.Host = service.ExposeService
|
||||
}
|
||||
return route
|
||||
}
|
||||
|
||||
// Transform maps komposeObject to openshift objects
|
||||
// returns objects that are already sorted in the way that Services are first
|
||||
func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.ConvertOptions) []runtime.Object {
|
||||
@@ -186,6 +216,10 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C
|
||||
if o.PortsExist(name, service) {
|
||||
svc := o.CreateService(name, service, objects)
|
||||
objects = append(objects, svc)
|
||||
|
||||
if service.ExposeService != "" {
|
||||
objects = append(objects, o.initRoute(name, service, svc.Spec.Ports[0].Port))
|
||||
}
|
||||
}
|
||||
}
|
||||
o.UpdateKubernetesObjects(name, service, &objects)
|
||||
@@ -257,6 +291,12 @@ func (o *OpenShift) Deploy(komposeObject kobject.KomposeObject, opt kobject.Conv
|
||||
return err
|
||||
}
|
||||
logrus.Infof("Successfully created PersistentVolumeClaim: %s", t.Name)
|
||||
case *routeapi.Route:
|
||||
_, err := oclient.Routes(namespace).Create(t)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Infof("Successfully created Route: %s", t.Name)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -323,6 +363,14 @@ func (o *OpenShift) Undeploy(komposeObject kobject.KomposeObject, opt kobject.Co
|
||||
} else {
|
||||
logrus.Infof("Successfully deleted PersistentVolumeClaim: %s", t.Name)
|
||||
}
|
||||
case *routeapi.Route:
|
||||
// delete route
|
||||
err = oclient.Routes(namespace).Delete(t.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
logrus.Infof("Successfully deleted Route: %s", t.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -61,3 +61,34 @@ func TestInitDeploymentConfig(t *testing.T) {
|
||||
t.Errorf("Expected myfoobarname for name, actual %s", spec.Spec.Triggers[1].ImageChangeParams.ContainerNames[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestKomposeConvertRoute(t *testing.T) {
|
||||
|
||||
o := OpenShift{}
|
||||
name := "app"
|
||||
sc := newServiceConfig()
|
||||
sc.ExposeService = "true"
|
||||
var port int32 = 5555
|
||||
route := o.initRoute(name, sc, port)
|
||||
|
||||
if route.ObjectMeta.Name != name {
|
||||
t.Errorf("Expected %s for name, actual %s", name, route.ObjectMeta.Name)
|
||||
}
|
||||
if route.Spec.To.Name != name {
|
||||
t.Errorf("Expected %s for name, actual %s", name, route.Spec.To.Name)
|
||||
}
|
||||
if route.Spec.Port.TargetPort.IntVal != port {
|
||||
t.Errorf("Expected %d for port, actual %d", port, route.Spec.Port.TargetPort.IntVal)
|
||||
}
|
||||
if route.Spec.Host != "" {
|
||||
t.Errorf("Expected Spec.Host to not be set, got %s instead", route.Spec.Host)
|
||||
}
|
||||
|
||||
sc.ExposeService = "example.com"
|
||||
route = o.initRoute(name, sc, port)
|
||||
|
||||
if route.Spec.Host != sc.ExposeService {
|
||||
t.Errorf("Expected %s for Spec.Host, actual %s", sc.ExposeService, route.Spec.Host)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user