-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathkv.go
136 lines (111 loc) · 2.76 KB
/
kv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package traefikkop
import (
"encoding/json"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/traefik/traefik/v2/pkg/config/dynamic"
)
type KV struct {
data map[string]interface{}
base string
}
func NewKV() *KV {
return &KV{data: make(map[string]interface{})}
}
func (kv *KV) SetBase(b string) {
kv.base = b
}
func (kv *KV) add(val interface{}, format string, a ...interface{}) {
if val == nil {
return // todo: log it? debug?
}
str := fmt.Sprintf("%s", val)
if str == "" {
return // todo: log it? debug?
}
if kv.base != "" {
format = kv.base + "/" + format
}
key := fmt.Sprintf(format, a...)
if strings.HasPrefix(key, "traefik/tls/") {
// ignore tls options, only interested in things that can be set per-container
return
}
kv.data[key] = val
}
// ConfigToKV flattens the given configuration into a format suitable for
// putting into a KV store such as redis
func ConfigToKV(conf dynamic.Configuration) (map[string]interface{}, error) {
b, err := json.Marshal(conf)
if err != nil {
return nil, errors.Wrap(err, "failed to create kv")
}
hash := make(map[string]interface{})
err = json.Unmarshal(b, &hash)
if err != nil {
return nil, errors.Wrap(err, "failed to create kv")
}
kv := NewKV()
walk(kv, "traefik", hash, "")
return kv.data, nil
}
var reKeyName = regexp.MustCompile(`^traefik/(http|tcp|udp)/(router|service|middleware)s$`)
func walk(kv *KV, path string, obj interface{}, pos string) {
if obj == nil {
return
}
val := reflect.ValueOf(obj)
switch val.Kind() {
case reflect.Map:
iter := val.MapRange()
for iter.Next() {
key := iter.Key()
val := iter.Value()
if !val.CanInterface() {
continue
}
strKey := key.String()
if reKeyName.MatchString(path) {
strKey = strings.TrimSuffix(strKey, "@docker")
}
walk(kv, path+"/"+strKey, val.Interface(), strKey)
}
case reflect.Struct:
num := val.NumField()
for i := 0; i < num; i++ {
val := val.Field(i)
if !val.CanInterface() {
continue
}
walk(kv, path, val.Interface(), pos)
}
case reflect.Slice:
n := val.Len()
for i := 0; i < n; i++ {
val := val.Index(i)
if !val.CanInterface() {
continue
}
walk(kv, fmt.Sprintf("%s/%d", path, i), val.Interface(), pos)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.String, reflect.Bool:
// stringify it
kv.add(stringify(val.Interface()), path)
case reflect.Float32, reflect.Float64:
kv.add(stringifyFloat(val.Float()), path)
default:
logrus.Warnf("unhandled kind %s: %#v\n", val.Kind(), obj)
}
}
func stringify(val interface{}) string {
return fmt.Sprintf("%v", val)
}
func stringifyFloat(val float64) string {
return strconv.FormatFloat(val, 'f', -1, 64)
}