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

Commit 3e46e83

Browse files
committed
Merge pull request #576 from sgotti/discovery_separate_endpoints_keys
discovery: correctly split DiscoverEndpoints and DiscoverPublicKeys
2 parents ec54868 + dd98bd3 commit 3e46e83

File tree

10 files changed

+485
-127
lines changed

10 files changed

+485
-127
lines changed

actool/discover.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,42 @@ func runDiscover(args []string) (exit int) {
6262
if transportFlags.Insecure {
6363
insecure = discovery.InsecureTLS | discovery.InsecureHTTP
6464
}
65-
eps, attempts, err := discovery.DiscoverEndpoints(*app, nil, insecure)
65+
eps, attempts, err := discovery.DiscoverACIEndpoints(*app, nil, insecure)
6666
if err != nil {
67-
stderr("error fetching %s: %s", name, err)
67+
stderr("error fetching endpoints for %s: %s", name, err)
6868
return 1
6969
}
7070
for _, a := range attempts {
71-
fmt.Printf("discover walk: prefix: %s error: %v\n", a.Prefix, a.Error)
71+
fmt.Printf("discover endpoints walk: prefix: %s error: %v\n", a.Prefix, a.Error)
7272
}
73+
publicKeys, attempts, err := discovery.DiscoverPublicKeys(*app, nil, insecure)
74+
if err != nil {
75+
stderr("error fetching public keys for %s: %s", name, err)
76+
return 1
77+
}
78+
for _, a := range attempts {
79+
fmt.Printf("discover public keys walk: prefix: %s error: %v\n", a.Prefix, a.Error)
80+
}
81+
82+
type discoveryData struct {
83+
ACIEndpoints []discovery.ACIEndpoint
84+
PublicKeys []string
85+
}
86+
7387
if outputJson {
74-
jsonBytes, err := json.MarshalIndent(&eps, "", " ")
88+
dd := discoveryData{ACIEndpoints: eps, PublicKeys: publicKeys}
89+
jsonBytes, err := json.MarshalIndent(dd, "", " ")
7590
if err != nil {
7691
stderr("error generating JSON: %s", err)
7792
return 1
7893
}
7994
fmt.Println(string(jsonBytes))
8095
} else {
81-
for _, aciEndpoint := range eps.ACIEndpoints {
96+
for _, aciEndpoint := range eps {
8297
fmt.Printf("ACI: %s, ASC: %s\n", aciEndpoint.ACI, aciEndpoint.ASC)
8398
}
84-
if len(eps.Keys) > 0 {
85-
fmt.Println("Keys: " + strings.Join(eps.Keys, ","))
99+
if len(publicKeys) > 0 {
100+
fmt.Println("PublicKeys: " + strings.Join(publicKeys, ","))
86101
}
87102
}
88103
}

discovery/discovery.go

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,17 @@ type ACIEndpoint struct {
3737
ASC string
3838
}
3939

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 {
4144
ACIEndpoints []ACIEndpoint
42-
Keys []string
45+
PublicKeys []string
4346
}
4447

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
4951

5052
const (
5153
defaultVersion = "latest"
@@ -124,7 +126,7 @@ func createTemplateVars(app App) []string {
124126
return tplVars
125127
}
126128

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) {
128130
app = *app.Copy()
129131
if app.Labels["version"] == "" {
130132
app.Labels["version"] = defaultVersion
@@ -140,7 +142,7 @@ func doDiscover(pre string, hostHeaders map[string]http.Header, app App, insecur
140142

141143
tplVars := createTemplateVars(app)
142144

143-
de := &Endpoints{}
145+
dd := &discoveryData{}
144146

145147
for _, m := range meta {
146148
if !strings.HasPrefix(app.Name.String(), m.prefix) {
@@ -159,41 +161,37 @@ func doDiscover(pre string, hostHeaders map[string]http.Header, app App, insecur
159161
if !ok {
160162
continue
161163
}
162-
de.ACIEndpoints = append(de.ACIEndpoints, ACIEndpoint{ACI: aci, ASC: asc})
164+
dd.ACIEndpoints = append(dd.ACIEndpoints, ACIEndpoint{ACI: aci, ASC: asc})
163165

164166
case "ac-discovery-pubkeys":
165-
de.Keys = append(de.Keys, m.uri)
167+
dd.PublicKeys = append(dd.PublicKeys, m.uri)
166168
}
167169
}
168170

169-
return de, nil
171+
return dd, nil
170172
}
171173

172174
// DiscoverWalk will make HTTPS requests to find discovery meta tags and
173175
// optionally will use HTTP if insecure is set. hostHeaders specifies the
174176
// header to apply depending on the host (e.g. authentication). Based on the
175177
// 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) {
181179
parts := strings.Split(string(app.Name), "/")
182180
for i := range parts {
183181
end := len(parts) - i
184182
pre := strings.Join(parts[:end], "/")
185183

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
189187
}
190188
}
191189

192-
return
190+
return nil, fmt.Errorf("discovery failed")
193191
}
194192

195193
// 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
197195

198196
// FailedAttempt represents a failed discovery attempt. This is for debugging
199197
// and user feedback.
@@ -202,59 +200,58 @@ type FailedAttempt struct {
202200
Error error
203201
}
204202

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 {
207205
if err != nil {
208206
*attempts = append(*attempts, FailedAttempt{pre, err})
209207
return nil
210208
}
211-
out.Append(*eps)
212-
if err := testFn(pre, eps, err); err != nil {
209+
if err := testFn(pre, dd, err); err != nil {
213210
return err
214211
}
215212
return nil
216213
}
217214
}
218215

219-
// DiscoverEndpoints will make HTTPS requests to find the ac-discovery meta
216+
// DiscoverACIEndpoints will make HTTPS requests to find the ac-discovery meta
220217
// tags and optionally will use HTTP if insecure is set. hostHeaders
221218
// specifies the header to apply depending on the host (e.g. authentication).
222219
// It will not give up until it has exhausted the path or found an image
223220
// 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 {
228224
return errEnough
229225
}
230226
return nil
231227
}
232228

233-
err = DiscoverWalk(app, hostHeaders, insecure, walker(out, &attempts, testFn))
229+
attempts := []FailedAttempt{}
230+
dd, err := DiscoverWalk(app, hostHeaders, insecure, walker(&attempts, testFn))
234231
if err != nil && err != errEnough {
235232
return nil, attempts, err
236233
}
237234

238-
return out, attempts, nil
235+
return dd.ACIEndpoints, attempts, nil
239236
}
240237

241238
// DiscoverPublicKey will make HTTPS requests to find the ac-public-keys meta
242239
// tags and optionally will use HTTP if insecure is set. hostHeaders
243240
// specifies the header to apply depending on the host (e.g. authentication).
244241
// 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 {
249245
return errEnough
250246
}
251247
return nil
252248
}
253249

254-
err = DiscoverWalk(app, hostHeaders, insecure, walker(out, &attempts, testFn))
250+
attempts := []FailedAttempt{}
251+
dd, err := DiscoverWalk(app, hostHeaders, insecure, walker(&attempts, testFn))
255252
if err != nil && err != errEnough {
256253
return nil, attempts, err
257254
}
258255

259-
return out, attempts, nil
256+
return dd.PublicKeys, attempts, nil
260257
}

0 commit comments

Comments
 (0)