-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindextemplate.go
156 lines (135 loc) · 4.58 KB
/
indextemplate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2017 oci-discovery contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package indextemplate
import (
"encoding/json"
"fmt"
"mime"
"net/http"
"net/url"
"github.com/jtacoma/uritemplates"
"github.com/sirupsen/logrus"
"github.com/xiekeyang/oci-discovery/tools/hostbasedimagenames"
v1 "github.com/xiekeyang/oci-discovery/tools/newimagespec"
"github.com/xiekeyang/oci-discovery/tools/refengine"
"github.com/xiekeyang/oci-discovery/tools/util"
"golang.org/x/net/context"
)
// Engine implements the OCI Index Template ref-engine protocol.
type Engine struct {
uri *uritemplates.UriTemplate
base *url.URL
}
// New creates a new ref-engine instance.
func New(ctx context.Context, baseURI *url.URL, config interface{}) (engine refengine.Engine, err error) {
configMap, ok := config.(map[string]string)
if !ok {
configMap2, ok := config.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("index template config is not a map[string]string: %v", config)
}
uriInterface, ok := configMap2["uri"]
if !ok {
return nil, fmt.Errorf("index template config missing required 'uri' property: %v", configMap)
}
configMap = make(map[string]string)
configMap["uri"], ok = uriInterface.(string)
if !ok {
return nil, fmt.Errorf("index template config 'uri' is not a string: %v", uriInterface)
}
}
uriString, ok := configMap["uri"]
if !ok {
return nil, fmt.Errorf("index template config missing required 'uri' property: %v", configMap)
}
uriTemplate, err := uritemplates.Parse(uriString)
if err != nil {
return nil, err
}
return &Engine{
uri: uriTemplate,
base: baseURI,
}, nil
}
// Get returns an array of matching references from the store.
func (engine *Engine) Get(ctx context.Context, name string) (roots []refengine.MerkleRoot, err error) {
parsedName, err := hostbasedimagenames.Parse(name)
if err != nil {
return nil, err
}
request, err := engine.getPreFetch(parsedName)
if err != nil {
return nil, err
}
request = request.WithContext(ctx)
client := &http.Client{}
logrus.Debugf("requesting %s from %s", request.Header.Get("accept"), request.URL)
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
return engine.getPostFetch(response, parsedName)
}
// Close releases resources held by the engine.
func (engine *Engine) Close(ctx context.Context) (err error) {
return nil
}
func (engine *Engine) getPreFetch(parsedName map[string]string) (request *http.Request, err error) {
referenceURI, err := engine.uri.Expand(util.StringStringToStringInterface(parsedName))
if err != nil {
return nil, err
}
parsedReference, err := url.Parse(referenceURI)
if err != nil {
return nil, err
}
request = &http.Request{
Method: "GET",
URL: engine.base.ResolveReference(parsedReference),
Header: map[string][]string{
"Accept": {"application/vnd.oci.image.index.v1+json"},
},
}
return request, nil
}
func (engine *Engine) getPostFetch(response *http.Response, parsedName map[string]string) (roots []refengine.MerkleRoot, err error) {
mediatype, _, err := mime.ParseMediaType(response.Header.Get(`Content-Type`))
if err != nil {
return nil, err
}
if mediatype != response.Request.Header.Get(`Accept`) {
return nil, fmt.Errorf("requested %s from %s but got %s", response.Request.Header.Get(`Accept`), response.Request.URL, mediatype)
}
var index v1.Index
if err := json.NewDecoder(response.Body).Decode(&index); err != nil {
logrus.Errorf("%s claimed to return %s, but the response schema did not match: %s", response.Request.URL, mediatype, err)
return nil, err
}
roots = []refengine.MerkleRoot{}
for _, descriptor := range index.Manifests {
fragment, ok := parsedName["fragment"]
if !ok || fragment == "" || fragment == descriptor.Annotations[`org.opencontainers.image.ref.name`] {
roots = append(roots, refengine.MerkleRoot{
MediaType: `application/vnd.oci.descriptor.v1+json`,
Root: descriptor,
URI: response.Request.URL, // FIXME: get URI after any redirects
})
}
}
return roots, nil
}
func init() {
refengine.Constructors["oci-index-template-v1"] = New
}