Signed-off-by: Daniel Nephin <dnephin@docker.com>
| ... | ... |
@@ -84,10 +84,11 @@ func runDeploy(dockerCli *command.DockerCli, opts deployOptions) error {
|
| 84 | 84 |
} |
| 85 | 85 |
|
| 86 | 86 |
ctx := context.Background() |
| 87 |
- if err := createNetworks(ctx, dockerCli, config.Networks, opts.namespace); err != nil {
|
|
| 87 |
+ namespace := namespace{name: opts.namespace}
|
|
| 88 |
+ if err := createNetworks(ctx, dockerCli, config.Networks, namespace); err != nil {
|
|
| 88 | 89 |
return err |
| 89 | 90 |
} |
| 90 |
- return deployServices(ctx, dockerCli, config, opts.namespace, opts.sendRegistryAuth) |
|
| 91 |
+ return deployServices(ctx, dockerCli, config, namespace, opts.sendRegistryAuth) |
|
| 91 | 92 |
} |
| 92 | 93 |
|
| 93 | 94 |
func propertyWarnings(properties map[string]string) string {
|
| ... | ... |
@@ -136,11 +137,11 @@ func createNetworks( |
| 136 | 136 |
ctx context.Context, |
| 137 | 137 |
dockerCli *command.DockerCli, |
| 138 | 138 |
networks map[string]composetypes.NetworkConfig, |
| 139 |
- namespace string, |
|
| 139 |
+ namespace namespace, |
|
| 140 | 140 |
) error {
|
| 141 | 141 |
client := dockerCli.Client() |
| 142 | 142 |
|
| 143 |
- existingNetworks, err := getNetworks(ctx, client, namespace) |
|
| 143 |
+ existingNetworks, err := getNetworks(ctx, client, namespace.name) |
|
| 144 | 144 |
if err != nil {
|
| 145 | 145 |
return err |
| 146 | 146 |
} |
| ... | ... |
@@ -158,13 +159,13 @@ func createNetworks( |
| 158 | 158 |
continue |
| 159 | 159 |
} |
| 160 | 160 |
|
| 161 |
- name := fmt.Sprintf("%s_%s", namespace, internalName)
|
|
| 161 |
+ name := namespace.scope(internalName) |
|
| 162 | 162 |
if _, exists := existingNetworkMap[name]; exists {
|
| 163 | 163 |
continue |
| 164 | 164 |
} |
| 165 | 165 |
|
| 166 | 166 |
createOpts := types.NetworkCreate{
|
| 167 |
- Labels: getStackLabels(namespace, network.Labels), |
|
| 167 |
+ Labels: getStackLabels(namespace.name, network.Labels), |
|
| 168 | 168 |
Driver: network.Driver, |
| 169 | 169 |
Options: network.DriverOpts, |
| 170 | 170 |
} |
| ... | ... |
@@ -190,14 +191,13 @@ func createNetworks( |
| 190 | 190 |
|
| 191 | 191 |
func convertNetworks( |
| 192 | 192 |
networks map[string]*composetypes.ServiceNetworkConfig, |
| 193 |
- namespace string, |
|
| 193 |
+ namespace namespace, |
|
| 194 | 194 |
name string, |
| 195 | 195 |
) []swarm.NetworkAttachmentConfig {
|
| 196 | 196 |
if len(networks) == 0 {
|
| 197 | 197 |
return []swarm.NetworkAttachmentConfig{
|
| 198 | 198 |
{
|
| 199 |
- // TODO: only do this name mangling in one function |
|
| 200 |
- Target: namespace + "_" + "default", |
|
| 199 |
+ Target: namespace.scope("default"),
|
|
| 201 | 200 |
Aliases: []string{name},
|
| 202 | 201 |
}, |
| 203 | 202 |
} |
| ... | ... |
@@ -206,8 +206,7 @@ func convertNetworks( |
| 206 | 206 |
nets := []swarm.NetworkAttachmentConfig{}
|
| 207 | 207 |
for networkName, network := range networks {
|
| 208 | 208 |
nets = append(nets, swarm.NetworkAttachmentConfig{
|
| 209 |
- // TODO: only do this name mangling in one function |
|
| 210 |
- Target: namespace + "_" + networkName, |
|
| 209 |
+ Target: namespace.scope(networkName), |
|
| 211 | 210 |
Aliases: append(network.Aliases, name), |
| 212 | 211 |
}) |
| 213 | 212 |
} |
| ... | ... |
@@ -217,7 +216,7 @@ func convertNetworks( |
| 217 | 217 |
func convertVolumes( |
| 218 | 218 |
serviceVolumes []string, |
| 219 | 219 |
stackVolumes map[string]composetypes.VolumeConfig, |
| 220 |
- namespace string, |
|
| 220 |
+ namespace namespace, |
|
| 221 | 221 |
) ([]mount.Mount, error) {
|
| 222 | 222 |
var mounts []mount.Mount |
| 223 | 223 |
|
| ... | ... |
@@ -271,8 +270,7 @@ func convertVolumes( |
| 271 | 271 |
} |
| 272 | 272 |
} |
| 273 | 273 |
|
| 274 |
- // TODO: remove this duplication |
|
| 275 |
- source = fmt.Sprintf("%s_%s", namespace, source)
|
|
| 274 |
+ source = namespace.scope(source) |
|
| 276 | 275 |
} |
| 277 | 276 |
} |
| 278 | 277 |
|
| ... | ... |
@@ -292,7 +290,7 @@ func deployServices( |
| 292 | 292 |
ctx context.Context, |
| 293 | 293 |
dockerCli *command.DockerCli, |
| 294 | 294 |
config *composetypes.Config, |
| 295 |
- namespace string, |
|
| 295 |
+ namespace namespace, |
|
| 296 | 296 |
sendAuth bool, |
| 297 | 297 |
) error {
|
| 298 | 298 |
apiClient := dockerCli.Client() |
| ... | ... |
@@ -300,7 +298,7 @@ func deployServices( |
| 300 | 300 |
services := config.Services |
| 301 | 301 |
volumes := config.Volumes |
| 302 | 302 |
|
| 303 |
- existingServices, err := getServices(ctx, apiClient, namespace) |
|
| 303 |
+ existingServices, err := getServices(ctx, apiClient, namespace.name) |
|
| 304 | 304 |
if err != nil {
|
| 305 | 305 |
return err |
| 306 | 306 |
} |
| ... | ... |
@@ -311,7 +309,7 @@ func deployServices( |
| 311 | 311 |
} |
| 312 | 312 |
|
| 313 | 313 |
for _, service := range services {
|
| 314 |
- name := fmt.Sprintf("%s_%s", namespace, service.Name)
|
|
| 314 |
+ name := namespace.scope(service.Name) |
|
| 315 | 315 |
|
| 316 | 316 |
serviceSpec, err := convertService(namespace, service, volumes) |
| 317 | 317 |
if err != nil {
|
| ... | ... |
@@ -361,12 +359,11 @@ func deployServices( |
| 361 | 361 |
} |
| 362 | 362 |
|
| 363 | 363 |
func convertService( |
| 364 |
- namespace string, |
|
| 364 |
+ namespace namespace, |
|
| 365 | 365 |
service composetypes.ServiceConfig, |
| 366 | 366 |
volumes map[string]composetypes.VolumeConfig, |
| 367 | 367 |
) (swarm.ServiceSpec, error) {
|
| 368 |
- // TODO: remove this duplication |
|
| 369 |
- name := fmt.Sprintf("%s_%s", namespace, service.Name)
|
|
| 368 |
+ name := namespace.scope(service.Name) |
|
| 370 | 369 |
|
| 371 | 370 |
endpoint, err := convertEndpointSpec(service.Ports) |
| 372 | 371 |
if err != nil {
|
| ... | ... |
@@ -397,7 +394,7 @@ func convertService( |
| 397 | 397 |
serviceSpec := swarm.ServiceSpec{
|
| 398 | 398 |
Annotations: swarm.Annotations{
|
| 399 | 399 |
Name: name, |
| 400 |
- Labels: getStackLabels(namespace, service.Deploy.Labels), |
|
| 400 |
+ Labels: getStackLabels(namespace.name, service.Deploy.Labels), |
|
| 401 | 401 |
}, |
| 402 | 402 |
TaskTemplate: swarm.TaskSpec{
|
| 403 | 403 |
ContainerSpec: swarm.ContainerSpec{
|
| ... | ... |
@@ -406,7 +403,7 @@ func convertService( |
| 406 | 406 |
Args: service.Command, |
| 407 | 407 |
Hostname: service.Hostname, |
| 408 | 408 |
Env: convertEnvironment(service.Environment), |
| 409 |
- Labels: getStackLabels(namespace, service.Labels), |
|
| 409 |
+ Labels: getStackLabels(namespace.name, service.Labels), |
|
| 410 | 410 |
Dir: service.WorkingDir, |
| 411 | 411 |
User: service.User, |
| 412 | 412 |
Mounts: mounts, |