Skip to main content

readFileSync() Function

The readFileSync() function will read a local file in the jsPolicy container and return the contents as a string. If the file cannot be found undefined will be returned.


This example shows how you can directly access the Kubernetes API within a policy, without calling any of the predefined jsPolicy functions except readFileSync and fetchSync.

kind: JsPolicy
name: ""
operations: ["CREATE"]
resources: ["pods"]
javascript: |
// read service account token.
const token = readFileSync("/var/run/secrets/");
// retrieve all namespaces from the kube service directly, isn't that cool?
const res = fetchSync("https://kubernetes.default/api/v1/namespaces", {
"method": "GET",
"insecure": true,
"headers": {
"Authorization": "bearer " + token
if (res.ok) {
const namespaceList = res.json();
namespaceList.items.forEach(namespace => {
} else {
print("Something went wrong (" + res.status + "): " + res.text());