Slide 26
Slide 26 text
@makocchi
GCPUG Tokyo GKE Day March 2021
apiVersion: templates.gatekeeper.sh/v1beta1
kind: ConstraintTemplate
metadata:
name: autogkehostpathvolumes
spec:
targets:
rego: |-
violation_pod[{"msg": msg}] {
spec := helpers.get_spec(input.review.object)
spec["volumes"]
hostPathVolumes := {vol | vol := spec.volumes[_]; vol["hostPath"]}
containers := array.concat(object.get(spec, "initContainers", []), object.get(spec, "containers", []))
container := containers[_]
container["volumeMounts"]
volumeMount := container.volumeMounts[_]
hostPathVolume := hostPathVolumes[_]
hostPathVolume.name == volumeMount.name
object.get(volumeMount, "readOnly", false)
count({x | x := input.parameters.allowedPrefixes[_]; startswith(helpers.path(hostPathVolume.hostPath.path), x)}) == 0
msg := sprintf("hostPath volume %s used in container %s uses path %s which is not allowed in Autopilot. Allowed path prefixes
for hostPath volumes are: %v. Requesting user: <%s> and groups: <%v>", [hostPathVolume.name, container.name,
hostPathVolume.hostPath.path, input.parameters.allowedPrefixes, input.review.userInfo.username, input.review.userInfo.groups])
}
26
どんな Constraint templatesがあるのか
例によって抜粋していますが、先程の hostPath の prefix に関する
チェックはこの Rego で行われていますね