@@ -37,15 +37,17 @@ type ACIEndpoint struct {
37
37
ASC string
38
38
}
39
39
40
- type Endpoints struct {
40
+ // A struct containing both discovered endpoints and keys. Used to avoid
41
+ // function duplication (one for endpoints and one for keys, so to avoid two
42
+ // doDiscover, two DiscoverWalkFunc)
43
+ type discoveryData struct {
41
44
ACIEndpoints []ACIEndpoint
42
- Keys []string
45
+ PublicKeys []string
43
46
}
44
47
45
- func (e * Endpoints ) Append (ep Endpoints ) {
46
- e .ACIEndpoints = append (e .ACIEndpoints , ep .ACIEndpoints ... )
47
- e .Keys = append (e .Keys , ep .Keys ... )
48
- }
48
+ type ACIEndpoints []ACIEndpoint
49
+
50
+ type PublicKeys []string
49
51
50
52
const (
51
53
defaultVersion = "latest"
@@ -124,7 +126,7 @@ func createTemplateVars(app App) []string {
124
126
return tplVars
125
127
}
126
128
127
- func doDiscover (pre string , hostHeaders map [string ]http.Header , app App , insecure InsecureOption ) (* Endpoints , error ) {
129
+ func doDiscover (pre string , hostHeaders map [string ]http.Header , app App , insecure InsecureOption ) (* discoveryData , error ) {
128
130
app = * app .Copy ()
129
131
if app .Labels ["version" ] == "" {
130
132
app .Labels ["version" ] = defaultVersion
@@ -140,7 +142,7 @@ func doDiscover(pre string, hostHeaders map[string]http.Header, app App, insecur
140
142
141
143
tplVars := createTemplateVars (app )
142
144
143
- de := & Endpoints {}
145
+ dd := & discoveryData {}
144
146
145
147
for _ , m := range meta {
146
148
if ! strings .HasPrefix (app .Name .String (), m .prefix ) {
@@ -159,41 +161,37 @@ func doDiscover(pre string, hostHeaders map[string]http.Header, app App, insecur
159
161
if ! ok {
160
162
continue
161
163
}
162
- de .ACIEndpoints = append (de .ACIEndpoints , ACIEndpoint {ACI : aci , ASC : asc })
164
+ dd .ACIEndpoints = append (dd .ACIEndpoints , ACIEndpoint {ACI : aci , ASC : asc })
163
165
164
166
case "ac-discovery-pubkeys" :
165
- de . Keys = append (de . Keys , m .uri )
167
+ dd . PublicKeys = append (dd . PublicKeys , m .uri )
166
168
}
167
169
}
168
170
169
- return de , nil
171
+ return dd , nil
170
172
}
171
173
172
174
// DiscoverWalk will make HTTPS requests to find discovery meta tags and
173
175
// optionally will use HTTP if insecure is set. hostHeaders specifies the
174
176
// header to apply depending on the host (e.g. authentication). Based on the
175
177
// response of the discoverFn it will continue to recurse up the tree.
176
- func DiscoverWalk (app App , hostHeaders map [string ]http.Header , insecure InsecureOption , discoverFn DiscoverWalkFunc ) (err error ) {
177
- var (
178
- eps * Endpoints
179
- )
180
-
178
+ func DiscoverWalk (app App , hostHeaders map [string ]http.Header , insecure InsecureOption , discoverFn DiscoverWalkFunc ) (dd * discoveryData , err error ) {
181
179
parts := strings .Split (string (app .Name ), "/" )
182
180
for i := range parts {
183
181
end := len (parts ) - i
184
182
pre := strings .Join (parts [:end ], "/" )
185
183
186
- eps , err = doDiscover (pre , hostHeaders , app , insecure )
187
- if derr := discoverFn (pre , eps , err ); derr != nil {
188
- return derr
184
+ dd , err = doDiscover (pre , hostHeaders , app , insecure )
185
+ if derr := discoverFn (pre , dd , err ); derr != nil {
186
+ return dd , derr
189
187
}
190
188
}
191
189
192
- return
190
+ return nil , fmt . Errorf ( "discovery failed" )
193
191
}
194
192
195
193
// DiscoverWalkFunc can stop a DiscoverWalk by returning non-nil error.
196
- type DiscoverWalkFunc func (prefix string , eps * Endpoints , err error ) error
194
+ type DiscoverWalkFunc func (prefix string , dd * discoveryData , err error ) error
197
195
198
196
// FailedAttempt represents a failed discovery attempt. This is for debugging
199
197
// and user feedback.
@@ -202,59 +200,58 @@ type FailedAttempt struct {
202
200
Error error
203
201
}
204
202
205
- func walker (out * Endpoints , attempts * []FailedAttempt , testFn DiscoverWalkFunc ) DiscoverWalkFunc {
206
- return func (pre string , eps * Endpoints , err error ) error {
203
+ func walker (attempts * []FailedAttempt , testFn DiscoverWalkFunc ) DiscoverWalkFunc {
204
+ return func (pre string , dd * discoveryData , err error ) error {
207
205
if err != nil {
208
206
* attempts = append (* attempts , FailedAttempt {pre , err })
209
207
return nil
210
208
}
211
- out .Append (* eps )
212
- if err := testFn (pre , eps , err ); err != nil {
209
+ if err := testFn (pre , dd , err ); err != nil {
213
210
return err
214
211
}
215
212
return nil
216
213
}
217
214
}
218
215
219
- // DiscoverEndpoints will make HTTPS requests to find the ac-discovery meta
216
+ // DiscoverACIEndpoints will make HTTPS requests to find the ac-discovery meta
220
217
// tags and optionally will use HTTP if insecure is set. hostHeaders
221
218
// specifies the header to apply depending on the host (e.g. authentication).
222
219
// It will not give up until it has exhausted the path or found an image
223
220
// discovery.
224
- func DiscoverEndpoints (app App , hostHeaders map [string ]http.Header , insecure InsecureOption ) (out * Endpoints , attempts []FailedAttempt , err error ) {
225
- out = & Endpoints {}
226
- testFn := func (pre string , eps * Endpoints , err error ) error {
227
- if len (out .ACIEndpoints ) != 0 {
221
+ func DiscoverACIEndpoints (app App , hostHeaders map [string ]http.Header , insecure InsecureOption ) (ACIEndpoints , []FailedAttempt , error ) {
222
+ testFn := func (pre string , dd * discoveryData , err error ) error {
223
+ if len (dd .ACIEndpoints ) != 0 {
228
224
return errEnough
229
225
}
230
226
return nil
231
227
}
232
228
233
- err = DiscoverWalk (app , hostHeaders , insecure , walker (out , & attempts , testFn ))
229
+ attempts := []FailedAttempt {}
230
+ dd , err := DiscoverWalk (app , hostHeaders , insecure , walker (& attempts , testFn ))
234
231
if err != nil && err != errEnough {
235
232
return nil , attempts , err
236
233
}
237
234
238
- return out , attempts , nil
235
+ return dd . ACIEndpoints , attempts , nil
239
236
}
240
237
241
238
// DiscoverPublicKey will make HTTPS requests to find the ac-public-keys meta
242
239
// tags and optionally will use HTTP if insecure is set. hostHeaders
243
240
// specifies the header to apply depending on the host (e.g. authentication).
244
241
// It will not give up until it has exhausted the path or found an public key.
245
- func DiscoverPublicKeys (app App , hostHeaders map [string ]http.Header , insecure InsecureOption ) (out * Endpoints , attempts []FailedAttempt , err error ) {
246
- out = & Endpoints {}
247
- testFn := func (pre string , eps * Endpoints , err error ) error {
248
- if len (out .Keys ) != 0 {
242
+ func DiscoverPublicKeys (app App , hostHeaders map [string ]http.Header , insecure InsecureOption ) (PublicKeys , []FailedAttempt , error ) {
243
+ testFn := func (pre string , dd * discoveryData , err error ) error {
244
+ if len (dd .PublicKeys ) != 0 {
249
245
return errEnough
250
246
}
251
247
return nil
252
248
}
253
249
254
- err = DiscoverWalk (app , hostHeaders , insecure , walker (out , & attempts , testFn ))
250
+ attempts := []FailedAttempt {}
251
+ dd , err := DiscoverWalk (app , hostHeaders , insecure , walker (& attempts , testFn ))
255
252
if err != nil && err != errEnough {
256
253
return nil , attempts , err
257
254
}
258
255
259
- return out , attempts , nil
256
+ return dd . PublicKeys , attempts , nil
260
257
}
0 commit comments