Remove duplication of name mangling.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2016-11-03 17:50:03 -06:00
parent d89cb4c62f
commit ef845be6a5
2 changed files with 27 additions and 22 deletions

View File

@ -46,3 +46,11 @@ func getNetworks(
ctx, ctx,
types.NetworkListOptions{Filters: getStackFilter(namespace)}) types.NetworkListOptions{Filters: getStackFilter(namespace)})
} }
type namespace struct {
name string
}
func (n namespace) scope(name string) string {
return n.name + "_" + name
}

View File

@ -84,10 +84,11 @@ func runDeploy(dockerCli *command.DockerCli, opts deployOptions) error {
} }
ctx := context.Background() ctx := context.Background()
if err := createNetworks(ctx, dockerCli, config.Networks, opts.namespace); err != nil { namespace := namespace{name: opts.namespace}
if err := createNetworks(ctx, dockerCli, config.Networks, namespace); err != nil {
return err return err
} }
return deployServices(ctx, dockerCli, config, opts.namespace, opts.sendRegistryAuth) return deployServices(ctx, dockerCli, config, namespace, opts.sendRegistryAuth)
} }
func propertyWarnings(properties map[string]string) string { func propertyWarnings(properties map[string]string) string {
@ -136,11 +137,11 @@ func createNetworks(
ctx context.Context, ctx context.Context,
dockerCli *command.DockerCli, dockerCli *command.DockerCli,
networks map[string]composetypes.NetworkConfig, networks map[string]composetypes.NetworkConfig,
namespace string, namespace namespace,
) error { ) error {
client := dockerCli.Client() client := dockerCli.Client()
existingNetworks, err := getNetworks(ctx, client, namespace) existingNetworks, err := getNetworks(ctx, client, namespace.name)
if err != nil { if err != nil {
return err return err
} }
@ -158,13 +159,13 @@ func createNetworks(
continue continue
} }
name := fmt.Sprintf("%s_%s", namespace, internalName) name := namespace.scope(internalName)
if _, exists := existingNetworkMap[name]; exists { if _, exists := existingNetworkMap[name]; exists {
continue continue
} }
createOpts := types.NetworkCreate{ createOpts := types.NetworkCreate{
Labels: getStackLabels(namespace, network.Labels), Labels: getStackLabels(namespace.name, network.Labels),
Driver: network.Driver, Driver: network.Driver,
Options: network.DriverOpts, Options: network.DriverOpts,
} }
@ -190,14 +191,13 @@ func createNetworks(
func convertNetworks( func convertNetworks(
networks map[string]*composetypes.ServiceNetworkConfig, networks map[string]*composetypes.ServiceNetworkConfig,
namespace string, namespace namespace,
name string, name string,
) []swarm.NetworkAttachmentConfig { ) []swarm.NetworkAttachmentConfig {
if len(networks) == 0 { if len(networks) == 0 {
return []swarm.NetworkAttachmentConfig{ return []swarm.NetworkAttachmentConfig{
{ {
// TODO: only do this name mangling in one function Target: namespace.scope("default"),
Target: namespace + "_" + "default",
Aliases: []string{name}, Aliases: []string{name},
}, },
} }
@ -206,8 +206,7 @@ func convertNetworks(
nets := []swarm.NetworkAttachmentConfig{} nets := []swarm.NetworkAttachmentConfig{}
for networkName, network := range networks { for networkName, network := range networks {
nets = append(nets, swarm.NetworkAttachmentConfig{ nets = append(nets, swarm.NetworkAttachmentConfig{
// TODO: only do this name mangling in one function Target: namespace.scope(networkName),
Target: namespace + "_" + networkName,
Aliases: append(network.Aliases, name), Aliases: append(network.Aliases, name),
}) })
} }
@ -217,7 +216,7 @@ func convertNetworks(
func convertVolumes( func convertVolumes(
serviceVolumes []string, serviceVolumes []string,
stackVolumes map[string]composetypes.VolumeConfig, stackVolumes map[string]composetypes.VolumeConfig,
namespace string, namespace namespace,
) ([]mount.Mount, error) { ) ([]mount.Mount, error) {
var mounts []mount.Mount var mounts []mount.Mount
@ -271,8 +270,7 @@ func convertVolumes(
} }
} }
// TODO: remove this duplication source = namespace.scope(source)
source = fmt.Sprintf("%s_%s", namespace, source)
} }
} }
@ -292,7 +290,7 @@ func deployServices(
ctx context.Context, ctx context.Context,
dockerCli *command.DockerCli, dockerCli *command.DockerCli,
config *composetypes.Config, config *composetypes.Config,
namespace string, namespace namespace,
sendAuth bool, sendAuth bool,
) error { ) error {
apiClient := dockerCli.Client() apiClient := dockerCli.Client()
@ -300,7 +298,7 @@ func deployServices(
services := config.Services services := config.Services
volumes := config.Volumes volumes := config.Volumes
existingServices, err := getServices(ctx, apiClient, namespace) existingServices, err := getServices(ctx, apiClient, namespace.name)
if err != nil { if err != nil {
return err return err
} }
@ -311,7 +309,7 @@ func deployServices(
} }
for _, service := range services { for _, service := range services {
name := fmt.Sprintf("%s_%s", namespace, service.Name) name := namespace.scope(service.Name)
serviceSpec, err := convertService(namespace, service, volumes) serviceSpec, err := convertService(namespace, service, volumes)
if err != nil { if err != nil {
@ -361,12 +359,11 @@ func deployServices(
} }
func convertService( func convertService(
namespace string, namespace namespace,
service composetypes.ServiceConfig, service composetypes.ServiceConfig,
volumes map[string]composetypes.VolumeConfig, volumes map[string]composetypes.VolumeConfig,
) (swarm.ServiceSpec, error) { ) (swarm.ServiceSpec, error) {
// TODO: remove this duplication name := namespace.scope(service.Name)
name := fmt.Sprintf("%s_%s", namespace, service.Name)
endpoint, err := convertEndpointSpec(service.Ports) endpoint, err := convertEndpointSpec(service.Ports)
if err != nil { if err != nil {
@ -397,7 +394,7 @@ func convertService(
serviceSpec := swarm.ServiceSpec{ serviceSpec := swarm.ServiceSpec{
Annotations: swarm.Annotations{ Annotations: swarm.Annotations{
Name: name, Name: name,
Labels: getStackLabels(namespace, service.Deploy.Labels), Labels: getStackLabels(namespace.name, service.Deploy.Labels),
}, },
TaskTemplate: swarm.TaskSpec{ TaskTemplate: swarm.TaskSpec{
ContainerSpec: swarm.ContainerSpec{ ContainerSpec: swarm.ContainerSpec{
@ -406,7 +403,7 @@ func convertService(
Args: service.Command, Args: service.Command,
Hostname: service.Hostname, Hostname: service.Hostname,
Env: convertEnvironment(service.Environment), Env: convertEnvironment(service.Environment),
Labels: getStackLabels(namespace, service.Labels), Labels: getStackLabels(namespace.name, service.Labels),
Dir: service.WorkingDir, Dir: service.WorkingDir,
User: service.User, User: service.User,
Mounts: mounts, Mounts: mounts,