-
Notifications
You must be signed in to change notification settings - Fork 0
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
3 changed files
with
91 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
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,86 @@ | ||
module.exports = function updateDeploymentManifests(manifests, options = {}) { | ||
// Set default values for options if not provided | ||
const { | ||
initialDelaySeconds = 5, | ||
periodSeconds = 5, | ||
gracefulShutdownSeconds = 30, // Default graceful shutdown time | ||
disableAnnotation = "kontinuous/plugin.zeroDowntimeHandled", | ||
} = options | ||
|
||
manifests.forEach((manifest) => { | ||
// Check if the resource is a Deployment and doesn't have the disable annotation | ||
if ( | ||
manifest.kind === "Deployment" && | ||
(!manifest.metadata.annotations || | ||
manifest.metadata.annotations[disableAnnotation] !== "true") | ||
) { | ||
// Ensure metadata, spec, and template sections exist | ||
manifest.metadata.annotations = manifest.metadata.annotations || {} | ||
manifest.spec = manifest.spec || {} | ||
manifest.spec.template = manifest.spec.template || {} | ||
manifest.spec.template.spec = manifest.spec.template.spec || { | ||
containers: [], | ||
volumes: [], | ||
} | ||
|
||
// Setup the postStart and preStop lifecycle hooks and readiness probe | ||
manifest.spec.template.spec.containers.forEach((container) => { | ||
// Setup lifecycle hooks | ||
container.lifecycle = { | ||
postStart: { | ||
exec: { | ||
// Command to create the readiness file | ||
command: [ | ||
"sh", | ||
"-c", | ||
"touch /var/run/readiness-check/readiness-file", | ||
], | ||
}, | ||
}, | ||
preStop: { | ||
exec: { | ||
// Adding a sleep of gracefulShutdownSeconds before removing the readiness file | ||
command: [ | ||
"sh", | ||
"-c", | ||
`sleep ${gracefulShutdownSeconds}; rm -f /var/run/readiness-check/readiness-file`, | ||
], | ||
}, | ||
}, | ||
} | ||
|
||
// Setup readiness probe | ||
container.readinessProbe = { | ||
exec: { | ||
command: ["cat", "/var/run/readiness-check/readiness-file"], | ||
}, | ||
initialDelaySeconds, | ||
periodSeconds, | ||
} | ||
|
||
// Define a unique and precise mount path | ||
const mountPath = "/var/run/readiness-check" | ||
const uniqueVolumeName = "readiness-check-volume" | ||
container.volumeMounts = container.volumeMounts || [] | ||
container.volumeMounts.push({ | ||
name: uniqueVolumeName, | ||
mountPath, | ||
}) | ||
}) | ||
|
||
// Ensure the volume does not conflict and is added to the pod spec | ||
if ( | ||
!manifest.spec.template.spec.volumes.some( | ||
(v) => v.name === "readiness-check-volume" | ||
) | ||
) { | ||
manifest.spec.template.spec.volumes.push({ | ||
name: "readiness-check-volume", | ||
emptyDir: {}, | ||
}) | ||
} | ||
} | ||
}) | ||
|
||
return manifests | ||
} |
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