summaryrefslogtreecommitdiff
path: root/roles/kubernetes/kubeadm/control-plane/tasks/primary.yml
blob: c0591032b6e4aae78139af6f2bdb008d575d2240 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
---
- name: check if kubeconfig kubelet.conf already exists
  stat:
    path: /etc/kubernetes/kubelet.conf
  register: kubeconfig_kubelet_stats

- name: generate kubeadm.config
  template:
    src: kubeadm-init.config.j2
    dest: /etc/kubernetes/kubeadm.config
  register: kubeadm_config

### cluster not yet initialized

- name: create new cluster
  when: not kubeconfig_kubelet_stats.stat.exists
  block:

  - name: initialize kubernetes primary control-plane node and store log
    block:
    - name: initialize kubernetes primary control-plane node
      command: "kubeadm init --config /etc/kubernetes/kubeadm.config --skip-token-print"
      args:
        creates: /etc/kubernetes/pki/ca.crt
      register: kubeadm_init

    always:
    - name: dump output of kubeadm init to log file
      when: kubeadm_init.changed
      copy:
        content: "{{ kubeadm_init.stdout }}\n"
        dest: /etc/kubernetes/kubeadm-init.log

    - name: dump error output of kubeadm init to log file
      when: kubeadm_init.changed and kubeadm_init.stderr
      copy:
        content: "{{ kubeadm_init.stderr }}\n"
        dest: /etc/kubernetes/kubeadm-init.errors

  - name: create bootstrap token for new cluster
    check_mode: no
    command: kubeadm token create --ttl 42m
    register: kubeadm_token_generate


### cluster is already initialized but config has changed

- name: upgrade cluster config
  when: kubeconfig_kubelet_stats.stat.exists and kubeadm_config is changed
  block:

  - name: fail for cluster upgrades
    fail:
      msg: "upgrading cluster config is currently not supported!"


### cluster is already initialized

- name: prepare cluster for new nodes
  when: kubeconfig_kubelet_stats.stat.exists and kubeadm_config is not changed
  block:

  - name: fetch list of current nodes
    check_mode: no
    command: kubectl --kubeconfig /etc/kubernetes/admin.conf get nodes -o name
    changed_when: False
    register: kubectl_node_list

  - name: save list of current nodes
    set_fact:
      kubernetes_current_nodes: "{{ kubectl_node_list.stdout_lines | map('replace', 'node/', '') | list }}"

  - name: create bootstrap token for existing cluster
    check_mode: no
    when: "groups['_kubernetes_nodes_'] | map('extract', hostvars, 'kubernetes_node_name') | difference(kubernetes_current_nodes) | length > 0"
    command: kubeadm token create --ttl 42m
    register: kubeadm_token_create


## calculate certificate digest

- name: install openssl
  apt:
    name: openssl
    state: present

- name: get ca certificate digest
  check_mode: no
  shell: "set -o pipefail && openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'"
  args:
    executable: /bin/bash
  register: kube_ca_openssl
  changed_when: False

- name: set variables needed by kubernetes/nodes to join the cluster
  loop: "{{ groups['_kubernetes_nodes_'] }}"
  delegate_to: "{{ item }}"
  delegate_facts: True
  set_fact:
    kube_bootstrap_token: "{% if kubeadm_token_generate.stdout is defined %}{{ kubeadm_token_generate.stdout }}{% elif kubeadm_token_create.stdout is defined %}{{ kubeadm_token_create.stdout }}{% endif %}"
    kube_bootstrap_ca_cert_hash: "sha256:{{ kube_ca_openssl.stdout }}"


## Network Plugin

- name: create network plugin config directory
  file:
    path: /etc/kubernetes/network-plugin
    state: directory

- name: install network plugin
  include_tasks: "net_{{ kubernetes_network_plugin }}.yml"