From 418fc00718ce4e3b5faeb3d6ea85ee020ae0ffe3 Mon Sep 17 00:00:00 2001
From: cyril-corbon <corboncyril@gmail.com>
Date: Mon, 21 Feb 2022 11:48:11 +0100
Subject: [PATCH] fix: kube-dns service deletion (#8565)

Signed-off-by: Cyril Corbon <corboncyril@gmail.com>
---
 .../kubernetes-apps/ansible/tasks/cleanup_dns.yml  | 14 ++++++++++++--
 .../ansible/templates/coredns-svc.yml.j2           |  1 +
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/roles/kubernetes-apps/ansible/tasks/cleanup_dns.yml b/roles/kubernetes-apps/ansible/tasks/cleanup_dns.yml
index 382fb6602..7a66678a2 100644
--- a/roles/kubernetes-apps/ansible/tasks/cleanup_dns.yml
+++ b/roles/kubernetes-apps/ansible/tasks/cleanup_dns.yml
@@ -1,7 +1,16 @@
 ---
 - name: Kubernetes Apps | Register coredns deployment annotation `createdby`
   command: "{{ kubectl }} get deploy -n kube-system coredns -o jsonpath='{ .spec.template.metadata.annotations.createdby }'"
-  register: createdby_annotation
+  register: createdby_annotation_deploy
+  changed_when: false
+  ignore_errors: true  # noqa ignore-errors
+  when:
+    - dns_mode in ['coredns', 'coredns_dual']
+    - inventory_hostname == groups['kube_control_plane'][0]
+
+- name: Kubernetes Apps | Register coredns service annotation `createdby`
+  command: "{{ kubectl }} get svc -n kube-system kube-dns -o jsonpath='{ .metadata.annotations.createdby }'"
+  register: createdby_annotation_svc
   changed_when: false
   ignore_errors: true  # noqa ignore-errors
   when:
@@ -18,7 +27,7 @@
   when:
     - dns_mode in ['coredns', 'coredns_dual']
     - inventory_hostname == groups['kube_control_plane'][0]
-    - createdby_annotation.stdout != 'kubespray'
+    - createdby_annotation_deploy.stdout != 'kubespray'
 
 - name: Kubernetes Apps | Delete kubeadm Kube-DNS service
   kube:
@@ -30,3 +39,4 @@
   when:
     - dns_mode in ['coredns', 'coredns_dual']
     - inventory_hostname == groups['kube_control_plane'][0]
+    - createdby_annotation_svc.stdout != 'kubespray'
diff --git a/roles/kubernetes-apps/ansible/templates/coredns-svc.yml.j2 b/roles/kubernetes-apps/ansible/templates/coredns-svc.yml.j2
index 7903e2629..0e051c346 100644
--- a/roles/kubernetes-apps/ansible/templates/coredns-svc.yml.j2
+++ b/roles/kubernetes-apps/ansible/templates/coredns-svc.yml.j2
@@ -11,6 +11,7 @@ metadata:
   annotations:
     prometheus.io/port: "9153"
     prometheus.io/scrape: "true"
+    createdby: 'kubespray'
 spec:
   selector:
     k8s-app: kube-dns{{ coredns_ordinal_suffix }}
-- 
GitLab