Skip to content

Commit 0173232

Browse files
strzelecki-maciekjanisz
authored andcommitted
Support for a path in marathon location (#210)
In order to join multiple marathons (e.g. per dev-team, per project) we are discovering their (semi-random) locations through linkerd. This changes our --marathon-location value from the standard hostname:port into something like --marathon-location=localhost:4141/ops-staging/marathon-devteam-1 This fix checks, naively, for the existence of "path" and divides it into host and url that is then prefixing the path param.
1 parent b1e8dd8 commit 0173232

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

marathon/marathon.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,28 @@ func (m Marathon) url(path string) string {
212212

213213
type params map[string][]string
214214

215+
// urlWithQuery returns absolute path to marathon endpoint
216+
// if location is given with path e.g. "localhost:8080/proxy/url", then
217+
// host and path parts are appended to respective url.URL fields
215218
func (m Marathon) urlWithQuery(path string, params params) string {
216-
marathon := url.URL{
217-
Scheme: m.Protocol,
218-
User: m.Auth,
219-
Host: m.Location,
220-
Path: path,
219+
var marathon url.URL
220+
if strings.Contains(m.Location, "/") {
221+
parts := strings.SplitN(m.Location, "/", 2)
222+
marathon = url.URL{
223+
Scheme: m.Protocol,
224+
User: m.Auth,
225+
Host: parts[0],
226+
Path: "/" + parts[1] + path,
227+
}
228+
} else {
229+
marathon = url.URL{
230+
Scheme: m.Protocol,
231+
User: m.Auth,
232+
Host: m.Location,
233+
Path: path,
234+
}
221235
}
236+
222237
query := marathon.Query()
223238
for key, values := range params {
224239
for _, value := range values {

marathon/marathon_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,30 @@ func TestEventStream_PassingStreamerCreated(t *testing.T) {
408408
assert.IsType(t, &Streamer{}, streamer)
409409
}
410410

411+
func TestUrlWithQuery_NoProxyMarathon(t *testing.T) {
412+
t.Parallel()
413+
414+
// given
415+
m, _ := New(Config{Location: "localhost:8080", Protocol: "HTTP"}, "")
416+
// when
417+
path := m.urlWithQuery("/testpath", params{})
418+
419+
// then
420+
assert.Equal(t, "HTTP://localhost:8080/testpath", path)
421+
}
422+
423+
func TestUrlWithQuery_ProxyMarathon(t *testing.T) {
424+
t.Parallel()
425+
426+
// given
427+
m, _ := New(Config{Location: "localhost:8080/proxy/url/segments", Protocol: "HTTP"}, "")
428+
// when
429+
path := m.urlWithQuery("/testpath", params{})
430+
431+
// then
432+
assert.Equal(t, "HTTP://localhost:8080/proxy/url/segments/testpath", path)
433+
}
434+
411435
// http://keighl.com/post/mocking-http-responses-in-golang/
412436
func stubServer(uri string, body string) (*httptest.Server, *http.Transport) {
413437
return mockServer(func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)