diff --git a/discovery/discovery.go b/discovery/discovery.go index b2dc6595..844d2749 100644 --- a/discovery/discovery.go +++ b/discovery/discovery.go @@ -111,6 +111,9 @@ func renderTemplate(tpl string, kvs ...string) (string, bool) { for i := 0; i < len(kvs); i += 2 { k := kvs[i] v := kvs[i+1] + if k != "{name}" && !strings.Contains(tpl, k) { + return tpl, false + } tpl = strings.Replace(tpl, k, v, -1) } return tpl, !templateExpression.MatchString(tpl) @@ -118,9 +121,11 @@ func renderTemplate(tpl string, kvs ...string) (string, bool) { func createTemplateVars(app App) []string { tplVars := []string{"{name}", app.Name.String()} - // If a label is called "name", it will be ignored as it appears after - // in the slice + // Ignore labels called "name" for n, v := range app.Labels { + if n == "name" { + continue + } tplVars = append(tplVars, fmt.Sprintf("{%s}", n), v) } return tplVars @@ -151,13 +156,14 @@ func doDiscover(pre string, hostHeaders map[string]http.Header, app App, insecur switch m.name { case "ac-discovery": - // Ignore not handled variables as {ext} isn't already rendered. - uri, _ := renderTemplate(m.uri, tplVars...) - asc, ok := renderTemplate(uri, "{ext}", "aci.asc") + // Ignore not handled variables as only {ext} has been rendered + aci, _ := renderTemplate(m.uri, "{ext}", "aci") + asc, _ := renderTemplate(m.uri, "{ext}", "aci.asc") + aci, ok := renderTemplate(aci, tplVars...) if !ok { continue } - aci, ok := renderTemplate(uri, "{ext}", "aci") + asc, ok = renderTemplate(asc, tplVars...) if !ok { continue } diff --git a/discovery/discovery_test.go b/discovery/discovery_test.go index bb2ca254..4111fce0 100644 --- a/discovery/discovery_test.go +++ b/discovery/discovery_test.go @@ -460,7 +460,9 @@ func TestDiscoverEndpoints(t *testing.T) { []string{"https://example.com/pubkeys.gpg"}, nil, }, - // Test multiple ACIEndpoints. + // Test multiple endpoints. + // Should render two endpoint, since '' + // doesn't contain all the required labels { &mockHTTPDoer{ doer: fakeHTTPGet( @@ -487,10 +489,6 @@ func TestDiscoverEndpoints(t *testing.T) { ACI: "https://storage.example.com/example.com/myapp-1.0.0-linux-amd64.aci", ASC: "https://storage.example.com/example.com/myapp-1.0.0-linux-amd64.aci.asc", }, - ACIEndpoint{ - ACI: "https://storage.example.com/example.com/myapp-1.0.0.aci", - ASC: "https://storage.example.com/example.com/myapp-1.0.0.aci.asc", - }, ACIEndpoint{ ACI: "hdfs://storage.example.com/example.com/myapp-1.0.0-linux-amd64.aci", ASC: "hdfs://storage.example.com/example.com/myapp-1.0.0-linux-amd64.aci.asc", @@ -499,6 +497,37 @@ func TestDiscoverEndpoints(t *testing.T) { []string{"https://example.com/pubkeys.gpg"}, nil, }, + // Test a discovery string that has an hardcoded app name instead of using the provided {name} + { + &mockHTTPDoer{ + doer: fakeHTTPGet( + []meta{ + {"/myapp", + "meta07.html", + }, + }, + nil, + ), + }, + true, + true, + App{ + Name: "example.com/myapp", + Labels: map[types.ACIdentifier]string{ + "version": "1.0.0", + "os": "linux", + "arch": "amd64", + }, + }, + []ACIEndpoint{ + ACIEndpoint{ + ACI: "https://storage.example.com/myapp-1.0.0-linux-amd64.aci", + ASC: "https://storage.example.com/myapp-1.0.0-linux-amd64.aci.asc", + }, + }, + []string{"https://example.com/pubkeys.gpg"}, + nil, + }, // Test with an auth header { diff --git a/discovery/testdata/meta07.html b/discovery/testdata/meta07.html new file mode 100644 index 00000000..a9b0b800 --- /dev/null +++ b/discovery/testdata/meta07.html @@ -0,0 +1,13 @@ + + + +
+