2018-12-04 13:24:04 +00:00
|
|
|
package label
|
|
|
|
|
|
|
|
import (
|
2019-03-15 08:42:03 +00:00
|
|
|
"github.com/containous/traefik/pkg/config"
|
|
|
|
"github.com/containous/traefik/pkg/provider/label/internal"
|
2018-12-04 13:24:04 +00:00
|
|
|
)
|
|
|
|
|
2019-01-18 14:18:04 +00:00
|
|
|
// DecodeConfiguration Converts the labels to a configuration.
|
|
|
|
func DecodeConfiguration(labels map[string]string) (*config.Configuration, error) {
|
2019-03-14 08:30:04 +00:00
|
|
|
conf := &config.Configuration{
|
|
|
|
HTTP: &config.HTTPConfiguration{},
|
|
|
|
TCP: &config.TCPConfiguration{},
|
|
|
|
}
|
2019-01-18 14:18:04 +00:00
|
|
|
|
2019-03-14 08:30:04 +00:00
|
|
|
err := Decode(labels, conf, "traefik.http", "traefik.tcp")
|
2018-12-04 13:24:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-18 14:18:04 +00:00
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeConfiguration Converts a configuration to labels.
|
|
|
|
func EncodeConfiguration(conf *config.Configuration) (map[string]string, error) {
|
|
|
|
return Encode(conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode Converts the labels to an element.
|
|
|
|
// labels -> [ node -> node + metadata (type) ] -> element (node)
|
|
|
|
func Decode(labels map[string]string, element interface{}, filters ...string) error {
|
|
|
|
node, err := internal.DecodeToNode(labels, filters...)
|
2018-12-04 13:24:04 +00:00
|
|
|
if err != nil {
|
2019-01-18 14:18:04 +00:00
|
|
|
return err
|
2018-12-04 13:24:04 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 14:18:04 +00:00
|
|
|
err = internal.AddMetadata(element, node)
|
2018-12-04 13:24:04 +00:00
|
|
|
if err != nil {
|
2019-01-18 14:18:04 +00:00
|
|
|
return err
|
2018-12-04 13:24:04 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 14:18:04 +00:00
|
|
|
err = internal.Fill(element, node)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-12-04 13:24:04 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 14:18:04 +00:00
|
|
|
// Encode Converts an element to labels.
|
2018-12-04 13:24:04 +00:00
|
|
|
// element -> node (value) -> label (node)
|
2019-01-18 14:18:04 +00:00
|
|
|
func Encode(element interface{}) (map[string]string, error) {
|
|
|
|
node, err := internal.EncodeToNode(element)
|
2018-12-04 13:24:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return internal.EncodeNode(node), nil
|
|
|
|
}
|