2019-06-17 09:48:05 +00:00
|
|
|
// Package label implements the decoding and encoding between flat labels and a typed Configuration.
|
|
|
|
package label
|
|
|
|
|
|
|
|
import (
|
2019-08-03 01:58:23 +00:00
|
|
|
"github.com/containous/traefik/v2/pkg/config/dynamic"
|
2020-08-17 16:04:03 +00:00
|
|
|
"github.com/traefik/paerser/parser"
|
2019-06-17 09:48:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DecodeConfiguration converts the labels to a configuration.
|
2019-07-10 07:26:04 +00:00
|
|
|
func DecodeConfiguration(labels map[string]string) (*dynamic.Configuration, error) {
|
|
|
|
conf := &dynamic.Configuration{
|
|
|
|
HTTP: &dynamic.HTTPConfiguration{},
|
|
|
|
TCP: &dynamic.TCPConfiguration{},
|
2020-02-20 21:24:05 +00:00
|
|
|
UDP: &dynamic.UDPConfiguration{},
|
2019-06-17 09:48:05 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 21:24:05 +00:00
|
|
|
err := parser.Decode(labels, conf, parser.DefaultRootName, "traefik.http", "traefik.tcp", "traefik.udp")
|
2019-06-17 09:48:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EncodeConfiguration converts a configuration to labels.
|
2019-07-10 07:26:04 +00:00
|
|
|
func EncodeConfiguration(conf *dynamic.Configuration) (map[string]string, error) {
|
2019-06-21 08:08:04 +00:00
|
|
|
return parser.Encode(conf, parser.DefaultRootName)
|
2019-06-17 09:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decode converts the labels to an element.
|
2020-05-11 10:06:07 +00:00
|
|
|
// labels -> [ node -> node + metadata (type) ] -> element (node).
|
2019-06-17 09:48:05 +00:00
|
|
|
func Decode(labels map[string]string, element interface{}, filters ...string) error {
|
2019-06-21 08:08:04 +00:00
|
|
|
return parser.Decode(labels, element, parser.DefaultRootName, filters...)
|
2019-06-17 09:48:05 +00:00
|
|
|
}
|