|
| 1 | +# ValidatingAdmissionPolicy: prevent manual application of the kagenti.io/type |
| 2 | +# label on Deployments and StatefulSets. Only the kagenti-operator controller |
| 3 | +# (via an AgentRuntime CR) is allowed to set this label. Users who need the |
| 4 | +# label must create an AgentRuntime targeting their workload instead. |
| 5 | +# |
| 6 | +# The policy protects the label in TWO locations: |
| 7 | +# 1. object.metadata.labels — top-level workload label |
| 8 | +# 2. object.spec.template.metadata.labels — pod template label (propagated |
| 9 | +# to every Pod the workload creates) |
| 10 | +# |
| 11 | +# The policy is split into two layers: |
| 12 | +# |
| 13 | +# matchConditions — skip evaluation entirely when: |
| 14 | +# 1. The label is absent from BOTH locations (irrelevant request), OR |
| 15 | +# 2. The request originates from a trusted service account. |
| 16 | +# Two operator SA identities are listed because kustomize (make deploy) |
| 17 | +# and Helm use different namespaces and SA names: |
| 18 | +# - kagenti-operator-system/kagenti-operator-controller-manager (kustomize) |
| 19 | +# - kagenti-system/controller-manager (Helm) |
| 20 | +# |
| 21 | +# validations — for everyone else, allow only UPDATE requests where the |
| 22 | +# label already existed on the previous revision with the same value in |
| 23 | +# whichever location(s) it appears. |
| 24 | +# |
| 25 | +# CREATE requests that carry the label are always rejected for non-operator |
| 26 | +# callers, because the "label unchanged" check evaluates to false on CREATE |
| 27 | +# (there is no oldObject). |
| 28 | +apiVersion: admissionregistration.k8s.io/v1 |
| 29 | +kind: ValidatingAdmissionPolicy |
| 30 | +metadata: |
| 31 | + name: agent-label-protection |
| 32 | + labels: |
| 33 | + app.kubernetes.io/name: kagenti-operator |
| 34 | + app.kubernetes.io/component: admission |
| 35 | +spec: |
| 36 | + failurePolicy: Fail |
| 37 | + matchConstraints: |
| 38 | + resourceRules: |
| 39 | + - apiGroups: ["apps"] |
| 40 | + apiVersions: ["v1"] |
| 41 | + operations: ["CREATE", "UPDATE"] |
| 42 | + resources: ["deployments", "statefulsets"] |
| 43 | + |
| 44 | + matchConditions: |
| 45 | + - name: 'has-agent-type-label' |
| 46 | + expression: >- |
| 47 | + (has(object.metadata.labels) && 'kagenti.io/type' in object.metadata.labels) |
| 48 | + || (has(object.spec.template.metadata) |
| 49 | + && has(object.spec.template.metadata.labels) |
| 50 | + && 'kagenti.io/type' in object.spec.template.metadata.labels) |
| 51 | +
|
| 52 | + - name: 'not-operator-service-account' |
| 53 | + expression: >- |
| 54 | + !(request.userInfo.username == 'system:serviceaccount:kagenti-operator-system:kagenti-operator-controller-manager') |
| 55 | + && !(request.userInfo.username == 'system:serviceaccount:kagenti-system:controller-manager') |
| 56 | +
|
| 57 | + variables: |
| 58 | + - name: metaLabelUnchanged |
| 59 | + expression: >- |
| 60 | + !('kagenti.io/type' in object.metadata.labels) |
| 61 | + || (request.operation == 'UPDATE' |
| 62 | + && 'kagenti.io/type' in oldObject.metadata.labels |
| 63 | + && oldObject.metadata.labels['kagenti.io/type'] == object.metadata.labels['kagenti.io/type']) |
| 64 | +
|
| 65 | + - name: podTemplateLabelUnchanged |
| 66 | + expression: >- |
| 67 | + !(has(object.spec.template.metadata) |
| 68 | + && has(object.spec.template.metadata.labels) |
| 69 | + && 'kagenti.io/type' in object.spec.template.metadata.labels) |
| 70 | + || (request.operation == 'UPDATE' |
| 71 | + && has(oldObject.spec.template.metadata) |
| 72 | + && has(oldObject.spec.template.metadata.labels) |
| 73 | + && 'kagenti.io/type' in oldObject.spec.template.metadata.labels |
| 74 | + && oldObject.spec.template.metadata.labels['kagenti.io/type'] == object.spec.template.metadata.labels['kagenti.io/type']) |
| 75 | +
|
| 76 | + validations: |
| 77 | + - expression: "variables.metaLabelUnchanged" |
| 78 | + messageExpression: >- |
| 79 | + 'The kagenti.io/type label on ' |
| 80 | + + object.metadata.namespace + '/' + object.metadata.name |
| 81 | + + ' can only be applied by the kagenti-operator via an AgentRuntime CR.' |
| 82 | + + ' Create an AgentRuntime targeting this workload instead of manually setting the label.' |
| 83 | + reason: Forbidden |
| 84 | + - expression: "variables.podTemplateLabelUnchanged" |
| 85 | + messageExpression: >- |
| 86 | + 'The kagenti.io/type label in the pod template of ' |
| 87 | + + object.metadata.namespace + '/' + object.metadata.name |
| 88 | + + ' can only be applied by the kagenti-operator via an AgentRuntime CR.' |
| 89 | + + ' Create an AgentRuntime targeting this workload instead of manually setting the label.' |
| 90 | + reason: Forbidden |
0 commit comments