2019-01-18 14:18:04 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/label"
|
2019-01-18 14:18:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
labelDockerComposeProject = "com.docker.compose.project"
|
|
|
|
labelDockerComposeService = "com.docker.compose.service"
|
|
|
|
)
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// configuration Contains information from the labels that are globals (not related to the dynamic configuration)
|
|
|
|
// or specific to the provider.
|
2019-01-18 14:18:04 +00:00
|
|
|
type configuration struct {
|
|
|
|
Enable bool
|
|
|
|
Docker specificConfiguration
|
|
|
|
}
|
|
|
|
|
|
|
|
type specificConfiguration struct {
|
|
|
|
Network string
|
|
|
|
LBSwarm bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Provider) getConfiguration(container dockerData) (configuration, error) {
|
|
|
|
conf := configuration{
|
|
|
|
Enable: p.ExposedByDefault,
|
|
|
|
Docker: specificConfiguration{
|
|
|
|
Network: p.Network,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-06-21 07:24:04 +00:00
|
|
|
err := label.Decode(container.Labels, &conf, "traefik.docker.", "traefik.enable")
|
2019-01-18 14:18:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return configuration{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// getStringMultipleStrict get multiple string values associated to several labels.
|
|
|
|
// Fail if one label is missing.
|
2019-01-18 14:18:04 +00:00
|
|
|
func getStringMultipleStrict(labels map[string]string, labelNames ...string) (map[string]string, error) {
|
|
|
|
foundLabels := map[string]string{}
|
|
|
|
for _, name := range labelNames {
|
|
|
|
value := getStringValue(labels, name, "")
|
|
|
|
// Error out only if one of them is not defined.
|
|
|
|
if len(value) == 0 {
|
|
|
|
return nil, fmt.Errorf("label not found: %s", name)
|
|
|
|
}
|
|
|
|
foundLabels[name] = value
|
|
|
|
}
|
|
|
|
return foundLabels, nil
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:06:07 +00:00
|
|
|
// getStringValue get string value associated to a label.
|
2020-07-07 12:42:03 +00:00
|
|
|
func getStringValue(labels map[string]string, labelName, defaultValue string) string {
|
2019-01-18 14:18:04 +00:00
|
|
|
if value, ok := labels[labelName]; ok && len(value) > 0 {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
return defaultValue
|
|
|
|
}
|