-
Notifications
You must be signed in to change notification settings - Fork 102
runners: Add scaleCycle lambda to reuse runners #6892
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
Open
seemethere
wants to merge
2
commits into
main
Choose a base branch
from
gh/seemethere/3/head
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
terraform-aws-github-runner/modules/runners/lambdas/runners/src/scale-runners/scale-cycle.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { Config } from './config'; | ||
import { listRunners, RunnerInputParameters, tryReuseRunner } from './runners'; | ||
import { getRepo, getRepoKey } from './utils'; | ||
import { ScaleCycleMetrics } from './metrics'; | ||
import { getRunnerTypes } from './gh-runners'; | ||
import { createRunnerConfigArgument } from './scale-up'; | ||
|
||
export async function scaleCycle(metrics: ScaleCycleMetrics) { | ||
// Get runner types configuration first | ||
const scaleConfigRepo = getRepo(Config.Instance.scaleConfigOrg, Config.Instance.scaleConfigRepo); | ||
const runnerTypes = await getRunnerTypes(scaleConfigRepo, metrics); | ||
|
||
// Get all valid runner type names for filtering | ||
const validRunnerTypeNames = Array.from(runnerTypes.keys()); | ||
|
||
// Make separate calls for each runner type to filter at EC2 level | ||
const allRunners = await Promise.all( | ||
validRunnerTypeNames.map((runnerTypeName) => | ||
listRunners(metrics, { | ||
containsTags: ['GithubRunnerID', 'EphemeralRunnerFinished', 'RunnerType'], | ||
runnerType: runnerTypeName, | ||
}), | ||
), | ||
); | ||
|
||
// Flatten the results | ||
const runners = allRunners.flat(); | ||
|
||
for (const runner of runners) { | ||
// Skip if required fields are missing (org/repo still need to be checked) | ||
if (!runner.runnerType || !runner.org || !runner.repo) { | ||
console.warn(`Skipping runner ${runner.instanceId} due to missing required tags`); | ||
continue; | ||
} | ||
|
||
// Get the RunnerType object from the string (we know it exists since we filtered by it) | ||
const runnerType = runnerTypes.get(runner.runnerType); | ||
if (!runnerType) { | ||
console.warn(`Unknown runner type: ${runner.runnerType}, skipping`); | ||
continue; | ||
} | ||
|
||
// Create repo object | ||
const repo = getRepo(runner.org, runner.repo); | ||
|
||
// For each runner send an EBS volume replacement task | ||
const runnerInputParameters: RunnerInputParameters = { | ||
runnerConfig: (awsRegion: string, experimentalRunner: boolean) => { | ||
return createRunnerConfigArgument( | ||
runnerType, | ||
repo, | ||
// NOTE: installationId can actually be undefined here but this may incur lower rate limits | ||
// TODO: figure out if we need to pass an actual installationId here | ||
undefined, | ||
metrics, | ||
awsRegion, | ||
experimentalRunner, | ||
); | ||
}, | ||
environment: Config.Instance.environment, | ||
runnerType: runnerType, | ||
}; | ||
|
||
// Set orgName or repoName based on configuration | ||
if (Config.Instance.enableOrganizationRunners) { | ||
runnerInputParameters.orgName = runner.org; | ||
metrics.scaleCycleRunnerReuseFoundOrg(runner.org, runner.runnerType); | ||
console.info(`Reusing runner ${runner.instanceId} for ${runner.org}`); | ||
} else { | ||
runnerInputParameters.repoName = getRepoKey(repo); | ||
metrics.scaleCycleRunnerReuseFoundRepo(getRepoKey(repo), runner.runnerType); | ||
console.info(`Reusing runner ${runner.instanceId} for ${getRepoKey(repo)}`); | ||
} | ||
|
||
await tryReuseRunner(runnerInputParameters, metrics); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be a little too high level of a function to be invoking here.
In this loop you've already identified a candidate list of runners that are probably ready to be scaled down.
tryReuseRunner
however looses context on what those candidates were and on each loop it'll starts from scratch again to find a potential runner it can scale down, starting with asking EC2 to list all runners of this runner type.What do you think about adding a refactor to do something like the following:
Goal is to not end up looping over the same instances over and over again during a single ScaleCycle invocation