Skip to content

Commit d6aafd7

Browse files
authored
feat(run-controller): client side filter for running pods list (#577)
1 parent 6963bad commit d6aafd7

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

internal/controllers/terraformrun/states.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -212,44 +212,36 @@ func getStateString(state State) string {
212212
}
213213

214214
func isMaxConcurrentRunnerPodsReached(ctx context.Context, r *Reconciler, repo *configv1alpha1.TerraformRepository) (bool, error) {
215-
// If a global parameter is set, use it, otherwise use the repository parameter
216215
maxConcurrentRunnerPods := r.Config.Controller.MaxConcurrentRunnerPods
217216
if repo.Spec.MaxConcurrentRunnerPods > 0 {
218217
maxConcurrentRunnerPods = repo.Spec.MaxConcurrentRunnerPods
219218
}
220219
if maxConcurrentRunnerPods > 0 {
221-
// count all running and pending burrito pods to avoid exceeding the maximum number of concurrent runs
222220
labelSelector := labels.NewSelector()
223221
requirement, err := labels.NewRequirement("burrito/component", selection.Equals, []string{"runner"})
224222
if err != nil {
225-
log.Errorf("could not list runner pods: %s", err)
223+
log.Errorf("could not create label requirement for runner pods list: %s", err)
226224
return false, err
227225
}
228226
labelSelector = labelSelector.Add(*requirement)
229227

230-
runningPods := &corev1.PodList{}
231-
pendingPods := &corev1.PodList{}
232-
233-
err = r.Client.List(ctx, runningPods,
234-
client.MatchingLabelsSelector{Selector: labelSelector},
235-
client.MatchingFields{"status.phase": "Running"},
236-
)
228+
allPods := &corev1.PodList{}
229+
err = r.Client.List(ctx, allPods, &client.ListOptions{
230+
LabelSelector: labelSelector,
231+
})
237232
if err != nil {
238-
log.Errorf("could not list running pods: %s", err)
233+
log.Errorf("could not list runner pods: %s", err)
239234
return false, err
240235
}
241236

242-
err = r.Client.List(ctx, pendingPods,
243-
client.MatchingLabelsSelector{Selector: labelSelector},
244-
client.MatchingFields{"status.phase": "Pending"},
245-
)
246-
if err != nil {
247-
log.Errorf("could not list pending pods: %s", err)
248-
return false, err
237+
activeCount := 0
238+
for _, pod := range allPods.Items {
239+
if pod.Status.Phase == corev1.PodRunning || pod.Status.Phase == corev1.PodPending {
240+
activeCount++
241+
}
249242
}
250243

251-
totalPods := len(runningPods.Items) + len(pendingPods.Items)
252-
if totalPods >= maxConcurrentRunnerPods {
244+
if activeCount >= maxConcurrentRunnerPods {
253245
log.Infof("max concurrent pods reached, requeuing resource")
254246
return true, nil
255247
}

0 commit comments

Comments
 (0)