Defining a default network policy for projects¶
To enforce consistent network isolation and security controls across projects, configure a default project template to automatically apply network policies to newly created projects.
As a cluster administrator, you can modify the new project template to automatically include network policies when you create a new project. If you do not yet have a customized template for new projects, you must first create one.
Modify the template for new projects¶
To modify the default project template to customize the resources and settings applied when users create new projects, you can create a custom project template.
As a cluster administrator, you can modify the default project template so that new projects are created using your custom requirements.
To create your own custom project template:
Prerequisites
- You have access to an OpenShift Container Platform cluster using an account with
cluster-adminpermissions.
Procedure
-
Log in as a user with
cluster-adminprivileges. -
Generate the default project template:
-
Use a text editor to modify the generated
template.yamlfile by adding objects or modifying existing objects. -
The project template must be created in the
openshift-confignamespace. Load your modified template: -
Edit the project configuration resource using the web console or CLI.
-
Using the web console, complete the following tasks:
- Navigate to the Administration → Cluster Settings page.
- Click Configuration to view all configuration resources.
- Find the entry for Project and click Edit YAML.
-
Using the CLI, complete the following tasks:
-
Edit the
project.config.openshift.io/clusterresource:
-
-
-
Update the
specsection to include theprojectRequestTemplateandnameparameters. Ensure you set the name of your uploaded project template. The default name isproject-request. -
After you save your changes, create a new project to verify that your changes were successfully applied.
Add network policies to the new project template¶
You can add NetworkPolicy objects to the default project template so that new projects automatically include predefined network isolation rules. Applying network policies through templates helps enforce consistent network security controls across projects.
Prerequisites
- Your cluster uses a default container network interface (CNI) network plugin that supports
NetworkPolicyobjects, such as the OVN-Kubernetes. - You installed the OpenShift CLI (
oc). - You must log in to the cluster with a user with
cluster-adminprivileges. - You must have created a custom default project template for new projects.
Procedure
-
Edit the default template for a new project by running the following command:
Replace
<project_template>with the name of the default template that you configured for your cluster. The default template name isproject-request. -
In the template, add each
NetworkPolicyobject as an element to theobjectsparameter. Theobjectsparameter accepts a collection of one or more objects.In the following example, the
objectsparameter collection includes severalNetworkPolicyobjects.objects: - apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-from-same-namespace spec: podSelector: {} ingress: - from: - podSelector: {} - apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-from-openshift-ingress spec: ingress: - from: - namespaceSelector: matchLabels: policy-group.network.openshift.io/ingress: podSelector: {} policyTypes: - Ingress - apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allow-from-kube-apiserver-operator spec: ingress: - from: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: openshift-kube-apiserver-operator podSelector: matchLabels: app: kube-apiserver-operator policyTypes: - Ingress ... -
Optional: Create a new project and confirm the successful creation of your network policy objects.
-
Create a new project:
Replace
<project>with the name of the project you want to create. -
Confirm that the network policy objects in the new project template exist in the new project:
Expected output:
-