-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlabeled.go
164 lines (126 loc) · 3.27 KB
/
labeled.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package ngroklistener
import (
"fmt"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"go.uber.org/zap"
"golang.ngrok.com/ngrok/config"
)
func init() {
caddy.RegisterModule(new(Labeled))
}
// ngrok Labeled Tunnel
type Labeled struct {
opts []config.LabeledTunnelOption
// A map of label, value pairs for this tunnel.
Labels map[string]string `json:"labels,omitempty"`
// opaque metadata string for this tunnel.
Metadata string `json:"metadata,omitempty"`
l *zap.Logger
}
// CaddyModule implements caddy.Module
func (*Labeled) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "caddy.listeners.ngrok.tunnels.labeled",
New: func() caddy.Module {
return new(Labeled)
},
}
}
// Provision implements caddy.Provisioner
func (t *Labeled) Provision(ctx caddy.Context) error {
t.l = ctx.Logger()
t.doReplace()
if err := t.provisionOpts(); err != nil {
return fmt.Errorf("provisioning labeled tunnel opts: %v", err)
}
return nil
}
func (t *Labeled) provisionOpts() error {
if t.Labels == nil || len(t.Labels) == 0 {
return fmt.Errorf("a label is required for labeled tunnels")
}
for label, value := range t.Labels {
t.opts = append(t.opts, config.WithLabel(label, value))
t.l.Info("applying label", zap.String("label", label), zap.String("value", value))
}
if t.Metadata != "" {
t.opts = append(t.opts, config.WithMetadata(t.Metadata))
}
return nil
}
func (t *Labeled) doReplace() {
repl := caddy.NewReplacer()
replaceableFields := []*string{
&t.Metadata,
}
for _, field := range replaceableFields {
actual := repl.ReplaceKnown(*field, "")
*field = actual
}
replacedLabels := make(map[string]string)
for labelName, labelValue := range t.Labels {
actualLabelName := repl.ReplaceKnown(labelName, "")
actualLabelValue := repl.ReplaceKnown(labelValue, "")
replacedLabels[actualLabelName] = actualLabelValue
}
t.Labels = replacedLabels
}
// convert to ngrok's Tunnel type
func (t *Labeled) NgrokTunnel() config.Tunnel {
return config.LabeledTunnel(t.opts...)
}
func (t *Labeled) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
if d.NextArg() {
return d.ArgErr()
}
for nesting := d.Nesting(); d.NextBlock(nesting); {
subdirective := d.Val()
switch subdirective {
case "metadata":
if !d.AllArgs(&t.Metadata) {
return d.ArgErr()
}
case "label":
if err := t.unmarshalLabels(d); err != nil {
return err
}
default:
return d.Errf("unrecognized subdirective %s", subdirective)
}
}
}
return nil
}
func (t *Labeled) unmarshalLabels(d *caddyfile.Dispenser) error {
var (
label string
labelValue string
)
if t.Labels == nil {
t.Labels = map[string]string{}
}
label = d.Val()
if d.CountRemainingArgs() != 0 { // label is defined inline
if !d.AllArgs(&label, &labelValue) {
return d.ArgErr()
}
t.Labels[label] = labelValue
return nil
}
for nesting := d.Nesting(); d.NextBlock(nesting); { // block of labels
label := d.Val()
if !d.AllArgs(&labelValue) {
return d.ArgErr()
}
t.Labels[label] = labelValue
}
return nil
}
var (
_ caddy.Module = (*Labeled)(nil)
_ Tunnel = (*Labeled)(nil)
_ caddy.Provisioner = (*Labeled)(nil)
_ caddyfile.Unmarshaler = (*Labeled)(nil)
)