Network Isolation
Model overview
kornercode restricts workspace traffic with Kubernetes NetworkPolicy. The model is label-based rather than per-workspace: cluster administrators define a set of policies once in the operator namespace, the Team controller copies them into every team namespace, and the Workspace controller activates them on a workspace pod through pod labels.
┌─────────────────────┐
cluster admin │ operator namespace │
creates │ │
──────────────► │ NetworkPolicy A │ code.kornercloud.dev/managed=true
│ podSelector: │ code.kornercloud.dev/network-policy-id=A
│ netpol.../A=true │
└─────────┬───────────┘
│ Team controller copies
│ (ReconcileNetworkPolicies)
▼
┌─────────────────────┐
│ team namespace │
│ │
│ NetworkPolicy A │
│ podSelector: │
│ netpol.../A=true │
└─────────────────────┘
▲
│ selects pods labeled netpol.../A=true
│ (set at pod creation by ReconcilePod,
│ kept in sync by ReconcileNetworkPolicy)
┌────────────┴────────┐
│ workspace pod │
│ (team namespace) │
└─────────────────────┘The operator does not create a NetworkPolicy per workspace, and the chart ships no policies. Without at least one policy activated on a workspace pod, Kubernetes applies no network restrictions to it.
Predefined NetworkPolicy structure
A predefined policy is an ordinary NetworkPolicy in the operator namespace (OPERATOR_NAMESPACE, the operator’s release namespace) that carries two labels:
| Label | Value | Purpose |
|---|---|---|
code.kornercloud.dev/managed | "true" | Marks the policy as managed; the Team controller discovers policies by this label |
code.kornercloud.dev/network-policy-id | e.g. "internet-only" | Policy identifier; referenced from SecurityProfile.spec.networkPolicy.policyRefs and used in the activation label |
The policy’s podSelector must match the activation label netpol.code.kornercloud.dev/<id>: "true". The operator does not check this; a policy whose selector uses a different label is copied but never selects a workspace pod.
Sample policies
hack/local-dev/workspace-profiles.yaml defines two sample policies for the local development environment. Use them as a starting point; adapt the namespace and label selectors to your installation.
kornercode-internet-only
For workspaces that need internet access (package registries, Git hosting) but must not reach other cluster services.
metadata:
name: kornercode-internet-only
namespace: kornercode
labels:
code.kornercloud.dev/managed: "true"
code.kornercloud.dev/network-policy-id: internet-only
spec:
podSelector:
matchLabels:
netpol.code.kornercloud.dev/internet-only: "true"
policyTypes: [Ingress, Egress]
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kornercode
podSelector:
matchLabels:
app.kubernetes.io/name: kornercode-gateway
egress:
- ports: # DNS
- protocol: TCP
port: 53
- protocol: UDP
port: 53
- to:
- ipBlock:
cidr: 0.0.0.0/0
except: ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"]- Ingress: only the gateway pod, selected by namespace and by the
app.kubernetes.io/name: kornercode-gatewaylabel that the chart sets on the gateway, may open connections. - Egress: DNS on port 53 is allowed to any destination. Other connections are allowed only to addresses outside the RFC 1918 ranges, which prevents the workspace from reaching other pods, the operator, the gateway and cluster Services, provided the cluster’s pod and Service networks use those ranges. Check your CNI’s address ranges; add them to
exceptif they are outside RFC 1918.
kornercode-cluster-access
For workspaces that need to reach cluster services or the Kubernetes API, for example platform teams running cluster tooling.
metadata:
name: kornercode-cluster-access
namespace: kornercode
labels:
code.kornercloud.dev/managed: "true"
code.kornercloud.dev/network-policy-id: cluster-access
spec:
podSelector:
matchLabels:
netpol.code.kornercloud.dev/cluster-access: "true"
policyTypes: [Ingress, Egress]
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kornercode
podSelector:
matchLabels:
app.kubernetes.io/name: kornercode-gateway
egress:
- ports:
- protocol: TCP
port: 53
- protocol: UDP
port: 53
- to:
- ipBlock:
cidr: 0.0.0.0/0Ingress is the same as for internet-only. Egress allows every IPv4 destination, including cluster Services, other pods and the internet.
Policy activation
The set of policies active on a workspace pod is derived from the Team’s SecurityProfile by helpers.NetpolLabels (operator/internal/controllers/v1alpha1/workspace/helpers/helpers.go):
func NetpolLabels(profile *WorkspaceProfile, secProfile *SecurityProfile) map[string]string {
labels := map[string]string{}
if NetworkPolicyDisabled(profile, secProfile) || secProfile == nil || secProfile.Spec.NetworkPolicy == nil {
return labels
}
for _, ref := range secProfile.Spec.NetworkPolicy.PolicyRefs {
labels[codev1alpha1.NetpolPodLabelPrefix+ref] = "true" // "netpol.code.kornercloud.dev/<ref>"
}
return labels
}The labels are applied in two places:
ReconcilePodsets them when it creates the pod, so a new or replacement pod is selected by its policies from the start.ReconcileNetworkPolicy(operator/internal/controllers/v1alpha1/workspace/apply/reconcilenetworkpolicy.go) keeps them in sync on the running pod: it adds missing activation labels and removesnetpol.code.kornercloud.dev/labels that are no longer wanted. Changes to a SecurityProfile, WorkspaceProfile or Team trigger this step through the Workspace controller’s watches, so the labels of running pods follow the configuration without a restart.
Adding an activation label makes the matching NetworkPolicy, already present in the namespace, select the pod immediately.
A policyRefs entry must be a valid Kubernetes label value; the SecurityProfile webhook rejects anything else. The webhook does not check that a policy with that identifier exists. A reference to a missing policy produces a label that selects nothing.
When no policy applies
A workspace pod carries no activation label, and is therefore unrestricted by NetworkPolicy, in any of these cases:
| Condition | Effect |
|---|---|
WorkspaceProfile.spec.disableNetworkPolicy: true | Activation labels removed |
SecurityProfile.spec.networkPolicy.disabled: true | Activation labels removed |
The Team has no securityProfileRef | No labels to apply |
The SecurityProfile has no networkPolicy block, or an empty policyRefs list | No labels to apply |
Either disable flag is sufficient on its own; neither takes precedence over the other.
Visibility
ReconcileNetworkPolicy records the state of the two disable flags in the Workspace condition NetworkPolicyDisabled:
| Status | Reason | Meaning |
|---|---|---|
True | Disabled | disableNetworkPolicy or networkPolicy.disabled is set |
False | Enforced | Neither flag is set |
The condition reflects only the disable flags. A Workspace whose Team has no SecurityProfile, or whose SecurityProfile lists no policyRefs, reports Enforced although no policy selects its pod. To audit the effective state, check the pod labels:
kubectl get pods -n <team-namespace> -l code.kornercloud.dev/managed-by=kornercode-operator --show-labelsSee Audit and observability for alerting on the condition.
Team namespace copies
NetworkPolicies are namespace-scoped, so the policies in the operator namespace must exist in every team namespace that runs workspace pods. The Team controller’s ReconcileNetworkPolicies step (operator/internal/controllers/v1alpha1/team/apply/reconcilenetworkpolicies.go) does this:
- Lists the NetworkPolicies in the operator namespace labeled
code.kornercloud.dev/managed=true. - Creates a copy with the same name and spec in the team namespace, or updates an existing copy whose spec or labels have drifted. Copies carry only the two kornercode labels; other labels on the source are not propagated.
- Sets a controller reference from each copy to the cluster-scoped Team, so Kubernetes garbage-collects the copies when the Team is deleted.
- Deletes managed copies in the team namespace whose
network-policy-idno longer exists in the operator namespace. - Does nothing when
team.spec.namespaceequals the operator namespace, because the source policies already apply there.
The Team controller watches managed NetworkPolicies in the operator namespace and re-reconciles every Team when one changes, so cluster administrators maintain the policies in one place.
Namespace isolation
Each Team names a target namespace in spec.namespace. The Team controller’s ReconcileNamespace step creates the namespace if it does not exist and merges spec.namespaceLabels and spec.namespaceAnnotations onto it. It never removes keys from the namespace, including keys it set earlier and that were later removed from the Team.
The Team admission webhook and the gateway’s admin API reject a Team whose namespace or namespace labels would affect cluster infrastructure (operator/api/v1alpha1/validation/namespace.go):
| Rule | Values |
|---|---|
Reserved namespaces (validation.DefaultReservedNamespaces) | kube-system, kube-public, kube-node-lease, default, cert-manager, local-path-storage |
Reserved label prefixes (validation.ReservedNamespaceLabelPrefixes) | pod-security.kubernetes.io/, kubernetes.io/, k8s.io/, node-role.kubernetes.io/ |
The label rule prevents a Team from, for example, relaxing Pod Security Admission on its namespace. The operator namespace itself is not reserved; the local development samples place every Team in kornercode.
Workspace pods run in the team namespace. With dedicated team namespaces:
- the operator and the gateway stay in their own namespace, and a workspace can reach them only if an active policy allows it (
internet-onlydoes not); - the default
workspace-runnerServiceAccount has no role bindings in the team namespace and no mounted token (see Pod security); - PVCs, NetworkPolicy copies and the Secrets and ConfigMaps a Team mounts live in the team namespace, which is where Kubernetes requires them to be for the pods that use them.
Gateway policy for the client-certificate gate
With the client-certificate gate on (clientCertificates.enabled), the chart
also renders the NetworkPolicy kornercode-gateway-client-cert for the gateway
pod. It keeps the gateway’s other ports (8080, 8443 and the metrics port) open
as before, and admits connections to the client-certificate listener (port
8444), the only one that serves the dashboard, dev and test hosts with the gate
on, only from the ingress controller’s namespace
(clientCertificates.networkPolicy.ingressNamespace). The policy has no egress
rules, so the gateway can still fetch certificate revocation lists.
It is an extra layer: the listener already refuses any TLS client that does not
present the ingress’s certificate from kornercode’s internal CA, and with the gate
on ports 8080 and 8443 answer only /healthz. Turn it off with
clientCertificates.networkPolicy.enabled: false only when the cluster’s
network plugin cannot enforce it reliably.
Adding a predefined policy
Create a NetworkPolicy in the operator namespace with the managed labels:
metadata: namespace: kornercode # the operator namespace labels: code.kornercloud.dev/managed: "true" code.kornercloud.dev/network-policy-id: my-policy spec: podSelector: matchLabels: netpol.code.kornercloud.dev/my-policy: "true" # ingress and egress rulesReference it in a SecurityProfile:
spec: networkPolicy: policyRefs: [my-policy]Assign the SecurityProfile to a Team with
spec.securityProfileRef.
The Team controller copies the policy into the team namespaces as soon as it observes it. Updating the SecurityProfile triggers the Workspace controller, which adds the activation label to the running pods of that Team’s Workspaces.
Admins can list the managed policies through the gateway (GET /api/v1/admin/network-policies) and edit SecurityProfiles from the admin panel.