apiVersion: policy.jspolicy.com/v1beta1
kind: JsPolicy
metadata:
name: "remove.resource.example"
spec:
operations: ["DELETE"]
resources: ["pods"]
javascript: |
// remove() is able to delete any resource in the kubernetes cluster
// remove() takes parameters in the form of (oldObject)
// this will delete a configmap in the namespace with the same name as the pod
const configMap = get("ConfigMap", "v1", request.namespace + "/" + request.name);
if (configMap) {
const result = remove(configMap);
if (!result.ok) {
// check for a specific error type
if (result.reason === "NotFound") {
warn(`ConfigMap was deleted by someone else`);
} else {
warn(`Error deleting ConfigMap (Reason ${result.reason}): ${result.message}`);
}
} else {
print("Deleted ConfigMap", result.object);
}
} else {
warn(`ConfigMap ${request.namespace}/${request.name} was not found`);
}