summaryrefslogtreecommitdiff
path: root/roles/kubernetes/standalone/pod/tasks/main.yml
blob: 9ac6b49700de1d3ac784341e9e382616d709d551 (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
---
- name: generate config-hash
  when: "'config_hash_items' in kubernetes_standalone_pod"
  block:
  - name: create directory for config-hash files
    file:
      path: /etc/kubernetes/config-hashes
      state: directory

  - name: gather stats for config-hash items
    loop: "{{ kubernetes_standalone_pod.config_hash_items }}"
    loop_control:
      loop_var: config_hash_item
      label: "{{ config_hash_item.path }} ({{ config_hash_item.properties | sort | join(', ') }})"
    stat:
      path: "{{ config_hash_item.path }}"
      get_checksum: yes
      checksum_algorithm: sha256
    register: config_hash_items_stat

  - assert:
      msg: "at least one config-hash item does not exist"
      that: false not in (config_hash_items_stat.results | map(attribute='stat.exists'))

  - name: generate config-hash file
    copy:
      content: |
        {% for result in config_hash_items_stat.results %}
        {{ result.config_hash_item.path }}:
        {%   for property in (result.config_hash_item.properties | sort) %}
          {{ property }}: {{ result.stat[property] }}
        {%   endfor %}
        {% endfor %}
      dest: "/etc/kubernetes/config-hashes/{{ kubernetes_standalone_pod.name }}.yml"

  - name: compute config-hash value from file
    stat:
      path: "/etc/kubernetes/config-hashes/{{ kubernetes_standalone_pod.name }}.yml"
      get_checksum: yes
      checksum_algorithm: sha256
    register: config_hash_file_stat

  - name: set config-hash value
    set_fact:
      config_hash_value: "{{ config_hash_file_stat.stat.checksum }}"

- name: make sure to unset config-hash value
  when: "'config_hash_items' not in kubernetes_standalone_pod"
  block:
  - name: remove config-hash file
    file:
      path: "/etc/kubernetes/config-hashes/{{ kubernetes_standalone_pod.name }}.yml"
      state: absent

    ## this is needed in case the role gets included multiple times within the same playbook
  - name: set config-hash value
    set_fact:
      config_hash_value:

- name: generate pod manifest
  copy:
    content: |
      apiVersion: v1
      kind: Pod
      metadata:
        name: "{{ kubernetes_standalone_pod.name }}"
      {% if 'labels' in kubernetes_standalone_pod %}
        labels:
          {{ kubernetes_standalone_pod.labels | to_nice_yaml(indent=2) | indent(4) }}{% endif %}
      {% if config_hash_value or 'annotations' in kubernetes_standalone_pod %}
        annotations:
      {%   if config_hash_value %}
          config-hash: "{{ config_hash_value }}"
      {%   endif %}
      {%   if 'annotations' in kubernetes_standalone_pod %}
          {{ kubernetes_standalone_pod.annotations | default({}) | to_nice_yaml(indent=2) | indent(4) }}{% endif %}
      {% endif %}
      spec:
        {{ kubernetes_standalone_pod.spec | indent(2) }}
    dest: "/etc/kubernetes/manifests/{{ kubernetes_standalone_pod.name }}.yml"
    mode: "{{ kubernetes_standalone_pod.mode | default(omit) }}"