Skip to content

Commit c1fb9eb

Browse files
authored
Add Validating Admission Policy Examples to authorize Custom Compute Classes (#1814)
* Add Validatiing Admission Policies to authorize Compute Classes * Add list of CCs the VAP monitors * re-contextualize references to K8s API objects in VAP README * Remove CCC reference in toleration VAP name * Fix link in VAP README
1 parent d6d5c57 commit c1fb9eb

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# ComputeClasses Validating Admission Policy example
2+
3+
[![Open in Cloud Shell](https://gstatic.com/cloudssh/images/open-btn.svg)](https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https://github.com/GoogleCloudPlatform/kubernetes-engine-samples&cloudshell_workspace=autoscaling/custom-compute-classes/vap)
4+
5+
This example shows how to secure ComputeClasses (CC) and workloads scheduled onto them with Validating Admission Policies (VAP).
6+
Follow the tutorial to use VAP with CC to secure workloads for use with Resource Manager Tags - https://cloud.google.com/kubernetes-engine/docs/how-to/tags-firewall-policies#authorize-ccc-workloads
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: admissionregistration.k8s.io/v1
16+
kind: ValidatingAdmissionPolicy
17+
metadata:
18+
name: restrict-toleration
19+
spec:
20+
failurePolicy: Fail
21+
paramKind:
22+
apiVersion: v1
23+
kind: ConfigMap
24+
matchConstraints:
25+
# GKE will mutate any pod specifying a CC label in a nodeSelector
26+
# or in a nodeAffinity with a toleration for the CC node label.
27+
# Mutation hooks will always mutate the K8s object before validating
28+
# the admission request.
29+
# Pods created by Jobs, CronJobs, Deployments, etc. will also be validated.
30+
# See https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#admission-control-phases for details
31+
resourceRules:
32+
- apiGroups: [""]
33+
apiVersions: ["v1"]
34+
operations: ["CREATE", "UPDATE"]
35+
resources: ["pods"]
36+
matchConditions:
37+
- name: 'match-tolerations'
38+
# Validate only if compute class toleration exists
39+
# and the CC label tolerated is listed in the configmap.
40+
expression: >
41+
object.spec.tolerations.exists(t, has(t.key) &&
42+
t.key == 'cloud.google.com/compute-class' &&
43+
params.data.computeClasses.split('\\n').exists(cc, cc == t.value))
44+
validations:
45+
# ConfigMap with permitted namespace list referenced via `params`.
46+
- expression: "params.data.namespaces.split('\\n').exists(ns, ns == object.metadata.namespace)"
47+
messageExpression: "'Compute class toleration not permitted on workloads in namespace ' + object.metadata.namespace"
48+
49+
---
50+
apiVersion: admissionregistration.k8s.io/v1
51+
kind: ValidatingAdmissionPolicyBinding
52+
metadata:
53+
name: restrict-toleration-binding
54+
spec:
55+
policyName: restrict-toleration
56+
validationActions: ["Deny"]
57+
paramRef:
58+
name: allowed-ccc-namespaces
59+
namespace: default
60+
parameterNotFoundAction: Deny
61+
---
62+
apiVersion: v1
63+
kind: ConfigMap
64+
metadata:
65+
name: allowed-ccc-namespaces
66+
namespace: default
67+
data:
68+
# Replace example namespaces in line-separated list below.
69+
namespaces: |
70+
foo
71+
bar
72+
baz
73+
# ComputeClass names to monitor with this validation policy.
74+
# The 'autopilot' and 'autopilot-spot' CCs are present on
75+
# all NAP Standard and Autopilot clusters.
76+
computeClasses: |
77+
MY_COMPUTE_CLASS
78+
autopilot
79+
autopilot-spot
80+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: admissionregistration.k8s.io/v1
16+
kind: ValidatingAdmissionPolicy
17+
metadata:
18+
name: restrict-wildcard-toleration
19+
spec:
20+
failurePolicy: Fail
21+
variables:
22+
# This variable identifies tolerations that could match any taint key or any existing taint.
23+
- name: hasWildcardToleration
24+
expression: >-
25+
has(object.spec.tolerations) &&
26+
object.spec.tolerations.exists(t,
27+
(t.operator == 'Exists' && !(has(t.key) && t.key != ''))
28+
)
29+
matchConstraints:
30+
resourceRules:
31+
- apiGroups: [""]
32+
apiVersions: ["v1"]
33+
operations: ["CREATE", "UPDATE"]
34+
resources: ["pods"]
35+
validations:
36+
- expression: "!variables.hasWildcardToleration"
37+
message: "Pods in this namespace cannot use wildcard tolerations."
38+
---
39+
apiVersion: admissionregistration.k8s.io/v1
40+
kind: ValidatingAdmissionPolicyBinding
41+
metadata:
42+
name: restrict-wildcard-toleration-binding
43+
spec:
44+
policyName: restrict-wildcard-toleration
45+
validationActions: ["Deny"]
46+
matchResources:
47+
namespaceSelector:
48+
matchLabels:
49+
kubernetes.io/metadata.name: FORBIDDEN_NAMESPACE

0 commit comments

Comments
 (0)