-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhelpers.go
More file actions
105 lines (83 loc) · 2.77 KB
/
helpers.go
File metadata and controls
105 lines (83 loc) · 2.77 KB
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
package caddyconsul
import (
"fmt"
"reflect"
"regexp"
"strconv"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp/reverseproxy"
"github.com/hashicorp/consul/api"
)
// getLastIndex is a function that takes an error as input and returns it after
// logging it via caddy.Log().Error()
func logAndReturn(err error) error {
caddy.Log().Error(err.Error())
return err
}
// getLastIndex is an easy accessor to handle getting a Consul index value
// from a sync.Map.
// We know we only stored uint64 values, so we cast the returned value as uint64.
func getLastIndex(key string) uint64 {
inferaceVal, ok := lastIndexes.Load(key)
if !ok {
return 0
}
return inferaceVal.(uint64)
}
// storeLastIndex is an easy accessor to handle storing a Consul index value
// in a sync.Map.
// For even more ease, we return the value we just stored.
func storeLastIndex(key string, value interface{}) uint64 {
lastIndexes.Store(key, value)
return value.(uint64)
}
// parseConsulService parses the entries returned by the Consul request to determine:
// - the available upstreams
// - the options specified as tags on the Consul service
// All the options extracted from the Consul tags are parsed via the `reflect` package
// and handled as tags on the struct.
// Handled cases:
// - a struct tag like this: caddy:"enable-auth" corresponds to a tag matching
// `caddy:enable-auth` in Consul
// A struct tag like this: caddy:"name=(.*)" corresponds to a tag matching
// `caddy:name=test` and will store "test" in the options returned
func parseConsulService(entries []*api.ServiceEntry) (upstreams []*reverseproxy.Upstream, options *SubdomainReverseProxyOptions) {
options = &SubdomainReverseProxyOptions{}
upstreams = make([]*reverseproxy.Upstream, 0, len(entries))
t := reflect.TypeOf(*options)
v := reflect.ValueOf(options).Elem()
for _, entry := range entries {
// We add the instance as an upstream
upstreams = append(upstreams, &reverseproxy.Upstream{
Dial: fmt.Sprintf("%s:%d", entry.Service.Address, entry.Service.Port),
})
// We check the options on that instance
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
optionTag := field.Tag.Get("caddy")
for _, tag := range entry.Service.Tags {
regex, err := regexp.Compile(fmt.Sprintf("caddy:%s", optionTag))
if err != nil {
continue
}
match := regex.FindStringSubmatch(tag)
if len(match) > 0 {
fieldValue := v.FieldByName(field.Name)
switch fieldValue.Kind() {
case reflect.Bool:
fieldValue.SetBool(true)
case reflect.String:
fieldValue.SetString(match[1])
case reflect.Int:
val, err := strconv.ParseInt(match[1], 10, 0)
if err != nil {
continue
}
fieldValue.SetInt(val)
}
}
}
}
}
return
}