forked from LaconicNetwork/kompose
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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user