Skip to content
This repository was archived by the owner on Aug 14, 2020. It is now read-only.

Commit d56ead8

Browse files
committed
discovery: match only templates that satisfy all required labels
Actually the spec doesn't clarify when an endpoint template should be accepted or not. Now, the appc/spec implementation returns only endpoints that can be fully rendered. This means that it will also accept a template if it doesn't contain some of the required discovery labels. This looks good, but, trying to implement some meta discovery ideas it will bring to unwanted endpoints. Example 1: Suppose I want to implement the "latest" pattern behavior: ```html <!-- ACIs with specific version --> <meta name="ac-discovery" content="example.com https://storage.example.com/{name}-{version}-{os}-{arch}.{ext}"> <!-- Latest ACIs --> <meta name="ac-discovery" content="example.com https://storage.example.com/{name}-latest-{os}-{arch}.{ext}"> ``` If, on discovery, I ask for the _name_, _os_ and _arch_ labels only the second template will be rendered (since the first cannot be fully rendered due to missing _version_ label). So I achieved latest pattern. But if I'm asking for a specific _version_ both templates will be rendered. Example 2: As an extension of the first example, suppose I want to create a global meta discovery for all my images on example.com. So on the root of my server I'll put some meta tags using example.com as prefix: ```html <!-- ACIs with specific version --> <meta name="ac-discovery" content="example.com https://storage.example.com/{name}-{version}-{os}-{arch}.{ext}"> <!-- noarch ACIs --> <meta name="ac-discovery" content="example.com https://storage.example.com/{name}-{version}-noarch.{ext}"> <!-- Latest ACIs --> <meta name="ac-discovery" content="example.com https://storage.example.com/{name}-latest-{os}-{arch}.{ext}"> <meta name="ac-discovery" content="example.com https://storage.example.com/{name}-latest.{ext}"> ``` With this tags I want to implement a "latest" and also "noarch" pattern. If, on discovery, I ask only for the _name_ and _version_ labels the second template will be rendered. So I achieved noarch pattern. But, unfortunately, also the last template will be rendered. And, as the first example, if I'm asking for a specific _name_, _version_, _os_ and _arch_ ALL the templates will be rendered. Since the spec says: ``` Note that multiple `ac-discovery` tags MAY be returned for a given prefix-match [snip] In this case, the client implementation MAY choose which to use at its own discretion. ``` If an implementation chooses the second it can download the wrong image version. This patch makes template matching stricter choosing only template that fully satisfy the required labels. It can probably BREAK some of the current meta tags implementations. It also documents this behavior.
1 parent a22774b commit d56ead8

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

discovery/discovery.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,21 @@ func renderTemplate(tpl string, kvs ...string) (string, bool) {
116116
for i := 0; i < len(kvs); i += 2 {
117117
k := kvs[i]
118118
v := kvs[i+1]
119+
if !strings.Contains(tpl, k) {
120+
return tpl, false
121+
}
119122
tpl = strings.Replace(tpl, k, v, -1)
120123
}
121124
return tpl, !templateExpression.MatchString(tpl)
122125
}
123126

124127
func createTemplateVars(app App) []string {
125128
tplVars := []string{"{name}", app.Name.String()}
126-
// If a label is called "name", it will be ignored as it appears after
127-
// in the slice
129+
// Ignore labels called "name"
128130
for n, v := range app.Labels {
131+
if n == "name" {
132+
continue
133+
}
129134
tplVars = append(tplVars, fmt.Sprintf("{%s}", n), v)
130135
}
131136
return tplVars
@@ -156,13 +161,14 @@ func doDiscover(pre string, hostHeaders map[string]http.Header, app App, insecur
156161

157162
switch m.name {
158163
case "ac-discovery":
159-
// Ignore not handled variables as {ext} isn't already rendered.
160-
uri, _ := renderTemplate(m.uri, tplVars...)
161-
asc, ok := renderTemplate(uri, "{ext}", "aci.asc")
164+
// Ignore not handled variables as only {ext} has been rendered
165+
aci, _ := renderTemplate(m.uri, "{ext}", "aci")
166+
asc, _ := renderTemplate(m.uri, "{ext}", "aci.asc")
167+
aci, ok := renderTemplate(aci, tplVars...)
162168
if !ok {
163169
continue
164170
}
165-
aci, ok := renderTemplate(uri, "{ext}", "aci")
171+
asc, ok = renderTemplate(asc, tplVars...)
166172
if !ok {
167173
continue
168174
}

discovery/discovery_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,8 @@ func TestDiscoverEndpoints(t *testing.T) {
461461
nil,
462462
},
463463
// Test multiple endpoints.
464+
// Should render two endpoint, since '<meta name="ac-discovery" content="example.com https://storage.example.com/{name}-{version}.{ext}">'
465+
// doesn't contain all the required labels
464466
{
465467
&mockHTTPDoer{
466468
doer: fakeHTTPGet(
@@ -487,10 +489,6 @@ func TestDiscoverEndpoints(t *testing.T) {
487489
ACI: "https://storage.example.com/example.com/myapp-1.0.0-linux-amd64.aci",
488490
ASC: "https://storage.example.com/example.com/myapp-1.0.0-linux-amd64.aci.asc",
489491
},
490-
ACIEndpoint{
491-
ACI: "https://storage.example.com/example.com/myapp-1.0.0.aci",
492-
ASC: "https://storage.example.com/example.com/myapp-1.0.0.aci.asc",
493-
},
494492
ACIEndpoint{
495493
ACI: "hdfs://storage.example.com/example.com/myapp-1.0.0-linux-amd64.aci",
496494
ASC: "hdfs://storage.example.com/example.com/myapp-1.0.0-linux-amd64.aci.asc",

spec/discovery.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ curl $(echo "$urltmpl" | sed -e "s/{name}/$name/" -e "s/{version}/$version/" -e
5959

6060
where _name_, _version_, _os_, and _arch_ are set to their respective values for the image, and _ext_ is either `aci` or `aci.asc` for retrieving an App Container Image or signature respectively.
6161

62+
The client MUST accept only templates that can be fully substituted and that satisfy all the required labels.
63+
64+
For example given these meta tags:
65+
```html
66+
<meta name="ac-discovery" content="example.com https://storage.example.com/{os}/{arch}/{name}-{version}.{ext}">
67+
<meta name="ac-discovery" content="example.com https://storage.example.com/{os}/{arch}/{name}-latest.{ext}">
68+
```
69+
70+
If the client requires the labels _name_, _version_, _os_, _arch_ only the first template will be rendered since the second doesn't satisfy the _version_ label.
71+
If the client requires the labels _name_, _os_, _arch_ only the second template will be rendered since in the first template '{version}' cannot be substituted.
72+
6273
Note that multiple `ac-discovery` tags MAY be returned for a given prefix-match (for example, with different scheme names representing different transport mechanisms).
6374
In this case, the client implementation MAY choose which to use at its own discretion.
6475
Public discovery implementations SHOULD always provide at least one HTTPS URL template.

0 commit comments

Comments
 (0)