Architecture
jsPolicy is a policy engine for Kubernetes that allows you to write policies in JavaScript or TypeScript.
#
ComponentsAlthough jsPolicy runs all of its components in a single container (not considering replicas when you scale up the replica number for high-availability), jsPolicy logically consists of three main components:
#
Webhook ManagerThe webhook manager is responsible for registering and managing admission webhooks with the Kubernetes API server, so that API server requests will apply the mutating and validating webhooks that are defined as JsPolicy
objects.
#
V8 JavaScript Sandbox PoolFor faster execution of policy code, jsPolicy maintains a pool of pre-heated V8 JavaScript sandboxes that can be used to run JavaScript code containing policy logic.
#
Policy CompilerThe policy compiler is a controller that monitors JsPolicy
resources and that creates and updates JsPolicyBundle
objects for all JsPolicy
objects that define the spec.javascript
field. The compilation process looks about like this:
- Retrieve all required
npm
packages specified inspec.dependencies
(similar tonpm install
downloading thedependencies
specified in apackage.json
file of a regular JavaScript project) - Run
webpack
to create a highly optimized bundle of JavaScript code that contains the code fromspec.javascript
and all dependencies while only bundling the functions that are actually needed for the execution of the code. - Compress the bundle using
gzip
. - Encode the bundle using
base64
. - Store the bundle in
spec.bundle
within the respectiveJsPolicyBundle
object.