Skip to content

Commit

Permalink
adding min cpu platform selection for GCP Machines and MachinePools
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennenMM7 committed Mar 13, 2024
1 parent 1f45913 commit 9c7ee0b
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 4 deletions.
5 changes: 5 additions & 0 deletions api/v1beta1/gcpmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ type GCPMachineSpec struct {
// InstanceType is the type of instance to create. Example: n1.standard-2
InstanceType string `json:"instanceType"`

// MinCPUPlatform is the minimum CPU platform to use for the instance. The instance may be scheduled on the specified or newer CPU platform.
// applicable values are the friendly names of CPU platforms, such as minCpuPlatform: "Intel Haswell" or minCpuPlatform: "Intel Sandy Bridge".
// +optional
MinCPUPlatform *string `json:"minCPUPlatform,omitempty"`

// Subnet is a reference to the subnetwork to use for this instance. If not specified,
// the first subnetwork retrieved from the Cluster Region and Network is picked.
// +optional
Expand Down
5 changes: 5 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 34 additions & 3 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,36 @@ func (m *MachineScope) InstanceImageSpec() *compute.AttachedDisk {
return disk
}

// MinCPUPlatform returns the min cpu platform for the machine pool.
func (m *MachineScope) MinCPUPlatform() string {

Check failure on line 272 in cloud/scope/machine.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)

// map of machine types to their respective processors (e2 cannot have a min cpu platform set, so it is not included here)
var processors = map[string]string{
"n1": "Intel Skylake",
"n2": "Intel Ice Lake",
"n2d": "AMD Milan",
"c3": "Intel Sapphire Rapids",
"c2": "Intel Cascade Lake",
"t2d": "AMD Milan",
"m1": "Intel Skylake",
}

// If the min cpu platform is set on the GCPMachinePool, use the specified value.
if m.GCPMachine.Spec.MinCPUPlatform != nil {
return *m.GCPMachine.Spec.MinCPUPlatform
}

// If the min cpu platform is not set on the GCPMachinePool, use the default value for the machine type.
for machineType, processor := range processors {
if strings.HasPrefix(m.GCPMachine.Spec.InstanceType, machineType) {
return processor
}
}

// If the machine type is not recognized, return an empty string (This will hand off the processor selection to GCP).
return ""
}

// InstanceAdditionalDiskSpec returns compute instance additional attched-disk spec.
func (m *MachineScope) InstanceAdditionalDiskSpec() []*compute.AttachedDisk {
additionalDisks := make([]*compute.AttachedDisk, 0, len(m.GCPMachine.Spec.AdditionalDisks))
Expand Down Expand Up @@ -370,9 +400,10 @@ func (m *MachineScope) InstanceAdditionalMetadataSpec() *compute.Metadata {
// InstanceSpec returns instance spec.
func (m *MachineScope) InstanceSpec(log logr.Logger) *compute.Instance {
instance := &compute.Instance{
Name: m.Name(),
Zone: m.Zone(),
MachineType: path.Join("zones", m.Zone(), "machineTypes", m.GCPMachine.Spec.InstanceType),
Name: m.Name(),
Zone: m.Zone(),
MachineType: path.Join("zones", m.Zone(), "machineTypes", m.GCPMachine.Spec.InstanceType),
MinCpuPlatform: m.MinCPUPlatform(),
Tags: &compute.Tags{
Items: append(
m.GCPMachine.Spec.AdditionalNetworkTags,
Expand Down
33 changes: 32 additions & 1 deletion cloud/scope/machinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ func (m *MachinePoolScope) InstanceGroupTemplateBuilder(bootstrapData string) *c
instanceTemplate := &compute.InstanceTemplate{
Name: m.GCPMachinePool.Name,
Properties: &compute.InstanceProperties{
MachineType: m.GCPMachinePool.Spec.InstanceType,
MachineType: m.GCPMachinePool.Spec.InstanceType,
MinCpuPlatform: m.MinCPUPlatform(),
Tags: &compute.Tags{
Items: append(
m.GCPMachinePool.Spec.AdditionalNetworkTags,
Expand Down Expand Up @@ -272,6 +273,36 @@ func (m *MachinePoolScope) InstanceImageSpec() *compute.AttachedDisk {
}
}

// MinCPUPlatform returns the min cpu platform for the machine pool.
func (m *MachinePoolScope) MinCPUPlatform() string {

Check failure on line 277 in cloud/scope/machinepool.go

View workflow job for this annotation

GitHub Actions / lint

unnecessary leading newline (whitespace)

// map of machine types to their respective processors (e2 cannot have a min cpu platform set, so it is not included here)
var processors = map[string]string{
"n1": "Intel Skylake",
"n2": "Intel Ice Lake",
"n2d": "AMD Milan",
"c3": "Intel Sapphire Rapids",
"c2": "Intel Cascade Lake",
"t2d": "AMD Milan",
"m1": "Intel Skylake",
}

// If the min cpu platform is set on the GCPMachinePool, use the specified value.
if m.GCPMachinePool.Spec.MinCPUPlatform != nil {
return *m.GCPMachinePool.Spec.MinCPUPlatform
}

// If the min cpu platform is not set on the GCPMachinePool, use the default value for the machine type.
for machineType, processor := range processors {
if strings.HasPrefix(m.GCPMachinePool.Spec.InstanceType, machineType) {
return processor
}
}

// If the machine type is not recognized, return an empty string (This will hand off the processor selection to GCP).
return ""
}

// Zone returns the zone for the machine pool.
func (m *MachinePoolScope) Zone() string {
if m.MachinePool.Spec.Template.Spec.FailureDomain == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ spec:
location:
description: Location is the GCP region location ex us-central1
type: string
minCPUPlatform:
description: 'MinCPUPlatform is the minimum CPU platform to use for
the instance. The instance may be scheduled on the specified or
newer CPU platform. applicable values are the friendly names of
CPU platforms, such as minCpuPlatform: "Intel Haswell" or minCpuPlatform:
"Intel Sandy Bridge".'
type: string
network:
description: Network is the network to be used by machines in the
machine pool.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ spec:
- Enabled
- Disabled
type: string
minCPUPlatform:
description: 'MinCPUPlatform is the minimum CPU platform to use for
the instance. The instance may be scheduled on the specified or
newer CPU platform. applicable values are the friendly names of
CPU platforms, such as minCpuPlatform: "Intel Haswell" or minCpuPlatform:
"Intel Sandy Bridge".'
type: string
onHostMaintenance:
description: OnHostMaintenance determines the behavior when a maintenance
event occurs that might cause the instance to reboot. If omitted,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,13 @@ spec:
- Enabled
- Disabled
type: string
minCPUPlatform:
description: 'MinCPUPlatform is the minimum CPU platform to
use for the instance. The instance may be scheduled on the
specified or newer CPU platform. applicable values are the
friendly names of CPU platforms, such as minCpuPlatform:
"Intel Haswell" or minCpuPlatform: "Intel Sandy Bridge".'
type: string
onHostMaintenance:
description: OnHostMaintenance determines the behavior when
a maintenance event occurs that might cause the instance
Expand Down
5 changes: 5 additions & 0 deletions exp/api/v1beta1/gcpmachinepool_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ type GCPMachinePoolSpec struct {
// Location is the GCP region location ex us-central1
Location string `json:"location"`

// MinCPUPlatform is the minimum CPU platform to use for the instance. The instance may be scheduled on the specified or newer CPU platform.
// applicable values are the friendly names of CPU platforms, such as minCpuPlatform: "Intel Haswell" or minCpuPlatform: "Intel Sandy Bridge".
// +optional
MinCPUPlatform *string `json:"minCPUPlatform,omitempty"`

// Network is the network to be used by machines in the machine pool.
Network string `json:"network"`

Expand Down
5 changes: 5 additions & 0 deletions exp/api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9c7ee0b

Please sign in to comment.