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

Commit

Permalink
discovery: match only templates that satisfy all required labels
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sgotti committed Mar 25, 2016
1 parent 9bdac40 commit c7b344f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
18 changes: 12 additions & 6 deletions discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,21 @@ func renderTemplate(tpl string, kvs ...string) (string, bool) {
for i := 0; i < len(kvs); i += 2 {
k := kvs[i]
v := kvs[i+1]
if !strings.Contains(tpl, k) {
return tpl, false
}
tpl = strings.Replace(tpl, k, v, -1)
}
return tpl, !templateExpression.MatchString(tpl)
}

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
Expand Down Expand Up @@ -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
}
Expand Down
8 changes: 3 additions & 5 deletions discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<meta name="ac-discovery" content="example.com https://storage.example.com/{name}-{version}.{ext}">'
// doesn't contain all the required labels
{
&mockHTTPDoer{
doer: fakeHTTPGet(
Expand All @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions spec/discovery.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ curl $(echo "$urltmpl" | sed -e "s/{name}/$name/" -e "s/{version}/$version/" -e

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.

The client MUST accept only templates that can be fully substituted and that satisfy all the required labels.

For example given these meta tags:
```html
<meta name="ac-discovery" content="example.com https://storage.example.com/{os}/{arch}/{name}-{version}.{ext}">
<meta name="ac-discovery" content="example.com https://storage.example.com/{os}/{arch}/{name}-latest.{ext}">
```

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.
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.

Note that multiple `ac-discovery` tags MAY be returned for a given prefix-match (for example, with different scheme names representing different transport mechanisms).
In this case, the client implementation MAY choose which to use at its own discretion.
Public discovery implementations SHOULD always provide at least one HTTPS URL template.
Expand Down

0 comments on commit c7b344f

Please sign in to comment.