Skip to content

Commit

Permalink
chore: Refactor runner config to be an array of strings (philips-labs…
Browse files Browse the repository at this point in the history
…#1842)

run on branch

fix test
  • Loading branch information
ulich authored Mar 15, 2022
1 parent c88a005 commit bf79711
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 35 deletions.
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

0 comments on commit bf79711

Please sign in to comment.