Skip to content

Commit

Permalink
processor map values need - to prevent mapping to wrong processor
Browse files Browse the repository at this point in the history
  • Loading branch information
cfullen committed Mar 24, 2024
1 parent 1443ddb commit 9239b77
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 12 deletions.
24 changes: 12 additions & 12 deletions cloud/scope/machinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ type (
}
)

// map of machine types to their respective processors (e2 cannot have a min cpu platform set, so it is not included here)

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

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
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",
}

// NewMachinePoolScope creates a new MachinePoolScope from the supplied parameters.
func NewMachinePoolScope(params MachinePoolScopeParams) (*MachinePoolScope, error) {
if params.Client == nil {
Expand Down Expand Up @@ -275,24 +286,13 @@ func (m *MachinePoolScope) InstanceImageSpec() *compute.AttachedDisk {

// MinCPUPlatform returns the min cpu platform for the machine pool.
func (m *MachinePoolScope) MinCPUPlatform() string {
// 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 {
for machineType, processor := range Processors {
if strings.HasPrefix(m.GCPMachinePool.Spec.InstanceType, machineType) {
return processor
}
Expand Down
74 changes: 74 additions & 0 deletions cloud/scope/machinepool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package scope_test

import (
"testing"

Check failure on line 4 in cloud/scope/machinepool_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofmt`-ed with `-s` (gofmt)
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
infrav1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-gcp/cloud"
"sigs.k8s.io/cluster-api-provider-gcp/exp/api/v1beta1"
clusterv1exp "sigs.k8s.io/cluster-api/exp/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/cluster-api-provider-gcp/cloud/scope"
)


var _ = Describe("GCPManagedMachinePool Scope", func() {
var TestMachinePoolScope *scope.MachinePoolScope
var getter cloud.ClusterGetter
var t *testing.T

BeforeEach(func() {
// Register the MachinePool, GCPMachinePool and GCPMachinePoolList in a schema.
schema, err := infrav1.SchemeBuilder.Register(&clusterv1exp.MachinePool{},&v1beta1.GCPMachinePool{}, &v1beta1.GCPMachinePoolList{}).Build()
// Make sure no errors were triggered.
assert.Nil(t, err)

// Create a controller fake client.
// It's best to use envtest but in this case we are not really using the client
// just passing it as parameter to the NewMachinePoolScope to test the mincpu.
testClient := fake.NewClientBuilder().WithScheme(schema).Build()

// Create the machinepool scope
params := scope.MachinePoolScopeParams{
Client: testClient,
ClusterGetter: getter,
MachinePool: &clusterv1exp.MachinePool{
ObjectMeta: metav1.ObjectMeta{},
Spec: clusterv1exp.MachinePoolSpec{},
},
GCPMachinePool: &v1beta1.GCPMachinePool{
ObjectMeta: metav1.ObjectMeta{},
Spec: v1beta1.GCPMachinePoolSpec{},
},
}
TestMachinePoolScope, _ = scope.NewMachinePoolScope(params)

// Make sure the machinepool scope is created correctly.
assert.Nil(t, err)
assert.NotNil(t, TestMachinePoolScope)

})

Describe("Min CPU Mappings", func() {
Context("instance types", func() {
It("should match all", func() {
for k := range(scope.Processors) {
TestMachinePoolScope.GCPMachinePool.Spec.InstanceType = fmt.Sprintf("%sstandard-8", k)
cpu := TestMachinePoolScope.MinCPUPlatform()
Expect(cpu).To(Equal(scope.Processors[k]))
}
})
It("should not match n2", func() {
TestMachinePoolScope.GCPMachinePool.Spec.InstanceType = "n2d-standard-8"
cpu := TestMachinePoolScope.MinCPUPlatform()
Expect(cpu).NotTo(Equal(scope.Processors["n2"]))
})
})
})
})

0 comments on commit 9239b77

Please sign in to comment.