Add Promtail sidecar container to Gerrit replica

Change-Id: I8d891ee94a8c81f8da3f85bbb518e89a51e6608c
diff --git a/helm-charts/gerrit-replica/README.md b/helm-charts/gerrit-replica/README.md
index a2998ef..7c4d31a 100644
--- a/helm-charts/gerrit-replica/README.md
+++ b/helm-charts/gerrit-replica/README.md
@@ -168,6 +168,27 @@
 | `istio.tls.key`     | TLS key                                                                   | `-----BEGIN RSA PRIVATE KEY-----` |
 | `istio.ssh.enabled` | Whether to enable SSH                                                     | `false`                           |
 
+### Promtail Sidecar
+
+To collect Gerrit logs, a Promtail sidecar can be deployed into the Gerrit replica
+pods. This can for example be used together with the [gerrit-monitoring](https://gerrit-review.googlesource.com/admin/repos/gerrit-monitoring)
+project.
+
+| Parameter                        | Description                                                         | Default                       |
+|----------------------------------|---------------------------------------------------------------------|-------------------------------|
+| `promtailSidecar.enabled`        | Whether to install the Promatil sidecar container                   | `false`                       |
+| `promtailSidecar.image`          | The promtail container image to use                                 | `grafana/promtail`            |
+| `promtailSidecar.version`        | The promtail container image version                                | `1.3.0`                       |
+| `promtailSidecar.resources`      | Configure the amount of resources the container requests/is allowed | `requests.cpu: 100m`          |
+|                                  |                                                                     | `requests.memory: 128Mi`      |
+|                                  |                                                                     | `limits.cpu: 200m`            |
+|                                  |                                                                     | `limits.memory: 128Mi`        |
+| `promtailSidecar.tls.skipverify` | Whether to skip TLS verification                                    | `true`                        |
+| `promtailSidecar.tls.caCert`     | CA certificate for TLS verification                                 | `-----BEGIN CERTIFICATE-----` |
+| `promtailSidecar.loki.url`       | URL to reach Loki                                                   | `loki.example.com`            |
+| `promtailSidecar.loki.user`      | Loki user                                                           | `admin`                       |
+| `promtailSidecar.loki.password`  | Loki password                                                       | `secret`                      |
+
 
 ### Apache-Git-HTTP-Backend (Git-Backend)
 
diff --git a/helm-charts/gerrit-replica/templates/gerrit-replica.deployment.yaml b/helm-charts/gerrit-replica/templates/gerrit-replica.deployment.yaml
index de71e78..41afd75 100644
--- a/helm-charts/gerrit-replica/templates/gerrit-replica.deployment.yaml
+++ b/helm-charts/gerrit-replica/templates/gerrit-replica.deployment.yaml
@@ -179,6 +179,34 @@
         {{- end }}
         resources:
 {{ toYaml .Values.gerritReplica.resources | indent 10 }}
+      {{ if .Values.promtailSidecar.enabled -}}
+      - name: promtail
+        image: {{ .Values.promtailSidecar.image }}:v{{ .Values.promtailSidecar.version }}
+        imagePullPolicy: {{ .Values.images.imagePullPolicy }}
+        command:
+        - sh
+        - -ec
+        args:
+        - |-
+          /usr/bin/promtail \
+            -config.file=/etc/promtail/promtail.yaml \
+            -client.url={{ .Values.promtailSidecar.loki.url }}/loki/api/v1/push \
+            -client.external-labels=instance=$HOSTNAME
+        resources:
+{{ toYaml .Values.promtailSidecar.resources | indent 10 }}
+        volumeMounts:
+          - name: promtail-config
+            mountPath: /etc/promtail/promtail.yaml
+            subPath: promtail.yaml
+          - name: promtail-secret
+            mountPath: /etc/promtail/promtail.secret
+            subPath: promtail.secret
+          - name: promtail-secret
+            mountPath: /etc/promtail/promtail.ca.crt
+            subPath: promtail.ca.crt
+          - name: gerrit-site
+            mountPath: "/var/gerrit"
+      {{- end }}
       volumes:
       - name: gerrit-site
         {{ if .Values.gerritReplica.persistence.enabled -}}
@@ -218,3 +246,11 @@
         secret:
           secretName: {{ .Release.Name }}-gerrit-replica-ssh-secret
       {{- end }}
+      {{ if .Values.promtailSidecar.enabled -}}
+      - name: promtail-config
+        configMap:
+          name: {{ .Release.Name }}-promtail-gerrit-configmap
+      - name: promtail-secret
+        secret:
+          secretName: {{ .Release.Name }}-promtail-secret
+      {{- end }}
diff --git a/helm-charts/gerrit-replica/templates/promtail.configmap.yaml b/helm-charts/gerrit-replica/templates/promtail.configmap.yaml
new file mode 100644
index 0000000..b5496fb
--- /dev/null
+++ b/helm-charts/gerrit-replica/templates/promtail.configmap.yaml
@@ -0,0 +1,91 @@
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: {{ .Release.Name }}-promtail-gerrit-configmap
+  labels:
+    app: gerrit-replica
+    chart: {{ template "gerrit-replica.chart" . }}
+    heritage: {{ .Release.Service }}
+    release: {{ .Release.Name }}
+data:
+  promtail.yaml: |-
+    positions:
+      filename: /var/gerrit/etc/promtail-positions.yaml
+
+    clients:
+      - url: {{ .Values.promtailSidecar.loki.url }}/loki/api/v1/push
+        tls_config:
+          insecure_skip_verify: {{ .Values.promtailSidecar.tls.skipVerify }}
+          {{- if not .Values.promtailSidecar.tls.skipVerify }}
+          ca_file: /etc/promtail/promtail.ca.crt
+          {{- end }}
+        basic_auth:
+          username: {{ .Values.promtailSidecar.loki.user }}
+          password_file: /etc/promtail/promtail.secret
+    scrape_configs:
+    - job_name: gerrit_error
+      static_configs:
+      - targets:
+        - localhost
+        labels:
+          job: gerrit_error
+          __path__: /var/gerrit/logs/error_log.json
+      entry_parser: raw
+      pipeline_stages:
+      - json:
+          expressions:
+            timestamp: '"@timestamp"'
+            message:
+      - template:
+          source: timestamp
+          template: {{`'{{ Replace .Value "," "." 1 }}'`}}
+      - template:
+          source: timestamp
+          template: {{`'{{ Replace .Value "Z" " +0000" 1 }}'`}}
+      - template:
+          source: timestamp
+          template: {{`'{{ Replace .Value "T" " " 1 }}'`}}
+      - timestamp:
+          source: timestamp
+          format: "2006-01-02 15:04:05.999 -0700"
+      - regex:
+          source: message
+          expression: "Gerrit Code Review (?P<gerrit_version>.*) ready"
+      - labels:
+          gerrit_version:
+    - job_name: gerrit_httpd
+      static_configs:
+      - targets:
+        - localhost
+        labels:
+          job: gerrit_httpd
+          __path__: /var/gerrit/logs/httpd_log.json
+      entry_parser: raw
+      pipeline_stages:
+      - json:
+          expressions:
+            timestamp: null
+      - template:
+          source: timestamp
+          template: {{`'{{ Replace .Value "," "." 1 }}'`}}
+      - timestamp:
+          format: 02/Jan/2006:15:04:05.999 -0700
+          source: timestamp
+    - job_name: gerrit_sshd
+      static_configs:
+      - targets:
+        - localhost
+        labels:
+          job: gerrit_sshd
+          __path__: /var/gerrit/logs/sshd_log.json
+      entry_parser: raw
+      pipeline_stages:
+      - json:
+          expressions:
+            timestamp:
+      - template:
+          source: timestamp
+          template: {{`'{{ Replace .Value "," "." 1 }}'`}}
+      - timestamp:
+          source: timestamp
+          format: 2006-01-02 15:04:05.999 -0700
diff --git a/helm-charts/gerrit-replica/templates/promtail.secret.yaml b/helm-charts/gerrit-replica/templates/promtail.secret.yaml
new file mode 100644
index 0000000..ae05c2a
--- /dev/null
+++ b/helm-charts/gerrit-replica/templates/promtail.secret.yaml
@@ -0,0 +1,17 @@
+{{ if .Values.promtailSidecar.enabled -}}
+apiVersion: v1
+kind: Secret
+metadata:
+  name:  {{ .Release.Name }}-promtail-secret
+  labels:
+    app: gerrit-replica
+    chart: {{ template "gerrit-replica.chart" . }}
+    heritage: {{ .Release.Service }}
+    release: {{ .Release.Name }}
+type: Opaque
+data:
+  promtail.secret: {{ .Values.promtailSidecar.loki.password | b64enc }}
+  {{- if not .Values.promtailSidecar.tls.skipVerify }}
+  promtail.ca.crt: {{ .Values.promtailSidecar.tls.caCert | b64enc }}
+  {{- end }}
+{{- end }}
diff --git a/helm-charts/gerrit-replica/values.yaml b/helm-charts/gerrit-replica/values.yaml
index 3fc398e..e93f6f1 100644
--- a/helm-charts/gerrit-replica/values.yaml
+++ b/helm-charts/gerrit-replica/values.yaml
@@ -69,6 +69,29 @@
     enabled: false
 
 
+promtailSidecar:
+  enabled: false
+  image: grafana/promtail
+  version: 1.3.0
+  resources:
+    requests:
+      cpu: 100m
+      memory: 128Mi
+    limits:
+      cpu: 200m
+      memory: 128Mi
+  tls:
+    skipVerify: true
+    caCert: |-
+      -----BEGIN CERTIFICATE-----
+
+      -----END CERTIFICATE-----
+  loki:
+    url: loki.example.com
+    user: admin
+    password: secret
+
+
 gitBackend:
   image: k8sgerrit/apache-git-http-backend