Configure network policies
To restrict Pod-to-Pod traffic, enable network policies for the Che components, for each workspace, or for both. The Che Operator manages the NetworkPolicy resources for the components, and the DevWorkspace Operator provisions a NetworkPolicy for each workspace. Both features are disabled by default.
Network policies
By default, all Pods in a Kubernetes cluster can communicate with each other across namespaces. In Che, this allows a workspace Pod in one user namespace to send traffic to workspace Pods in other user namespaces.
Network policies restrict network traffic to only the flows required by Che components and workspaces.
When Operator-managed network policies for the Che components are enabled, the Che Operator creates the following resources:
| Name | Description |
|---|---|
|
Allows ingress traffic between Che Pods in the same namespace. |
|
Allows ingress traffic from user workspace namespaces. |
|
Allows ingress traffic from the OpenShift ingress namespace. |
|
Allows ingress traffic from Pods that use the host network, for example the OpenShift router. |
|
Allows ingress traffic from the OpenShift monitoring namespace. |
|
Allows ingress traffic from the Operator Pod to Che components. |
|
Allows all egress traffic from Che Pods. |
On Kubernetes, the Operator creates only the allow-all-ingress and allow-all-egress NetworkPolicies, which allow all ingress and egress traffic.
The namespace of the cluster ingress controller is not known to the Operator, so enabling this setting on Kubernetes does not restrict any traffic by itself.
When network isolation for workspaces is enabled, the DevWorkspace Operator creates a <workspace_id>-networkpolicy NetworkPolicy resource in the user namespace for each workspace.
See Configure network policies for workspaces.
For how the ingress and egress fields of the spec.devEnvironments.networking.networkPolicy CheCluster field behave, see Workspace network policy rules.
Workspace network policy rules
The ingress and egress fields of the spec.devEnvironments.networking.networkPolicy CheCluster field accept standard Kubernetes NetworkPolicy rules.
| Field state | ingress result |
egress result |
|---|---|---|
Not specified |
The default rules of the DevWorkspace Operator apply, and the Che Operator adds one rule allowing traffic from the Che components. On OpenShift, the defaults also allow traffic from the DevWorkspace Operator namespace and from the OpenShift monitoring, ingress, host-network, and console namespaces. On Kubernetes, the default allows all ingress because the DevWorkspace Operator cannot identify the namespace of the cluster ingress controller. |
The default rule of the DevWorkspace Operator applies, which allows all egress traffic. |
Empty list |
All ingress traffic to the workspace Pods is denied. |
All egress traffic from the workspace Pods is denied. |
Non-empty list |
Exactly those rules apply: the default rules are replaced, and the rule allowing traffic from the Che components is not added automatically. If the Che components must communicate with the workspaces, include a corresponding ingress rule. |
Exactly those rules apply. |
To restrict ingress on Kubernetes, define custom ingress rules.
Enable Operator-managed network policies
Enable Operator-managed network policies to restrict ingress traffic to the Che components, such as the gateway, server, dashboard, and registries.
The Che Operator creates and manages the NetworkPolicy resources automatically.
This feature is controlled by the spec.networking.networkPolicy.enabled CheCluster field and is disabled by default.
The NetworkPolicies are created in the Che namespace and apply only to the Pods labeled with app.kubernetes.io/part-of=che.eclipse.org.
When this field is set to false, the NetworkPolicies previously created by the Operator are removed.
-
An active
kubectlsession with administrative permissions to the destination Kubernetes cluster. See Overview of kubectl.
-
Enable Operator-managed network policies:
kubectl patch checluster/eclipse-che \ --namespace eclipse-che \ --type merge \ --patch '{"spec":{"networking":{"networkPolicy":{"enabled":true}}}}' -
Because workspace isolation inherits this setting, set
spec.devEnvironments.networking.networkPolicy.enabledexplicitly tofalseif you want to isolate only the Che components:kubectl patch checluster/eclipse-che \ --namespace eclipse-che \ --type merge \ --patch '{"spec":{"devEnvironments":{"networking":{"networkPolicy":{"enabled":false}}}}}'
-
Verify that the NetworkPolicy resources are created in the Che namespace:
kubectl get networkpolicies -n eclipse-che -
If network isolation for workspaces is enabled, start a workspace and verify that a NetworkPolicy resource is created for it in the user namespace:
kubectl get networkpolicies -n <user_namespace>
Configure network policies for workspaces
To restrict the traffic to and from workspace Pods, enable the NetworkPolicy that the DevWorkspace Operator provisions for each workspace.
The feature is controlled by the spec.devEnvironments.networking.networkPolicy CheCluster field and is disabled by default.
-
An active
kubectlsession with administrative permissions to the destination Kubernetes cluster. See Overview of kubectl.
-
Enable network policies for workspaces:
kubectl patch checluster/eclipse-che \ --namespace eclipse-che \ --type merge \ --patch '{"spec":{"devEnvironments":{"networking":{"networkPolicy":{"enabled":true}}}}}'The change applies to a workspace the next time that workspace is started or stopped. A running workspace picks it up after you stop and start it.
-
Optional: Define custom
ingressrules in thespec.devEnvironments.networking.networkPolicyfield to restrict the traffic that is allowed to reach the workspace Pods. A non-emptyingresslist replaces the default rules, so include rules for the traffic that must still be allowed, for example traffic from the Che components:spec: devEnvironments: networking: networkPolicy: enabled: true ingress: - from: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: eclipse-cheIn this example, the workspace accepts ingress traffic only from Pods in the Che components namespace. To allow traffic from additional namespaces, add a corresponding rule for each namespace.
-
Optional: Define custom
egressrules to restrict the traffic that the workspace Pods can send:spec: devEnvironments: networking: networkPolicy: enabled: true egress: - to: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: <peer_namespace>In this example, the workspace Pods can send traffic only to Pods in the
<peer_namespace>namespace.For how the
ingressandegressfields behave when they are not specified, empty, or non-empty, see Workspace network policy rules.
-
Start a workspace and verify that the NetworkPolicy resource is created for it in the user namespace:
kubectl get networkpolicies -n <user_namespace>