Skip to content

Commit

Permalink
Rename scale-runners to scale-up
Browse files Browse the repository at this point in the history
  • Loading branch information
gertjanmaas committed May 11, 2020
1 parent 6af4656 commit c80615e
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 60 deletions.
4 changes: 2 additions & 2 deletions modules/runners/lambdas/scale-runners/src/lambda.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { handle } from './scale-runners/handler';
import { scaleUp } from './scale-runners/scale-up';
import { scaleDown } from './scale-runners/scale-down';
import { SQSEvent } from 'aws-lambda';

module.exports.handler = async (event: SQSEvent, context: any, callback: any) => {
console.log(event);
try {
for (const e of event.Records) {
await handle(e.eventSource, JSON.parse(e.body));
await scaleUp(e.eventSource, JSON.parse(e.body));
}
return callback(null);
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { mocked } from 'ts-jest/utils';
import { ActionRequestMessage, handle } from './handler';
import { ActionRequestMessage, scaleUp } from './scale-up';

import { createAppAuth } from '@octokit/auth-app';
import { Octokit } from '@octokit/rest';
Expand Down Expand Up @@ -30,7 +30,7 @@ const TEST_DATA: ActionRequestMessage = {
installationId: 2,
};

describe('handler', () => {
describe('scaleUp', () => {
beforeEach(() => {
process.env.GITHUB_APP_KEY_BASE64 = 'TEST_CERTIFICATE_DATA';
process.env.GITHUB_APP_ID = '1337';
Expand Down Expand Up @@ -64,11 +64,11 @@ describe('handler', () => {

it('ignores non-sqs events', async () => {
expect.assertions(1);
expect(handle('aws:s3', TEST_DATA)).rejects.toEqual(Error('Cannot handle non-SQS events!'));
expect(scaleUp('aws:s3', TEST_DATA)).rejects.toEqual(Error('Cannot handle non-SQS events!'));
});

it('checks queued workflows', async () => {
await handle('aws:sqs', TEST_DATA);
await scaleUp('aws:sqs', TEST_DATA);
expect(mockOctokit.actions.listRepoWorkflowRuns).toBeCalledWith({
owner: TEST_DATA.repositoryOwner,
repo: TEST_DATA.repositoryName,
Expand All @@ -80,7 +80,7 @@ describe('handler', () => {
mockOctokit.actions.listRepoWorkflowRuns.mockImplementation(() => ({
data: { total_count: 0, runners: [] },
}));
await handle('aws:sqs', TEST_DATA);
await scaleUp('aws:sqs', TEST_DATA);
expect(listRunners).not.toBeCalled();
});

Expand All @@ -90,7 +90,7 @@ describe('handler', () => {
});

it('gets the current org level runners', async () => {
await handle('aws:sqs', TEST_DATA);
await scaleUp('aws:sqs', TEST_DATA);
expect(listRunners).toBeCalledWith({
environment: 'unit-test-environment',
repoName: undefined,
Expand All @@ -99,20 +99,20 @@ describe('handler', () => {

it('does not create a token when maximum runners has been reached', async () => {
process.env.RUNNERS_MAXIMUM_COUNT = '1';
await handle('aws:sqs', TEST_DATA);
await scaleUp('aws:sqs', TEST_DATA);
expect(mockOctokit.actions.createRegistrationTokenForOrg).not.toBeCalled();
});

it('creates a token when maximum runners has not been reached', async () => {
await handle('aws:sqs', TEST_DATA);
await scaleUp('aws:sqs', TEST_DATA);
expect(mockOctokit.actions.createRegistrationTokenForOrg).toBeCalled();
expect(mockOctokit.actions.createRegistrationTokenForOrg).toBeCalledWith({
org: TEST_DATA.repositoryOwner,
});
});

it('creates a runner with correct config', async () => {
await handle('aws:sqs', TEST_DATA);
await scaleUp('aws:sqs', TEST_DATA);
expect(createRunner).toBeCalledWith({
environment: 'unit-test-environment',
runnerConfig: `--url https://github.com/${TEST_DATA.repositoryOwner} --token 1234abcd`,
Expand All @@ -128,7 +128,7 @@ describe('handler', () => {
});

it('gets the current repo level runners', async () => {
await handle('aws:sqs', TEST_DATA);
await scaleUp('aws:sqs', TEST_DATA);
expect(listRunners).toBeCalledWith({
environment: 'unit-test-environment',
repoName: `${TEST_DATA.repositoryOwner}/${TEST_DATA.repositoryName}`,
Expand All @@ -137,20 +137,20 @@ describe('handler', () => {

it('does not create a token when maximum runners has been reached', async () => {
process.env.RUNNERS_MAXIMUM_COUNT = '1';
await handle('aws:sqs', TEST_DATA);
await scaleUp('aws:sqs', TEST_DATA);
expect(mockOctokit.actions.createRegistrationTokenForRepo).not.toBeCalled();
});

it('creates a token when maximum runners has not been reached', async () => {
await handle('aws:sqs', TEST_DATA);
await scaleUp('aws:sqs', TEST_DATA);
expect(mockOctokit.actions.createRegistrationTokenForRepo).toBeCalledWith({
owner: TEST_DATA.repositoryOwner,
repo: TEST_DATA.repositoryName,
});
});

it('creates a runner with correct config', async () => {
await handle('aws:sqs', TEST_DATA);
await scaleUp('aws:sqs', TEST_DATA);
expect(createRunner).toBeCalledWith({
environment: 'unit-test-environment',
runnerConfig: `--url https://github.com/${TEST_DATA.repositoryOwner}/${TEST_DATA.repositoryName} --token 1234abcd`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async function createInstallationClient(githubAppAuth: AppAuth): Promise<Octokit
return new Octokit({ auth: auth.token });
}

export const handle = async (eventSource: string, payload: ActionRequestMessage): Promise<void> => {
export const scaleUp = async (eventSource: string, payload: ActionRequestMessage): Promise<void> => {
if (eventSource !== 'aws:sqs') throw Error('Cannot handle non-SQS events!');
const enableOrgLevel = yn(process.env.ENABLE_ORGANIZATION_RUNNERS, { default: true });
const maximumRunners = parseInt(process.env.RUNNERS_MAXIMUM_COUNT || '3');
Expand Down
2 changes: 1 addition & 1 deletion modules/runners/policies-lambda-common.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ resource "aws_iam_policy" "lambda_logging" {

resource "aws_iam_policy_attachment" "lambda_logging" {
name = "${var.environment}-lambda-logging"
roles = [aws_iam_role.scale_down.name, aws_iam_role.scale_runners_lambda.name]
roles = [aws_iam_role.scale_down.name, aws_iam_role.scale_up.name]
policy_arn = aws_iam_policy.lambda_logging.arn
}
14 changes: 0 additions & 14 deletions modules/runners/policies/lambda-scale-runners.json

This file was deleted.

9 changes: 9 additions & 0 deletions modules/runners/policies/lambda-scale-up.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
"Effect": "Allow",
"Action": ["ssm:PutParameter"],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"sqs:ReceiveMessage",
"sqs:GetQueueAttributes",
"sqs:DeleteMessage"
],
"Resource": "${sqs_arn}"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
resource "aws_lambda_function" "scale_runners_lambda" {
resource "aws_lambda_function" "scale_up" {
filename = "${path.module}/lambdas/scale-runners/scale-runners.zip"
source_code_hash = filebase64sha256("${path.module}/lambdas/scale-runners/scale-runners.zip")
function_name = "${var.environment}-scale-runners"
role = aws_iam_role.scale_runners_lambda.arn
handler = "index.handler"
function_name = "${var.environment}-scale-up"
role = aws_iam_role.scale_up.arn
handler = "index.scaleUp"
runtime = "nodejs12.x"
timeout = 60

Expand All @@ -22,52 +22,37 @@ resource "aws_lambda_function" "scale_runners_lambda" {
}
}

resource "aws_lambda_event_source_mapping" "scale_runners_lambda" {
resource "aws_lambda_event_source_mapping" "scale_up" {
event_source_arn = var.sqs.arn
function_name = aws_lambda_function.scale_runners_lambda.arn
function_name = aws_lambda_function.scale_up.arn
}

resource "aws_lambda_permission" "scale_runners_lambda" {
statement_id = "AllowExecutionFromSQS"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.scale_runners_lambda.function_name
function_name = aws_lambda_function.scale_up.function_name
principal = "sqs.amazonaws.com"
source_arn = var.sqs.arn
}

resource "aws_iam_role" "scale_runners_lambda" {
name = "${var.environment}-action-scale-runners-lambda-role"
resource "aws_iam_role" "scale_up" {
name = "${var.environment}-action-scale-up-lambda-role"
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role_policy.json
}

resource "aws_iam_policy" "scale_runners_lambda_sqs" {
name = "${var.environment}-lambda-scale-runners-sqs-receive-policy"
description = "Lambda scale up sqs policy"

policy = templatefile("${path.module}/policies/lambda-scale-runners.json", {
sqs_arn = var.sqs.arn
})
}

resource "aws_iam_policy_attachment" "scale_runners_lambda_sqs" {
name = "${var.environment}-scale-up-sqs"
roles = [aws_iam_role.scale_runners_lambda.name]
policy_arn = aws_iam_policy.scale_runners_lambda_sqs.arn
}


resource "aws_iam_policy" "scale_runners_lambda" {
resource "aws_iam_policy" "scale_up" {
name = "${var.environment}-lambda-scale-up-policy"
description = "Lambda scale up policy"

policy = templatefile("${path.module}/policies/lambda-scale-up.json", {
arn_runner_instance_role = aws_iam_role.runner.arn
sqs_arn = var.sqs.arn
})
}

resource "aws_iam_policy_attachment" "scale_runners_lambda" {
resource "aws_iam_policy_attachment" "scale_up" {
name = "${var.environment}-scale-up"
roles = [aws_iam_role.scale_runners_lambda.name]
policy_arn = aws_iam_policy.scale_runners_lambda.arn
roles = [aws_iam_role.scale_up.name]
policy_arn = aws_iam_policy.scale_up.arn
}

0 comments on commit c80615e

Please sign in to comment.