Added service name to dockerData struct.

In Swarm mode with with Docker Swarm’s Load Balancer disabled (traefik.backend.loadbalancer.swarm=false)
service name will be the name of the docker service and name will be the container task name
(e.g. whoami0.1).  When generating backend and fronted rules, we will use service name instead of name if a
rule is not provided.

Initialize dockerData.ServiceName to dockerData.Name to support non-swarm mode.
This commit is contained in:
Mike Foley 2017-02-02 20:18:12 -05:00 committed by Emile Vauge
parent d2b47a5681
commit e0a4c58081
No known key found for this signature in database
GPG key ID: D808B4C167352E59
2 changed files with 6 additions and 6 deletions

View file

@ -120,16 +120,12 @@ docker-machine ssh manager "docker service create \
--name whoami0 \ --name whoami0 \
--label traefik.port=80 \ --label traefik.port=80 \
--network traefik-net \ --network traefik-net \
--label traefik.frontend.rule=Host:whoami0.traefik \
--label traefik.backend=whoami0 \
emilevauge/whoami" emilevauge/whoami"
docker-machine ssh manager "docker service create \ docker-machine ssh manager "docker service create \
--name whoami1 \ --name whoami1 \
--label traefik.port=80 \ --label traefik.port=80 \
--network traefik-net \ --network traefik-net \
--label traefik.frontend.rule=Host:whoam1.traefik \
--label traefik.backend=whoami1 \
--label traefik.backend.loadbalancer.sticky=true \ --label traefik.backend.loadbalancer.sticky=true \
emilevauge/whoami" emilevauge/whoami"
``` ```

View file

@ -52,6 +52,7 @@ type Docker struct {
// dockerData holds the need data to the Docker provider // dockerData holds the need data to the Docker provider
type dockerData struct { type dockerData struct {
ServiceName string
Name string Name string
Labels map[string]string // List of labels set to container or service Labels map[string]string // List of labels set to container or service
NetworkSettings networkSettings NetworkSettings networkSettings
@ -393,14 +394,14 @@ func (provider *Docker) getFrontendRule(container dockerData) string {
if label, err := getLabel(container, "traefik.frontend.rule"); err == nil { if label, err := getLabel(container, "traefik.frontend.rule"); err == nil {
return label return label
} }
return "Host:" + provider.getSubDomain(container.Name) + "." + provider.Domain return "Host:" + provider.getSubDomain(container.ServiceName) + "." + provider.Domain
} }
func (provider *Docker) getBackend(container dockerData) string { func (provider *Docker) getBackend(container dockerData) string {
if label, err := getLabel(container, "traefik.backend"); err == nil { if label, err := getLabel(container, "traefik.backend"); err == nil {
return normalize(label) return normalize(label)
} }
return normalize(container.Name) return normalize(container.ServiceName)
} }
func (provider *Docker) getIPAddress(container dockerData) string { func (provider *Docker) getIPAddress(container dockerData) string {
@ -559,6 +560,7 @@ func parseContainer(container dockertypes.ContainerJSON) dockerData {
if container.ContainerJSONBase != nil { if container.ContainerJSONBase != nil {
dockerData.Name = container.ContainerJSONBase.Name dockerData.Name = container.ContainerJSONBase.Name
dockerData.ServiceName = dockerData.Name //Default ServiceName to be the container's Name.
if container.ContainerJSONBase.HostConfig != nil { if container.ContainerJSONBase.HostConfig != nil {
dockerData.NetworkSettings.NetworkMode = container.ContainerJSONBase.HostConfig.NetworkMode dockerData.NetworkSettings.NetworkMode = container.ContainerJSONBase.HostConfig.NetworkMode
@ -641,6 +643,7 @@ func (provider *Docker) listServices(ctx context.Context, dockerClient client.AP
func parseService(service swarmtypes.Service, networkMap map[string]*dockertypes.NetworkResource) dockerData { func parseService(service swarmtypes.Service, networkMap map[string]*dockertypes.NetworkResource) dockerData {
dockerData := dockerData{ dockerData := dockerData{
ServiceName: service.Spec.Annotations.Name,
Name: service.Spec.Annotations.Name, Name: service.Spec.Annotations.Name,
Labels: service.Spec.Annotations.Labels, Labels: service.Spec.Annotations.Labels,
NetworkSettings: networkSettings{}, NetworkSettings: networkSettings{},
@ -691,6 +694,7 @@ func listTasks(ctx context.Context, dockerClient client.APIClient, serviceID str
func parseTasks(task swarmtypes.Task, serviceDockerData dockerData, networkMap map[string]*dockertypes.NetworkResource) dockerData { func parseTasks(task swarmtypes.Task, serviceDockerData dockerData, networkMap map[string]*dockertypes.NetworkResource) dockerData {
dockerData := dockerData{ dockerData := dockerData{
ServiceName: serviceDockerData.Name,
Name: serviceDockerData.Name + "." + strconv.Itoa(task.Slot), Name: serviceDockerData.Name + "." + strconv.Itoa(task.Slot),
Labels: serviceDockerData.Labels, Labels: serviceDockerData.Labels,
NetworkSettings: networkSettings{}, NetworkSettings: networkSettings{},