From a48b9a50c325549c8a280cd6e96be3fc53b49607 Mon Sep 17 00:00:00 2001
From: Jimmy Zelinskie <jimmy.zelinskie+git@gmail.com>
Date: Mon, 28 Nov 2016 12:58:27 -0500
Subject: [PATCH 1/4] initial helm package

---
 contrib/helm/chihaya/.helmignore              | 21 ++++++++++
 contrib/helm/chihaya/Chart.yaml               | 10 +++++
 contrib/helm/chihaya/templates/NOTES.txt      |  6 +++
 contrib/helm/chihaya/templates/_helpers.tpl   | 16 ++++++++
 contrib/helm/chihaya/templates/configmap.yaml |  9 ++++
 .../helm/chihaya/templates/deployment.yaml    | 41 +++++++++++++++++++
 contrib/helm/chihaya/templates/service.yaml   | 27 ++++++++++++
 contrib/helm/chihaya/values.yaml              | 33 +++++++++++++++
 8 files changed, 163 insertions(+)
 create mode 100644 contrib/helm/chihaya/.helmignore
 create mode 100755 contrib/helm/chihaya/Chart.yaml
 create mode 100644 contrib/helm/chihaya/templates/NOTES.txt
 create mode 100644 contrib/helm/chihaya/templates/_helpers.tpl
 create mode 100644 contrib/helm/chihaya/templates/configmap.yaml
 create mode 100644 contrib/helm/chihaya/templates/deployment.yaml
 create mode 100644 contrib/helm/chihaya/templates/service.yaml
 create mode 100644 contrib/helm/chihaya/values.yaml

diff --git a/contrib/helm/chihaya/.helmignore b/contrib/helm/chihaya/.helmignore
new file mode 100644
index 0000000..f0c1319
--- /dev/null
+++ b/contrib/helm/chihaya/.helmignore
@@ -0,0 +1,21 @@
+# Patterns to ignore when building packages.
+# This supports shell glob matching, relative path matching, and
+# negation (prefixed with !). Only one pattern per line.
+.DS_Store
+# Common VCS dirs
+.git/
+.gitignore
+.bzr/
+.bzrignore
+.hg/
+.hgignore
+.svn/
+# Common backup files
+*.swp
+*.bak
+*.tmp
+*~
+# Various IDEs
+.project
+.idea/
+*.tmproj
diff --git a/contrib/helm/chihaya/Chart.yaml b/contrib/helm/chihaya/Chart.yaml
new file mode 100755
index 0000000..1754677
--- /dev/null
+++ b/contrib/helm/chihaya/Chart.yaml
@@ -0,0 +1,10 @@
+apiVersion: v1
+name: chihaya
+home: https://chihaya.io
+version: 0.1.0
+description: A Helm chart for running the Chihaya BitTorrent tracker on Kubernetes.
+sources:
+  - https://github.com/chihaya/chihaya
+maintainers:
+  - name: Jimmy Zelinskie
+    email: jimmyzelinskie@gmail.com
diff --git a/contrib/helm/chihaya/templates/NOTES.txt b/contrib/helm/chihaya/templates/NOTES.txt
new file mode 100644
index 0000000..f894f25
--- /dev/null
+++ b/contrib/helm/chihaya/templates/NOTES.txt
@@ -0,0 +1,6 @@
+You can port forward a local port to Prometheus or the HTTP tracker by running:
+  export POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app={{ template "fullname" . }}" -o jsonpath="{.items[0].metadata.name}")
+  # Prometheus port
+  kubectl port-forward $POD_NAME 8080:{{ $v := .Values.config.chihaya.prometheus_addr | split ":" }}{{ $v._1 }}
+  # HTTP tracker port
+  kubectl port-forward $POD_NAME 8080:{{ $v := .Values.config.chihaya.http.addr | split ":" }}{{ $v._1 }}
diff --git a/contrib/helm/chihaya/templates/_helpers.tpl b/contrib/helm/chihaya/templates/_helpers.tpl
new file mode 100644
index 0000000..234480d
--- /dev/null
+++ b/contrib/helm/chihaya/templates/_helpers.tpl
@@ -0,0 +1,16 @@
+{{/* vim: set filetype=mustache: */}}
+{{/*
+Expand the name of the chart.
+*/}}
+{{- define "name" -}}
+{{- default .Chart.Name .Values.nameOverride | trunc 24 -}}
+{{- end -}}
+
+{{/*
+Create a default fully qualified app name.
+We truncate at 24 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
+*/}}
+{{- define "fullname" -}}
+{{- $name := default .Chart.Name .Values.nameOverride -}}
+{{- printf "%s-%s" .Release.Name $name | trunc 24 -}}
+{{- end -}}
diff --git a/contrib/helm/chihaya/templates/configmap.yaml b/contrib/helm/chihaya/templates/configmap.yaml
new file mode 100644
index 0000000..8b3ca87
--- /dev/null
+++ b/contrib/helm/chihaya/templates/configmap.yaml
@@ -0,0 +1,9 @@
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: {{ template "fullname" . }}
+  labels:
+    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
+data:
+  config.yaml: |
+{{ toYaml .Values.config | indent 4 }}
diff --git a/contrib/helm/chihaya/templates/deployment.yaml b/contrib/helm/chihaya/templates/deployment.yaml
new file mode 100644
index 0000000..b66d6d4
--- /dev/null
+++ b/contrib/helm/chihaya/templates/deployment.yaml
@@ -0,0 +1,41 @@
+apiVersion: extensions/v1beta1
+kind: Deployment
+metadata:
+  name: {{ template "fullname" . }}
+  labels:
+    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
+spec:
+  replicas: {{ .Values.replicaCount }}
+  template:
+    metadata:
+      labels:
+        app: {{ template "fullname" . }}
+    spec:
+      volumes:
+      - name: config
+        configMap:
+          name: {{ template "fullname" . }}
+      containers:
+      - name: {{ .Chart.Name }}
+        image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
+        imagePullPolicy: {{ .Values.image.pullPolicy }}
+        args:
+        - "--config=/etc/chihaya/config.yaml"
+        ports:
+        - name: bittorrent-http
+          containerPort: {{ $v := .Values.config.chihaya.http.addr | split ":" }}{{ $v._1 }}
+          protocol: TCP
+        - name: bittorrent-udp
+          containerPort: {{ $v := .Values.config.chihaya.udp.addr | split ":" }}{{ $v._1 }}
+          protocol: UDP
+        - name: metrics
+          containerPort: {{ $v := .Values.config.chihaya.prometheus_addr | split ":" }}{{ $v._1 }}
+        livenessProbe:
+          httpGet:
+            path: /
+            port: {{ $v := .Values.config.chihaya.prometheus_addr | split ":" }}{{ $v._1 }}
+        volumeMounts:
+        - name: config
+          mountPath: /etc/chihaya
+        resources:
+{{ toYaml .Values.resources | indent 10 }}
diff --git a/contrib/helm/chihaya/templates/service.yaml b/contrib/helm/chihaya/templates/service.yaml
new file mode 100644
index 0000000..c798c39
--- /dev/null
+++ b/contrib/helm/chihaya/templates/service.yaml
@@ -0,0 +1,27 @@
+apiVersion: v1
+kind: Service
+metadata:
+  name: {{ template "fullname" . }}
+  labels:
+    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
+  annotations:
+    prometheus.io/scrape: "true"
+    prometheus.io/path: "/"
+    prometheus.io/port: {{ $v := .Values.config.chihaya.prometheus_addr | split ":" }}{{ $v._1 | quote }}
+spec:
+  type: {{ .Values.service.type }}
+  ports:
+  - name: bittorrent-http
+    port: {{ $v := .Values.config.chihaya.http.addr | split ":" }}{{ $v._1 }}
+    targetPort: {{ $v := .Values.config.chihaya.http.addr | split ":" }}{{ $v._1 }}
+    protocol: TCP
+  - name: bittorrent-udp
+    port: {{ $v := .Values.config.chihaya.udp.addr | split ":" }}{{ $v._1 }}
+    targetPort: {{ $v := .Values.config.chihaya.udp.addr | split ":" }}{{ $v._1 }}
+    protocol: UDP
+  - name: metrics
+    port: {{ $v := .Values.config.chihaya.prometheus_addr | split ":" }}{{ $v._1 }}
+    targetPort: {{ $v := .Values.config.chihaya.prometheus_addr | split ":" }}{{ $v._1 }}
+    protocol: TCP
+  selector:
+    app: {{ template "fullname" . }}
diff --git a/contrib/helm/chihaya/values.yaml b/contrib/helm/chihaya/values.yaml
new file mode 100644
index 0000000..d82a940
--- /dev/null
+++ b/contrib/helm/chihaya/values.yaml
@@ -0,0 +1,33 @@
+replicaCount: 1
+image:
+  repository: quay.io/jzelinskie/chihaya
+  tag: v2.0.0-rc.1
+  pullPolicy: IfNotPresent
+service:
+  name: chihaya
+  type: ClusterIP
+resources:
+  limits:
+    cpu: 100m
+    memory: 1Gi
+config:
+  chihaya:
+    announce_interval: 15m
+    prometheus_addr: 0.0.0.0:6880
+    http:
+      addr: 0.0.0.0:6881
+      allow_ip_spoofing: true
+      real_ip_header: X-Forwarded-For
+      read_timeout: 5s
+      write_timeout: 5s
+      request_timeout: 5s
+    udp:
+      addr: 0.0.0.0:6881
+      allow_ip_spoofing: true
+      max_clock_skew: 10s
+      private_key: "/K8AybyuKkob03fZJb+3Fu87eVcBNWsvVyDDxGd3wfvVGix10j6iGDEyS/ELPvQIIZvUPl2PnyfnpG6g1YJ/sw=="
+    storage:
+      gc_interval: 14m
+      peer_lifetime: 15m
+      shards: 16
+      max_numwant: 50

From 6deebdd6d44ae42cf9fcc543f2c6f941d9be4f4c Mon Sep 17 00:00:00 2001
From: Jimmy Zelinskie <jimmy.zelinskie+git@gmail.com>
Date: Mon, 28 Nov 2016 19:49:53 -0500
Subject: [PATCH 2/4] frontend/udp: generate private key when empty

---
 contrib/helm/chihaya/values.yaml |  1 -
 frontend/udp/frontend.go         | 15 +++++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/contrib/helm/chihaya/values.yaml b/contrib/helm/chihaya/values.yaml
index d82a940..4322ba5 100644
--- a/contrib/helm/chihaya/values.yaml
+++ b/contrib/helm/chihaya/values.yaml
@@ -25,7 +25,6 @@ config:
       addr: 0.0.0.0:6881
       allow_ip_spoofing: true
       max_clock_skew: 10s
-      private_key: "/K8AybyuKkob03fZJb+3Fu87eVcBNWsvVyDDxGd3wfvVGix10j6iGDEyS/ELPvQIIZvUPl2PnyfnpG6g1YJ/sw=="
     storage:
       gc_interval: 14m
       peer_lifetime: 15m
diff --git a/frontend/udp/frontend.go b/frontend/udp/frontend.go
index 2f5047d..910b3a9 100644
--- a/frontend/udp/frontend.go
+++ b/frontend/udp/frontend.go
@@ -6,6 +6,7 @@ import (
 	"bytes"
 	"context"
 	"encoding/binary"
+	"math/rand"
 	"net"
 	"sync"
 	"time"
@@ -18,6 +19,8 @@ import (
 	"github.com/chihaya/chihaya/frontend/udp/bytepool"
 )
 
+var allowedGeneratedPrivateKeyRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
+
 func init() {
 	prometheus.MustRegister(promResponseDurationMilliseconds)
 }
@@ -68,6 +71,18 @@ type Frontend struct {
 
 // NewFrontend allocates a new instance of a Frontend.
 func NewFrontend(logic frontend.TrackerLogic, cfg Config) *Frontend {
+	// Generate a private key if one isn't provided by the user.
+	if cfg.PrivateKey == "" {
+		rand.Seed(time.Now().UnixNano())
+		pkeyRunes := make([]rune, 64)
+		for i := range pkeyRunes {
+			pkeyRunes[i] = allowedGeneratedPrivateKeyRunes[rand.Intn(len(allowedGeneratedPrivateKeyRunes))]
+		}
+		cfg.PrivateKey = string(pkeyRunes)
+
+		log.Warn("UDP private key was not provided, using generated key: ", cfg.PrivateKey)
+	}
+
 	return &Frontend{
 		closing: make(chan struct{}),
 		logic:   logic,

From f40cd33b12ee866b2034eee80a81ff2b9446c5d7 Mon Sep 17 00:00:00 2001
From: Jimmy Zelinskie <jimmy.zelinskie+git@gmail.com>
Date: Tue, 31 Jan 2017 19:59:24 -0500
Subject: [PATCH 3/4] *: bump default shards to 1024

---
 example_config.yaml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/example_config.yaml b/example_config.yaml
index 2fafc60..9fef721 100644
--- a/example_config.yaml
+++ b/example_config.yaml
@@ -62,7 +62,7 @@ chihaya:
 
     # The number of partitions data will be divided into in order to provide a
     # higher degree of parallelism.
-    shards: 1
+    shards: 1024
 
   # This block defines configuration used for middleware executed before a
   # response has been returned to a BitTorrent client.

From 7aebb44852fa385c83ed1d55d3419400ae4b8701 Mon Sep 17 00:00:00 2001
From: Jimmy Zelinskie <jimmy.zelinskie+git@gmail.com>
Date: Tue, 31 Jan 2017 19:59:48 -0500
Subject: [PATCH 4/4] contrib/helm: update config schema

---
 contrib/helm/chihaya/values.yaml | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/contrib/helm/chihaya/values.yaml b/contrib/helm/chihaya/values.yaml
index 4322ba5..27c45b5 100644
--- a/contrib/helm/chihaya/values.yaml
+++ b/contrib/helm/chihaya/values.yaml
@@ -14,6 +14,8 @@ config:
   chihaya:
     announce_interval: 15m
     prometheus_addr: 0.0.0.0:6880
+    max_numwant: 50
+    default_numwant: 25
     http:
       addr: 0.0.0.0:6881
       allow_ip_spoofing: true
@@ -27,6 +29,5 @@ config:
       max_clock_skew: 10s
     storage:
       gc_interval: 14m
-      peer_lifetime: 15m
-      shards: 16
-      max_numwant: 50
+      peer_lifetime: 16m
+      shards: 1024