Skip to content

Improved support for Pod related Validating Admission Policies. #129939

@vinayakankugoyal

Description

@vinayakankugoyal

What would you like to be added?

Validating Admission Policies is one of my favorite features in kubernetes. I have been using it to write several policies. The most common type of of policies I write are related to PodSpec (most security folks also likely end up doing this). However most of the time I end up writing a policy like the following.

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: "no-gitrepo-volumes"
spec:
  failurePolicy: Fail
  matchConstraints:
    resourceRules:
    - apiGroups:   [""]
      apiVersions: ["v1"]
      operations:  ["CREATE", "UPDATE"]
      resources:   ["pods", "podtemplates", "replicationcontrollers"]
    - apiGroups:   ["apps"]
      apiVersions: ["v1"]
      operations:  ["CREATE", "UPDATE"]
      resources:   ["deployments", "replicasets", "statefulsets", "daemonsets"]
    - apiGroups:   ["batch"]
      apiVersions: ["v1"]
      operations:  ["CREATE", "UPDATE"]
      resources:   ["jobs", "cronjobs"]
  validations:
    - expression: |-
        object.kind != 'Pod' ||
        !object.spec.?volumes.orValue([]).exists(v, has(v.gitRepo))
      message: "gitRepo volumes are not allowed."
    - expression: |-
        object.kind != 'PodTemplate' ||
        !object.template.spec.?volumes.orValue([]).exists(v, has(v.gitRepo))
      message: "gitRepo volumes are not allowed."
    - expression: |-
        !['Deployment','ReplicaSet','DaemonSet','StatefulSet','Job', 'ReplicationController'].exists(kind, object.kind == kind) ||
        !object.spec.template.spec.?volumes.orValue([]).exists(v, has(v.gitRepo))
      message: "gitRepo volumes are not allowed."
    - expression: |-
        object.kind != 'CronJob' ||
        !object.spec.jobTemplate.spec.template.spec.?volumes.orValue([]).exists(v, has(v.gitRepo))
      message: "gitRepo volumes are not allowed."

Notice how I have to extract the PodSpec from different object types and so have to write multiple validating expressions. This gets annoying and messy. Could we provide a function or something that extracts the PodSpec from the templates? Then we could write policies like:

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: "no-gitrepo-volumes"
spec:
  failurePolicy: Fail
  matchConstraints:
    resourceRules:
    - apiGroups:   [""]
      apiVersions: ["v1"]
      operations:  ["CREATE", "UPDATE"]
      resources:   ["pods", "podtemplates", "replicationcontrollers"]
    - apiGroups:   ["apps"]
      apiVersions: ["v1"]
      operations:  ["CREATE", "UPDATE"]
      resources:   ["deployments", "replicasets", "statefulsets", "daemonsets"]
    - apiGroups:   ["batch"]
      apiVersions: ["v1"]
      operations:  ["CREATE", "UPDATE"]
      resources:   ["jobs", "cronjobs"]
  validations:
    - expression: |-
        !extractPodSpec(object).?volumes.orValue([]).exists(v, has(v.gitRepo))
      message: "gitRepo volumes are not allowed."

Why is this needed?

Adding something similar to what I proposed above will greatly help in simplifying policy authoring and therefore reduce the chances of bugs in policies.

Metadata

Metadata

Assignees

Labels

kind/featureCategorizes issue or PR as related to a new feature.lifecycle/rottenDenotes an issue or PR that has aged beyond stale and will be auto-closed.needs-triageIndicates an issue or PR lacks a `triage/foo` label and requires one.sig/api-machineryCategorizes an issue or PR as relevant to SIG API Machinery.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions