Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
--volume=/lib/modules:/lib/modules:ro \
--cgroupns=host \
--network=k3s-ansible \
rancher/systemd-node:v0.0.8 /usr/lib/systemd/systemd --unit=noop.target --show-status=true
${{ env.container_os }} ${{ env.container_cmd }}

# Start the Agent node
docker run -d --name agent-node \
Expand All @@ -76,16 +76,17 @@ jobs:
--network=k3s-ansible \
${{ env.container_os }} ${{ env.container_cmd }}

- name: Install server dependencies
run: docker exec server-node zypper install -y python3-rpm

- name: Install agent dependencies (systemd)
- name: Install node dependencies (systemd)
if: matrix.service_mgr == 'systemd'
run: docker exec agent-node zypper install -y python3-rpm
run: |
docker exec server-node zypper install -y python3-rpm
docker exec agent-node zypper install -y python3-rpm

- name: Install agent dependencies (openrc)
- name: Install node dependencies (openrc)
if: matrix.service_mgr == 'openrc'
run: docker exec agent-node apk add curl python3
run: |
docker exec server-node apk add curl python3
docker exec agent-node apk add curl python3

- name: Run Playbook
env:
Expand Down Expand Up @@ -193,15 +194,24 @@ jobs:
echo "CONFIG FILE"
docker exec server-node cat /etc/rancher/k3s/config.yaml
echo "Server ENV"
docker exec server-node cat /etc/systemd/system/k3s.service.env
if [ $service_mgr == "systemd" ]; then
docker exec server-node cat /etc/systemd/system/k3s.service.env
elif [ $service_mgr == "openrc" ]; then
docker exec server-node cat /etc/rancher/k3s/k3s.env
fi
echo "Agent ENV"
if [ $service_mgr == "systemd" ]; then
docker exec agent-node cat /etc/systemd/system/k3s-agent.service.env
elif [ $service_mgr == "openrc" ]; then
docker exec agent-node cat /etc/rancher/k3s/k3s-agent.env
fi
echo "SERVER LOGS"
docker exec server-node journalctl -u k3s -n 10
if [ $service_mgr == "systemd" ]; then
docker exec server-node journalctl -u k3s -n 10
elif [ $service_mgr == "openrc" ]; then
tail -n 10 /var/log/k3s.log
fi


- name: Remove K3s from Server and Agent
run: ansible-playbook playbooks/reset.yml -i tests/${{ matrix.inventory }}.yml
Expand Down
63 changes: 31 additions & 32 deletions roles/k3s_server/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@
regexp: '\.\s+<\(k3s completion bash\)'
line: ". <(k3s completion bash) # Added by k3s-ansible"

- name: Set k3s server environment file based on init system
ansible.builtin.set_fact:
k3s_server_env_file: "{{ (ansible_facts['service_mgr'] == 'systemd') | ternary(systemd_dir ~ '/k3s.service.env', '/etc/rancher/k3s/k3s.env') }}"

- name: Add service environment variables
when: extra_service_envs is defined
ansible.builtin.lineinfile:
path: "{{ k3s_server_env_file }}"
line: "{{ item }}"
loop: "{{ extra_service_envs }}"

- name: Init first server node
when: inventory_hostname == groups[server_group][0] or ansible_host == groups[server_group][0]
block:
Expand All @@ -145,35 +156,23 @@
mode: "0644"
register: k3s_server_config_result

- name: Add service environment variables
when: extra_service_envs is defined
ansible.builtin.lineinfile:
path: "{{ systemd_dir }}/k3s.service.env"
line: "{{ item }}"
loop: "{{ extra_service_envs }}"

- name: Delete any existing token from the environment if different from the new one
ansible.builtin.lineinfile:
state: absent
path: "{{ systemd_dir }}/k3s.service.env"
path: "{{ k3s_server_env_file }}"
regexp: "^K3S_TOKEN=\\s*(?!{{ token | default('') | regex_escape }}\\s*$)"

- name: Restart K3s service
- name: Reload systemd daemon
when:
- ansible_facts.services['k3s.service'] is defined
- ansible_facts.services['k3s.service'].state == 'running'
- k3s_server_config_result.changed
- not ansible_check_mode
- ansible_facts['service_mgr'] == 'systemd'
ansible.builtin.systemd:
name: k3s
daemon_reload: true
state: restarted

- name: Enable and check K3s service
when: ansible_facts.services['k3s.service'] is not defined or ansible_facts.services['k3s.service'].state != 'running'
ansible.builtin.systemd:
- name: Enable and start K3s service
ansible.builtin.service:
name: k3s
daemon_reload: true
state: started
state: restarted
enabled: true

- name: Pause to allow first server startup
Expand All @@ -193,12 +192,14 @@

# Copy the k3s config to a second file to detect changes.
# If no changes are found, we can skip copying the kubeconfig to the control node.
# This is dependent on the speed of the target node for k3s startup time, so retries are used.
- name: Copy k3s.yaml to second file
ansible.builtin.copy:
src: /etc/rancher/k3s/k3s.yaml
dest: /etc/rancher/k3s/k3s-copy.yaml
mode: "0600"
remote_src: true
retries: 3
register: k3s_server_copy_yaml
tags: kubeconfig

Expand Down Expand Up @@ -280,34 +281,32 @@
mode: "0644"
register: k3s_server_config_result

- name: Add service environment variables
when: extra_service_envs is defined
ansible.builtin.lineinfile:
path: "{{ systemd_dir }}/k3s.service.env"
line: "{{ item }}"
loop: "{{ extra_service_envs }}"

- name: Delete any existing token from the environment if different from the new one
ansible.builtin.lineinfile:
state: absent
path: "{{ systemd_dir }}/k3s.service.env"
path: "{{ k3s_server_env_file }}"
regexp: "^K3S_TOKEN=\\s*(?!{{ token | regex_escape }}\\s*$)"

- name: Reload systemd daemon
when:
- not ansible_check_mode
- ansible_facts['service_mgr'] == 'systemd'
ansible.builtin.systemd:
daemon_reload: true

- name: Restart K3s service
when:
- ansible_facts.services['k3s.service'] is defined
- ansible_facts.services['k3s.service'].state == 'running'
- k3s_server_config_result.changed
ansible.builtin.systemd:
ansible.builtin.service:
name: k3s
daemon_reload: true
state: restarted

- name: Enable and check K3s service
- name: Enable and start K3s service
when: ansible_facts.services['k3s.service'] is not defined or ansible_facts.services['k3s.service'].state != 'running'
ansible.builtin.systemd:
ansible.builtin.service:
name: k3s
daemon_reload: true
state: started
enabled: true

Expand Down
1 change: 1 addition & 0 deletions roles/k3s_upgrade/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@
when: server_group in group_names
block:
# Start with an empty config
- name: Set empty server config

Check warning on line 52 in roles/k3s_upgrade/tasks/main.yml

View workflow job for this annotation

GitHub Actions / Lint

var-naming[no-role-prefix]

Variables names from within roles should use k3s_upgrade_ as a prefix. (set_fact: k3s_server_config)
ansible.builtin.set_fact:
k3s_server_config: {}

# If token is provided, add it to the config
- name: Add token to server config

Check warning on line 57 in roles/k3s_upgrade/tasks/main.yml

View workflow job for this annotation

GitHub Actions / Lint

var-naming[no-role-prefix]

Variables names from within roles should use k3s_upgrade_ as a prefix. (set_fact: k3s_server_config)
when: token is defined
ansible.builtin.set_fact:
k3s_server_config: "{{ k3s_server_config | combine({'token': token}) }}"

# If token is not defined, use the old token
- name: Add old token to server config

Check warning on line 63 in roles/k3s_upgrade/tasks/main.yml

View workflow job for this annotation

GitHub Actions / Lint

var-naming[no-role-prefix]

Variables names from within roles should use k3s_upgrade_ as a prefix. (set_fact: k3s_server_config)
when: token is not defined
ansible.builtin.set_fact:
k3s_server_config: "{{ k3s_server_config | combine({'token': k3s_upgrade_old_token.stdout}) }}"
Expand All @@ -69,7 +69,7 @@
# noqa var-naming[no-role-prefix]
ansible.builtin.set_fact:
_api_endpoint_in_config: >-
{% if server_config_yaml is defined and api_endpoint is defined and server_config_yaml | regex_search('tls-san:.*' + api_endpoint | regex_escape(), ignorecase=True) %}

Check warning on line 72 in roles/k3s_upgrade/tasks/main.yml

View workflow job for this annotation

GitHub Actions / Lint

yaml[line-length]

Line too long (181 > 180 characters)
true
{% else %}
false
Expand All @@ -81,7 +81,7 @@
false
{% endif %}

- name: Add TLS SAN to config if needed

Check warning on line 84 in roles/k3s_upgrade/tasks/main.yml

View workflow job for this annotation

GitHub Actions / Lint

var-naming[no-role-prefix]

Variables names from within roles should use k3s_upgrade_ as a prefix. (set_fact: k3s_server_config)
when:
- api_endpoint is defined
- api_endpoint != ansible_hostname
Expand All @@ -90,7 +90,7 @@
ansible.builtin.set_fact:
k3s_server_config: "{{ k3s_server_config | combine({'tls-san': api_endpoint}) }}"

- name: Add cluster-init to server config for first server in HA-IC setup

Check warning on line 93 in roles/k3s_upgrade/tasks/main.yml

View workflow job for this annotation

GitHub Actions / Lint

var-naming[no-role-prefix]

Variables names from within roles should use k3s_upgrade_ as a prefix. (set_fact: k3s_server_config)
when:
- (groups[server_group] | length) > 1
- inventory_hostname == groups[server_group][0] or ansible_host == groups[server_group][0]
Expand All @@ -100,18 +100,18 @@

# If not the first server in an HA-IC setup, setup the server: URL for joining the cluster
# server: https://{{ api_endpoint }}:{{ api_port }}
- name: Add server URL to server config for joining servers in HA-IC setup

Check warning on line 103 in roles/k3s_upgrade/tasks/main.yml

View workflow job for this annotation

GitHub Actions / Lint

var-naming[no-role-prefix]

Variables names from within roles should use k3s_upgrade_ as a prefix. (set_fact: k3s_server_config)
when: (groups[server_group] | length) > 1 and inventory_hostname != groups[server_group][0] and not use_external_database
ansible.builtin.set_fact:
k3s_server_config: "{{ k3s_server_config | combine({'server': 'https://' + api_endpoint + ':' + api_port | string}) }}"

# If the user has provided additional server config, merge it with the generated config
- name: Merge user server config with generated server config

Check warning on line 109 in roles/k3s_upgrade/tasks/main.yml

View workflow job for this annotation

GitHub Actions / Lint

var-naming[no-role-prefix]

Variables names from within roles should use k3s_upgrade_ as a prefix. (set_fact: k3s_server_config)
when: server_config_yaml is defined
ansible.builtin.set_fact:
k3s_server_config: "{{ k3s_server_config | combine(server_config_yaml | from_yaml) }}"

- name: Convert server config to YAML and write to file

Check warning on line 114 in roles/k3s_upgrade/tasks/main.yml

View workflow job for this annotation

GitHub Actions / Lint

var-naming[no-role-prefix]

Variables names from within roles should use k3s_upgrade_ as a prefix. (register: k3s_server_config_result)
when: not ansible_check_mode
ansible.builtin.copy:
content: "{{ k3s_server_config | to_nice_yaml }}"
Expand All @@ -133,6 +133,7 @@
| combine({
"INSTALL_K3S_SKIP_START": "true",
"INSTALL_K3S_VERSION": k3s_version,
"INSTALL_K3S_EXEC": extra_server_args,
})
| combine(airgap_dir is defined and {"INSTALL_K3S_SKIP_DOWNLOAD": "true"} or {}) }}
changed_when: true
Expand Down
Loading