Commit ffa1d294 authored by Muhammad Umair Khan's avatar Muhammad Umair Khan
Browse files

fix ansible kubernetes & calico idempotentency and role

parent 77558cf7
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
collections:
  - name: community.general
  - name: ansible.posix
  - name: community.docker
  - name: ansible.posix
  - name: kubernetes.core
roles: []
+6 −3
Original line number Diff line number Diff line
@@ -36,15 +36,18 @@ docker_repo_arch: >-
    else ansible_facts['architecture']
  }}
docker_repo_codename: "{{ ansible_facts['lsb']['codename'] | default('jammy') }}"
docker_version: "5:29.2.1-1~ubuntu.{{ ansible_distribution_version }}~{{ docker_repo_codename }}" # Docker Version


# Kubernetes
kubernetes_version: "v1.35.1"
kubernetes_version: "v1.35.1"   # exact version for package installation/pinning
kubernetes_version_series: "v1.35"  # minor version for repo URL

kubernetes_repo_apt_key_url: >-
  https://pkgs.k8s.io/core:/stable:/{{ kubernetes_version }}/deb/Release.key
  https://pkgs.k8s.io/core:/stable:/{{ kubernetes_version_series }}/deb/Release.key
kubernetes_repo_apt_entry: >-
  deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg]
  https://pkgs.k8s.io/core:/stable:/{{ kubernetes_version }}/deb/ /
  https://pkgs.k8s.io/core:/stable:/{{ kubernetes_version_series }}/deb/ /
kubeadm_cluster_name: "mec-sandbox"
pod_network_cidr: "92.68.0.0/16"
service_cidr: "10.96.0.0/12"

playbooks/requirements.yml

deleted100644 → 0
+0 −6
Original line number Diff line number Diff line
collections:
- name: community.general
- name: community.docker
- name: ansible.posix
- name: kubernetes.core
roles: []
+156 −27
Original line number Diff line number Diff line
---
# - name: Check if calico-system namespace exists
#   command: kubectl get ns tigera-operator --kubeconfig /etc/kubernetes/admin.conf
#   register: calico_ns
#   failed_when: false
#   changed_when: false

- name: Check if calico-system namespace exists
  command: kubectl get ns tigera-operator --kubeconfig /etc/kubernetes/admin.conf
  register: calico_ns
  failed_when: false
  changed_when: false

- name: Install Calico operator
  when: calico_ns.rc != 0
  command: >
    kubectl apply -f {{ calico_operator_manifest }}
    --kubeconfig /etc/kubernetes/admin.conf
  register: calico_operator_result
  changed_when: "'created' in calico_operator_result.stdout"

- name: Wait before applying Calico custom resources (allow operator to initialize)
  pause:
    seconds: 30
  when: calico_ns.rc != 0

- name: Install Calico custom resources
  when: calico_ns.rc != 0
  command: >
    kubectl apply -f {{ calico_custom_resources_manifest }}
    --kubeconfig /etc/kubernetes/admin.conf
  register: calico_cr_result
  changed_when: "'created' in calico_cr_result.stdout"
# - name: Install Calico operator
#   when: calico_ns.rc != 0
#   command: >
#     kubectl apply -f {{ calico_operator_manifest }}
#     --kubeconfig /etc/kubernetes/admin.conf
#   register: calico_operator_result
#   changed_when: "'created' in calico_operator_result.stdout"

# - name: Wait before applying Calico custom resources (allow operator to initialize)
#   pause:
#     seconds: 30
#   when: calico_ns.rc != 0

# - name: Install Calico custom resources
#   when: calico_ns.rc != 0
#   command: >
#     kubectl apply -f {{ calico_custom_resources_manifest }}
#     --kubeconfig /etc/kubernetes/admin.conf
#   register: calico_cr_result
#   changed_when: "'created' in calico_cr_result.stdout"
# - block:
#     - name: Create temporary kubeconfig directory
#       file:
#         path: /home/ansible/.kube
#         state: directory
#         mode: '0700'
#         owner: ansible
#         group: ansible

#     - name: Copy admin.conf to temporary kubeconfig
#       copy:
#         src: /etc/kubernetes/admin.conf
#         dest: /home/ansible/.kube/config
#         owner: ansible
#         group: ansible
#         mode: '0600'
- name: Ensure .kube directory exists for user
  file:
    path: "/home/{{ target_user }}/.kube"
    state: directory
    owner: "{{ target_user }}"
    group: "{{ target_user }}"
    mode: '0700'
  become: true

- name: copy admin.conf for user
  become: true
  copy:
    src: /etc/kubernetes/admin.conf
    dest: /home/{{ target_user }}/.kube/config
    owner: "{{ target_user }}"
    mode: '0600'
    remote_src: true

# - name: Ensure tigera-operator namespace exists
#   kubernetes.core.k8s:
#     api_version: v1
#     kind: Namespace
#     name: tigera-operator
#     kubeconfig: /home/{{ target_user }}/.kube/config
#     state: present
#   become: true

# - name: Apply Calico operator manifest
#   kubernetes.core.k8s:
#     kubeconfig: /home/{{ target_user }}/.kube/config
#     state: present
#     src: "{{ calico_operator_manifest }}"
#     wait: true
#     wait_condition:
#       type: Ready
#       status: "True"
#   become: true

- name: Apply Calico operator manifest
  kubernetes.core.k8s:
    kubeconfig: /home/{{ target_user }}/.kube/config
    state: present
    src: "{{ calico_operator_manifest }}"
  become: true
  register: operator_manifest_result
  ignore_errors: true

- name: Wait for tigera-operator Deployment to be Ready
  kubernetes.core.k8s:
    kubeconfig: /home/{{ target_user }}/.kube/config
    state: present
    kind: Deployment
    name: tigera-operator
    namespace: tigera-operator
    wait: true
    wait_condition:
      type: Available
      status: "True"
  become: true
  when: operator_manifest_result is not failed

- name: Wait for operator.tigera.io/v1.Installation CRD to exist
  kubernetes.core.k8s_info:
    kubeconfig: /home/{{ target_user }}/.kube/config
    kind: CustomResourceDefinition
    name: installations.operator.tigera.io
  register: installation_crd
  retries: 30
  delay: 5
  until: installation_crd.resources | length > 0
  become: true

# - name: Apply Calico custom resources manifest
#   kubernetes.core.k8s:
#     kubeconfig: /home/{{ target_user }}/.kube/config
#     state: present
#     src: "{{ calico_custom_resources_manifest }}"
#     wait: true
#     wait_condition:
#       type: Ready
#       status: "True"
#   become: true

- name: Apply Calico custom resources manifest
  kubernetes.core.k8s:
    kubeconfig: /home/{{ target_user }}/.kube/config
    state: present
    src: "{{ calico_custom_resources_manifest }}"
  become: true
  register: calico_custom_resources_result

- name: Wait for Calico Installation to be ready
  retries: 60
  delay: 10
  until: >
    calico_installation.resources[0].status.conditions is defined
    and (calico_installation.resources[0].status.conditions
         | selectattr('type', 'equalto', 'Degraded')
         | map(attribute='status')
         | list | first) == "False"
  kubernetes.core.k8s_info:
    kubeconfig: /home/{{ target_user }}/.kube/config
    kind: Installation
    api_version: operator.tigera.io/v1
    name: default
  register: calico_installation
  become: true

- name: Display CNI installation notice
  debug:
@@ -33,4 +155,11 @@
      CNI (Calico) is being installed — this involves downloading container images and may take several minutes.
      You can check the status in another terminal by running:
        kubectl get po -A
      Wait until every pod (especially coredns, calico-node, tigera-operator) shows Running/Ready.

# - name: Display CNI installation notice
#   debug:
#     msg: |
#       CNI (Calico) is being installed — this involves downloading container images and may take several minutes.
#       You can check the status in another terminal by running:
#         kubectl get po -A
#       Wait until every pod (especially coredns, calico-node, tigera-operator) shows Running/Ready.
+5 −8
Original line number Diff line number Diff line
@@ -4,15 +4,16 @@
    name: docker
    tasks_from: repo.yml

- name: Ensure containerd is installed
- name: Install containerd
  apt:
    name: "containerd.io={{ containerd_version }}"
    state: present
    update_cache: true
    allow_downgrade: true
    update_cache: true
    cache_valid_time: 3600   # Only updates cache if older than 1 hour
  become: true
  retries: 3        # try up to 3 times
  delay: 10         # wait 10 between retries
  retries: 2        # try up to 3 times
  delay: 5         # wait 10 between retries

- name: Generate default containerd config
  shell: containerd config default > {{ containerd_config_path }}
@@ -35,10 +36,6 @@
    regexp: 'sandbox_image = "registry.k8s.io/pause:3.8'
    replace: 'sandbox_image = "registry.k8s.io/pause:3.10'
  become: true

- name: Trigger containerd restart
  debug:
    msg: "Triggering handler restart"
  notify: Restart containerd
  changed_when: true

Loading