|
| 1 | +# Sourcegraph Executor network policies for native Kubernetes |
| 2 | +# |
| 3 | +# Description: |
| 4 | +# |
| 5 | +# - Executors must not be allowed to connect to any Sourcegraph-internal services, |
| 6 | +# except the sourcegraph-frontend service |
| 7 | +# |
| 8 | +# - Many Kubernetes platforms do not block inter-namespace communication by default |
| 9 | +# |
| 10 | +# - Unless your instance requires external pods to be able to connect to |
| 11 | +# Sourcegraph-internal services, ex. external Prometheus scraping pod metrics, |
| 12 | +# then Sourcegraph-internal services should not be reachable outside of |
| 13 | +# the instance's namespace |
| 14 | +# |
| 15 | +# Native Kubernetes NetworkPolicy supports allow rules only, like: |
| 16 | +# |
| 17 | +# - Block all ingress by default |
| 18 | +# - Allow ingress by specified exceptions |
| 19 | +# |
| 20 | +# Usage: |
| 21 | +# |
| 22 | +# - Deploy both network policies in your Sourcegraph instance's namespace |
| 23 | +# |
| 24 | +# - You can still use `frontendUrl: http://sourcegraph-frontend:30080` |
| 25 | +# in your override file for the sourcegraph-executor Helm charts, |
| 26 | +# to avoid paying egress / ingress charges for the Executors' traffic |
| 27 | +# |
| 28 | +# Effect: |
| 29 | +# |
| 30 | +# - Executor pods can reach the frontend HTTP service |
| 31 | +# - Executor pods cannot reach frontend internal/debug ports |
| 32 | +# - Executor pods cannot reach other Sourcegraph pods or services |
| 33 | +# - Executor egress to DNS, kube API, code hosts, package registries, and other |
| 34 | +# external services is unchanged |
| 35 | +# |
| 36 | +# Notes: |
| 37 | +# |
| 38 | +# - Kubernetes NetworkPolicy rules are additive; if another policy already allows |
| 39 | +# executor ingress to backend pods, remove or narrow that broader allow policy |
| 40 | +# |
| 41 | +# - This policy relies on all Sourcegraph + executor pods having at least |
| 42 | +# the default labels from their respective Helm charts |
| 43 | +# |
| 44 | +# - To protect additional pods (ex. src-serve-git), either: |
| 45 | +# - Apply the deploy=sourcegraph label to them |
| 46 | +# - Or add an identifying label for them in the spec.podSelector.matchExpressions |
| 47 | +# of the sourcegraph-internal-pods-block-executors policy |
| 48 | +# |
| 49 | +# - If you need pods from outside of your Sourcegraph instance's namespace |
| 50 | +# to continue to be able to connect to backend pods, then uncomment the |
| 51 | +# `namespaceSelector: {}` line |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +apiVersion: networking.k8s.io/v1 |
| 56 | +kind: NetworkPolicy |
| 57 | +metadata: |
| 58 | + name: sourcegraph-pods-block-executors |
| 59 | +spec: |
| 60 | + podSelector: |
| 61 | + matchExpressions: |
| 62 | + - key: deploy |
| 63 | + operator: In |
| 64 | + values: |
| 65 | + - sourcegraph |
| 66 | + - key: app.kubernetes.io/component |
| 67 | + operator: NotIn |
| 68 | + values: |
| 69 | + - executor |
| 70 | + - key: sourcegraph/job-id |
| 71 | + operator: DoesNotExist |
| 72 | + - key: sourcegraph/run-id |
| 73 | + operator: DoesNotExist |
| 74 | + policyTypes: |
| 75 | + - Ingress |
| 76 | + ingress: |
| 77 | + - from: |
| 78 | + - podSelector: |
| 79 | + matchExpressions: |
| 80 | + - key: app.kubernetes.io/component |
| 81 | + operator: NotIn |
| 82 | + values: |
| 83 | + - executor |
| 84 | + - key: sourcegraph/job-id |
| 85 | + operator: DoesNotExist |
| 86 | + - key: sourcegraph/run-id |
| 87 | + operator: DoesNotExist |
| 88 | + # namespaceSelector: {} |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +apiVersion: networking.k8s.io/v1 |
| 93 | +kind: NetworkPolicy |
| 94 | +metadata: |
| 95 | + name: sourcegraph-frontend-allow-http-from-anywhere |
| 96 | +spec: |
| 97 | + podSelector: |
| 98 | + matchLabels: |
| 99 | + deploy: sourcegraph |
| 100 | + app: sourcegraph-frontend |
| 101 | + policyTypes: |
| 102 | + - Ingress |
| 103 | + ingress: |
| 104 | + - ports: |
| 105 | + - protocol: TCP |
| 106 | + port: http |
0 commit comments