apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: default-deny-ingress-egress namespace: cosmic-clash spec: podSelector: {} policyTypes: [Ingress, Egress] --- apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: control-plane-allowed-flows namespace: cosmic-clash spec: podSelector: matchLabels: app.kubernetes.io/name: control-plane policyTypes: [Ingress, Egress] ingress: - from: - namespaceSelector: {} podSelector: matchLabels: app.kubernetes.io/name: edge-gateway ports: - protocol: TCP port: 8080 # Allocated game servers are control-plane clients too: roster fetch, # registration, connection receipts, shutdown acknowledgement and result # submission all target this port. Their egress was already permitted, but # without a matching ingress rule every one of those calls was dropped, so # no allocated match could complete even inside the cluster. - from: - podSelector: matchLabels: app.kubernetes.io/name: game-server ports: - protocol: TCP port: 8080 egress: - to: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: data podSelector: matchLabels: app.kubernetes.io/name: postgres ports: - protocol: TCP port: 5432 - to: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: data podSelector: matchLabels: app.kubernetes.io/name: redis ports: - protocol: TCP port: 6379 - ports: - protocol: UDP port: 53 - protocol: TCP port: 53 to: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: kube-system podSelector: matchLabels: k8s-app: kube-dns --- apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: game-server-allowed-egress namespace: cosmic-clash spec: podSelector: matchLabels: app.kubernetes.io/name: game-server policyTypes: [Egress] egress: - to: - podSelector: matchLabels: app.kubernetes.io/name: control-plane ports: - protocol: TCP port: 8080 - ports: - protocol: UDP port: 53 - protocol: TCP port: 53 to: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: kube-system podSelector: matchLabels: k8s-app: kube-dns --- apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: allocator-allowed-flows namespace: cosmic-clash spec: podSelector: matchLabels: app.kubernetes.io/name: allocator policyTypes: [Ingress, Egress] ingress: - from: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: monitoring podSelector: matchLabels: app.kubernetes.io/name: prometheus ports: - protocol: TCP port: 9091 egress: - to: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: data podSelector: matchLabels: app.kubernetes.io/name: postgres ports: - protocol: TCP port: 5432 # The kubernetes.default Service endpoint is implementation-specific and # may be a control-plane/node IP that cannot be selected by pod labels. # Keep API egress portable while limiting it to TLS only. - ports: - protocol: TCP port: 443 - ports: - protocol: UDP port: 53 - protocol: TCP port: 53 to: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: kube-system podSelector: matchLabels: k8s-app: kube-dns --- apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: maintenance-allowed-egress namespace: cosmic-clash spec: podSelector: matchLabels: app.kubernetes.io/name: maintenance policyTypes: [Egress] egress: - to: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: data podSelector: matchLabels: app.kubernetes.io/name: postgres ports: - protocol: TCP port: 5432 - ports: - protocol: UDP port: 53 - protocol: TCP port: 53 to: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: kube-system podSelector: matchLabels: k8s-app: kube-dns --- # Public players connect straight to the allocated GameServer's UDP port; the # control plane only ever hands out its address. The namespace-wide default # deny blocked that ingress entirely, so an allocated server was unreachable # from the internet and no matchmade game could be joined. # # The source cannot be narrowed by selector: these peers are player machines # outside the cluster. It is narrowed instead to exactly one protocol and port # on exactly the game-server pods, and the game server admits a peer only with # a valid signed join authorisation for its own match. apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: game-server-allowed-ingress namespace: cosmic-clash spec: podSelector: matchLabels: app.kubernetes.io/name: game-server policyTypes: - Ingress ingress: - ports: - protocol: UDP port: 7777 --- # The matcher reads queued candidates and writes proposals. It exposes nothing # and talks to nobody but its two datastores. apiVersion: networking.k8s.io/v1 kind: NetworkPolicy metadata: name: matcher-allowed-egress namespace: cosmic-clash spec: podSelector: matchLabels: app.kubernetes.io/name: matcher policyTypes: - Egress egress: - to: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: data podSelector: matchLabels: app.kubernetes.io/name: postgres ports: - protocol: TCP port: 5432 - to: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: data podSelector: matchLabels: app.kubernetes.io/name: redis ports: - protocol: TCP port: 6379 - to: - namespaceSelector: matchLabels: kubernetes.io/metadata.name: kube-system podSelector: matchLabels: k8s-app: kube-dns ports: - protocol: UDP port: 53 - protocol: TCP port: 53