Skip to content

Commit

Permalink
Avoid expesive pointer copy in capi nodegroup
Browse files Browse the repository at this point in the history
  • Loading branch information
enxebre committed May 7, 2024
1 parent 3fd892a commit 31fdc39
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
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 @@ -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

0 comments on commit 31fdc39

Please sign in to comment.