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

chore: Refactor runnerServiceConfig to be an array of strings #1842

Merged
merged 1 commit into from
Mar 15, 2022
Merged
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
6 changes: 3 additions & 3 deletions modules/runners/lambdas/runners/src/aws/runners.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ describe('create runner', () => {
expect(mockSSM.putParameter).toBeCalledWith({
Name: `unit-test-environment-${instance}`,
Type: 'SecureString',
Value: 'bla',
Value: '--token foo --url http://github.com',
});
}
});
Expand Down Expand Up @@ -247,7 +247,7 @@ describe('create runner', () => {
await createRunner(createRunnerConfig(defaultRunnerConfig));
expect(mockSSM.putParameter).toBeCalledWith({
Name: `${ENVIRONMENT}-i-1234`,
Value: 'bla',
Value: '--token foo --url http://github.com',
Type: 'SecureString',
});
});
Expand Down Expand Up @@ -358,7 +358,7 @@ interface RunnerConfig {

function createRunnerConfig(runnerConfig: RunnerConfig): RunnerInputParameters {
return {
runnerServiceConfig: 'bla',
runnerServiceConfig: ['--token foo', '--url http://github.com'],
environment: ENVIRONMENT,
runnerType: runnerConfig.type,
runnerOwner: REPO_NAME,
Expand Down
4 changes: 2 additions & 2 deletions modules/runners/lambdas/runners/src/aws/runners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface ListRunnerFilters {
}

export interface RunnerInputParameters {
runnerServiceConfig: string;
runnerServiceConfig: string[];
environment: string;
runnerType: 'Org' | 'Repo';
runnerOwner: string;
Expand Down Expand Up @@ -207,7 +207,7 @@ export async function createRunner(runnerParameters: RunnerInputParameters): Pro
await ssm
.putParameter({
Name: `${runnerParameters.environment}-${instance}`,
Value: runnerParameters.runnerServiceConfig,
Value: runnerParameters.runnerServiceConfig.join(' '),
Type: 'SecureString',
})
.promise();
Expand Down
60 changes: 41 additions & 19 deletions modules/runners/lambdas/runners/src/scale-runners/scale-up.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const cleanEnv = process.env;

const EXPECTED_RUNNER_PARAMS: RunnerInputParameters = {
environment: 'unit-test-environment',
runnerServiceConfig: `--url https://github.enterprise.something/${TEST_DATA.repositoryOwner} --token 1234abcd`,
runnerServiceConfig: [`--url https://github.enterprise.something/${TEST_DATA.repositoryOwner}`, '--token 1234abcd'],
runnerType: 'Org',
runnerOwner: TEST_DATA.repositoryOwner,
launchTemplateName: 'lt-1',
Expand Down Expand Up @@ -236,8 +236,11 @@ describe('scaleUp with GHES', () => {
process.env.RUNNER_EXTRA_LABELS = 'label1,label2';
process.env.RUNNER_GROUP_NAME = 'TEST_GROUP';
await scaleUpModule.scaleUp('aws:sqs', TEST_DATA);
expectedRunnerParams.runnerServiceConfig =
expectedRunnerParams.runnerServiceConfig + ` --labels label1,label2 --runnergroup TEST_GROUP`;
expectedRunnerParams.runnerServiceConfig = [
...expectedRunnerParams.runnerServiceConfig,
'--labels label1,label2',
'--runnergroup TEST_GROUP',
];
expect(createRunner).toBeCalledWith(expectedRunnerParams);
});
});
Expand All @@ -248,10 +251,10 @@ describe('scaleUp with GHES', () => {
expectedRunnerParams = { ...EXPECTED_RUNNER_PARAMS };
expectedRunnerParams.runnerType = 'Repo';
expectedRunnerParams.runnerOwner = `${TEST_DATA.repositoryOwner}/${TEST_DATA.repositoryName}`;
expectedRunnerParams.runnerServiceConfig =
`--url ` +
`https://github.enterprise.something/${TEST_DATA.repositoryOwner}/${TEST_DATA.repositoryName} ` +
`--token 1234abcd`;
expectedRunnerParams.runnerServiceConfig = [
`--url https://github.enterprise.something/${TEST_DATA.repositoryOwner}/${TEST_DATA.repositoryName}`,
`--token 1234abcd`,
];
});

it('gets the current repo level runners', async () => {
Expand Down Expand Up @@ -317,15 +320,21 @@ describe('scaleUp with GHES', () => {
it('creates a runner with correct config and labels', async () => {
process.env.RUNNER_EXTRA_LABELS = 'label1,label2';
await scaleUpModule.scaleUp('aws:sqs', TEST_DATA);
expectedRunnerParams.runnerServiceConfig = expectedRunnerParams.runnerServiceConfig + ` --labels label1,label2`;
expectedRunnerParams.runnerServiceConfig = [
...expectedRunnerParams.runnerServiceConfig,
`--labels label1,label2`,
];
expect(createRunner).toBeCalledWith(expectedRunnerParams);
});

it('creates a runner and ensure the group argument is ignored', async () => {
process.env.RUNNER_EXTRA_LABELS = 'label1,label2';
process.env.RUNNER_GROUP_NAME = 'TEST_GROUP_IGNORED';
await scaleUpModule.scaleUp('aws:sqs', TEST_DATA);
expectedRunnerParams.runnerServiceConfig = expectedRunnerParams.runnerServiceConfig + ` --labels label1,label2`;
expectedRunnerParams.runnerServiceConfig = [
...expectedRunnerParams.runnerServiceConfig,
`--labels label1,label2`,
];
expect(createRunner).toBeCalledWith(expectedRunnerParams);
});

Expand Down Expand Up @@ -393,8 +402,10 @@ describe('scaleUp with public GH', () => {
beforeEach(() => {
process.env.ENABLE_ORGANIZATION_RUNNERS = 'true';
expectedRunnerParams = { ...EXPECTED_RUNNER_PARAMS };
expectedRunnerParams.runnerServiceConfig =
`--url https://github.com/${TEST_DATA.repositoryOwner} ` + `--token 1234abcd`;
expectedRunnerParams.runnerServiceConfig = [
`--url https://github.com/${TEST_DATA.repositoryOwner}`,
`--token 1234abcd`,
];
});

it('gets the current org level runners', async () => {
Expand Down Expand Up @@ -435,8 +446,11 @@ describe('scaleUp with public GH', () => {
process.env.RUNNER_EXTRA_LABELS = 'label1,label2';
process.env.RUNNER_GROUP_NAME = 'TEST_GROUP';
await scaleUpModule.scaleUp('aws:sqs', TEST_DATA);
expectedRunnerParams.runnerServiceConfig =
expectedRunnerParams.runnerServiceConfig + ` --labels label1,label2 --runnergroup TEST_GROUP`;
expectedRunnerParams.runnerServiceConfig = [
...expectedRunnerParams.runnerServiceConfig,
`--labels label1,label2`,
`--runnergroup TEST_GROUP`,
];
expect(createRunner).toBeCalledWith(expectedRunnerParams);
});
});
Expand All @@ -447,8 +461,10 @@ describe('scaleUp with public GH', () => {
expectedRunnerParams = { ...EXPECTED_RUNNER_PARAMS };
expectedRunnerParams.runnerType = 'Repo';
expectedRunnerParams.runnerOwner = `${TEST_DATA.repositoryOwner}/${TEST_DATA.repositoryName}`;
expectedRunnerParams.runnerServiceConfig =
`--url https://github.com/${TEST_DATA.repositoryOwner}/${TEST_DATA.repositoryName} ` + `--token 1234abcd`;
expectedRunnerParams.runnerServiceConfig = [
`--url https://github.com/${TEST_DATA.repositoryOwner}/${TEST_DATA.repositoryName}`,
`--token 1234abcd`,
];
});

it('gets the current repo level runners', async () => {
Expand Down Expand Up @@ -499,15 +515,21 @@ describe('scaleUp with public GH', () => {
it('creates a runner with correct config and labels', async () => {
process.env.RUNNER_EXTRA_LABELS = 'label1,label2';
await scaleUpModule.scaleUp('aws:sqs', TEST_DATA);
expectedRunnerParams.runnerServiceConfig = expectedRunnerParams.runnerServiceConfig + ` --labels label1,label2`;
expectedRunnerParams.runnerServiceConfig = [
...expectedRunnerParams.runnerServiceConfig,
`--labels label1,label2`,
];
expect(createRunner).toBeCalledWith(expectedRunnerParams);
});

it('creates a runner and ensure the group argument is ignored', async () => {
process.env.RUNNER_EXTRA_LABELS = 'label1,label2';
process.env.RUNNER_GROUP_NAME = 'TEST_GROUP_IGNORED';
await scaleUpModule.scaleUp('aws:sqs', TEST_DATA);
expectedRunnerParams.runnerServiceConfig = expectedRunnerParams.runnerServiceConfig + ` --labels label1,label2`;
expectedRunnerParams.runnerServiceConfig = [
...expectedRunnerParams.runnerServiceConfig,
`--labels label1,label2`,
];
expect(createRunner).toBeCalledWith(expectedRunnerParams);
});

Expand All @@ -524,14 +546,14 @@ describe('scaleUp with public GH', () => {
it('creates a ephemeral runner.', async () => {
process.env.ENABLE_EPHEMERAL_RUNNERS = 'true';
await scaleUpModule.scaleUp('aws:sqs', TEST_DATA);
expectedRunnerParams.runnerServiceConfig = expectedRunnerParams.runnerServiceConfig + ` --ephemeral`;
expectedRunnerParams.runnerServiceConfig = [...expectedRunnerParams.runnerServiceConfig, `--ephemeral`];
expect(createRunner).toBeCalledWith(expectedRunnerParams);
});

it('disable auto update on the runner.', async () => {
process.env.DISABLE_RUNNER_AUTOUPDATE = 'true';
await scaleUpModule.scaleUp('aws:sqs', TEST_DATA);
expectedRunnerParams.runnerServiceConfig = expectedRunnerParams.runnerServiceConfig + ` --disableupdate`;
expectedRunnerParams.runnerServiceConfig = [...expectedRunnerParams.runnerServiceConfig, `--disableupdate`];
expect(createRunner).toBeCalledWith(expectedRunnerParams);
});

Expand Down
33 changes: 22 additions & 11 deletions modules/runners/lambdas/runners/src/scale-runners/scale-up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,28 @@ interface CreateEC2RunnerConfig {
}

function generateRunnerServiceConfig(githubRunnerConfig: CreateGitHubRunnerConfig, token: string) {
const labelsArgument =
githubRunnerConfig.runnerExtraLabels !== undefined ? `--labels ${githubRunnerConfig.runnerExtraLabels} ` : '';
const runnerGroupArgument =
githubRunnerConfig.runnerGroup !== undefined ? `--runnergroup ${githubRunnerConfig.runnerGroup} ` : '';
const configBaseUrl = githubRunnerConfig.ghesBaseUrl ? githubRunnerConfig.ghesBaseUrl : 'https://github.com';
const ephemeralArgument = githubRunnerConfig.ephemeral ? '--ephemeral ' : '';
const disableUpdateArgument = githubRunnerConfig.disableAutoUpdate ? '--disableupdate ' : '';
const runnerArgs = `--token ${token} ${labelsArgument}${ephemeralArgument}${disableUpdateArgument}`;
return githubRunnerConfig.runnerType === 'Org'
? `--url ${configBaseUrl}/${githubRunnerConfig.runnerOwner} ${runnerArgs}${runnerGroupArgument}`.trim()
: `--url ${configBaseUrl}/${githubRunnerConfig.runnerOwner} ${runnerArgs}`.trim();
const config = [
`--url ${githubRunnerConfig.ghesBaseUrl ?? 'https://github.com'}/${githubRunnerConfig.runnerOwner}`,
`--token ${token}`,
];

if (githubRunnerConfig.runnerExtraLabels !== undefined) {
config.push(`--labels ${githubRunnerConfig.runnerExtraLabels}`);
}

if (githubRunnerConfig.ephemeral) {
config.push(`--ephemeral`);
}

if (githubRunnerConfig.disableAutoUpdate) {
config.push('--disableupdate');
}

if (githubRunnerConfig.runnerType === 'Org' && githubRunnerConfig.runnerGroup !== undefined) {
config.push(`--runnergroup ${githubRunnerConfig.runnerGroup}`);
}

return config;
}

async function getGithubRunnerRegistrationToken(githubRunnerConfig: CreateGitHubRunnerConfig, ghClient: Octokit) {
Expand Down