Skip to content

Commit 07ad240

Browse files
committed
Images: allow 'restarting' (recreating) in addition to rollout
1 parent 469c7e8 commit 07ad240

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
{
22
"name": "k8s-autodeploy",
3-
"version": "1.7.0",
3+
"version": "1.8.0",
44
"private": true,
55
"scripts": {
66
"start": "NODE_ENV='production' DEBUG=k8s-autodeploy:* node ./bin/www",
77
"dev": "DEBUG=k8s-autodeploy:* node ./bin/www",
88
"debug": "DEBUG=* node ./bin/www",
9-
"build": "docker build -t k8s-autodeploy . && docker tag k8s-autodeploy 163030813197.dkr.ecr.eu-central-1.amazonaws.com/k8s-autodeploy:v1.6.1",
10-
"dist": "docker push 163030813197.dkr.ecr.eu-central-1.amazonaws.com/k8s-autodeploy:v1.6.1"
9+
"build": "docker build -t k8s-autodeploy ."
1110
},
1211
"dependencies": {
1312
"body-parser": "~1.19.0",

routes/images.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,48 @@ const debug = require("debug")("k8s-autodeploy:services");
44
const { executeCommand, deployment } = require("../util/commands");
55
const { commonErrorHandler } = require("../util/errors");
66

7-
// Restart all deployments that run a specific image
7+
// Recreate all deployments that run a specific image
8+
router.post("/:images/restart", async function (request, response, next) {
9+
const dockerTag = request.body.push_data.tag;
10+
const images = request.params.images.split(",");
11+
12+
debug(
13+
"NEW restart request. Images => %s, DockerTag => %s",
14+
request.params.images,
15+
dockerTag
16+
);
17+
18+
// Gets all deployments running any of the given images separated by space
19+
// returns string like: "deployment1 image1\ndeployment2 image2\n"
20+
const command = `kubectl get deployments -o jsonpath="{range .items[*]}{.metadata.name}{' '}{.spec.template.spec.containers[*].image}{'\\n'}{end}"`;
21+
22+
const stdout = await executeCommand("sh", ["-c", command]);
23+
const deployments = stdout.trim().split("\n");
24+
25+
// Filter deployments which match the given images
26+
const filteredDeployments = deployments.filter((deploymentInfo) => {
27+
const [, imageName] = deploymentInfo.split(" ");
28+
return images.includes(imageName);
29+
});
30+
31+
debug("Found deployments => %s", filteredDeployments);
32+
33+
response.status(200).json({});
34+
35+
// Note: This approach initiates the recreate process in the background after responding to the request.
36+
// It does not handle concurrency control for multiple overlapping requests.
37+
filteredDeployments.forEach(async (deploymentInfo) => {
38+
const [deploymentName] = deploymentInfo.split(" ");
39+
try {
40+
await deployment.restart(deploymentName);
41+
debug("Deployment restarted => %s", deploymentName);
42+
} catch (error) {
43+
debug("Error during restart => %s", error.message);
44+
}
45+
});
46+
});
47+
48+
// Rollout all deployments that run a specific image
849
router.post("/:images/rollout", async function (request, response, next) {
950
const dockerTag = request.body.push_data.tag;
1051
const images = request.params.images.split(",");

routes/services.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const debug = require('debug')('k8s-autodeploy:services')
44
const { deployment } = require('../util/commands')
55
const {NotSupportedDockerTagError, commonErrorHandler} = require('../util/errors');
66

7-
// Restart deployment
7+
// Recreate deployment
88
router.post('/:services/restart', function (request, response, next) {
99
const dockerTag = request.body.push_data.tag
1010
const services = request.params.services.split(',')

0 commit comments

Comments
 (0)