|
|
@@ -25,6 +25,7 @@ import (
|
|
|
"time"
|
|
|
|
|
|
"github.com/compose-spec/compose-go/types"
|
|
|
+ "github.com/docker/compose-cli/api/compose"
|
|
|
apps "k8s.io/api/apps/v1"
|
|
|
core "k8s.io/api/core/v1"
|
|
|
resource "k8s.io/apimachinery/pkg/api/resource"
|
|
|
@@ -33,6 +34,10 @@ import (
|
|
|
"k8s.io/apimachinery/pkg/util/intstr"
|
|
|
)
|
|
|
|
|
|
+const (
|
|
|
+ clusterIPHeadless = "None"
|
|
|
+)
|
|
|
+
|
|
|
//MapToKubernetesObjects maps compose project to Kubernetes objects
|
|
|
func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object, error) {
|
|
|
objects := map[string]runtime.Object{}
|
|
|
@@ -46,13 +51,13 @@ func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object,
|
|
|
}
|
|
|
|
|
|
if service.Deploy != nil && service.Deploy.Mode == "global" {
|
|
|
- daemonset, err := mapToDaemonset(project, service, project.Name)
|
|
|
+ daemonset, err := mapToDaemonset(project, service)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
objects[fmt.Sprintf("%s-daemonset.yaml", service.Name)] = daemonset
|
|
|
} else {
|
|
|
- deployment, err := mapToDeployment(project, service, project.Name)
|
|
|
+ deployment, err := mapToDeployment(project, service)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
@@ -61,7 +66,7 @@ func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object,
|
|
|
for _, vol := range service.Volumes {
|
|
|
if vol.Type == "volume" {
|
|
|
vol.Source = strings.ReplaceAll(vol.Source, "_", "-")
|
|
|
- objects[fmt.Sprintf("%s-persistentvolumeclaim.yaml", vol.Source)] = mapToPVC(service, vol)
|
|
|
+ objects[fmt.Sprintf("%s-persistentvolumeclaim.yaml", vol.Source)] = mapToPVC(project, service, vol)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -70,7 +75,12 @@ func MapToKubernetesObjects(project *types.Project) (map[string]runtime.Object,
|
|
|
|
|
|
func mapToService(project *types.Project, service types.ServiceConfig) *core.Service {
|
|
|
ports := []core.ServicePort{}
|
|
|
+ serviceType := core.ServiceTypeClusterIP
|
|
|
+ clusterIP := ""
|
|
|
for _, p := range service.Ports {
|
|
|
+ if p.Published != 0 {
|
|
|
+ serviceType = core.ServiceTypeLoadBalancer
|
|
|
+ }
|
|
|
ports = append(ports,
|
|
|
core.ServicePort{
|
|
|
Name: fmt.Sprintf("%d-%s", p.Target, strings.ToLower(p.Protocol)),
|
|
|
@@ -79,8 +89,8 @@ func mapToService(project *types.Project, service types.ServiceConfig) *core.Ser
|
|
|
Protocol: toProtocol(p.Protocol),
|
|
|
})
|
|
|
}
|
|
|
- if len(ports) == 0 {
|
|
|
- return nil
|
|
|
+ if len(ports) == 0 { // headless service
|
|
|
+ clusterIP = clusterIPHeadless
|
|
|
}
|
|
|
return &core.Service{
|
|
|
TypeMeta: meta.TypeMeta{
|
|
|
@@ -91,46 +101,25 @@ func mapToService(project *types.Project, service types.ServiceConfig) *core.Ser
|
|
|
Name: service.Name,
|
|
|
},
|
|
|
Spec: core.ServiceSpec{
|
|
|
- Selector: map[string]string{"com.docker.compose.service": service.Name},
|
|
|
- Ports: ports,
|
|
|
- Type: mapServiceToServiceType(project, service),
|
|
|
+ ClusterIP: clusterIP,
|
|
|
+ Selector: selectorLabels(project.Name, service.Name),
|
|
|
+ Ports: ports,
|
|
|
+ Type: serviceType,
|
|
|
},
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func mapServiceToServiceType(project *types.Project, service types.ServiceConfig) core.ServiceType {
|
|
|
- serviceType := core.ServiceTypeClusterIP
|
|
|
- if len(service.Networks) == 0 {
|
|
|
- // service is implicitly attached to "default" network
|
|
|
- serviceType = core.ServiceTypeLoadBalancer
|
|
|
- }
|
|
|
- for name := range service.Networks {
|
|
|
- if !project.Networks[name].Internal {
|
|
|
- serviceType = core.ServiceTypeLoadBalancer
|
|
|
- }
|
|
|
- }
|
|
|
- for _, port := range service.Ports {
|
|
|
- if port.Published != 0 {
|
|
|
- serviceType = core.ServiceTypeNodePort
|
|
|
- }
|
|
|
- }
|
|
|
- return serviceType
|
|
|
-}
|
|
|
-
|
|
|
-func mapToDeployment(project *types.Project, service types.ServiceConfig, name string) (*apps.Deployment, error) {
|
|
|
- labels := map[string]string{
|
|
|
- "com.docker.compose.service": service.Name,
|
|
|
- "com.docker.compose.project": name,
|
|
|
- }
|
|
|
- podTemplate, err := toPodTemplate(project, service, labels)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
+func mapToDeployment(project *types.Project, service types.ServiceConfig) (*apps.Deployment, error) {
|
|
|
+ labels := selectorLabels(project.Name, service.Name)
|
|
|
selector := new(meta.LabelSelector)
|
|
|
selector.MatchLabels = make(map[string]string)
|
|
|
for key, val := range labels {
|
|
|
selector.MatchLabels[key] = val
|
|
|
}
|
|
|
+ podTemplate, err := toPodTemplate(project, service, labels)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
return &apps.Deployment{
|
|
|
TypeMeta: meta.TypeMeta{
|
|
|
Kind: "Deployment",
|
|
|
@@ -149,11 +138,15 @@ func mapToDeployment(project *types.Project, service types.ServiceConfig, name s
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
-func mapToDaemonset(project *types.Project, service types.ServiceConfig, name string) (*apps.DaemonSet, error) {
|
|
|
- labels := map[string]string{
|
|
|
- "com.docker.compose.service": service.Name,
|
|
|
- "com.docker.compose.project": name,
|
|
|
+func selectorLabels(projectName string, serviceName string) map[string]string {
|
|
|
+ return map[string]string{
|
|
|
+ compose.ProjectTag: projectName,
|
|
|
+ compose.ServiceTag: serviceName,
|
|
|
}
|
|
|
+}
|
|
|
+
|
|
|
+func mapToDaemonset(project *types.Project, service types.ServiceConfig) (*apps.DaemonSet, error) {
|
|
|
+ labels := selectorLabels(project.Name, service.Name)
|
|
|
podTemplate, err := toPodTemplate(project, service, labels)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
@@ -196,7 +189,7 @@ func toDeploymentStrategy(deploy *types.DeployConfig) apps.DeploymentStrategy {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func mapToPVC(service types.ServiceConfig, vol types.ServiceVolumeConfig) runtime.Object {
|
|
|
+func mapToPVC(project *types.Project, service types.ServiceConfig, vol types.ServiceVolumeConfig) runtime.Object {
|
|
|
rwaccess := core.ReadWriteOnce
|
|
|
if vol.ReadOnly {
|
|
|
rwaccess = core.ReadOnlyMany
|
|
|
@@ -208,7 +201,7 @@ func mapToPVC(service types.ServiceConfig, vol types.ServiceVolumeConfig) runtim
|
|
|
},
|
|
|
ObjectMeta: meta.ObjectMeta{
|
|
|
Name: vol.Source,
|
|
|
- Labels: map[string]string{"com.docker.compose.service": service.Name},
|
|
|
+ Labels: selectorLabels(project.Name, service.Name),
|
|
|
},
|
|
|
Spec: core.PersistentVolumeClaimSpec{
|
|
|
VolumeName: vol.Source,
|