Use a mutation policy to edit Kubernetes resources when pods are deployed in your clusters.
Before you can create a mutation policy for an object, you must be associated with the .admin
role for that object.
To confirm you have the correct permissions:
Using VMware Tanzu Platform for Kubernetes, you can make the deployments to your clusters more consistent by implementing policies that add or edit resources in those deployments before they are admitted. Mutation policies, implemented using OPA Gatekeeper, can help prevent the deployment of pods that don’t conform to your specifications. You can use a mutation policy to alter the following types of Kubernetes resource properties:
A mutation policy can alter (mutate) the pod specification (podspec) to enforce conformance to the policy. When a pod is deployed to the cluster, the policy mutates the podspec as defined in the policy before admitting the request to deploy the pod.
You can create a mutation policy to specify a label to be applied to a Kubernetes resource before it is admitted to a cluster. When a deployment is evaluated for admission, the policy creates the key-value pair for the label only if it does not already exist. If there is an existing label on the resource, the policy cannot change it. Because the policy evaluates deployments only during admission, you can manually alter mutations implemented by the policy after admission.
You can apply mutation policies in the clusters hierarchy (infrastructure view) in Tanzu Platform for Kubernetes, and they are inherited down through the hierarchy.
When multiple mutation policies are applied to a cluster, through either direct or inherited policies, they are evaluated as follows:
Because a mutation policy can overwrite changes applied by a previously run mutation policy, the order in which they are run is significant. When a workload is deployed to a cluster that has both mutation policies and security policies, first the mutation policies are evaluated and applied to mutate the podspec as specified. After all mutations are applied, the security policies are evaluated to verify that the podspec is conformant to allow deployment.
To create a policy in Tanzu Platform hub:
In Operations Policies > Cluster Policies, click the Cluster Groups and Clusters tab.
Use the tree control to navigate to and select the object for which you want to create a mutation policy.
Click Create Policy and select Mutation.
Provide a policy name. The Scope and Object name is automatically generated based on the object you selected in the tree.
Select a mutation template.
Provide the details about how you want to mutate the property. Click Save.
(Optional) You can provide label selectors to specify particular namespaces that you want to include or exclude for this policy.
Click Create Policy.
After clicking Create Policy, the policy is created and applied to the object and is displayed on the Policies page.
Because a mutation policy assesses and mutates pods only at the time of admission, the policy impacts only new, incoming requests for creating or updating pods. The mutation policy does not impact pods that have already been admitted to the cluster.