diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fbbc265c..e1d67f5d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -149,3 +149,99 @@ jobs: --set serviceMonitor.interval=30s \ --set 'serviceMonitor.labels.release=prometheus' \ >/dev/null + helm template proxy deploy/charts/proxy \ + --set replicaCount=3 \ + --set config.data.database.driver=postgres \ + --set deploymentStrategy.type=RollingUpdate \ + --set persistence.enabled=false \ + --set persistence.emptyDirSizeLimit=1Gi \ + --set podDisruptionBudget.enabled=true \ + --set serviceAccount.create=true \ + --set-string 'serviceAccount.annotations.eks\.amazonaws\.com/role-arn=arn:aws:iam::123456789012:role/proxy' \ + --set httpRoute.enabled=true \ + --set 'httpRoute.parentRefs[0].name=public' \ + --set 'httpRoute.hostnames[0]=proxy.example.com' \ + --set httpRoute.timeouts.request=600s \ + --set service.type=LoadBalancer \ + --set 'service.loadBalancerSourceRanges[0]=10.0.0.0/8' \ + --set priorityClassName=high \ + --set revisionHistoryLimit=3 \ + --set 'extraContainers[0].name=sidecar' \ + --set 'extraContainers[0].image=busybox:1.37' \ + --set networkPolicy.enabled=true \ + --set 'networkPolicy.from[0].namespaceSelector.matchLabels.kubernetes\.io/metadata\.name=ingress-nginx' \ + --set 'networkPolicy.egress[0].ports[0].protocol=UDP' \ + --set 'networkPolicy.egress[0].ports[0].port=53' \ + --set-string 'config.data.upstream.auth.npm\.pkg\.github\.com.token=${GITHUB_TOKEN}' \ + >build-chart-full.yaml + grep -F 'kind: PodDisruptionBudget' build-chart-full.yaml >/dev/null + grep -F 'kind: HTTPRoute' build-chart-full.yaml >/dev/null + grep -F 'serviceAccountName: "proxy"' build-chart-full.yaml >/dev/null + grep -F 'kind: NetworkPolicy' build-chart-full.yaml >/dev/null + + - name: Check the values the chart derives and refuses + run: | + set -euo pipefail + fails() { + if "$@" >/dev/null 2>&1; then + echo "expected to fail: $*" >&2 + return 1 + fi + } + # The container port follows the listen address. + helm template proxy deploy/charts/proxy --set-string config.data.listen=:9090 | + grep -F 'containerPort: 9090' >/dev/null + # Replicas can't share SQLite, unless something else sets the database. + fails helm template proxy deploy/charts/proxy --set replicaCount=2 + helm template proxy deploy/charts/proxy --set replicaCount=2 \ + --set 'extraEnv[0].name=PROXY_DATABASE_DRIVER' \ + --set 'extraEnv[0].value=postgres' >/dev/null + # A budget for one replica would block node drains. + if helm template proxy deploy/charts/proxy --set podDisruptionBudget.enabled=true | + grep -F 'kind: PodDisruptionBudget' >/dev/null; then + echo "a PodDisruptionBudget was rendered for one replica" >&2 + exit 1 + fi + # Secrets stay out of the ConfigMap unless allowed. + fails helm template proxy deploy/charts/proxy \ + --set 'config.data.upstream.auth.npm.token=plain' + fails helm template proxy deploy/charts/proxy \ + --set-string 'config.data.database.url=postgres://proxy:secret@db/proxy' + fails helm template proxy deploy/charts/proxy \ + --set-string 'config.data.database.url=postgres://proxy@db/proxy?password=secret' + fails helm template proxy deploy/charts/proxy \ + --set-string 'config.data.database.url=host=db user=proxy password=secret' + helm template proxy deploy/charts/proxy \ + --set-string 'config.data.database.url=host=db user=proxy dbname=proxy' >/dev/null + helm template proxy deploy/charts/proxy \ + --set-string 'config.data.database.url=postgres://proxy@db/proxy?sslmode=require' >/dev/null + helm template proxy deploy/charts/proxy \ + --set 'config.data.upstream.auth.npm.token=plain' \ + --set config.allowSecretsInConfigMap=true >/dev/null + # A NetworkPolicy without peers would shut everyone out. + fails helm template proxy deploy/charts/proxy --set networkPolicy.enabled=true + # The schema rejects a misspelled key. + fails helm template proxy deploy/charts/proxy --set persistance.enabled=false + fails helm template proxy deploy/charts/proxy --set httpRoute.enabled=true + fails helm template proxy deploy/charts/proxy \ + --set httpRoute.enabled=true \ + --set 'httpRoute.parentRefs[0].name=public' \ + --set ingress.enabled=true \ + --set 'ingress.hosts[0].host=proxy.example.com' \ + --set 'ingress.hosts[0].paths[0].path=/' \ + --set 'ingress.hosts[0].paths[0].pathType=Prefix' + + - uses: actions/setup-go@d8cbb8045d7f43706b9177cceb36c1f91a6644d5 # v7.1.0 + with: + go-version-file: go.mod + + # HTTPRoute and ServiceMonitor are CRDs without a schema here, so they + # are skipped. + - name: Validate manifests against the Kubernetes schemas + run: | + set -euo pipefail + go install github.com/yannh/kubeconform/cmd/kubeconform@v0.7.0 + kubeconform="$(go env GOPATH)/bin/kubeconform" + helm template proxy deploy/charts/proxy | + "$kubeconform" -strict -summary -ignore-missing-schemas + "$kubeconform" -strict -summary -ignore-missing-schemas build-chart-full.yaml diff --git a/README.md b/README.md index ab38b4da..eae26092 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,8 @@ The default chart deploys one replica backed by a 10 GiB persistent volume, using SQLite and filesystem artifact storage under `/data`. See [`deploy/charts/proxy/values.yaml`](deploy/charts/proxy/values.yaml) for ingress, external database and object-storage configuration options. +The [chart's README](deploy/charts/proxy/README.md) covers running more than one +replica, cloud credentials through a ServiceAccount, and exposing the proxy. Set `deploymentAnnotations` to add annotations to the Deployment itself, separately from `podAnnotations`. diff --git a/deploy/charts/proxy/README.md b/deploy/charts/proxy/README.md new file mode 100644 index 00000000..002089cb --- /dev/null +++ b/deploy/charts/proxy/README.md @@ -0,0 +1,142 @@ +# git-pkgs proxy + +A caching proxy for package registries. This chart runs it as a Deployment +with a Service, and optionally an Ingress or HTTPRoute, a ServiceMonitor, a +ServiceAccount, a PodDisruptionBudget and a NetworkPolicy. + +```bash +helm install proxy oci://ghcr.io/git-pkgs/charts/proxy \ + --set config.data.base_url=https://proxy.example.com +``` + +The defaults give one replica with a 10 GiB volume at `/data`, holding a +SQLite database and the cached artifacts. [`values.yaml`](values.yaml) +describes every value; `values.schema.json` rejects a value the chart doesn't +know, so a misspelled key fails the install instead of being ignored. + +## Configuration + +`config.data` is the proxy's configuration file, passed through as it is; the +[configuration reference](https://github.com/git-pkgs/proxy/blob/main/docs/configuration.md) +lists the keys. Environment variables override it, and are the place for +secrets: + +```yaml +config: + data: + base_url: https://proxy.example.com + database: + driver: postgres + +extraEnv: + - name: PROXY_DATABASE_URL + valueFrom: + secretKeyRef: + name: proxy-database + key: url +``` + +Secrets in `config.data` would be stored in the ConfigMap in plain text, so +the chart refuses to render a `token`, `password`, `header_value` or +`signing_key` that isn't a `${VAR}` reference, or a `database.url` with a +password, whether as `user:password@`, as `?password=` or as a `password=` +keyword (set such a URL through `PROXY_DATABASE_URL` instead). The proxy +expands `${VAR}` in the other keys, so the value can come from a Secret: + +```yaml +config: + data: + upstream: + auth: + "https://npm.pkg.github.com": + type: bearer + token: "${GITHUB_TOKEN}" + +extraEnv: + - name: GITHUB_TOKEN + valueFrom: + secretKeyRef: + name: proxy-upstream + key: github-token +``` + +`config.allowSecretsInConfigMap: true` turns the check off. Other keys that +can hold credentials, such as scanner `headers`, aren't checked. + +Set `config.existingConfigMap` to use a ConfigMap of your own instead. + +The container port follows `config.data.listen`. Set `service.containerPort` +when the listen address comes from an existing ConfigMap or `PROXY_LISTEN`. + +## More than one replica + +Replicas can't share a SQLite file, so they need: + +- `config.data.database.driver: postgres`, with the URL in `PROXY_DATABASE_URL` +- `deploymentStrategy.type: RollingUpdate` +- artifact storage all replicas reach: object storage with + `persistence.enabled: false`, or a `file://` URL on a `ReadWriteMany` volume + +The chart refuses to render more than one replica when the generated +configuration uses SQLite and nothing else can set the database. +`podDisruptionBudget.enabled` adds a budget once there is more than one. + +## Cloud credentials + +The `ecr` upstream auth type and S3 storage take AWS credentials from the +SDK's default chain. To bind them to the pod, give it a ServiceAccount: + +```yaml +serviceAccount: + create: true + annotations: + eks.amazonaws.com/role-arn: arn:aws:iam::123456789012:role/proxy +``` + +## Exposing the proxy + +Use `ingress` or `httpRoute` (Gateway API), not both. Artifacts and hosted +uploads can be large, and most controllers cap the request body and time out +long responses by default; `values.yaml` shows the annotations for +ingress-nginx, and `httpRoute.timeouts` sets the route's. + +## Limiting who reaches the proxy + +`/ui`, `/api/cache`, `/api/mirror` and `/_internal/scan-fetch` have no +authentication of their own. `networkPolicy.enabled` adds a NetworkPolicy that +admits only the peers in `networkPolicy.from` to the proxy's port, and the pod +of `helm test`: + +```yaml +networkPolicy: + enabled: true + from: + - namespaceSelector: + matchLabels: + kubernetes.io/metadata.name: ingress-nginx + - namespaceSelector: + matchLabels: + kubernetes.io/metadata.name: ci +``` + +List the ingress controller or gateway, the clients that pull packages, and +Prometheus if it scrapes `/metrics`. `networkPolicy.egress` restricts outgoing +traffic as well; once set, everything it doesn't list is denied, DNS included. + +The chart's policy doesn't replace other NetworkPolicies that select the proxy +pods: Kubernetes adds their allow rules together, so a policy that admits all +traffic keeps `/ui`, `/api/cache`, `/api/mirror` and `/_internal/scan-fetch` +open to everyone it admits. Look for such policies in the namespace and +restrict them, for ingress and egress. + +A NetworkPolicy sees addresses and ports, not paths, and the cluster's network +plugin has to enforce it. To keep `/ui` or the APIs from clients that may pull +packages, restrict those paths at the ingress or gateway too. + +## Checking an install + +```bash +helm test proxy +``` + +runs a pod that asks the proxy for `/health` through its Service. diff --git a/deploy/charts/proxy/templates/NOTES.txt b/deploy/charts/proxy/templates/NOTES.txt index 3b29f515..82da7beb 100644 --- a/deploy/charts/proxy/templates/NOTES.txt +++ b/deploy/charts/proxy/templates/NOTES.txt @@ -9,7 +9,16 @@ To access the proxy locally: Then visit http://localhost:{{ .Values.service.port }}/. +To check that it answers: + + helm -n {{ .Release.Namespace }} test {{ .Release.Name }} {{- if not .Values.persistence.enabled }} + WARNING: persistence is disabled. Cached artifacts and the default SQLite database will be lost when the pod is replaced. {{- end }} +{{- if and (gt (int .Values.replicaCount) 1) (eq (toString .Values.deploymentStrategy.type) "Recreate") }} + +WARNING: deploymentStrategy is Recreate with {{ .Values.replicaCount }} replicas, so every +upgrade stops all of them first. Set deploymentStrategy.type to RollingUpdate. +{{- end }} diff --git a/deploy/charts/proxy/templates/_helpers.tpl b/deploy/charts/proxy/templates/_helpers.tpl index 9408a9f8..326dce6a 100644 --- a/deploy/charts/proxy/templates/_helpers.tpl +++ b/deploy/charts/proxy/templates/_helpers.tpl @@ -44,3 +44,100 @@ app.kubernetes.io/instance: {{ .Release.Name }} {{- printf "%s:%s" .Values.image.repository (default .Chart.AppVersion .Values.image.tag) -}} {{- end -}} {{- end }} + +{{/* +The name is always rendered, "default" included. Leaving the field out doesn't +take a pod back to the default account: an upgrade that drops +serviceAccountName keeps the old name in the deprecated serviceAccount field. +*/}} +{{- define "proxy.serviceAccountName" -}} +{{- if .Values.serviceAccount.create -}} +{{- default (include "proxy.fullname" .) .Values.serviceAccount.name -}} +{{- else -}} +{{- default "default" .Values.serviceAccount.name -}} +{{- end -}} +{{- end }} + +{{/* +The port the container listens on: service.containerPort when set, else the +port of config.data.listen in the generated configuration, else 8080. +*/}} +{{- define "proxy.containerPort" -}} +{{- $data := .Values.config.data | default dict -}} +{{- $listen := get $data "listen" | default "" | toString -}} +{{- if .Values.service.containerPort -}} +{{- int .Values.service.containerPort -}} +{{- else if and (not .Values.config.existingConfigMap) (regexMatch ":[0-9]+$" $listen) -}} +{{- regexFind "[1-9][0-9]*$" $listen | int -}} +{{- else -}} +8080 +{{- end -}} +{{- end }} + +{{/* +proxy.sqliteForCertain is "true" when the generated configuration uses SQLite +and nothing outside it can set the database. +*/}} +{{- define "proxy.sqliteForCertain" -}} +{{- $data := .Values.config.data | default dict -}} +{{- $database := get $data "database" | default dict -}} +{{- $driver := get $database "driver" | default "sqlite" | toString | lower -}} +{{- $fromEnv := false -}} +{{- range .Values.extraEnv -}} +{{- if hasPrefix "PROXY_DATABASE_" (toString .name) -}} +{{- $fromEnv = true -}} +{{- end -}} +{{- end -}} +{{- if and (eq $driver "sqlite") (not .Values.config.existingConfigMap) (not $fromEnv) (not .Values.extraEnvFrom) -}} +true +{{- end -}} +{{- end }} + +{{/* +proxy.secretPaths lists, space-separated, the paths in config.data whose key +names a secret (token, password, header_value, signing_key) and whose value +is set without a ${VAR} reference. Called with a dict of path and value. +*/}} +{{- define "proxy.secretPaths" -}} +{{- $path := .path -}} +{{- if kindIs "map" .value -}} +{{- range $key, $value := .value -}} +{{- $at := ternary (toString $key) (printf "%s.%s" $path (toString $key)) (eq $path "") -}} +{{- if or (kindIs "map" $value) (kindIs "slice" $value) -}} +{{- include "proxy.secretPaths" (dict "path" $at "value" $value) -}} +{{- else if and (has (lower (toString $key)) (list "token" "password" "header_value" "signing_key")) (not (kindIs "invalid" $value)) (ne (toString $value) "") (not (contains "${" (toString $value))) -}} +{{- printf "%s " $at -}} +{{- end -}} +{{- end -}} +{{- else if kindIs "slice" .value -}} +{{- range $index, $value := .value -}} +{{- include "proxy.secretPaths" (dict "path" (printf "%s[%d]" $path $index) "value" $value) -}} +{{- end -}} +{{- end -}} +{{- end }} + +{{/* Fail on values that can't work, before anything is installed. */}} +{{- define "proxy.validate" -}} +{{- if and (gt (int .Values.replicaCount) 1) (include "proxy.sqliteForCertain" .) -}} +{{- fail "replicaCount > 1 needs Postgres: set config.data.database.driver to postgres and supply PROXY_DATABASE_URL through extraEnv. Replicas can't share a SQLite file." -}} +{{- end -}} +{{- if and .Values.ingress.enabled .Values.httpRoute.enabled -}} +{{- fail "ingress.enabled and httpRoute.enabled are both set; pick one" -}} +{{- end -}} +{{- if and (not .Values.config.existingConfigMap) (not .Values.config.allowSecretsInConfigMap) -}} +{{- $data := .Values.config.data | default dict -}} +{{- $secrets := include "proxy.secretPaths" (dict "path" "" "value" $data) | trim | splitList " " | compact -}} +{{- $database := get $data "database" | default dict -}} +{{- $url := "" -}} +{{- if kindIs "map" $database -}} +{{- $url = get $database "url" | default "" | toString -}} +{{- end -}} +{{- /* A password as userinfo, as a query parameter or as a keyword DSN pair. */ -}} +{{- if or (regexMatch `://[^/@]*:[^/@]+@` $url) (regexMatch `(?i)[?&]password=[^&]` $url) (regexMatch `(?i)(^|\s)password\s*=\s*[^\s]` $url) -}} +{{- $secrets = append $secrets "database.url" -}} +{{- end -}} +{{- if $secrets -}} +{{- fail (printf "config.data would put secrets in the ConfigMap in plain text: %s. Write them as ${VAR} references and set the variables through extraEnv from a Secret (the database URL as PROXY_DATABASE_URL), or set config.allowSecretsInConfigMap=true." (join ", " $secrets)) -}} +{{- end -}} +{{- end -}} +{{- end }} diff --git a/deploy/charts/proxy/templates/deployment.yaml b/deploy/charts/proxy/templates/deployment.yaml index 67285b95..0fad83d0 100644 --- a/deploy/charts/proxy/templates/deployment.yaml +++ b/deploy/charts/proxy/templates/deployment.yaml @@ -1,3 +1,4 @@ +{{- include "proxy.validate" . -}} apiVersion: apps/v1 kind: Deployment metadata: @@ -11,6 +12,9 @@ metadata: {{- include "proxy.labels" . | nindent 4 }} spec: replicas: {{ .Values.replicaCount }} + {{- if not (kindIs "invalid" .Values.revisionHistoryLimit) }} + revisionHistoryLimit: {{ .Values.revisionHistoryLimit }} + {{- end }} strategy: {{- toYaml .Values.deploymentStrategy | nindent 4 }} selector: @@ -29,7 +33,11 @@ spec: {{- toYaml . | nindent 8 }} {{- end }} spec: - automountServiceAccountToken: false + serviceAccountName: {{ include "proxy.serviceAccountName" . | quote }} + automountServiceAccountToken: {{ .Values.serviceAccount.automountToken }} + {{- with .Values.priorityClassName }} + priorityClassName: {{ . | quote }} + {{- end }} terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }} securityContext: {{- toYaml .Values.podSecurityContext | nindent 8 }} @@ -61,7 +69,7 @@ spec: {{- end }} ports: - name: http - containerPort: {{ .Values.service.containerPort }} + containerPort: {{ include "proxy.containerPort" . }} protocol: TCP startupProbe: {{- toYaml .Values.startupProbe | nindent 12 }} @@ -83,6 +91,9 @@ spec: {{- with .Values.extraVolumeMounts }} {{- toYaml . | nindent 12 }} {{- end }} + {{- with .Values.extraContainers }} + {{- toYaml . | nindent 8 }} + {{- end }} volumes: - name: config configMap: @@ -95,7 +106,12 @@ spec: persistentVolumeClaim: claimName: {{ include "proxy.claimName" . }} {{- else }} - emptyDir: {} + emptyDir: + {{- with .Values.persistence.emptyDirSizeLimit }} + sizeLimit: {{ . }} + {{- else }} + {} + {{- end }} {{- end }} - name: tmp emptyDir: diff --git a/deploy/charts/proxy/templates/httproute.yaml b/deploy/charts/proxy/templates/httproute.yaml new file mode 100644 index 00000000..934f9985 --- /dev/null +++ b/deploy/charts/proxy/templates/httproute.yaml @@ -0,0 +1,38 @@ +{{- if .Values.httpRoute.enabled }} +{{- if not .Values.httpRoute.parentRefs }} +{{- fail "httpRoute.parentRefs must be set when httpRoute.enabled=true" }} +{{- end }} +apiVersion: gateway.networking.k8s.io/v1 +kind: HTTPRoute +metadata: + name: {{ include "proxy.fullname" . }} + namespace: {{ .Release.Namespace }} + labels: + {{- include "proxy.labels" . | nindent 4 }} + {{- with .Values.httpRoute.labels }} + {{- toYaml . | nindent 4 }} + {{- end }} + {{- with .Values.httpRoute.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +spec: + parentRefs: + {{- toYaml .Values.httpRoute.parentRefs | nindent 4 }} + {{- with .Values.httpRoute.hostnames }} + hostnames: + {{- toYaml . | nindent 4 }} + {{- end }} + rules: + - backendRefs: + - name: {{ include "proxy.fullname" . }} + port: {{ .Values.service.port }} + {{- with .Values.httpRoute.matches }} + matches: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.httpRoute.timeouts }} + timeouts: + {{- toYaml . | nindent 8 }} + {{- end }} +{{- end }} diff --git a/deploy/charts/proxy/templates/networkpolicy.yaml b/deploy/charts/proxy/templates/networkpolicy.yaml new file mode 100644 index 00000000..49d3f0ca --- /dev/null +++ b/deploy/charts/proxy/templates/networkpolicy.yaml @@ -0,0 +1,36 @@ +{{- if .Values.networkPolicy.enabled }} +{{- if not .Values.networkPolicy.from }} +{{- fail "networkPolicy.from must be set when networkPolicy.enabled=true: with no peer, nothing but helm test reaches the proxy" }} +{{- end }} +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: {{ include "proxy.fullname" . }} + namespace: {{ .Release.Namespace }} + labels: + {{- include "proxy.labels" . | nindent 4 }} +spec: + podSelector: + matchLabels: + {{- include "proxy.selectorLabels" . | nindent 6 }} + policyTypes: + - Ingress + {{- if .Values.networkPolicy.egress }} + - Egress + {{- end }} + ingress: + - from: + {{- toYaml .Values.networkPolicy.from | nindent 8 }} + # The pod of helm test. + - podSelector: + matchLabels: + app.kubernetes.io/instance: {{ .Release.Name }} + app.kubernetes.io/component: test + ports: + - protocol: TCP + port: {{ include "proxy.containerPort" . }} + {{- with .Values.networkPolicy.egress }} + egress: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/deploy/charts/proxy/templates/poddisruptionbudget.yaml b/deploy/charts/proxy/templates/poddisruptionbudget.yaml new file mode 100644 index 00000000..2e4bda93 --- /dev/null +++ b/deploy/charts/proxy/templates/poddisruptionbudget.yaml @@ -0,0 +1,20 @@ +{{- if and .Values.podDisruptionBudget.enabled (gt (int .Values.replicaCount) 1) }} +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: {{ include "proxy.fullname" . }} + namespace: {{ .Release.Namespace }} + labels: + {{- include "proxy.labels" . | nindent 4 }} +spec: + {{- if not (kindIs "invalid" .Values.podDisruptionBudget.minAvailable) }} + minAvailable: {{ .Values.podDisruptionBudget.minAvailable }} + {{- else if not (kindIs "invalid" .Values.podDisruptionBudget.maxUnavailable) }} + maxUnavailable: {{ .Values.podDisruptionBudget.maxUnavailable }} + {{- else }} + maxUnavailable: 1 + {{- end }} + selector: + matchLabels: + {{- include "proxy.selectorLabels" . | nindent 6 }} +{{- end }} diff --git a/deploy/charts/proxy/templates/service.yaml b/deploy/charts/proxy/templates/service.yaml index ee5931e6..cee77d42 100644 --- a/deploy/charts/proxy/templates/service.yaml +++ b/deploy/charts/proxy/templates/service.yaml @@ -5,12 +5,33 @@ metadata: namespace: {{ .Release.Namespace }} labels: {{- include "proxy.labels" . | nindent 4 }} + {{- with .Values.service.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} spec: type: {{ .Values.service.type }} + {{- if eq .Values.service.type "LoadBalancer" }} + {{- with .Values.service.loadBalancerClass }} + loadBalancerClass: {{ . | quote }} + {{- end }} + {{- with .Values.service.loadBalancerSourceRanges }} + loadBalancerSourceRanges: + {{- toYaml . | nindent 4 }} + {{- end }} + {{- end }} + {{- if or (eq .Values.service.type "NodePort") (eq .Values.service.type "LoadBalancer") }} + {{- with .Values.service.externalTrafficPolicy }} + externalTrafficPolicy: {{ . }} + {{- end }} + {{- end }} ports: - name: http port: {{ .Values.service.port }} targetPort: http protocol: TCP + {{- if and .Values.service.nodePort (or (eq .Values.service.type "NodePort") (eq .Values.service.type "LoadBalancer")) }} + nodePort: {{ .Values.service.nodePort }} + {{- end }} selector: {{- include "proxy.selectorLabels" . | nindent 4 }} diff --git a/deploy/charts/proxy/templates/serviceaccount.yaml b/deploy/charts/proxy/templates/serviceaccount.yaml new file mode 100644 index 00000000..077968c7 --- /dev/null +++ b/deploy/charts/proxy/templates/serviceaccount.yaml @@ -0,0 +1,17 @@ +{{- if .Values.serviceAccount.create }} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "proxy.serviceAccountName" . | quote }} + namespace: {{ .Release.Namespace }} + labels: + {{- include "proxy.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.labels }} + {{- toYaml . | nindent 4 }} + {{- end }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +automountServiceAccountToken: {{ .Values.serviceAccount.automountToken }} +{{- end }} diff --git a/deploy/charts/proxy/templates/tests/health.yaml b/deploy/charts/proxy/templates/tests/health.yaml new file mode 100644 index 00000000..a7372edd --- /dev/null +++ b/deploy/charts/proxy/templates/tests/health.yaml @@ -0,0 +1,48 @@ +# helm test: asks the proxy for /health through its Service, with the proxy's +# own image, which has wget. The pod has no app.kubernetes.io/name label, so no +# selector of the chart matches it and the Service doesn't count it as a backend. +apiVersion: v1 +kind: Pod +metadata: + name: {{ printf "%s-test-health" (include "proxy.fullname" . | trunc 51 | trimSuffix "-") }} + namespace: {{ .Release.Namespace }} + labels: + app.kubernetes.io/component: test + app.kubernetes.io/instance: {{ .Release.Name }} + app.kubernetes.io/managed-by: {{ .Release.Service }} + annotations: + helm.sh/hook: test + # Removed once it succeeds, so an uninstall leaves no pod behind. A failed + # one stays for its logs until the next run. + helm.sh/hook-delete-policy: before-hook-creation,hook-succeeded +spec: + restartPolicy: Never + automountServiceAccountToken: false + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 4 }} + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 4 }} + {{- end }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 4 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 4 }} + {{- end }} + containers: + - name: health + image: {{ include "proxy.image" . | quote }} + imagePullPolicy: {{ .Values.image.pullPolicy }} + securityContext: + {{- toYaml .Values.containerSecurityContext | nindent 8 }} + command: + - wget + - -q + - -T + - "15" + - -O + - /dev/null + - http://{{ include "proxy.fullname" . }}:{{ .Values.service.port }}/health diff --git a/deploy/charts/proxy/values.schema.json b/deploy/charts/proxy/values.schema.json new file mode 100644 index 00000000..d0bd399b --- /dev/null +++ b/deploy/charts/proxy/values.schema.json @@ -0,0 +1,510 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "git-pkgs proxy chart values", + "type": "object", + "additionalProperties": false, + "properties": { + "global": { + "type": "object" + }, + "replicaCount": { + "type": "integer", + "minimum": 0 + }, + "revisionHistoryLimit": { + "type": [ + "integer", + "null" + ], + "minimum": 0 + }, + "nameOverride": { + "type": "string" + }, + "fullnameOverride": { + "type": "string" + }, + "image": { + "type": "object", + "properties": { + "repository": { + "type": "string", + "minLength": 1 + }, + "tag": { + "type": "string" + }, + "digest": { + "type": "string" + }, + "pullPolicy": { + "type": "string", + "enum": [ + "Always", + "IfNotPresent", + "Never" + ] + } + }, + "additionalProperties": false + }, + "imagePullSecrets": { + "type": "array", + "items": { + "type": "object" + } + }, + "service": { + "type": "object", + "properties": { + "type": { + "type": "string", + "enum": [ + "ClusterIP", + "NodePort", + "LoadBalancer" + ] + }, + "port": { + "type": "integer", + "minimum": 1, + "maximum": 65535 + }, + "containerPort": { + "type": [ + "integer", + "null" + ], + "minimum": 1, + "maximum": 65535 + }, + "annotations": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "nodePort": { + "type": [ + "integer", + "null" + ] + }, + "loadBalancerClass": { + "type": "string" + }, + "loadBalancerSourceRanges": { + "type": "array", + "items": { + "type": "string" + } + }, + "externalTrafficPolicy": { + "type": "string", + "enum": [ + "", + "Cluster", + "Local" + ] + } + }, + "additionalProperties": false + }, + "serviceAccount": { + "type": "object", + "properties": { + "create": { + "type": "boolean" + }, + "name": { + "type": "string" + }, + "annotations": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "labels": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "automountToken": { + "type": "boolean" + } + }, + "additionalProperties": false + }, + "config": { + "type": "object", + "properties": { + "allowSecretsInConfigMap": { + "type": "boolean" + }, + "existingConfigMap": { + "type": "string" + }, + "existingConfigMapKey": { + "type": "string", + "minLength": 1 + }, + "data": { + "type": [ + "object", + "null" + ] + } + }, + "additionalProperties": false + }, + "extraEnv": { + "type": "array", + "items": { + "type": "object" + } + }, + "extraEnvFrom": { + "type": "array", + "items": { + "type": "object" + } + }, + "extraVolumes": { + "type": "array", + "items": { + "type": "object" + } + }, + "extraVolumeMounts": { + "type": "array", + "items": { + "type": "object" + } + }, + "extraInitContainers": { + "type": "array", + "items": { + "type": "object" + } + }, + "extraContainers": { + "type": "array", + "items": { + "type": "object" + } + }, + "persistence": { + "type": "object", + "properties": { + "enabled": { + "type": "boolean" + }, + "mountPath": { + "type": "string", + "minLength": 1 + }, + "emptyDirSizeLimit": { + "type": "string" + }, + "existingClaim": { + "type": "string" + }, + "annotations": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "accessModes": { + "type": "array", + "items": { + "type": "string" + } + }, + "size": { + "type": "string", + "minLength": 1 + }, + "storageClass": { + "type": "string" + } + }, + "additionalProperties": false + }, + "deploymentStrategy": { + "type": "object" + }, + "podDisruptionBudget": { + "type": "object", + "properties": { + "enabled": { + "type": "boolean" + }, + "minAvailable": { + "type": [ + "integer", + "string", + "null" + ] + }, + "maxUnavailable": { + "type": [ + "integer", + "string", + "null" + ] + } + }, + "additionalProperties": false + }, + "ingress": { + "type": "object", + "properties": { + "enabled": { + "type": "boolean" + }, + "className": { + "type": "string" + }, + "annotations": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "hosts": { + "type": "array", + "items": { + "type": "object", + "properties": { + "host": { + "type": "string" + }, + "paths": { + "type": "array", + "items": { + "type": "object", + "properties": { + "path": { + "type": "string" + }, + "pathType": { + "type": "string", + "enum": [ + "Prefix", + "Exact", + "ImplementationSpecific" + ] + } + }, + "additionalProperties": false, + "required": [ + "path", + "pathType" + ] + } + } + }, + "additionalProperties": false, + "required": [ + "paths" + ] + } + }, + "tls": { + "type": "array", + "items": { + "type": "object" + } + } + }, + "additionalProperties": false + }, + "httpRoute": { + "type": "object", + "properties": { + "enabled": { + "type": "boolean" + }, + "annotations": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "labels": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "parentRefs": { + "type": "array", + "items": { + "type": "object" + } + }, + "hostnames": { + "type": "array", + "items": { + "type": "string" + } + }, + "matches": { + "type": "array", + "items": { + "type": "object" + } + }, + "timeouts": { + "type": "object" + } + }, + "additionalProperties": false + }, + "networkPolicy": { + "type": "object", + "properties": { + "enabled": { + "type": "boolean" + }, + "from": { + "type": "array", + "items": { + "type": "object" + } + }, + "egress": { + "type": "array", + "items": { + "type": "object" + } + } + }, + "additionalProperties": false + }, + "serviceMonitor": { + "type": "object", + "properties": { + "enabled": { + "type": "boolean" + }, + "namespace": { + "type": "string" + }, + "labels": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "annotations": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "path": { + "type": "string" + }, + "interval": { + "type": "string" + }, + "scrapeTimeout": { + "type": "string" + }, + "honorLabels": { + "type": "boolean" + }, + "relabelings": { + "type": "array", + "items": { + "type": "object" + } + }, + "metricRelabelings": { + "type": "array", + "items": { + "type": "object" + } + } + }, + "additionalProperties": false + }, + "deploymentAnnotations": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "podAnnotations": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "podLabels": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "podSecurityContext": { + "type": "object" + }, + "containerSecurityContext": { + "type": "object" + }, + "tmpDir": { + "type": "object", + "properties": { + "sizeLimit": { + "type": "string" + } + }, + "additionalProperties": false + }, + "resources": { + "type": "object" + }, + "startupProbe": { + "type": "object" + }, + "readinessProbe": { + "type": "object" + }, + "livenessProbe": { + "type": "object" + }, + "terminationGracePeriodSeconds": { + "type": "integer", + "minimum": 0 + }, + "priorityClassName": { + "type": "string" + }, + "nodeSelector": { + "type": "object", + "additionalProperties": { + "type": "string" + } + }, + "tolerations": { + "type": "array", + "items": { + "type": "object" + } + }, + "affinity": { + "type": "object" + }, + "topologySpreadConstraints": { + "type": "array", + "items": { + "type": "object" + } + } + } +} diff --git a/deploy/charts/proxy/values.yaml b/deploy/charts/proxy/values.yaml index 9458556a..024c73dd 100644 --- a/deploy/charts/proxy/values.yaml +++ b/deploy/charts/proxy/values.yaml @@ -3,8 +3,15 @@ # persistence disabled) or a file:// URL on a ReadWriteMany volume # (persistence.accessModes: [ReadWriteMany]). With upstream.git, each # replica also needs its own git.cache_dir. +# +# The chart refuses to render more than one replica when the generated +# configuration uses SQLite and nothing else can set the database (an existing +# ConfigMap, a PROXY_DATABASE_* variable in extraEnv, or extraEnvFrom). replicaCount: 1 +# Old ReplicaSets kept for rollback. Empty uses the Kubernetes default. +revisionHistoryLimit: + nameOverride: "" fullnameOverride: "" @@ -21,12 +28,42 @@ imagePullSecrets: [] service: type: ClusterIP port: 8080 - # Keep this aligned with config.data.listen (or the listen address in an - # existing ConfigMap). - containerPort: 8080 + # Empty takes the port from config.data.listen, or 8080 with an existing + # ConfigMap. Set it when the listen address comes from somewhere else: an + # existing ConfigMap with another port, or PROXY_LISTEN. + containerPort: + annotations: {} + # Only for type NodePort or LoadBalancer. + nodePort: + # Only for type LoadBalancer. + loadBalancerClass: "" + loadBalancerSourceRanges: [] + # Only for type NodePort or LoadBalancer. + externalTrafficPolicy: "" + +# A ServiceAccount of the proxy's own, for cloud credentials bound to one: +# IAM roles for service accounts on EKS, or Workload Identity on GKE and AKS. +# The ecr upstream auth type and S3 storage get their credentials this way. +serviceAccount: + create: false + # With create, empty uses the release's full name. Without it, the name of + # an existing ServiceAccount; empty uses the namespace default. + name: "" + annotations: {} + # eks.amazonaws.com/role-arn: arn:aws:iam::123456789012:role/proxy + labels: {} + # The proxy doesn't call the Kubernetes API, so the token stays unmounted. + # The cloud identity webhooks mount their own. + automountToken: false # The generated configuration is ignored when existingConfigMap is set. config: + # The chart refuses to render config.data with a token, password, + # header_value or signing_key set without a ${VAR} reference, or a + # database.url with a password in any form (user:password@, ?password= or a + # password= keyword): a ConfigMap is readable by anyone allowed to read + # ConfigMaps in the namespace. Set this to render them anyway. + allowSecretsInConfigMap: false existingConfigMap: "" existingConfigMapKey: config.yaml data: @@ -90,6 +127,8 @@ persistence: # Keep this aligned with config.data.storage.url and config.data.database.path # (or the equivalent paths in an existing ConfigMap). mountPath: /data + # Size limit of the emptyDir used when persistence is disabled. + emptyDirSizeLimit: "" existingClaim: "" annotations: {} accessModes: @@ -100,6 +139,19 @@ persistence: deploymentStrategy: type: Recreate +# Only rendered with more than one replica: with one, a budget either blocks +# node drains or protects nothing. +podDisruptionBudget: + enabled: false + # Set one of the two. + minAvailable: + maxUnavailable: 1 + +# Artifacts and hosted uploads can be large and slow. Most ingress controllers +# cap the request body and time out long responses by default; raise both, for +# example with ingress-nginx: +# nginx.ingress.kubernetes.io/proxy-body-size: "0" +# nginx.ingress.kubernetes.io/proxy-read-timeout: "600" ingress: enabled: false className: "" @@ -114,6 +166,59 @@ ingress: # hosts: # - proxy.example.com +# Gateway API HTTPRoute, as an alternative to ingress. Requires the +# gateway.networking.k8s.io/v1 CRDs and a Gateway to attach to. +httpRoute: + enabled: false + annotations: {} + labels: {} + parentRefs: [] + # - name: public + # namespace: gateway-system + hostnames: [] + # - proxy.example.com + matches: + - path: + type: PathPrefix + value: / + # Request timeouts of the route's rule. Large downloads can need more than + # a gateway's default. + timeouts: {} + # request: 600s + +# A NetworkPolicy limiting who reaches the proxy, for clusters whose network +# plugin enforces them. /ui, /api/cache, /api/mirror and /_internal/scan-fetch +# have no authentication of their own, so only trusted clients should reach +# the proxy. A NetworkPolicy works on addresses and ports, not paths: to keep +# a path from some clients, restrict it at the ingress or gateway as well. +networkPolicy: + enabled: false + # Peers allowed to reach the proxy's port: the ingress controller or + # gateway, the build pods, and the Prometheus scraping /metrics. Required + # when enabled. The pod of helm test is always allowed. + from: [] + # - namespaceSelector: + # matchLabels: + # kubernetes.io/metadata.name: ingress-nginx + # - podSelector: + # matchLabels: + # app: ci-runner + # Egress rules. Empty leaves egress open; once set, everything else is + # denied, so allow DNS, the upstream registries, the database and object + # storage. + egress: [] + # - ports: + # - protocol: UDP + # port: 53 + # - protocol: TCP + # port: 53 + # - to: + # - ipBlock: + # cidr: 0.0.0.0/0 + # ports: + # - protocol: TCP + # port: 443 + # Prometheus Operator ServiceMonitor for the /metrics endpoint. Requires the # monitoring.coreos.com/v1 CRDs to be installed in the cluster. serviceMonitor: @@ -186,6 +291,12 @@ livenessProbe: terminationGracePeriodSeconds: 30 +priorityClassName: "" + +# Additional containers for the pod, for example a database proxy sidecar. +# They can mount extraVolumes. +extraContainers: [] + nodeSelector: {} tolerations: [] affinity: {}