I know how to use RBAC with X.509 certificates to identify a user of kubectl
and restrict them (using Role
and RoleBinding
) from creating pods of any kind in a namespace. However, I don't know how I can prevent them from putting specific labels on a pod (or any resource) they're trying to create.
What I want to do is something like:
- Create a
NetworkPolicy
that only resources in other namespaces with the labelgroup: cross-ns
are allowed to reach a resource in thespecial-namespace
- Have a user who cannot create pods or other resources with the label
group: cross-ns
- Have another user who can create resources with the label
group: cross-ns
Is this possible?
You can use the Kubernetes-native policy engine called Kyverno:
A Kyverno policy is a collection of rules that can be applied to the entire cluster (
ClusterPolicy
) or to the specific namespace (Policy
).I will create an example to illustrate how it may work.
First we need to install Kyverno, you have the option of installing Kyverno directly from the latest release manifest, or using Helm (see: Quick Start guide):
After successful installation, let's create a simple
ClusterPolicy
:In the example above, only using the
cluster-admin
ClusterRole
you can modify a Pod with a labelpurpose
.Suppose I have two users (
john
anddave
), but onlyjohn
is linked to thecluster-admin
ClusterRole
viaClusterRoleBinding
:Finally, we can test if it works as expected:
More examples with detailed explanations can be found in the Kyverno Writing Policies documentation.