Skip to content

Commit 6615c88

Browse files
committed
fix: make the action really works
1 parent 7dff449 commit 6615c88

File tree

7 files changed

+93
-31
lines changed

7 files changed

+93
-31
lines changed

.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'ovh';

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# direnv
2+
.envrc
3+
14
# Logs
25
logs
36
*.log

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
# github-action-ovh-mks-scaling
2+
23
Scale up or down a OVH MKS nodepool
4+
5+
You need first to :
6+
7+
- create an application in OVH api at : https://www.ovh.com/auth/api/createApp
8+
- export env vars `OVH_APPLICATION_KEY` and `OVH_APPLICATION_SECRET`
9+
- and to run the script `scripts/create-ovh-creds.sh`
10+
- note the consumer key
11+
- click on the link for credentials validation

action.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ inputs:
99
required: true
1010
description: 'The OVH endpoint to use'
1111
default: 'eu.api.ovh.com'
12-
region:
13-
required: true
14-
description: 'The OVH region to use'
15-
default: 'GRA'
1612
project_id:
1713
required: true
1814
description: 'The project ID of the OVH MKS project'

index.js

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,65 @@
1-
import { getInput, setOutput, setFailed } from '@actions/core';
2-
import { readFile } from 'node:fs/promises';
3-
import ovh from 'ovh';
1+
import { getInput, setOutput, setFailed } from "@actions/core";
2+
import ovh from "ovh";
43

5-
try {
6-
const ovhEndpoint = getInput('endpoint');
7-
const ovhRegion = getInput('region');
8-
const ovhProjectId = getInput('project_id');
9-
const ovhClusterId = getInput('cluster_id');
10-
const ovhNodepoolId = getInput('nodepool_id');
11-
const nodesNumber = getInput('number_of_nodes');
4+
const ovhEndpoint = getInput("endpoint");
5+
const ovhProjectId = getInput("project_id");
6+
const ovhClusterId = getInput("cluster_id");
7+
const ovhNodepoolId = getInput("nodepool_id");
8+
const nodesNumber = getInput("number_of_nodes");
9+
10+
// setup OVH api client
11+
const client = ovh({
12+
endpoint: ovhEndpoint,
13+
appKey: process.env.OVH_APPLICATION_KEY,
14+
appSecret: process.env.OVH_APPLICATION_SECRET,
15+
consumerKey: process.env.OVH_CONSUMER_KEY,
16+
});
1217

13-
// setup OVH api client
14-
const client = ovh({
15-
endpoint: ovhEndpoint,
16-
appKey: process.env.OVH_APP_KEY,
17-
appSecret: process.env.OVH_APP_SECRET,
18-
consumerKey: process.env.OVH_CONSUMER_KEY,
19-
});
18+
// Call the OVH API to scale the nodepool
19+
// see https://eu.api.ovh.com/console/?section=%2Fcloud&branch=v1#put-/cloud/project/-serviceName-/kube/-kubeId-/nodepool/-nodePoolId-
20+
// curl -X PUT "https://eu.api.ovh.com/v1/cloud/project//kube//nodepool/" \
21+
// -H "content-type: application/json" \
22+
// -d '{"autoscale":false,"autoscaling":{"scaleDownUnneededTimeSeconds":0,"scaleDownUnreadyTimeSeconds":0,"scaleDownUtilizationThreshold":0},"desiredNodes":0,"maxNodes":0,"minNodes":0,"nodesToRemove":["string"],"template":{"metadata":{"annotations":{"any-key":"string"},"finalizers":["string"],"labels":{"any-key":"string"}},"spec":{"taints":[{"effect":"NoExecute","key":"string","value":"string"}],"unschedulable":false}}}' \
2023

21-
// Call the OVH API to scale the nodepool
22-
// see https://eu.api.ovh.com/console/?section=%2Fcloud&branch=v1#put-/cloud/project/-serviceName-/kube/-kubeId-/nodepool/-nodePoolId-
23-
// curl -X PUT "https://eu.api.ovh.com/v1/cloud/project//kube//nodepool/" \
24-
// -H "content-type: application/json" \
25-
// -d '{"autoscale":false,"autoscaling":{"scaleDownUnneededTimeSeconds":0,"scaleDownUnreadyTimeSeconds":0,"scaleDownUtilizationThreshold":0},"desiredNodes":0,"maxNodes":0,"minNodes":0,"nodesToRemove":["string"],"template":{"metadata":{"annotations":{"any-key":"string"},"finalizers":["string"],"labels":{"any-key":"string"}},"spec":{"taints":[{"effect":"NoExecute","key":"string","value":"string"}],"unschedulable":false}}}' \
24+
console.log("Scaling nodepool with the following parameters:", {
25+
endpoint: ovhEndpoint,
26+
projectId: ovhProjectId,
27+
clusterId: ovhClusterId,
28+
nodepoolId: ovhNodepoolId,
29+
desiredNodes: parseInt(nodesNumber),
30+
url: `/cloud/project/${ovhProjectId}/kube/${ovhClusterId}/nodepool/${ovhNodepoolId}`,
31+
method: "PUT",
32+
body: {
33+
autoscale: false,
34+
autoscaling: {},
35+
minNodes: parseInt(nodesNumber),
36+
maxNodes: parseInt(nodesNumber),
37+
desiredNodes: parseInt(nodesNumber),
38+
},
39+
});
40+
41+
let response;
42+
try {
43+
response = await client.requestPromised(
44+
"PUT",
45+
`/cloud/project/${ovhProjectId}/kube/${ovhClusterId}/nodepool/${ovhNodepoolId}`,
46+
{
47+
autoscale: false,
48+
autoscaling: {},
49+
minNodes: parseInt(nodesNumber),
50+
maxNodes: parseInt(nodesNumber),
51+
desiredNodes: parseInt(nodesNumber),
52+
}
53+
);
2654

27-
const response = await client.requestPromised('PUT', `/v1/cloud/project/${ovhProjectId}/kube/${ovhClusterId}/nodepool/${ovhNodepoolId}`, {
28-
"desiredNodes": nodesNumber,
29-
});
30-
setOutput("response", response)
55+
console.log("OVH API response:", response.code);
56+
setOutput("response", response);
3157
} catch (err) {
32-
setFailed(err.message)
58+
// Handle OVH API 204 No Content as success
59+
if (err && err.error === 204) {
60+
console.log("OVH API returned 204 No Content (success)");
61+
setOutput("response", { code: 204, message: "No Content (success)" });
62+
} else {
63+
setFailed(`Action error: ${err && err.message ? err.message : JSON.stringify(err)}`);
64+
}
3365
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.0.0",
44
"description": "Scale up or down your OVH MKS nodepool",
55
"main": "index.js",
6+
"type": "module",
67
"scripts": {
78
"test": "echo \"Error: no test specified\" && exit 1"
89
},

scripts/create-ovh-creds.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
set -x
6+
7+
if [ -z "${OVH_APPLICATION_KEY}" ]; then
8+
echo "OVH_APPLICATION_KEY is not set"
9+
exit 1
10+
fi
11+
12+
curl -XPOST -H "X-Ovh-Application: ${OVH_APPLICATION_KEY}" -H "Content-type: application/json" https://eu.api.ovh.com/1.0/auth/credential -d '{
13+
"accessRules": [
14+
{
15+
"method": "PUT",
16+
"path": "/cloud/project/*/kube/*/nodepool/*"
17+
}
18+
],
19+
"redirection": "https://hoverkraft.cloud"
20+
}' | jq

0 commit comments

Comments
 (0)