traefik/provider/kubernetes/annotations_parser.go

72 lines
1.8 KiB
Go
Raw Normal View History

2017-11-28 12:36:03 +00:00
package kubernetes
import (
2017-12-04 10:40:03 +00:00
"net/http"
2017-11-28 12:36:03 +00:00
"strings"
"github.com/containous/traefik/log"
"github.com/containous/traefik/types"
"k8s.io/client-go/pkg/apis/extensions/v1beta1"
)
func getBoolAnnotation(meta *v1beta1.Ingress, name string, defaultValue bool) bool {
annotationValue := defaultValue
annotationStringValue, ok := meta.Annotations[name]
switch {
case !ok:
// No op.
case annotationStringValue == "false":
annotationValue = false
case annotationStringValue == "true":
annotationValue = true
default:
log.Warnf("Unknown value %q for %q, falling back to %v", annotationStringValue, name, defaultValue)
2017-11-28 12:36:03 +00:00
}
return annotationValue
}
func getStringAnnotation(meta *v1beta1.Ingress, name string) string {
value := meta.Annotations[name]
return value
}
func getSliceAnnotation(meta *v1beta1.Ingress, name string) []string {
var value []string
if annotation, ok := meta.Annotations[name]; ok && annotation != "" {
2017-11-30 08:26:03 +00:00
value = types.SplitAndTrimString(annotation)
2017-11-28 12:36:03 +00:00
}
if len(value) == 0 {
log.Debugf("Could not load %v annotation, skipping...", name)
return nil
}
return value
}
func getMapAnnotation(meta *v1beta1.Ingress, annotName string) map[string]string {
if values, ok := meta.Annotations[annotName]; ok {
if len(values) == 0 {
log.Errorf("Missing value for annotation %q", annotName)
return nil
}
mapValue := make(map[string]string)
2017-12-04 10:40:03 +00:00
for _, parts := range strings.Split(values, "||") {
pair := strings.SplitN(parts, ":", 2)
2017-11-28 12:36:03 +00:00
if len(pair) != 2 {
log.Warnf("Could not load %q: %v, skipping...", annotName, pair)
2017-11-28 12:36:03 +00:00
} else {
2017-12-04 10:40:03 +00:00
mapValue[http.CanonicalHeaderKey(strings.TrimSpace(pair[0]))] = strings.TrimSpace(pair[1])
2017-11-28 12:36:03 +00:00
}
}
if len(mapValue) == 0 {
log.Errorf("Could not load %q, skipping...", annotName)
return nil
}
return mapValue
2017-11-28 12:36:03 +00:00
}
return nil
2017-11-28 12:36:03 +00:00
}