Propagate dots into valid service names (#1063)

* normalize '.' (dot) to '-' (dash) in service names

* added warning about the dot character replacements in service names

* fix gofmt compliance

* also normalize container names (need lowercase)

* splitted normalization for volumes and service names into different functions (it conflicted with './' volumes replacement)

* changed volume normalization to use the new own normalization function

* changed container normalization to use its new own normalization function

* updated as per discussed in PR #1063 review
This commit is contained in:
Roberto C. Morano 2018-09-28 14:20:01 +02:00 committed by Hang Yan
parent a0cc04499b
commit 424f1a9a2c
4 changed files with 15 additions and 5 deletions

View File

@ -429,7 +429,7 @@ services:
If the Docker Compose file has a volume specified for a service, the Deployment (Kubernetes) or DeploymentConfig (OpenShift) strategy is changed to "Recreate" instead of "RollingUpdate" (default). This is done to avoid multiple instances of a service from accessing a volume at the same time.
If the Docker Compose file has service name with `_` in it (eg.`web_service`), then it will be replaced by `-` and the service name will be renamed accordingly (eg.`web-service`). Kompose does this because "Kubernetes" doesn't allow `_` in object name.
If the Docker Compose file has service name with `_` or `.` in it (eg.`web_service` or `web.service`), then it will be replaced by `-` and the service name will be renamed accordingly (eg.`web-service`). Kompose does this because "Kubernetes" doesn't allow `_` in object name.
Please note that changing service name might break some `docker-compose` files.

View File

@ -19,6 +19,7 @@ package compose
import (
"os"
"path/filepath"
"regexp"
"strings"
"github.com/kubernetes/kompose/pkg/kobject"
@ -112,6 +113,15 @@ func handleServiceType(ServiceType string) (string, error) {
}
}
func normalizeContainerNames(svcName string) string {
return strings.ToLower(svcName)
}
func normalizeServiceNames(svcName string) string {
re := regexp.MustCompile("[._]")
return strings.ToLower(re.ReplaceAllString(svcName, "-"))
}
func normalizeVolumes(svcName string) string {
return strings.Replace(svcName, "_", "-", -1)
}

View File

@ -193,7 +193,7 @@ func libComposeToKomposeMapping(composeObject *project.Project) (kobject.Kompose
serviceConfig := kobject.ServiceConfig{}
serviceConfig.Image = composeServiceConfig.Image
serviceConfig.Build = composeServiceConfig.Build.Context
newName := normalizeServiceNames(composeServiceConfig.ContainerName)
newName := normalizeContainerNames(composeServiceConfig.ContainerName)
serviceConfig.ContainerName = newName
if newName != composeServiceConfig.ContainerName {
log.Infof("Container name in service %q has been changed from %q to %q", name, composeServiceConfig.ContainerName, newName)
@ -224,7 +224,7 @@ func libComposeToKomposeMapping(composeObject *project.Project) (kobject.Kompose
if composeServiceConfig.Volumes != nil {
for _, volume := range composeServiceConfig.Volumes.Volumes {
v := normalizeServiceNames(volume.String())
v := normalizeVolumes(volume.String())
serviceConfig.VolList = append(serviceConfig.VolList, v)
}
}

View File

@ -160,7 +160,7 @@ func loadV3Volumes(volumes []types.ServiceVolumeConfig) []string {
for _, vol := range volumes {
// There will *always* be Source when parsing
v := normalizeServiceNames(vol.Source)
v := normalizeVolumes(vol.Source)
if vol.Target != "" {
v = v + ":" + vol.Target
@ -270,7 +270,7 @@ func dockerComposeToKomposeMapping(composeObject *types.Config) (kobject.Kompose
serviceConfig.Stdin = composeServiceConfig.StdinOpen
serviceConfig.Tty = composeServiceConfig.Tty
serviceConfig.TmpFs = composeServiceConfig.Tmpfs
serviceConfig.ContainerName = composeServiceConfig.ContainerName
serviceConfig.ContainerName = normalizeContainerNames(composeServiceConfig.ContainerName)
serviceConfig.Command = composeServiceConfig.Entrypoint
serviceConfig.Args = composeServiceConfig.Command
serviceConfig.Labels = composeServiceConfig.Labels