-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,509 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,27 @@ | ||
config.json | ||
|
||
# Ignore node_modules | ||
node_modules | ||
|
||
# Ignore logs | ||
logs | ||
*.log | ||
|
||
# Ignore environment variables | ||
.env | ||
|
||
# Ignore Git files | ||
.git | ||
.gitignore | ||
|
||
# Ignore Docker files | ||
Dockerfile | ||
docker-compose.yml | ||
|
||
# Ignore IDE files | ||
.vscode/ | ||
.idea/ | ||
|
||
# Ignore OS files | ||
.DS_Store | ||
Thumbs.db |
This file contains 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,18 @@ | ||
# Node.js modules | ||
node_modules/ | ||
|
||
# Logs | ||
logs/ | ||
*.log | ||
npm-debug.log* | ||
|
||
# Environment variables | ||
.env | ||
|
||
# Operating system files | ||
.DS_Store | ||
Thumbs.db | ||
|
||
# IDE files | ||
.vscode/ | ||
.idea/ |
This file contains 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,18 @@ | ||
# Use the official Node.js image | ||
FROM node:20-alpine | ||
|
||
# Create and change to the app directory | ||
WORKDIR /usr/src/app | ||
|
||
# Install app dependencies | ||
COPY package*.json ./ | ||
RUN npm install | ||
|
||
# Install cpln | ||
RUN npm install -g @controlplane/cli | ||
|
||
# Copy app files | ||
COPY . . | ||
|
||
# Run the app | ||
CMD ["node", "index.js"] |
This file contains 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,50 @@ | ||
# SQS Auto-Scaling Example | ||
|
||
This workload will autoscale any workload based on an SQS queue length. | ||
|
||
Multiple workloads can be configured. Each workload is scaled based on the length of one associated queue. | ||
|
||
A `config.json` file (stored as an opaque secret named `sqs-autoscale`), contains the auto-scaling rules. See below for an example. | ||
|
||
## Sample config.json | ||
|
||
```json | ||
[ | ||
{ | ||
"gvc": "default-gvc", | ||
"workloadName": "test-workload", | ||
"sqsEndpoint": "https://sqs.us-east-1.amazonaws.com/123456789012/queue1", | ||
"scalingRules": [ | ||
{ "length": 0, "scaleAmount": 2 }, | ||
{ "length": 10, "scaleAmount": 3 }, | ||
{ "length": 50, "scaleAmount": 4 }, | ||
{ "length": 101, "scaleAmount": 5 } | ||
] | ||
} | ||
] | ||
``` | ||
|
||
Queue length of 0 to 9, will be scaled to 2. Length of 10-49, to 3. | ||
|
||
## Building the Image | ||
|
||
Use `cpln` to build and push the image. Authentication using `cpln login` and permissions to push an image is required. | ||
|
||
`cpln image build --name sqs-autoscale:0.1 --org ORG_NAME --push` | ||
|
||
## Configure CRON Workload | ||
|
||
See `sample-workload.yaml` for a sample Control Plane YAML manifest file. The sample executes every minute. | ||
|
||
## Identity Configuration | ||
|
||
An identity is required to be associated with this workload with the following: | ||
|
||
1. `Cloud Access` configured with read permissions to the SQS queues being inspected. | ||
|
||
## Policy Configuration | ||
|
||
The identity requires the following polices to be created: | ||
|
||
1. `Reveal` permission to the secret containing the `config.json` contents. | ||
2. `Manage` permission for the workloads being autoscaled. |
This file contains 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,13 @@ | ||
[ | ||
{ | ||
"gvc": "default-gvc", | ||
"workloadName": "test-workload", | ||
"sqsEndpoint": "https://sqs.us-east-1.amazonaws.com/123456789012/queue1", | ||
"scalingRules": [ | ||
{ "length": 0, "scaleAmount": 2 }, | ||
{ "length": 10, "scaleAmount": 3 }, | ||
{ "length": 50, "scaleAmount": 4 }, | ||
{ "length": 101, "scaleAmount": 5 } | ||
] | ||
} | ||
] |
This file contains 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,94 @@ | ||
const { SQSClient, GetQueueAttributesCommand } = require("@aws-sdk/client-sqs"); | ||
const fs = require("fs"); | ||
const { execSync } = require("child_process"); | ||
|
||
// Load the JSON array | ||
const data = JSON.parse(fs.readFileSync("config.json", "utf8")); | ||
|
||
var awsRegion = process.env.AWS_REGION; | ||
|
||
// Ensure AWS_REGION environment variable is set | ||
if (!awsRegion) { | ||
console.error( | ||
"Error: AWS_REGION environment variable is not set. Using 'us-west-2'" | ||
); | ||
awsRegion = "us-west-2"; | ||
} | ||
|
||
// Configure AWS SDK client | ||
const client = new SQSClient({ region: awsRegion }); | ||
|
||
data.forEach(async (item) => { | ||
const params = { | ||
QueueUrl: item.sqsEndpoint, | ||
AttributeNames: ["ApproximateNumberOfMessages"], | ||
}; | ||
|
||
try { | ||
const command = new GetQueueAttributesCommand(params); | ||
const result = await client.send(command); | ||
const queueSize = parseInt( | ||
result.Attributes.ApproximateNumberOfMessages, | ||
10 | ||
); | ||
console.log( | ||
`Queue for workload ${item.workloadName} has ${queueSize} messages.` | ||
); | ||
|
||
// Find the rule with the highest length that is less than or equal to the queue size | ||
const applicableRule = item.scalingRules | ||
.filter((rule) => queueSize >= rule.length) | ||
.reduce( | ||
(maxRule, currentRule) => { | ||
return currentRule.length > maxRule.length ? currentRule : maxRule; | ||
}, | ||
{ length: -1, scaleAmount: 0 } | ||
); | ||
|
||
console.log( | ||
`Applicable Length: ${applicableRule.length} - Applicable Rule: ${applicableRule.scaleAmount}` | ||
); | ||
|
||
if (applicableRule.length >= 0) { | ||
try { | ||
console.log( | ||
`Scaling ${item.gvc}/${item.workloadName} by ${applicableRule.scaleAmount}` | ||
); | ||
|
||
// Perform your scaling logic here | ||
|
||
// Fetch the workload as a JSON object through cpln | ||
const workload = JSON.parse( | ||
execSync( | ||
`cpln workload get ${item.workloadName} -o json --gvc ${item.gvc}` | ||
).toString() | ||
); | ||
|
||
workload.spec.defaultOptions.autoscaling.minScale = `${applicableRule.scaleAmount}`; | ||
workload.spec.defaultOptions.autoscaling.maxScale = `${applicableRule.scaleAmount}`; | ||
|
||
const response = execSync( | ||
`echo '${JSON.stringify(workload)}' | cpln apply --gvc ${ | ||
item.gvc | ||
} --file -` | ||
).toString(); | ||
|
||
console.log(`Response from cpln: ${response}`); | ||
} catch (err) { | ||
console.error( | ||
`Error in scaling logic for workload ${item.workloadName}:`, | ||
err | ||
); | ||
} | ||
} else { | ||
console.log( | ||
`No applicable scaling rule found for workload ${item.workloadName} with queue size ${queueSize}.` | ||
); | ||
} | ||
} catch (err) { | ||
console.error( | ||
`Error fetching queue attributes for workload ${item.workloadName}:`, | ||
err | ||
); | ||
} | ||
}); |
Oops, something went wrong.