浏览代码

fix: make kubeconfig dir configurable

Kamesh Sampath 4 年之前
父节点
当前提交
6aed577d34
共有 3 个文件被更改,包括 45 次插入4 次删除
  1. 2 1
      defaults/main.yml
  2. 35 2
      tasks/minikube_create.yml
  3. 8 1
      tasks/minikube_destroy.yml

+ 2 - 1
defaults/main.yml

@@ -17,4 +17,5 @@ minikube_profiles:
      - registry-aliases
      - registry-aliases
      - metallb
      - metallb
 profile_name_query: 'valid[*].Name'
 profile_name_query: 'valid[*].Name'
-force_enable_addon: false
+force_enable_addon: false
+kubeconfig_dir: "{{ playbook_dir }}/.kube"

+ 35 - 2
tasks/minikube_create.yml

@@ -39,7 +39,7 @@
 - name: "Create Kubeconfig dir"
 - name: "Create Kubeconfig dir"
   file:
   file:
     state: directory
     state: directory
-    path: "{{ playbook_dir }}/.kube"
+    path: "{{ kubeconfig_dir }}"
 
 
 - name: "Create minikube Kubernetes Cluster"
 - name: "Create minikube Kubernetes Cluster"
   command:
   command:
@@ -62,7 +62,7 @@
   loop_control:
   loop_control:
     label: "{{ item.key }}"
     label: "{{ item.key }}"
   environment:
   environment:
-    KUBECONFIG: "{{ playbook_dir }}/.kube/{{ item.key }}.kubeconfig"
+    KUBECONFIG: "{{ kubeconfig_dir }}/{{ item.key }}.kubeconfig"
   when: (item.value.create | bool) and not ( item.value.destroy | bool )
   when: (item.value.create | bool) and not ( item.value.destroy | bool )
 
 
 - name: "Fail when cluster not started"
 - name: "Fail when cluster not started"
@@ -73,6 +73,39 @@
   loop_control:
   loop_control:
     label: "{{ item.item.key }}"
     label: "{{ item.item.key }}"
 
 
+- name: "Check if local .kube directory exists"
+  stat:
+    path: "{{ kubeconfig_dir }}"
+  register: kubeconfig_dir_result
+
+- debug:
+    var: kubeconfig_dir_result
+
+- name: "Merge Kubconfig"
+  set_fact: 
+    kubeconfig: "{{ lookup('fileglob', kubeconfig_dir +'/*.kubeconfig') | regex_replace(',',':') }}"
+  when: kubeconfig_dir_result.stat.exists
+
+- name: "Merge and Flatten Kubeconfig"
+  local_action:
+    module: ansible.builtin.command
+    argv:
+      - kubectl 
+      - config
+      - view 
+      - --flatten
+  register: merged_kubeconfig
+  environment:
+      KUBECONFIG: "{{ kubeconfig  }}"
+  when: kubeconfig_dir_result.stat.exists
+
+- name: "Save work Kubeconfig"
+  local_action:
+    module: ansible.builtin.copy
+    dest: "{{ kubeconfig_dir }}/config"
+    content: "{{ merged_kubeconfig.stdout | from_yaml | to_nice_yaml(indent=2) }}"
+  when: kubeconfig_dir_result.stat.exists
+
 - name: "Enabling addon"
 - name: "Enabling addon"
   ansible.builtin.command:
   ansible.builtin.command:
     argv:
     argv:

+ 8 - 1
tasks/minikube_destroy.yml

@@ -15,7 +15,7 @@
 
 
 - name: "Cleanup kubeconfig files"
 - name: "Cleanup kubeconfig files"
   ansible.builtin.file:
   ansible.builtin.file:
-    path: "{{ playbook_dir }}/.kube/{{ item.key }}.kubeconfig"
+    path: "{{ kubeconfig_dir }}/{{ item.key }}.kubeconfig"
     state: absent
     state: absent
   loop: "{{ minikube_profiles | dict2items }}"
   loop: "{{ minikube_profiles | dict2items }}"
   loop_control:
   loop_control:
@@ -23,6 +23,13 @@
   when: (item.value.destroy | bool ) and not ( item.value.create | bool)
   when: (item.value.destroy | bool ) and not ( item.value.create | bool)
   register: kubeconfig_cleanup
   register: kubeconfig_cleanup
 
 
+- name: "Cleanup {{ kubeconfig_dir }}/config file"
+  ansible.builtin.file:
+    path: "{{ kubeconfig_dir }}/config"
+    state: absent
+  when: kubeconfig_cleanup.changed
+  register: kubeconfig_cleanup
+
 - name: "Debug Delete Cluster"
 - name: "Debug Delete Cluster"
   debug:
   debug:
     var: delete_test_res
     var: delete_test_res