@@ -4,7 +4,48 @@ const debug = require("debug")("k8s-autodeploy:services");
44const { executeCommand, deployment } = require ( "../util/commands" ) ;
55const { 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
849router . post ( "/:images/rollout" , async function ( request , response , next ) {
950 const dockerTag = request . body . push_data . tag ;
1051 const images = request . params . images . split ( "," ) ;
0 commit comments