|
diff --git a/pkg/provider/docker/docker.go b/pkg/provider/docker/docker.go |
|
index 0516620ce..488624047 100644 |
|
--- a/pkg/provider/docker/docker.go |
|
+++ b/pkg/provider/docker/docker.go |
|
@@ -401,7 +401,7 @@ func parseContainer(container dockertypes.ContainerJSON) dockerData { |
|
} |
|
|
|
if container.Config != nil && container.Config.Labels != nil { |
|
- dData.Labels = container.Config.Labels |
|
+ dData.Labels = parseLabels(container) |
|
} |
|
|
|
if container.NetworkSettings != nil { |
|
@@ -427,6 +427,81 @@ func parseContainer(container dockertypes.ContainerJSON) dockerData { |
|
return dData |
|
} |
|
|
|
+func parseLabels(container dockertypes.ContainerJSON) map[string]string { |
|
+ if container.Config.Env == nil { |
|
+ return container.Config.Labels |
|
+ } |
|
+ if _, isTreafikDefined := container.Config.Labels["traefik.enable"]; isTreafikDefined { |
|
+ return container.Config.Labels |
|
+ } |
|
+ |
|
+ virtualHostString, hasVirtualHost := getEnvValue(container, "VIRTUAL_HOST") |
|
+ if !hasVirtualHost { |
|
+ return container.Config.Labels |
|
+ } |
|
+ _, hasLetsEncryptHost := getEnvValue(container, "LETSENCRYPT_HOST") |
|
+ |
|
+ var routerName string |
|
+ if projectName, isComposeContainer := container.Config.Labels["com.docker.compose.project"]; isComposeContainer { |
|
+ serviceName := container.Config.Labels["com.docker.compose.service"] |
|
+ routerName = projectName + "__" + serviceName |
|
+ } else { |
|
+ if strings.HasPrefix(container.Name, "/") { |
|
+ routerName = container.Name[1:] |
|
+ } else { |
|
+ routerName = container.Name |
|
+ } |
|
+ } |
|
+ |
|
+ var virtualHosts []string |
|
+ for _, str := range strings.Split(virtualHostString, ",") { |
|
+ virtualHosts = append(virtualHosts, "Host(`"+strings.TrimSpace(str)+"`)") |
|
+ } |
|
+ hostRule := strings.Join(virtualHosts, " || ") |
|
+ |
|
+ var labels map[string]string |
|
+ if hasLetsEncryptHost { |
|
+ labels = map[string]string{ |
|
+ // https |
|
+ "traefik.http.routers." + routerName + ".rule": hostRule, |
|
+ "traefik.http.routers." + routerName + ".entrypoints": "websecure", |
|
+ |
|
+ // http |
|
+ "traefik.http.routers." + routerName + "-http.rule": hostRule, |
|
+ "traefik.http.routers." + routerName + "-http.entrypoints": "web", |
|
+ "traefik.http.routers." + routerName + "-http.middlewares": routerName + "-http", |
|
+ "traefik.http.middlewares." + routerName + "-http.redirectscheme.scheme": "https", |
|
+ } |
|
+ } else { |
|
+ labels = map[string]string{ |
|
+ "traefik.http.routers." + routerName + ".rule": hostRule, |
|
+ "traefik.http.routers." + routerName + ".entrypoints": "web", |
|
+ } |
|
+ } |
|
+ |
|
+ if port, ok := getEnvValue(container, "VIRTUAL_PORT"); ok { |
|
+ labels["traefik.http.services."+routerName+".loadbalancer.server.port"] = port |
|
+ } |
|
+ |
|
+ for k, v := range container.Config.Labels { |
|
+ labels[k] = v |
|
+ } |
|
+ |
|
+ return labels |
|
+} |
|
+ |
|
+func getEnvValue(container dockertypes.ContainerJSON, key string) (string, bool) { |
|
+ for _, env := range container.Config.Env { |
|
+ if strings.HasPrefix(env, key+"=") { |
|
+ return env[len(key)+1:], true |
|
+ } |
|
+ } |
|
+ |
|
+ return "", false |
|
+} |
|
+ |
|
func (p *Provider) listServices(ctx context.Context, dockerClient client.APIClient) ([]dockerData, error) { |
|
logger := log.FromContext(ctx) |