Skip to content

Commit

Permalink
Add ensure shape config as a method, rename oci black box tests
Browse files Browse the repository at this point in the history
This commit adds a ensureShapeConfig method to make sure we are setting
the correct shape config before launching an OCI instance.
Add white box unit tests on provider/oci and therefore rename all the
current tests to *_integration_test.go.
  • Loading branch information
nvinuesa committed Oct 3, 2023
1 parent 400940d commit 7c938cd
Show file tree
Hide file tree
Showing 12 changed files with 1,407 additions and 1,229 deletions.
9 changes: 6 additions & 3 deletions environs/instances/instancetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ type InstanceType struct {
Cost uint64
RootDisk uint64
// These attributes are not supported by all clouds.
VirtType *string // The type of virtualisation used by the hypervisor, must match the image.
CpuPower *uint64
Tags []string
VirtType *string // The type of virtualisation used by the hypervisor, must match the image.
CpuPower *uint64
Tags []string
// These two values are needed to know the maximum value of cpu and
// memory on flexible/custom instances. Currently only supported on
// OCI.
MaxCpuCores *uint64
MaxMem *uint64
}
Expand Down
File renamed without changes.
48 changes: 30 additions & 18 deletions provider/oci/environ.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,24 +617,7 @@ func (e *Environ) startInstance(
FreeformTags: tags,
}

// If the selected spec is a flexible shape, we must provide the number
// of OCPUs at least, so if the user hasn't provided cpu constraints we
// must pass the default value.
if (spec.InstanceType.MaxCpuCores != nil && spec.InstanceType.MaxCpuCores != &spec.InstanceType.CpuCores) ||
(spec.InstanceType.MaxMem != nil && spec.InstanceType.MaxMem != &spec.InstanceType.Mem) {
instanceDetails.ShapeConfig = &ociCore.LaunchInstanceShapeConfigDetails{}
if args.Constraints.HasCpuCores() {
cpuCores := float32(*args.Constraints.CpuCores)
instanceDetails.ShapeConfig.Ocpus = &cpuCores
} else {
cpuCores := float32(instances.MinCpuCores)
instanceDetails.ShapeConfig.Ocpus = &cpuCores
}
if args.Constraints.HasMem() {
mem := float32(*args.Constraints.Mem)
instanceDetails.ShapeConfig.MemoryInGBs = &mem
}
}
ensureShapeConfig(spec.InstanceType, args.Constraints, &instanceDetails)

request := ociCore.LaunchInstanceRequest{
LaunchInstanceDetails: instanceDetails,
Expand Down Expand Up @@ -672,6 +655,35 @@ func (e *Environ) startInstance(
return result, nil
}

func ensureShapeConfig(
instanceSpec instances.InstanceType,
constraints constraints.Value,
instanceDetails *ociCore.LaunchInstanceDetails) {

// If the selected spec is a flexible shape, we must provide the number
// of OCPUs at least, so if the user hasn't provided cpu constraints we
// must pass the default value.
if (instanceSpec.MaxCpuCores != nil && instanceSpec.MaxCpuCores != &instanceSpec.CpuCores) ||
(instanceSpec.MaxMem != nil && instanceSpec.MaxMem != &instanceSpec.Mem) {
instanceDetails.ShapeConfig = &ociCore.LaunchInstanceShapeConfigDetails{}
if constraints.HasCpuCores() {
cpuCores := float32(*constraints.CpuCores)
instanceDetails.ShapeConfig.Ocpus = &cpuCores
} else {
cpuCores := float32(instances.MinCpuCores)
instanceDetails.ShapeConfig.Ocpus = &cpuCores
}
// If we don't set the memory on ShapeConfig, OCI uses a
// default value of memory per Ocpu core. For example, for the
// VM.Standard.A1.Flex, if we set 2 Ocpus OCI will set 12GB of
// memory (default is 6GB per core).
if constraints.HasMem() {
mem := float32(*constraints.Mem)
instanceDetails.ShapeConfig.MemoryInGBs = &mem
}
}
}

// StopInstances implements environs.InstanceBroker.
func (e *Environ) StopInstances(ctx envcontext.ProviderCallContext, ids ...instance.Id) error {
ociInstances, err := e.getOciInstances(ctx, ids...)
Expand Down
Loading

0 comments on commit 7c938cd

Please sign in to comment.