From 11915a30a73d2acde5a2be39781b3ee0581dabb0 Mon Sep 17 00:00:00 2001
From: Vasyl Saienko <vsaienko@mirantis.com>
Date: Wed, 1 Jan 2025 11:04:29 +0000
Subject: [PATCH] [memcached] Unhardcode port in exporter

* Pick up port for exporter from endpoints
* Drop exporter port from service as we should not use
  service that do loadbalancing among pods which are independent

Change-Id: I0408039ba87aca5b8b3c9333644fa0c92f0ca01a
---
 memcached/templates/bin/_memcached-exporter.sh.tpl | 2 +-
 memcached/templates/service.yaml                   | 4 ----
 memcached/templates/statefulset.yaml               | 5 +++++
 3 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/memcached/templates/bin/_memcached-exporter.sh.tpl b/memcached/templates/bin/_memcached-exporter.sh.tpl
index d10e6b723..08b4d919e 100644
--- a/memcached/templates/bin/_memcached-exporter.sh.tpl
+++ b/memcached/templates/bin/_memcached-exporter.sh.tpl
@@ -18,7 +18,7 @@ set -ex
 COMMAND="${@:-start}"
 
 function start () {
-  exec /bin/memcached_exporter
+  exec /bin/memcached_exporter --memcached.address "$MEMCACHED_HOST:$MEMCACHED_PORT"
 }
 
 function stop () {
diff --git a/memcached/templates/service.yaml b/memcached/templates/service.yaml
index 7c5a28ff1..2776fd455 100644
--- a/memcached/templates/service.yaml
+++ b/memcached/templates/service.yaml
@@ -24,10 +24,6 @@ spec:
   ports:
     - name: memcache
       port: {{ tuple "oslo_cache" "internal" "memcache" . | include "helm-toolkit.endpoints.endpoint_port_lookup" }}
-{{- if .Values.monitoring.prometheus.enabled }}
-    - name: metrics
-      port: {{ tuple "oslo_cache" "internal" "metrics" . | include "helm-toolkit.endpoints.endpoint_port_lookup" }}
-{{- end }}
   selector:
 {{ tuple $envAll "memcached" "server" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 4 }}
 {{ .Values.network.memcached | include "helm-toolkit.snippets.service_params" | indent 2 }}
diff --git a/memcached/templates/statefulset.yaml b/memcached/templates/statefulset.yaml
index 65e172706..6d4c4f44d 100644
--- a/memcached/templates/statefulset.yaml
+++ b/memcached/templates/statefulset.yaml
@@ -102,6 +102,11 @@ spec:
               readOnly: true
 {{- if .Values.monitoring.prometheus.enabled }}
         - name: memcached-exporter
+          env:
+          - name: MEMCACHED_HOST
+            value: 127.0.0.1
+          - name: MEMCACHED_PORT
+            value: {{ tuple "oslo_cache" "internal" "memcache" . | include "helm-toolkit.endpoints.endpoint_port_lookup" | quote }}
 {{ tuple $envAll "prometheus_memcached_exporter" | include "helm-toolkit.snippets.image" | indent 10 }}
 {{ tuple $envAll $envAll.Values.pod.resources.prometheus_memcached_exporter | include "helm-toolkit.snippets.kubernetes_resources" | indent 10 }}
 {{ dict "envAll" $envAll "application" "server" "container" "memcached_exporter" | include "helm-toolkit.snippets.kubernetes_container_security_context" | indent 10 }}