From 5b66292c55fadd9d9c0f3beb7c85269c11a8afc6 Mon Sep 17 00:00:00 2001
From: Benjamin Schanzel <benjamin.schanzel@bmw.de>
Date: Mon, 17 Aug 2020 15:45:49 +0200
Subject: [PATCH] Multi Node/Context support for mirroring Git workspaces via
 kubectl

The mirroring of git workspaces to k8s pod nodes is done by piping a git
push command through `kubectl exec`. Without specifying a k8s context
for kubectl, the `current-context` from the kubeconfig is used. This is
problematic in multi-node scenarios where different nodes reside in
different namespaces and thus, are reachable via separate contexts.
Ansible cannot handle the context switch automatically as it would via
the inventory and its kubectl connection plugin because we are running
the git push from the executor/localhost.
This change makes sure the correct context of the corresponding node is
passed to `kubectl exec` explicitly, thus pushing the git repos to the
correct k8s pod.

Change-Id: Ic0b11b5860bb0062b3a1c068f4f963fb76018715
---
 roles/mirror-workspace-git-repos/tasks/main.yaml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/roles/mirror-workspace-git-repos/tasks/main.yaml b/roles/mirror-workspace-git-repos/tasks/main.yaml
index 3235d93eb..c0453a269 100644
--- a/roles/mirror-workspace-git-repos/tasks/main.yaml
+++ b/roles/mirror-workspace-git-repos/tasks/main.yaml
@@ -11,7 +11,7 @@
 - name: Synchronize src repos to workspace directory
   command: |-
     {% if ansible_connection == "kubectl" %}
-      git push {% if mirror_workspace_quiet %}--quiet{% endif %} --mirror "ext::kubectl -n {{ zuul.resources[inventory_hostname].namespace }} exec -i {{ zuul.resources[inventory_hostname].pod }} -- %S {{ ansible_user_dir }}/{{ zj_project.value.src_dir }}"
+      git push {% if mirror_workspace_quiet %}--quiet{% endif %} --mirror "ext::kubectl --context {{ zuul.resources[inventory_hostname].context }} -n {{ zuul.resources[inventory_hostname].namespace }} exec -i {{ zuul.resources[inventory_hostname].pod }} -- %S {{ ansible_user_dir }}/{{ zj_project.value.src_dir }}"
     {% else %}
       git push {% if mirror_workspace_quiet %}--quiet{% endif %} --mirror git+ssh://{{ ansible_user }}@{{ ansible_host | ipwrap }}:{{ ansible_port }}/{{ ansible_user_dir }}/{{ zj_project.value.src_dir }}
     {% endif %}