Skip to content

Commit 034166d

Browse files
Refactored to use options pattern for new command line args
Signed-off-by: Graeme Christie <[email protected]>
1 parent 2b26646 commit 034166d

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

Diff for: injectproxy/routes.go

+29-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"encoding/json"
1919
"fmt"
2020
"io"
21-
"log"
2221
"net/http"
2322
"net/http/httputil"
2423
"net/url"
@@ -50,10 +49,12 @@ type routes struct {
5049
}
5150

5251
type options struct {
53-
enableLabelAPIs bool
54-
passthroughPaths []string
55-
errorOnReplace bool
56-
registerer prometheus.Registerer
52+
enableLabelAPIs bool
53+
passthroughPaths []string
54+
errorOnReplace bool
55+
registerer prometheus.Registerer
56+
extraHttpHeaders map[string]string
57+
rewriteHostHeader string
5758
}
5859

5960
type Option interface {
@@ -97,6 +98,24 @@ func WithErrorOnReplace() Option {
9798
})
9899
}
99100

101+
func WithExtraHttpHeaders(headers []string) Option {
102+
return optionFunc(func(o *options) {
103+
o.extraHttpHeaders = make(map[string]string)
104+
for _, headerArg := range headers {
105+
header, val, found := strings.Cut(headerArg, ":")
106+
if found {
107+
o.extraHttpHeaders[strings.TrimSpace(header)] = strings.TrimSpace(val)
108+
}
109+
}
110+
})
111+
}
112+
113+
func WithRewriteHostHeader(host string) Option {
114+
return optionFunc(func(o *options) {
115+
o.rewriteHostHeader = host
116+
})
117+
}
118+
100119
// mux abstracts away the behavior we expect from the http.ServeMux type in this package.
101120
type mux interface {
102121
http.Handler
@@ -262,7 +281,8 @@ func (sle StaticLabelEnforcer) ExtractLabel(next http.HandlerFunc) http.Handler
262281
})
263282
}
264283

265-
func NewRoutes(upstream *url.URL, label string, extractLabeler ExtractLabeler, extraHttpHeaders []string, rewriteHostHeader string, opts ...Option) (*routes, error) {
284+
func NewRoutes(upstream *url.URL, label string, extractLabeler ExtractLabeler, opts ...Option) (*routes, error) {
285+
//extraHttpHeaders []string, rewriteHostHeader string
266286
opt := options{}
267287
for _, o := range opts {
268288
o.apply(&opt)
@@ -275,17 +295,12 @@ func NewRoutes(upstream *url.URL, label string, extractLabeler ExtractLabeler, e
275295
proxy := &httputil.ReverseProxy{
276296
Rewrite: func(r *httputil.ProxyRequest) {
277297
r.SetURL(upstream)
278-
if len(strings.TrimSpace(rewriteHostHeader)) == 0 {
298+
if len(opt.rewriteHostHeader) == 0 {
279299
r.Out.Host = r.In.Host
280300
} else {
281-
r.Out.Host = strings.TrimSpace(rewriteHostHeader)
301+
r.Out.Host = opt.rewriteHostHeader
282302
}
283-
for _, headerArg := range extraHttpHeaders {
284-
header, val, found := strings.Cut(headerArg, ":")
285-
if !found {
286-
log.Printf("Header %s specified but ':' delimited not found", headerArg)
287-
continue
288-
}
303+
for header, val := range opt.extraHttpHeaders {
289304
r.Out.Header[strings.TrimSpace(header)] = []string{strings.TrimSpace(val)}
290305
}
291306
},

Diff for: main.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ func main() {
137137
opts = append(opts, injectproxy.WithErrorOnReplace())
138138
}
139139

140+
if len(extraHttpHeaders) > 0 {
141+
opts = append(opts, injectproxy.WithExtraHttpHeaders(extraHttpHeaders))
142+
}
143+
144+
if len(rewriteHostHeader) > 0 {
145+
opts = append(opts, injectproxy.WithRewriteHostHeader(rewriteHostHeader))
146+
}
147+
140148
var extractLabeler injectproxy.ExtractLabeler
141149
switch {
142150
case len(labelValues) > 0:
@@ -151,7 +159,7 @@ func main() {
151159

152160
{
153161
// Run the insecure HTTP server.
154-
routes, err := injectproxy.NewRoutes(upstreamURL, label, extractLabeler, extraHttpHeaders, rewriteHostHeader, opts...)
162+
routes, err := injectproxy.NewRoutes(upstreamURL, label, extractLabeler, opts...)
155163
if err != nil {
156164
log.Fatalf("Failed to create injectproxy Routes: %v", err)
157165
}

0 commit comments

Comments
 (0)