From e39a1ba75046b96c848e23bfb27e5a3be2545eca Mon Sep 17 00:00:00 2001
From: Steve Wilkerson <wilkers.steve@gmail.com>
Date: Thu, 1 Mar 2018 13:47:11 -0600
Subject: [PATCH] Add prometheus memcached exporter

This adds a prometheus memcached exporter to the memcached chart
and disables it by default

Change-Id: Ia7725cd41b578730b853cbedaa97f28ff9806936
---
 memcached/templates/deployment.yaml           |  1 +
 .../prometheus/bin/_memcached-exporter.sh.tpl | 30 ++++++++
 .../prometheus/exporter-configmap-bin.yaml    | 28 ++++++++
 .../prometheus/exporter-deployment.yaml       | 68 +++++++++++++++++++
 .../prometheus/exporter-service.yaml          | 37 ++++++++++
 memcached/values.yaml                         | 50 +++++++++++++-
 .../armada/multinode/armada-osh.yaml          |  6 ++
 7 files changed, 218 insertions(+), 2 deletions(-)
 create mode 100644 memcached/templates/monitoring/prometheus/bin/_memcached-exporter.sh.tpl
 create mode 100644 memcached/templates/monitoring/prometheus/exporter-configmap-bin.yaml
 create mode 100644 memcached/templates/monitoring/prometheus/exporter-deployment.yaml
 create mode 100644 memcached/templates/monitoring/prometheus/exporter-service.yaml

diff --git a/memcached/templates/deployment.yaml b/memcached/templates/deployment.yaml
index 8845e8d7e9..1629658457 100644
--- a/memcached/templates/deployment.yaml
+++ b/memcached/templates/deployment.yaml
@@ -39,6 +39,7 @@ spec:
 {{ tuple $envAll "memcached" "server" | include "helm-toolkit.snippets.kubernetes_pod_anti_affinity" | indent 8 }}
       nodeSelector:
         {{ .Values.labels.server.node_selector_key }}: {{ .Values.labels.server.node_selector_value }}
+      terminationGracePeriodSeconds: {{ .Values.pod.lifecycle.termination_grace_period.memcached.timeout | default "30" }}
       initContainers:
 {{ tuple $envAll "memcached" list | include "helm-toolkit.snippets.kubernetes_entrypoint_init_container" | indent 9 }}
       containers:
diff --git a/memcached/templates/monitoring/prometheus/bin/_memcached-exporter.sh.tpl b/memcached/templates/monitoring/prometheus/bin/_memcached-exporter.sh.tpl
new file mode 100644
index 0000000000..0ebc94dd83
--- /dev/null
+++ b/memcached/templates/monitoring/prometheus/bin/_memcached-exporter.sh.tpl
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+{{/*
+Copyright 2017 The Openstack-Helm Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/}}
+
+set -ex
+COMMAND="${@:-start}"
+
+function start () {
+  exec /bin/memcached_exporter --memcached.address "$MEMCACHED_HOST"
+}
+
+function stop () {
+  kill -TERM 1
+}
+
+$COMMAND
diff --git a/memcached/templates/monitoring/prometheus/exporter-configmap-bin.yaml b/memcached/templates/monitoring/prometheus/exporter-configmap-bin.yaml
new file mode 100644
index 0000000000..7d58f2ffc2
--- /dev/null
+++ b/memcached/templates/monitoring/prometheus/exporter-configmap-bin.yaml
@@ -0,0 +1,28 @@
+{{/*
+Copyright 2017 The Openstack-Helm Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/}}
+
+{{- if and .Values.manifests.monitoring.prometheus.configmap_bin .Values.monitoring.prometheus.enabled }}
+{{- $envAll := . }}
+
+---
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name:  {{ printf "%s-%s" $envAll.Release.Name "memcached-exporter-bin" | quote }}
+data:
+  memcached-exporter.sh: |
+{{ tuple "bin/_memcached-exporter.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }}
+{{- end }}
diff --git a/memcached/templates/monitoring/prometheus/exporter-deployment.yaml b/memcached/templates/monitoring/prometheus/exporter-deployment.yaml
new file mode 100644
index 0000000000..b16e112e34
--- /dev/null
+++ b/memcached/templates/monitoring/prometheus/exporter-deployment.yaml
@@ -0,0 +1,68 @@
+{{/*
+Copyright 2017 The Openstack-Helm Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/}}
+
+{{- if and .Values.manifests.monitoring.prometheus.deployment_exporter .Values.monitoring.prometheus.enabled }}
+{{- $envAll := . }}
+
+{{- $rcControllerName := printf "%s-%s" $envAll.Release.Name "memcached-exporter"  }}
+{{- $configMapBinName := printf "%s-%s" $envAll.Release.Name "memcached-exporter-bin"  }}
+
+{{ tuple $envAll "prometheus_memcached_exporter" $rcControllerName | include "helm-toolkit.snippets.kubernetes_pod_rbac_serviceaccount" }}
+---
+apiVersion: extensions/v1beta1
+kind: Deployment
+metadata:
+  name: {{ $rcControllerName | quote }}
+spec:
+  replicas: {{ .Values.pod.replicas.prometheus_memcached_exporter }}
+{{ tuple $envAll | include "helm-toolkit.snippets.kubernetes_upgrades_deployment" | indent 2 }}
+  template:
+    metadata:
+      labels:
+{{ tuple $envAll "prometheus_memcached_exporter" "exporter" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 8 }}
+      namespace: {{ .Values.endpoints.prometheus_memcached_exporter.namespace }}
+    spec:
+      serviceAccountName: {{ $rcControllerName | quote }}
+      nodeSelector:
+        {{ .Values.labels.prometheus_memcached_exporter.node_selector_key }}: {{ .Values.labels.prometheus_memcached_exporter.node_selector_value }}
+      terminationGracePeriodSeconds: {{ .Values.pod.lifecycle.termination_grace_period.prometheus_memcached_exporter.timeout | default "30" }}
+      initContainers:
+{{ tuple $envAll "prometheus_memcached_exporter" list | include "helm-toolkit.snippets.kubernetes_entrypoint_init_container" | indent 8 }}
+      containers:
+        - name: memcached-exporter
+          image: {{ .Values.images.tags.prometheus_memcached_exporter }}
+          imagePullPolicy: {{ .Values.images.pull_policy }}
+{{ tuple $envAll $envAll.Values.pod.resources.prometheus_memcached_exporter | include "helm-toolkit.snippets.kubernetes_resources" | indent 10 }}
+          command:
+            - /tmp/memcached-exporter.sh
+            - start
+          ports:
+            - name: metrics
+              containerPort: {{ tuple "prometheus_memcached_exporter" "internal" "metrics" . | include "helm-toolkit.endpoints.endpoint_port_lookup" }}
+          env:
+          - name: MEMCACHED_HOST
+            value: {{ tuple "oslo_cache" "internal" "memcache" . | include "helm-toolkit.endpoints.host_and_port_endpoint_uri_lookup" }}
+          volumeMounts:
+            - name: memcached-exporter-bin
+              mountPath: /tmp/memcached-exporter.sh
+              subPath: memcached-exporter.sh
+              readOnly: true
+      volumes:
+        - name: memcached-exporter-bin
+          configMap:
+            name: {{ $configMapBinName | quote }}
+            defaultMode: 0555
+{{- end }}
diff --git a/memcached/templates/monitoring/prometheus/exporter-service.yaml b/memcached/templates/monitoring/prometheus/exporter-service.yaml
new file mode 100644
index 0000000000..c4687c66fb
--- /dev/null
+++ b/memcached/templates/monitoring/prometheus/exporter-service.yaml
@@ -0,0 +1,37 @@
+{{/*
+Copyright 2017 The Openstack-Helm Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/}}
+
+{{- if and .Values.manifests.monitoring.prometheus.service_exporter .Values.monitoring.prometheus.enabled }}
+{{- $envAll := . }}
+{{- $prometheus_annotations := $envAll.Values.monitoring.prometheus.memcached_exporter }}
+---
+apiVersion: v1
+kind: Service
+metadata:
+  name: {{ tuple "prometheus_memcached_exporter" "internal" . | include "helm-toolkit.endpoints.hostname_short_endpoint_lookup" }}
+  labels:
+{{ tuple $envAll "prometheus_memcached_exporter" "metrics" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 4 }}
+  annotations:
+{{- if .Values.monitoring.prometheus.enabled }}
+{{ tuple $prometheus_annotations | include "helm-toolkit.snippets.prometheus_service_annotations" | indent 4 }}
+{{- end }}
+spec:
+  ports:
+  - name: metrics
+    port: {{ tuple "prometheus_memcached_exporter" "internal" "metrics" . | include "helm-toolkit.endpoints.endpoint_port_lookup" }}
+  selector:
+{{ tuple $envAll "prometheus_memcached_exporter" "exporter" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 4 }}
+{{- end }}
diff --git a/memcached/values.yaml b/memcached/values.yaml
index 6cb281ece2..7604faa167 100644
--- a/memcached/values.yaml
+++ b/memcached/values.yaml
@@ -40,6 +40,10 @@ dependencies:
       services:
         - endpoint: internal
           service: local_image_registry
+    prometheus_memcached_exporter:
+      services:
+        - endpoint: internal
+          service: oslo_cache
 
 endpoints:
   cluster_domain_suffix: cluster.local
@@ -56,6 +60,7 @@ endpoints:
       registry:
         node: 5000
   oslo_cache:
+    namespace: null
     host_fqdn_override:
       default: null
     hosts:
@@ -64,12 +69,32 @@ endpoints:
     port:
       memcache:
         default: 11211
+  prometheus_memcached_exporter:
+    namespace: null
+    hosts:
+      default: memcached-exporter
+    host_fqdn_override:
+      default: null
+    path:
+      default: /metrics
+    scheme:
+      default: 'http'
+    port:
+      metrics:
+        default: 9150
+
+monitoring:
+  prometheus:
+    enabled: false
+    memcached_exporter:
+      scrape: true
 
 images:
   pull_policy: IfNotPresent
   tags:
     dep_check: 'quay.io/stackanetes/kubernetes-entrypoint:v0.3.1'
     memcached: 'docker.io/memcached:1.5.5'
+    prometheus_memcached_exporter: docker.io/prom/memcached-exporter:v0.4.1
     image_repo_sync: docker.io/docker:17.07.0
   local_registry:
     active: false
@@ -81,12 +106,20 @@ labels:
   server:
     node_selector_key: openstack-control-plane
     node_selector_value: enabled
+  prometheus_memcached_exporter:
+    node_selector_key: openstack-control-plane
+    node_selector_value: enabled
 
 manifests:
   configmap_bin: true
   deployment: true
   job_image_repo_sync: true
   service: true
+  monitoring:
+    prometheus:
+      configmap_bin: true
+      deployment_exporter: true
+      service_exporter: true
 
 pod:
   affinity:
@@ -103,14 +136,27 @@ pod:
         rolling_update:
           max_surge: 3
           max_unavailable: 1
+    termination_grace_period:
+      memcached:
+        timeout: 30
+      prometheus_memcached_exporter:
+        timeout: 30
   replicas:
     server: 1
+    prometheus_memcached_exporter: 1
   resources:
     enabled: false
     memcached:
       limits:
-        cpu: 500m
-        memory: 1024Mi
+        cpu: "2000m"
+        memory: "1024Mi"
+      requests:
+        cpu: "500m"
+        memory: "128Mi"
+    prometheus_memcached_exporter:
+      limits:
+        memory: "1024Mi"
+        cpu: "2000m"
       requests:
         cpu: 500m
         memory: 128Mi
diff --git a/tools/deployment/armada/multinode/armada-osh.yaml b/tools/deployment/armada/multinode/armada-osh.yaml
index 5767dc1369..6e5e44c1a3 100644
--- a/tools/deployment/armada/multinode/armada-osh.yaml
+++ b/tools/deployment/armada/multinode/armada-osh.yaml
@@ -164,6 +164,12 @@ data:
       server:
         node_selector_key: openstack-control-plane
         node_selector_value: enabled
+      prometheus_memcached_exporter:
+        node_selector_key: openstack-control-plane
+        node_selector_value: enabled
+    monitoring:
+      prometheus:
+        enabled: true
   source:
     type: local
     location: ${OSH_PATH}