Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid expesive pointer copy in capi nodegroup #6796

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -672,13 +672,13 @@ func (c *machineController) findScalableResourceProviderIDs(scalableResource *un
return providerIDs, nil
}

func (c *machineController) nodeGroups() ([]*nodegroup, error) {
func (c *machineController) nodeGroups() ([]cloudprovider.NodeGroup, error) {
scalableResources, err := c.listScalableResources()
if err != nil {
return nil, err
}

nodegroups := make([]*nodegroup, 0, len(scalableResources))
nodegroups := make([]cloudprovider.NodeGroup, 0, len(scalableResources))

for _, r := range scalableResources {
ng, err := newNodeGroupFromScalableResource(c, r)
Expand All @@ -688,6 +688,7 @@ func (c *machineController) nodeGroups() ([]*nodegroup, error) {

if ng != nil {
nodegroups = append(nodegroups, ng)
klog.V(4).Infof("discovered node group: %s", ng.Debug())
}
}
return nodegroups, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type testSpec struct {
const customCAPIGroup = "custom.x-k8s.io"
const fifteenSecondDuration = time.Second * 15

func mustCreateTestController(t *testing.T, testConfigs ...*testConfig) (*machineController, testControllerShutdownFunc) {
func mustCreateTestController(t testing.TB, testConfigs ...*testConfig) (*machineController, testControllerShutdownFunc) {
t.Helper()

nodeObjects := make([]runtime.Object, 0)
Expand Down Expand Up @@ -492,7 +492,7 @@ func makeLinkedNodeAndMachine(i int, namespace, clusterName string, owner metav1
return node, machine
}

func addTestConfigs(t *testing.T, controller *machineController, testConfigs ...*testConfig) error {
func addTestConfigs(t testing.TB, controller *machineController, testConfigs ...*testConfig) error {
t.Helper()

for _, config := range testConfigs {
Expand Down Expand Up @@ -2113,15 +2113,15 @@ func Test_machineController_nodeGroups(t *testing.T) {

// Sort results as order is not guaranteed.
sort.Slice(got, func(i, j int) bool {
return got[i].scalableResource.Name() < got[j].scalableResource.Name()
return got[i].(*nodegroup).scalableResource.Name() < got[j].(*nodegroup).scalableResource.Name()
})
sort.Slice(tc.expectedScalableResources, func(i, j int) bool {
return tc.expectedScalableResources[i].GetName() < tc.expectedScalableResources[j].GetName()
})

if err == nil {
for i := range got {
if !reflect.DeepEqual(got[i].scalableResource.unstructured, tc.expectedScalableResources[i]) {
if !reflect.DeepEqual(got[i].(*nodegroup).scalableResource.unstructured, tc.expectedScalableResources[i]) {
t.Errorf("nodeGroups() got = %v, expected to consist of nodegroups for scalable resources: %v", got, tc.expectedScalableResources)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func TestNodeGroupIncreaseSizeErrors(t *testing.T) {
t.Fatalf("expected 1 nodegroup, got %d", l)
}

ng := nodegroups[0]
ng := nodegroups[0].(*nodegroup)
currReplicas, err := ng.TargetSize()
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down Expand Up @@ -348,7 +348,7 @@ func TestNodeGroupIncreaseSize(t *testing.T) {
t.Fatalf("expected 1 nodegroup, got %d", l)
}

ng := nodegroups[0]
ng := nodegroups[0].(*nodegroup)
currReplicas, err := ng.TargetSize()
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down Expand Up @@ -427,7 +427,7 @@ func TestNodeGroupDecreaseTargetSize(t *testing.T) {
t.Fatalf("expected 1 nodegroup, got %d", l)
}

ng := nodegroups[0]
ng := nodegroups[0].(*nodegroup)

gvr, err := ng.scalableResource.GroupVersionResource()
if err != nil {
Expand Down Expand Up @@ -596,7 +596,7 @@ func TestNodeGroupDecreaseSizeErrors(t *testing.T) {
t.Fatalf("expected 1 nodegroup, got %d", l)
}

ng := nodegroups[0]
ng := nodegroups[0].(*nodegroup)
currReplicas, err := ng.TargetSize()
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down Expand Up @@ -676,7 +676,7 @@ func TestNodeGroupDeleteNodes(t *testing.T) {
t.Fatalf("expected 1 nodegroup, got %d", l)
}

ng := nodegroups[0]
ng := nodegroups[0].(*nodegroup)
nodeNames, err := ng.Nodes()
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down Expand Up @@ -889,7 +889,7 @@ func TestNodeGroupDeleteNodesTwice(t *testing.T) {
t.Fatalf("expected 1 nodegroup, got %d", l)
}

ng := nodegroups[0]
ng := nodegroups[0].(*nodegroup)
nodeNames, err := ng.Nodes()
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down Expand Up @@ -961,7 +961,7 @@ func TestNodeGroupDeleteNodesTwice(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}

ng = nodegroups[0]
ng = nodegroups[0].(*nodegroup)

// Check the nodegroup is at the expected size
actualSize, err := ng.TargetSize()
Expand Down Expand Up @@ -1066,7 +1066,7 @@ func TestNodeGroupDeleteNodesSequential(t *testing.T) {
t.Fatalf("expected 1 nodegroup, got %d", l)
}

ng := nodegroups[0]
ng := nodegroups[0].(*nodegroup)
nodeNames, err := ng.Nodes()
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down Expand Up @@ -1132,7 +1132,7 @@ func TestNodeGroupDeleteNodesSequential(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}

ng = nodegroups[0]
ng = nodegroups[0].(*nodegroup)

// Check the nodegroup is at the expected size
actualSize, err := ng.scalableResource.Replicas()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,12 @@ func (p *provider) GetResourceLimiter() (*cloudprovider.ResourceLimiter, error)
}

func (p *provider) NodeGroups() []cloudprovider.NodeGroup {
var result []cloudprovider.NodeGroup
nodegroups, err := p.controller.nodeGroups()
if err != nil {
klog.Errorf("error getting node groups: %v", err)
return nil
}
for _, ng := range nodegroups {
klog.V(4).Infof("discovered node group: %s", ng.Debug())
result = append(result, ng)
}
return result
return nodegroups
}

func (p *provider) NodeGroupForNode(node *corev1.Node) (cloudprovider.NodeGroup, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,29 @@ func TestProviderConstructorProperties(t *testing.T) {
t.Fatalf("expected 0 GPU types, got %d", got)
}
}
func BenchmarkNodeGroups(b *testing.B) {
resourceLimits := cloudprovider.ResourceLimiter{}
annotations := map[string]string{
nodeGroupMinSizeAnnotationKey: "1",
nodeGroupMaxSizeAnnotationKey: "2",
}

controller, stop := mustCreateTestController(b)
defer stop()
machineSetConfigs := createMachineSetTestConfigs("namespace", "", RandomString(6), 100, 1, annotations, nil)
if err := addTestConfigs(b, controller, machineSetConfigs...); err != nil {
b.Fatalf("unexpected error: %v", err)
}

provider := newProvider(cloudprovider.ClusterAPIProviderName, &resourceLimits, controller)
if actual := provider.Name(); actual != cloudprovider.ClusterAPIProviderName {
b.Errorf("expected %q, got %q", cloudprovider.ClusterAPIProviderName, actual)
}

b.ResetTimer()
b.Run("NodeGroups", func(b *testing.B) {
for i := 0; i < b.N; i++ {
provider.NodeGroups()
}
})
}
Loading