// findInstanceSpec returns an InstanceSpec satisfying the supplied instanceConstraint. func findInstanceSpec( allImageMetadata []*imagemetadata.ImageMetadata, ic *instances.InstanceConstraint, ) (*instances.InstanceSpec, error) { // If the instance type is set, don't also set a default CPU power // as this is implied. cons := ic.Constraints if cons.CpuPower == nil && (cons.InstanceType == nil || *cons.InstanceType == "") { ic.Constraints.CpuPower = instances.CpuPower(defaultCpuPower) } suitableImages := filterImages(allImageMetadata, ic) images := instances.ImageMetadataToImages(suitableImages) // Make a copy of the known EC2 instance types, filling in the cost for the specified region. regionCosts := allRegionCosts[ic.Region] if len(regionCosts) == 0 && len(allRegionCosts) > 0 { return nil, fmt.Errorf("no instance types found in %s", ic.Region) } var itypesWithCosts []instances.InstanceType for _, itype := range allInstanceTypes { cost, ok := regionCosts[itype.Name] if !ok { continue } itWithCost := itype itWithCost.Cost = cost itypesWithCosts = append(itypesWithCosts, itWithCost) } return instances.FindInstanceSpec(images, ic, itypesWithCosts) }
// FindInstanceSpec returns an InstanceSpec satisfying the supplied instanceConstraint. func (env *joyentEnviron) FindInstanceSpec(ic *instances.InstanceConstraint) (*instances.InstanceSpec, error) { // Require at least one VCPU so we get KVM rather than smart package. if ic.Constraints.CpuCores == nil { ic.Constraints.CpuCores = &defaultCpuCores } allInstanceTypes, err := env.listInstanceTypes() if err != nil { return nil, err } imageConstraint := imagemetadata.NewImageConstraint(simplestreams.LookupParams{ CloudSpec: simplestreams.CloudSpec{ic.Region, env.Ecfg().SdcUrl()}, Series: []string{ic.Series}, Arches: ic.Arches, }) sources, err := environs.ImageMetadataSources(env) if err != nil { return nil, err } matchingImages, _, err := imagemetadata.Fetch(sources, imageConstraint, signedImageDataOnly) if err != nil { return nil, err } images := instances.ImageMetadataToImages(matchingImages) spec, err := instances.FindInstanceSpec(images, ic, allInstanceTypes) if err != nil { return nil, err } return spec, nil }
// findInstanceSpec returns an image and instance type satisfying the constraint. // The instance type comes from querying the flavors supported by the deployment. func findInstanceSpec( e *Environ, ic *instances.InstanceConstraint, imageMetadata []*imagemetadata.ImageMetadata, ) (*instances.InstanceSpec, error) { // first construct all available instance types from the supported flavors. nova := e.nova() flavors, err := nova.ListFlavorsDetail() if err != nil { return nil, err } allInstanceTypes := []instances.InstanceType{} for _, flavor := range flavors { instanceType := instances.InstanceType{ Id: flavor.Id, Name: flavor.Name, Arches: ic.Arches, Mem: uint64(flavor.RAM), CpuCores: uint64(flavor.VCPUs), RootDisk: uint64(flavor.Disk * 1024), // tags not currently supported on openstack } allInstanceTypes = append(allInstanceTypes, instanceType) } images := instances.ImageMetadataToImages(imageMetadata) spec, err := instances.FindInstanceSpec(images, ic, allInstanceTypes) if err != nil { return nil, err } return spec, nil }
// findInstanceSpec initializes a new instance spec for the given // constraints and returns it. This only covers populating the // initial data for the spec. func (env *environ) findInstanceSpec( ic *instances.InstanceConstraint, imageMetadata []*imagemetadata.ImageMetadata, ) (*instances.InstanceSpec, error) { images := instances.ImageMetadataToImages(imageMetadata) spec, err := instances.FindInstanceSpec(images, ic, allInstanceTypes) return spec, errors.Trace(err) }
// findInstanceSpec returns the InstanceSpec that best satisfies the supplied // InstanceConstraint. func findInstanceSpec(env *azureEnviron, constraint *instances.InstanceConstraint) (*instances.InstanceSpec, error) { constraint.Constraints = defaultToBaselineSpec(constraint.Constraints) imageData, err := findMatchingImages(env, constraint.Region, constraint.Series, constraint.Arches) if err != nil { return nil, err } images := instances.ImageMetadataToImages(imageData) instanceTypes, err := listInstanceTypes(env) if err != nil { return nil, err } return instances.FindInstanceSpec(images, constraint, instanceTypes) }
// findInstanceSpec returns an image and instance type satisfying the constraint. // The instance type comes from querying the flavors supported by the deployment. func findInstanceSpec( e *Environ, ic *instances.InstanceConstraint, imageMetadata []*imagemetadata.ImageMetadata, ) (*instances.InstanceSpec, error) { // First construct all available instance types from the supported flavors. nova := e.nova() flavors, err := nova.ListFlavorsDetail() if err != nil { return nil, err } // Not all needed information is available in flavors, // for e.g. architectures or virtualisation types. // For these properties, we assume that all instance types support // all values. allInstanceTypes := []instances.InstanceType{} for _, flavor := range flavors { instanceType := instances.InstanceType{ Id: flavor.Id, Name: flavor.Name, Arches: ic.Arches, Mem: uint64(flavor.RAM), CpuCores: uint64(flavor.VCPUs), RootDisk: uint64(flavor.Disk * 1024), // tags not currently supported on openstack } if ic.Constraints.HasVirtType() { // Instance Type virtual type depends on the virtual type of the selected image, i.e. // picking an image with a virt type gives a machine with this virt type. instanceType.VirtType = ic.Constraints.VirtType } allInstanceTypes = append(allInstanceTypes, instanceType) } images := instances.ImageMetadataToImages(imageMetadata) spec, err := instances.FindInstanceSpec(images, ic, allInstanceTypes) if err != nil { return nil, err } // If instance constraints did not have a virtualisation type, // but image metadata did, we will have an instance type // with virtualisation type of an image. if !ic.Constraints.HasVirtType() && spec.Image.VirtType != "" { spec.InstanceType.VirtType = &spec.Image.VirtType } return spec, nil }
// findInstanceSpec returns an InstanceSpec satisfying the supplied instanceConstraint. func findInstanceSpec( sources []simplestreams.DataSource, stream string, ic *instances.InstanceConstraint) (*instances.InstanceSpec, error) { // If the instance type is set, don't also set a default CPU power // as this is implied. cons := ic.Constraints if cons.CpuPower == nil && (cons.InstanceType == nil || *cons.InstanceType == "") { ic.Constraints.CpuPower = instances.CpuPower(defaultCpuPower) } ec2Region := allRegions[ic.Region] imageConstraint := imagemetadata.NewImageConstraint(simplestreams.LookupParams{ CloudSpec: simplestreams.CloudSpec{ic.Region, ec2Region.EC2Endpoint}, Series: []string{ic.Series}, Arches: ic.Arches, Stream: stream, }) matchingImages, _, err := imagemetadata.Fetch(sources, imageConstraint, signedImageDataOnly) if err != nil { return nil, err } if len(matchingImages) == 0 { logger.Warningf("no matching image meta data for constraints: %v", ic) } suitableImages := filterImages(matchingImages, ic) images := instances.ImageMetadataToImages(suitableImages) // Make a copy of the known EC2 instance types, filling in the cost for the specified region. regionCosts := allRegionCosts[ic.Region] if len(regionCosts) == 0 && len(allRegionCosts) > 0 { return nil, fmt.Errorf("no instance types found in %s", ic.Region) } var itypesWithCosts []instances.InstanceType for _, itype := range allInstanceTypes { cost, ok := regionCosts[itype.Name] if !ok { continue } itWithCost := itype itWithCost.Cost = cost itypesWithCosts = append(itypesWithCosts, itWithCost) } return instances.FindInstanceSpec(images, ic, itypesWithCosts) }
// FindInstanceSpec returns an InstanceSpec satisfying the supplied instanceConstraint. func (env *joyentEnviron) FindInstanceSpec( ic *instances.InstanceConstraint, imageMetadata []*imagemetadata.ImageMetadata, ) (*instances.InstanceSpec, error) { // Require at least one VCPU so we get KVM rather than smart package. if ic.Constraints.CpuCores == nil { ic.Constraints.CpuCores = &defaultCpuCores } allInstanceTypes, err := env.listInstanceTypes() if err != nil { return nil, err } images := instances.ImageMetadataToImages(imageMetadata) spec, err := instances.FindInstanceSpec(images, ic, allInstanceTypes) if err != nil { return nil, err } return spec, nil }
// findInstanceSpec returns an image and instance type satisfying the constraint. // The instance type comes from querying the flavors supported by the deployment. func findInstanceSpec(e *environ, ic *instances.InstanceConstraint) (*instances.InstanceSpec, error) { // first construct all available instance types from the supported flavors. nova := e.nova() flavors, err := nova.ListFlavorsDetail() if err != nil { return nil, err } allInstanceTypes := []instances.InstanceType{} for _, flavor := range flavors { instanceType := instances.InstanceType{ Id: flavor.Id, Name: flavor.Name, Arches: ic.Arches, Mem: uint64(flavor.RAM), CpuCores: uint64(flavor.VCPUs), RootDisk: uint64(flavor.Disk * 1024), // tags not currently supported on openstack } allInstanceTypes = append(allInstanceTypes, instanceType) } imageConstraint := imagemetadata.NewImageConstraint(simplestreams.LookupParams{ CloudSpec: simplestreams.CloudSpec{ic.Region, e.ecfg().authURL()}, Series: []string{ic.Series}, Arches: ic.Arches, Stream: e.Config().ImageStream(), }) sources, err := imagemetadata.GetMetadataSources(e) if err != nil { return nil, err } // TODO (wallyworld): use an env parameter (default true) to mandate use of only signed image metadata. matchingImages, _, err := imagemetadata.Fetch(sources, simplestreams.DefaultIndexPath, imageConstraint, false) if err != nil { return nil, err } images := instances.ImageMetadataToImages(matchingImages) spec, err := instances.FindInstanceSpec(images, ic, allInstanceTypes) if err != nil { return nil, err } return spec, nil }
// findInstanceSpec initializes a new instance spec for the given stream // (and constraints) and returns it. This only covers populating the // initial data for the spec. However, it does include fetching the // correct simplestreams image data. func (env *environ) findInstanceSpec(stream string, ic *instances.InstanceConstraint) (*instances.InstanceSpec, error) { sources, err := environs.ImageMetadataSources(env) if err != nil { return nil, errors.Trace(err) } imageConstraint := imagemetadata.NewImageConstraint(simplestreams.LookupParams{ CloudSpec: env.cloudSpec(ic.Region), Series: []string{ic.Series}, Arches: ic.Arches, Stream: stream, }) signedImageDataOnly := false matchingImages, _, err := imageMetadataFetch(sources, imageConstraint, signedImageDataOnly) if err != nil { return nil, errors.Trace(err) } images := instances.ImageMetadataToImages(matchingImages) spec, err := instances.FindInstanceSpec(images, ic, allInstanceTypes) return spec, errors.Trace(err) }