From a0f41bf82a87f91b6ed5ad8dd989bcd976b8b80b Mon Sep 17 00:00:00 2001
From: Ugur Can Ozturk <57688057+ugur99@users.noreply.github.com>
Date: Wed, 7 Dec 2022 05:22:38 +0300
Subject: [PATCH] =?UTF-8?q?[metrics=5Fserver]:=20Enabled=20HA=20mode=20by?=
 =?UTF-8?q?=20adding=20'metrics=5Fserver=5Freplicas'=E2=80=A6=20(#9539)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* [metrics_server]: Enabled HA mode by adding 'metrics_server_replicas' variable and adding podAntiAffinity rule

Signed-off-by: Ugur Can Ozturk <57688057+ugur99@users.noreply.github.com>

* [metrics_server]: added namespaces selector

Signed-off-by: Ugur Can Ozturk <57688057+ugur99@users.noreply.github.com>

Signed-off-by: Ugur Can Ozturk <57688057+ugur99@users.noreply.github.com>
---
 .../sample/group_vars/k8s_cluster/addons.yml  |  1 +
 .../metrics_server/defaults/main.yml          |  1 +
 .../metrics-server-deployment.yaml.j2         | 19 ++++++++++++-------
 3 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/inventory/sample/group_vars/k8s_cluster/addons.yml b/inventory/sample/group_vars/k8s_cluster/addons.yml
index 73e4dfe12..9abf3a794 100644
--- a/inventory/sample/group_vars/k8s_cluster/addons.yml
+++ b/inventory/sample/group_vars/k8s_cluster/addons.yml
@@ -19,6 +19,7 @@ metrics_server_enabled: false
 # metrics_server_metric_resolution: 15s
 # metrics_server_kubelet_preferred_address_types: "InternalIP,ExternalIP,Hostname"
 # metrics_server_host_network: false
+# metrics_server_replicas: 1
 
 # Rancher Local Path Provisioner
 local_path_provisioner_enabled: false
diff --git a/roles/kubernetes-apps/metrics_server/defaults/main.yml b/roles/kubernetes-apps/metrics_server/defaults/main.yml
index a682b25cb..4e247a166 100644
--- a/roles/kubernetes-apps/metrics_server/defaults/main.yml
+++ b/roles/kubernetes-apps/metrics_server/defaults/main.yml
@@ -8,3 +8,4 @@ metrics_server_limits_memory: 200Mi
 metrics_server_requests_cpu: 100m
 metrics_server_requests_memory: 200Mi
 metrics_server_host_network: false
+metrics_server_replicas: 1
\ No newline at end of file
diff --git a/roles/kubernetes-apps/metrics_server/templates/metrics-server-deployment.yaml.j2 b/roles/kubernetes-apps/metrics_server/templates/metrics-server-deployment.yaml.j2
index d30b443c1..86247b9e1 100644
--- a/roles/kubernetes-apps/metrics_server/templates/metrics-server-deployment.yaml.j2
+++ b/roles/kubernetes-apps/metrics_server/templates/metrics-server-deployment.yaml.j2
@@ -9,6 +9,7 @@ metadata:
     addonmanager.kubernetes.io/mode: Reconcile
     version: {{ metrics_server_version }}
 spec:
+  replicas: {{ metrics_server_replicas }}
   selector:
     matchLabels:
       app.kubernetes.io/name: metrics-server
@@ -91,12 +92,16 @@ spec:
           effect: NoSchedule
 {% endif %}
       affinity:
-        nodeAffinity:
+        podAntiAffinity:
           preferredDuringSchedulingIgnoredDuringExecution:
           - weight: 100
-            preference:
-              matchExpressions:
-              - key: node-role.kubernetes.io/control-plane
-                operator: In
-                values:
-                - ""
+            podAffinityTerm:
+              labelSelector:
+                matchExpressions:
+                - key: app.kubernetes.io/name
+                  operator: In
+                  values:
+                  - metrics-server
+              topologyKey: kubernetes.io/hostname
+              namespaces:
+              - kube-system
\ No newline at end of file
-- 
GitLab