~fwereade/juju-core/manifest-deployer

« back to all changes in this revision

Viewing changes to provider/ec2/ec2.go

[r=wallyworld] Properly implement SupportedArchitectures()

Query the simplestreams image metadata to extract all the
architectures for the images defined for the environment's
region. These are the architectures we are intersted in.

Also refactor the azure tests to clean them up.

https://codereview.appspot.com/78030045/

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
type environ struct {
53
53
        name string
54
54
 
 
55
        // archMutex gates access to supportedArchitectures
 
56
        archMutex sync.Mutex
 
57
        // supportedArchitectures caches the architectures
 
58
        // for which images can be instantiated.
 
59
        supportedArchitectures []string
 
60
 
55
61
        // ecfgMutex protects the *Unlocked fields below.
56
62
        ecfgMutex       sync.Mutex
57
63
        ecfgUnlocked    *environConfig
230
236
        return p.Open(cfg)
231
237
}
232
238
 
233
 
// supportedArches lists the CPU architectures supported by EC2.
234
 
// TODO(wallyworld): EC2 could possibly support arm and ppc but we only record
235
 
// instance metadata for amd64 and i386. See allInstanceTypes in instancetype.go
236
 
var supportedArches = []string{arch.AMD64, arch.I386}
237
 
 
238
239
// MetadataLookupParams returns parameters which are used to query image metadata to
239
240
// find matching image information.
240
241
func (p environProvider) MetadataLookupParams(region string) (*simplestreams.MetadataLookupParams, error) {
248
249
        return &simplestreams.MetadataLookupParams{
249
250
                Region:        region,
250
251
                Endpoint:      ec2Region.EC2Endpoint,
251
 
                Architectures: supportedArches,
 
252
                Architectures: arch.AllSupportedArches,
252
253
        }, nil
253
254
}
254
255
 
338
339
}
339
340
 
340
341
// SupportedArchitectures is specified on the EnvironCapability interface.
341
 
func (*environ) SupportedArchitectures() ([]string, error) {
342
 
        return supportedArches, nil
 
342
func (e *environ) SupportedArchitectures() ([]string, error) {
 
343
        e.archMutex.Lock()
 
344
        defer e.archMutex.Unlock()
 
345
        if e.supportedArchitectures != nil {
 
346
                return e.supportedArchitectures, nil
 
347
        }
 
348
        // Create a filter to get all images from our region and for the correct stream.
 
349
        cloudSpec, err := e.Region()
 
350
        if err != nil {
 
351
                return nil, err
 
352
        }
 
353
        imageConstraint := imagemetadata.NewImageConstraint(simplestreams.LookupParams{
 
354
                CloudSpec: cloudSpec,
 
355
                Stream:    e.Config().ImageStream(),
 
356
        })
 
357
        e.supportedArchitectures, err = common.SupportedArchitectures(e, imageConstraint)
 
358
        return e.supportedArchitectures, err
343
359
}
344
360
 
345
361
// MetadataLookupParams returns parameters which are used to query simplestreams metadata.
347
363
        if region == "" {
348
364
                region = e.ecfg().region()
349
365
        }
350
 
        ec2Region, ok := allRegions[region]
351
 
        if !ok {
352
 
                return nil, fmt.Errorf("unknown region %q", region)
 
366
        cloudSpec, err := e.cloudSpec(region)
 
367
        if err != nil {
 
368
                return nil, err
353
369
        }
354
370
        return &simplestreams.MetadataLookupParams{
355
371
                Series:        e.ecfg().DefaultSeries(),
356
 
                Region:        region,
357
 
                Endpoint:      ec2Region.EC2Endpoint,
358
 
                Architectures: supportedArches,
 
372
                Region:        cloudSpec.Region,
 
373
                Endpoint:      cloudSpec.Endpoint,
 
374
                Architectures: arch.AllSupportedArches,
359
375
        }, nil
360
376
}
361
377
 
362
378
// Region is specified in the HasRegion interface.
363
379
func (e *environ) Region() (simplestreams.CloudSpec, error) {
364
 
        region := e.ecfg().region()
 
380
        return e.cloudSpec(e.ecfg().region())
 
381
}
 
382
 
 
383
func (e *environ) cloudSpec(region string) (simplestreams.CloudSpec, error) {
365
384
        ec2Region, ok := allRegions[region]
366
385
        if !ok {
367
386
                return simplestreams.CloudSpec{}, fmt.Errorf("unknown region %q", region)