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

Enable non-org autoscaler to read scale-config from any repo #5767

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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 @@ -193,7 +193,7 @@ describe('Config', () => {
expect(Config.Instance.mustHaveIssuesLabels).toEqual([]);
expect(Config.Instance.runnerGroupName).toBeUndefined();
expect(Config.Instance.runnersExtraLabels).toBeUndefined();
expect(Config.Instance.scaleConfigRepo).toEqual('test-infra');
expect(Config.Instance.scaleConfigRepo).toEqual('');
expect(Config.Instance.scaleConfigRepoPath).toEqual('.github/scale-config.yml');
expect(Config.Instance.secretsManagerSecretsId).toBeUndefined();
expect(Config.Instance.shuffledAwsRegionInstances).toEqual([]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class Config {
this.runnerGroupName = process.env.RUNNER_GROUP_NAME;
this.runnersExtraLabels = process.env.RUNNER_EXTRA_LABELS;
/* istanbul ignore next */
this.scaleConfigRepo = process.env.SCALE_CONFIG_REPO || 'test-infra';
this.scaleConfigRepo = process.env.SCALE_CONFIG_REPO || '';
this.scaleConfigRepoPath = process.env.SCALE_CONFIG_REPO_PATH || '.github/scale-config.yml';
this.secretsManagerSecretsId = process.env.SECRETSMANAGER_SECRETS_ID;
this.sSMParamCleanupAgeDays = Number(process.env.SSM_PARAM_CLEANUP_AGE_DAYS || '7');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1256,57 +1256,118 @@ describe('scale-down', () => {
describe('repo runners', () => {
const runnerType = 'runnerTypeDef';
const owner = 'the-org';
const repo: Repo = {
const runnerRepo: Repo = {
owner: owner,
repo: 'a-repo',
};
const repoKey = `${owner}/a-repo`;
const runnerRepoKey = `${owner}/a-repo`;

beforeEach(() => {
jest.spyOn(Config, 'Instance', 'get').mockImplementation(
() =>
({
...baseConfig,
enableOrganizationRunners: false,
} as unknown as Config),
);
});
describe('When no scaleConfigRepo is set, the runner repo is used as the source for scale config', () => {

it('is_ephemeral === undefined', async () => {
const mockedGetRunnerTypes = mocked(getRunnerTypes);
beforeEach(() => {
jest.spyOn(Config, 'Instance', 'get').mockImplementation(
() =>
({
...baseConfig,
enableOrganizationRunners: false,
} as unknown as Config),
);
});

mockedGetRunnerTypes.mockResolvedValueOnce(new Map([[runnerType, {} as RunnerType]]));
it('is_ephemeral === undefined', async () => {
const mockedGetRunnerTypes = mocked(getRunnerTypes);

expect(await isEphemeralRunner({ runnerType: runnerType, repo: repoKey } as RunnerInfo, metrics)).toEqual(
false,
);
mockedGetRunnerTypes.mockResolvedValueOnce(new Map([[runnerType, {} as RunnerType]]));

expect(mockedGetRunnerTypes).toBeCalledTimes(1);
expect(mockedGetRunnerTypes).toBeCalledWith(repo, metrics);
});
expect(await isEphemeralRunner({ runnerType: runnerType, repo: runnerRepoKey } as RunnerInfo, metrics)).toEqual(
false,
);

it('is_ephemeral === true', async () => {
const mockedGetRunnerTypes = mocked(getRunnerTypes);
expect(mockedGetRunnerTypes).toBeCalledTimes(1);
expect(mockedGetRunnerTypes).toBeCalledWith(runnerRepo, metrics);
});

mockedGetRunnerTypes.mockResolvedValueOnce(new Map([[runnerType, { is_ephemeral: true } as RunnerType]]));
it('is_ephemeral === true', async () => {
const mockedGetRunnerTypes = mocked(getRunnerTypes);

expect(await isEphemeralRunner({ runnerType: runnerType, repo: repoKey } as RunnerInfo, metrics)).toEqual(true);
mockedGetRunnerTypes.mockResolvedValueOnce(new Map([[runnerType, { is_ephemeral: true } as RunnerType]]));

expect(mockedGetRunnerTypes).toBeCalledTimes(1);
expect(mockedGetRunnerTypes).toBeCalledWith(repo, metrics);
expect(await isEphemeralRunner({ runnerType: runnerType, repo: runnerRepoKey } as RunnerInfo, metrics)).toEqual(true);

expect(mockedGetRunnerTypes).toBeCalledTimes(1);
expect(mockedGetRunnerTypes).toBeCalledWith(runnerRepo, metrics);
});

it('is_ephemeral === false', async () => {
const mockedGetRunnerTypes = mocked(getRunnerTypes);

mockedGetRunnerTypes.mockResolvedValueOnce(new Map([[runnerType, { is_ephemeral: false } as RunnerType]]));

expect(await isEphemeralRunner({ runnerType: runnerType, repo: runnerRepoKey } as RunnerInfo, metrics)).toEqual(
false,
);

expect(mockedGetRunnerTypes).toBeCalledTimes(1);
expect(mockedGetRunnerTypes).toBeCalledWith(runnerRepo, metrics);
});
});

it('is_ephemeral === false', async () => {
const mockedGetRunnerTypes = mocked(getRunnerTypes);
describe("When a scaleConfigRepo is set, it's used as the source for scale config", () => {
const centralRepoName = 'central-repo'; // to be the test-infra equivalent
const centralRepo: Repo = {
owner: owner,
repo: centralRepoName,
};

mockedGetRunnerTypes.mockResolvedValueOnce(new Map([[runnerType, { is_ephemeral: false } as RunnerType]]));
beforeEach(() => {
jest.spyOn(Config, 'Instance', 'get').mockImplementation(
() =>
({
...baseConfig,
enableOrganizationRunners: false,
scaleConfigRepo: centralRepoName,
} as unknown as Config),
);
});

it('is_ephemeral === undefined', async () => {
const mockedGetRunnerTypes = mocked(getRunnerTypes);

mockedGetRunnerTypes.mockResolvedValueOnce(new Map([[runnerType, {} as RunnerType]]));

expect(await isEphemeralRunner({ runnerType: runnerType, repo: runnerRepoKey } as RunnerInfo, metrics)).toEqual(
false,
);

expect(mockedGetRunnerTypes).toBeCalledTimes(1);
expect(mockedGetRunnerTypes).toBeCalledWith(centralRepo, metrics);
});

it('is_ephemeral === true', async () => {
const mockedGetRunnerTypes = mocked(getRunnerTypes);

mockedGetRunnerTypes.mockResolvedValueOnce(new Map([[runnerType, { is_ephemeral: true } as RunnerType]]));

expect(await isEphemeralRunner({ runnerType: runnerType, repo: runnerRepoKey } as RunnerInfo, metrics)).toEqual(true);

expect(mockedGetRunnerTypes).toBeCalledTimes(1);
expect(mockedGetRunnerTypes).toBeCalledWith(centralRepo, metrics);
});

it('is_ephemeral === false', async () => {
const mockedGetRunnerTypes = mocked(getRunnerTypes);

mockedGetRunnerTypes.mockResolvedValueOnce(new Map([[runnerType, { is_ephemeral: false } as RunnerType]]));

expect(await isEphemeralRunner({ runnerType: runnerType, repo: runnerRepoKey } as RunnerInfo, metrics)).toEqual(
false,
);

expect(mockedGetRunnerTypes).toBeCalledTimes(1);
expect(mockedGetRunnerTypes).toBeCalledWith(centralRepo, metrics);
});

expect(await isEphemeralRunner({ runnerType: runnerType, repo: repoKey } as RunnerInfo, metrics)).toEqual(
false,
);

expect(mockedGetRunnerTypes).toBeCalledTimes(1);
expect(mockedGetRunnerTypes).toBeCalledWith(repo, metrics);
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ export async function isEphemeralRunner(ec2runner: RunnerInfo, metrics: ScaleDow
}

const repo: Repo = (() => {
if (Config.Instance.enableOrganizationRunners) {
if (Config.Instance.scaleConfigRepo) {
return {
owner: ec2runner.org !== undefined ? (ec2runner.org as string) : getRepo(ec2runner.repo as string).owner,
repo: Config.Instance.scaleConfigRepo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export async function scaleUp(
const runnerTypes = await getRunnerTypes(
{
owner: repo.owner,
repo: Config.Instance.enableOrganizationRunners ? Config.Instance.scaleConfigRepo : repo.repo,
repo: Config.Instance.scaleConfigRepo || repo.repo,
},
metrics,
);
Expand Down
10 changes: 10 additions & 0 deletions terraform-aws-github-runner/modules/runners/scale-down.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ resource "aws_lambda_function" "scale_down" {
tags = local.tags
memory_size = 2048

lifecycle {
precondition {
# Enforce that a value for scale_config_repo is set when enable_organization_runners is set to true.
# Setting the value is optional when not using organization runners since we'll default to the
# job's repository.
condition = var.enable_organization_runners == true ? var.scale_config_repo != "" : true
error_message = "scale_config_repo is required when enable_organization_runners is set to true"
}
}

environment {
variables = {
AWS_REGION_INSTANCES = join(",", var.aws_region_instances)
Expand Down
10 changes: 10 additions & 0 deletions terraform-aws-github-runner/modules/runners/scale-up.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ resource "aws_lambda_function" "scale_up" {
memory_size = 2048
publish = true

lifecycle {
precondition {
# Enforce that a value for scale_config_repo is set when enable_organization_runners is set to true.
# Setting the value is optional when not using organization runners since we'll default to the
# job's repository.
condition = var.enable_organization_runners == true ? var.scale_config_repo != "" : true
error_message = "scale_config_repo is required when enable_organization_runners is set to true"
}
}

environment {
variables = {
CANT_HAVE_ISSUES_LABELS = join(",", var.cant_have_issues_labels)
Expand Down
4 changes: 2 additions & 2 deletions terraform-aws-github-runner/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ variable "cant_have_issues_labels" {
}

variable "scale_config_repo" {
description = "Repository to fetch scale config from if `enable_organization_runners` is set to true. Otherwise the job's repo will be used"
default = "" # Internally defaults to 'test-infra'
description = "Repository to fetch scale config from. Optional if `enable_organization_runners` is set to false, in which case the job's repo will be used"
default = ""
type = string
}

Expand Down
Loading