forked from LaconicNetwork/kompose
Add headless service type label
Also remove the relation between restart and service create
This commit is contained in:
@@ -33,6 +33,9 @@ const (
|
||||
LabelServiceExpose = "kompose.service.expose"
|
||||
// LabelServiceExposeTLSSecret provides the name of the TLS secret to use with the Kubernetes ingress controller
|
||||
LabelServiceExposeTLSSecret = "kompose.service.expose.tls-secret"
|
||||
|
||||
// ServiceTypeHeadless...
|
||||
ServiceTypeHeadless = "Headless"
|
||||
)
|
||||
|
||||
// load environment variables from compose file
|
||||
@@ -100,8 +103,10 @@ func handleServiceType(ServiceType string) (string, error) {
|
||||
return string(api.ServiceTypeNodePort), nil
|
||||
case "loadbalancer":
|
||||
return string(api.ServiceTypeLoadBalancer), nil
|
||||
case "headless":
|
||||
return ServiceTypeHeadless, nil
|
||||
default:
|
||||
return "", errors.New("Unknown value " + ServiceType + " , supported values are 'NodePort, ClusterIP or LoadBalancer'")
|
||||
return "", errors.New("Unknown value " + ServiceType + " , supported values are 'nodeport, clusterip, headless or loadbalancer'")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -301,7 +301,12 @@ func (k *Kubernetes) CreateService(name string, service kobject.ServiceConfig, o
|
||||
servicePorts := k.ConfigServicePorts(name, service)
|
||||
svc.Spec.Ports = servicePorts
|
||||
|
||||
svc.Spec.Type = api.ServiceType(service.ServiceType)
|
||||
if service.ServiceType == "Headless" {
|
||||
svc.Spec.Type = api.ServiceTypeClusterIP
|
||||
svc.Spec.ClusterIP = "None"
|
||||
} else {
|
||||
svc.Spec.Type = api.ServiceType(service.ServiceType)
|
||||
}
|
||||
|
||||
// Configure annotations
|
||||
annotations := transformer.ConfigAnnotations(service)
|
||||
@@ -311,7 +316,7 @@ func (k *Kubernetes) CreateService(name string, service kobject.ServiceConfig, o
|
||||
}
|
||||
|
||||
// CreateHeadlessService creates a k8s headless service.
|
||||
// Thi is used for docker-compose services without ports. For such services we can't create regular Kubernetes Service.
|
||||
// This is used for docker-compose services without ports. For such services we can't create regular Kubernetes Service.
|
||||
// and without Service Pods can't find each other using DNS names.
|
||||
// Instead of regular Kubernetes Service we create Headless Service. DNS of such service points directly to Pod IP address.
|
||||
// You can find more about Headless Services in Kubernetes documentation https://kubernetes.io/docs/user-guide/services/#headless-services
|
||||
|
||||
@@ -364,6 +364,7 @@ func TestServiceWithoutPort(t *testing.T) {
|
||||
service := kobject.ServiceConfig{
|
||||
ContainerName: "name",
|
||||
Image: "image",
|
||||
ServiceType: "Headless",
|
||||
}
|
||||
|
||||
komposeObject := kobject.KomposeObject{
|
||||
|
||||
@@ -725,15 +725,17 @@ func (k *Kubernetes) Transform(komposeObject kobject.KomposeObject, opt kobject.
|
||||
objects = append(objects, pod)
|
||||
} else {
|
||||
objects = k.CreateKubernetesObjects(name, service, opt)
|
||||
// If ports not provided in configuration we will not make service
|
||||
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))
|
||||
}
|
||||
} else {
|
||||
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))
|
||||
}
|
||||
} else {
|
||||
if service.ServiceType == "Headless" {
|
||||
svc := k.CreateHeadlessService(name, service, objects)
|
||||
objects = append(objects, svc)
|
||||
}
|
||||
|
||||
@@ -384,18 +384,18 @@ func (o *OpenShift) Transform(komposeObject kobject.KomposeObject, opt kobject.C
|
||||
log.Infof("Buildconfig using %s::%s as source.", buildRepo, buildBranch)
|
||||
}
|
||||
|
||||
// If ports not provided in configuration we will not make service
|
||||
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))
|
||||
}
|
||||
} else {
|
||||
svc := o.CreateHeadlessService(name, service, objects)
|
||||
objects = append(objects, svc)
|
||||
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))
|
||||
}
|
||||
} else if service.ServiceType == "Headless" {
|
||||
svc := o.CreateHeadlessService(name, service, objects)
|
||||
objects = append(objects, svc)
|
||||
}
|
||||
|
||||
err := o.UpdateKubernetesObjects(name, service, opt, &objects)
|
||||
|
||||
@@ -347,11 +347,12 @@ func TestInitBuildConfig(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestServiceWithoutPort this tests if Headless Service is created for services without Port.
|
||||
// TestServiceWithoutPort this tests if Headless Service is created for services without Port (with label)
|
||||
func TestServiceWithoutPort(t *testing.T) {
|
||||
service := kobject.ServiceConfig{
|
||||
ContainerName: "name",
|
||||
Image: "image",
|
||||
ServiceType: "Headless",
|
||||
}
|
||||
|
||||
komposeObject := kobject.KomposeObject{
|
||||
|
||||
Reference in New Issue
Block a user