Skip to main content

create() Function

The create() function is able to create any resource in the kubernetes cluster, by passing it a Kubernetes object. JsPolicy will then try to create the object in the cluster and return the created object or an error if creation has failed. The execution will block as long as the creation is in progress.


This example will create a new configmap every time a new pod is created.

kind: JsPolicy
name: "create.resource.example"
operations: ["CREATE"]
resources: ["pods"]
javascript: |
// create() is able to create any resource in the kubernetes cluster
// create() takes parameters in the form of (object)
// this will create a new configmap in the namespace with the same name as
// the pod and contains all the container names.
const result = create({
kind: "ConfigMap",
apiVersion: "v1",
metadata: {
"namespace": request.object.metadata.namespace
data: {
containers: (request.object.spec?.containers || []).map(container =>",")
if (!result.ok) {
// check for a specific error type
if (result.reason === "AlreadyExists") {
warn(`ConfigMap already exists`);
} else {
warn(`Error creating ConfigMap (Reason ${result.reason}): ${result.message}`);
} else {
print("Created ConfigMap", result.object);