Skip to content

Commit b5cca41

Browse files
authored
feat: add pagination to MachinePool list api calls (#100)
Since it is possible for a MachinePool to be quite large we need to make sure we iterate through all pages of possible machine pool instances. We have another ticket to make sure all other list calls are also paginated.
1 parent 1089d88 commit b5cca41

File tree

1 file changed

+47
-12
lines changed

1 file changed

+47
-12
lines changed

cloud/scope/machine_pool.go

+47-12
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,26 @@ func (m *MachinePoolScope) ListMachinePoolInstances(ctx context.Context) ([]core
165165
CompartmentId: common.String(m.OCICluster.Spec.CompartmentId),
166166
InstancePoolId: common.String(poolOid),
167167
}
168-
resp, err := m.ComputeManagementClient.ListInstancePoolInstances(ctx, req)
169-
if err != nil {
170-
return nil, err
168+
169+
var instanceSummaries []core.InstanceSummary
170+
listPoolInstances := func(ctx context.Context, request core.ListInstancePoolInstancesRequest) (core.ListInstancePoolInstancesResponse, error) {
171+
return m.ComputeManagementClient.ListInstancePoolInstances(ctx, request)
171172
}
173+
for resp, err := listPoolInstances(ctx, req); ; resp, err = listPoolInstances(ctx, req) {
174+
if err != nil {
175+
return nil, err
176+
}
177+
178+
instanceSummaries = append(instanceSummaries, resp.Items...)
172179

173-
return resp.Items, nil
180+
if resp.OpcNextPage == nil {
181+
break
182+
} else {
183+
req.Page = resp.OpcNextPage
184+
}
185+
}
186+
187+
return instanceSummaries, nil
174188
}
175189

176190
// SetListandSetMachinePoolInstances retrieves a machine pools instances and sets them in the ProviderIDList
@@ -412,6 +426,31 @@ func (m *MachinePoolScope) ReconcileInstanceConfiguration(ctx context.Context) e
412426
return nil
413427
}
414428

429+
// ListInstancePoolSummaries list the core.InstancePoolSummary for the given core.ListInstancePoolsRequest
430+
func (m *MachinePoolScope) ListInstancePoolSummaries(ctx context.Context, req core.ListInstancePoolsRequest) ([]core.InstancePoolSummary, error) {
431+
listInstancePools := func(ctx context.Context, request core.ListInstancePoolsRequest) (core.ListInstancePoolsResponse, error) {
432+
return m.ComputeManagementClient.ListInstancePools(ctx, request)
433+
}
434+
435+
var instancePoolSummaries []core.InstancePoolSummary
436+
for resp, err := listInstancePools(ctx, req); ; resp, err = listInstancePools(ctx, req) {
437+
if err != nil {
438+
return instancePoolSummaries, errors.Wrapf(err, "failed to query OCIMachinePool by name")
439+
}
440+
441+
instancePoolSummaries = append(instancePoolSummaries, resp.Items...)
442+
443+
if resp.OpcNextPage == nil {
444+
// no more pages
445+
break
446+
} else {
447+
req.Page = resp.OpcNextPage
448+
}
449+
}
450+
451+
return instancePoolSummaries, nil
452+
}
453+
415454
// FindInstancePool attempts to find the instance pool by name and checks to make sure
416455
// the instance pool was created by the cluster before returning the correct pool
417456
func (m *MachinePoolScope) FindInstancePool(ctx context.Context) (*core.InstancePool, error) {
@@ -422,18 +461,14 @@ func (m *MachinePoolScope) FindInstancePool(ctx context.Context) (*core.Instance
422461
CompartmentId: common.String(m.OCICluster.Spec.CompartmentId),
423462
DisplayName: common.String(m.OCIMachinePool.GetName()),
424463
}
425-
respList, err := m.ComputeManagementClient.ListInstancePools(ctx, reqList)
426-
if err != nil {
427-
return nil, errors.Wrapf(err, "failed to query OCIMachinePool by name")
428-
}
429464

430-
if len(respList.Items) <= 0 {
431-
m.Info("No machine pool found", "machinepool-name", m.OCIMachinePool.GetName())
432-
return nil, nil
465+
instancePoolSummaries, err := m.ListInstancePoolSummaries(ctx, reqList)
466+
if err != nil {
467+
return nil, err
433468
}
434469

435470
var instancePoolSummary *core.InstancePoolSummary
436-
for _, i := range respList.Items {
471+
for _, i := range instancePoolSummaries {
437472
if m.IsResourceCreatedByClusterAPI(i.FreeformTags) {
438473
instancePoolSummary = &i
439474
break

0 commit comments

Comments
 (0)