@@ -155,38 +155,38 @@ func (c *libcni) Setup(ctx context.Context, id string, path string, opts ...Name
155
155
}
156
156
157
157
type asynchAttachResult struct {
158
- res * types100.Result
159
- err error
158
+ index int
159
+ res * types100.Result
160
+ err error
160
161
}
161
162
162
- func asynchAttach (ctx context.Context , n * Network , ns * Namespace , wg * sync.WaitGroup , rc chan asynchAttachResult ) {
163
+ func asynchAttach (ctx context.Context , index int , n * Network , ns * Namespace , wg * sync.WaitGroup , rc chan asynchAttachResult ) {
163
164
defer wg .Done ()
164
165
r , err := n .Attach (ctx , ns )
165
- rc <- asynchAttachResult {res : r , err : err }
166
+ rc <- asynchAttachResult {index : index , res : r , err : err }
166
167
}
167
168
168
169
func (c * libcni ) attachNetworks (ctx context.Context , ns * Namespace ) ([]* types100.Result , error ) {
169
170
var wg sync.WaitGroup
170
- var lastError error
171
- var results []* types100.Result
171
+ var firstError error
172
+ results := make ( []* types100.Result , len ( c . Networks ()))
172
173
rc := make (chan asynchAttachResult )
173
174
174
- for _ , network := range c .Networks () {
175
+ for i , network := range c .Networks () {
175
176
wg .Add (1 )
176
- go asynchAttach (ctx , network , ns , & wg , rc )
177
+ go asynchAttach (ctx , i , network , ns , & wg , rc )
177
178
}
178
179
179
180
for range c .Networks () {
180
181
rs := <- rc
181
- if rs .err != nil {
182
- lastError = rs .err
183
- } else {
184
- results = append (results , rs .res )
182
+ if rs .err != nil && firstError == nil {
183
+ firstError = rs .err
185
184
}
185
+ results [rs .index ] = rs .res
186
186
}
187
187
wg .Wait ()
188
188
189
- return results , lastError
189
+ return results , firstError
190
190
}
191
191
192
192
// Remove removes the network config from the namespace
0 commit comments