Skip to content

Commit 4a8aaec

Browse files
committed
Support openrc systems on agent nodes, added openrc test matrix
Signed-off-by: Derek Nola <derek.nola@suse.com>
1 parent 67ca2bf commit 4a8aaec

3 files changed

Lines changed: 77 additions & 57 deletions

File tree

.github/workflows/integration.yml

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,19 @@ jobs:
1010
runs-on: ubuntu-latest
1111
strategy:
1212
matrix:
13-
container_os: [debian12]
13+
service_mgr: [systemd, openrc]
1414

1515
# K3s requires privileged containers to run inside Docker and access to cgrougs.
1616
steps:
17+
- name: Set container OS based on service manager
18+
id: set-container
19+
run: |
20+
if [ "${{ matrix.service_mgr }}" == "systemd" ]; then
21+
echo "container_os=geerlingguy/docker-debian12-ansible" >> $GITHUB_ENV
22+
else
23+
echo "container_os=jrei/openrc-alpine" >> $GITHUB_ENV
24+
fi
25+
1726
- name: Checkout codebase
1827
uses: actions/checkout@v6
1928

@@ -42,7 +51,7 @@ jobs:
4251
--volume=/lib/modules:/lib/modules:ro \
4352
--cgroupns=host \
4453
--network=k3s-ansible \
45-
geerlingguy/docker-${{ matrix.container_os }}-ansible:latest
54+
geerlingguy/docker-debian12-ansible
4655
4756
# Start the Agent node
4857
docker run -d --name agent-node \
@@ -51,20 +60,28 @@ jobs:
5160
--volume=/lib/modules:/lib/modules:ro \
5261
--cgroupns=host \
5362
--network=k3s-ansible \
54-
geerlingguy/docker-${{ matrix.container_os }}-ansible:latest
63+
${{ env.container_os }}:latest
5564
65+
- name: Setup openrc Image
66+
if: matrix.service_mgr == 'openrc'
67+
run: docker exec agent-node apk add curl python3
68+
5669
- name: Run Playbook
5770
env:
5871
ANSIBLE_FORCE_COLOR: '1'
5972
run: ansible-playbook playbooks/site.yml -i tests/basic.yml
6073

61-
6274
- name: Verify K3s is running on Server
6375
run: docker exec server-node k3s kubectl get nodes | grep Ready
6476

65-
- name: Verify K3s is running on Agent
77+
- name: Verify K3s is running on Agent (systemd)
78+
if: matrix.service_mgr == 'systemd'
6679
run: docker exec agent-node systemctl status k3s-agent | grep running
6780

81+
- name: Verify K3s is running on Agent (openrc)
82+
if: matrix.service_mgr == 'openrc'
83+
run: docker exec agent-node rc-service k3s-agent status | grep started
84+
6885
- name: Modify the k3s_version in inventory for upgrade
6986
run: |
7087
sed -i 's/k3s_version: v1.33.4+k3s1/k3s_version: v1.34.1+k3s1/' tests/basic.yml
@@ -85,4 +102,4 @@ jobs:
85102
run: |
86103
docker stop server-node && docker rm -f server-node
87104
docker stop agent-node && docker rm -f agent-node
88-
docker network rm k3s-ansible
105+
docker network rm k3s-ansible

roles/k3s_agent/tasks/main.yml

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@
2626
group: root
2727
mode: "0755"
2828

29-
- name: Download K3s binary
30-
# For some reason, ansible-lint thinks using enviroment with command is an error
31-
# even though its valid https://ansible.readthedocs.io/projects/lint/rules/inline-env-var/#correct-code
32-
ansible.builtin.command: # noqa inline-env-var
29+
- name: Download K3s and install binary
30+
# noqa var-naming[no-role-prefix]
31+
ansible.builtin.command:
3332
cmd: /usr/local/bin/k3s-install.sh
34-
# Ensures that extra_install_envs are combined with required env vars
35-
environment: >-
36-
{{ extra_install_envs | combine({
37-
"INSTALL_K3S_SKIP_START": "true",
38-
"INSTALL_K3S_SYSTEMD_DIR": systemd_dir,
39-
"INSTALL_K3S_VERSION": k3s_version,
40-
"INSTALL_K3S_EXEC": "agent"
41-
}) }}
33+
environment: "{{ _install_envs }}"
34+
vars:
35+
_base_envs:
36+
INSTALL_K3S_SKIP_START: "true"
37+
INSTALL_K3S_SYSTEMD_DIR: "{{ systemd_dir }}"
38+
INSTALL_K3S_VERSION: "{{ k3s_version }}"
39+
INSTALL_K3S_EXEC: "agent --server https://{{ api_endpoint }}:{{ api_port }} {{ extra_agent_args }}"
40+
# We overrides the extra_install_envs with required keys from _base_envs on purpose
41+
_install_envs: "{{ extra_install_envs | default({}) | combine(_base_envs) }}"
4242
changed_when: true
4343

4444
- name: Setup optional config file
@@ -62,41 +62,33 @@
6262
ansible.builtin.set_fact:
6363
token: "{{ hostvars[groups[server_group][0]].token }}"
6464

65+
- name: Set k3s agent environment file based on init system
66+
ansible.builtin.set_fact:
67+
k3s_agent_env_file: "{{ (ansible_facts['service_mgr'] == 'systemd') | ternary(systemd_dir ~ '/k3s-agent.service.env', '/etc/rancher/k3s/k3s-agent.env') }}"
68+
6569
- name: Add service environment variables
6670
when: extra_service_envs is defined
6771
ansible.builtin.lineinfile:
68-
path: "{{ systemd_dir }}/k3s-agent.service.env"
72+
path: "{{ k3s_agent_env_file }}"
6973
line: "{{ item }}"
7074
loop: "{{ extra_service_envs }}"
7175

7276
- name: Delete any existing token from the environment if different from the new one
7377
ansible.builtin.lineinfile:
7478
state: absent
75-
path: "{{ systemd_dir }}/k3s-agent.service.env"
79+
path: "{{ k3s_agent_env_file }}"
7680
regexp: "^K3S_TOKEN=\\s*(?!{{ token | regex_escape }}\\s*$)"
7781

7882
- name: Add the token for joining the cluster to the environment
7983
no_log: true # avoid logging the server token
8084
ansible.builtin.lineinfile:
81-
path: "{{ systemd_dir }}/k3s-agent.service.env"
85+
path: "{{ k3s_agent_env_file }}"
8286
line: "{{ item }}"
8387
loop:
8488
- "K3S_TOKEN={{ token }}"
8589

86-
- name: Modify ExecStart in k3s-agent.service to include API endpoint and extra args
87-
register: k3s_agent_service
88-
ansible.builtin.replace:
89-
path: "{{ systemd_dir }}/k3s-agent.service"
90-
regexp: '^ExecStart=\/usr\/local\/bin\/k3s \\\n\s*agent.*(?:\n(?:[\t\s].*|$))*'
91-
replace: |
92-
ExecStart=/usr/local/bin/k3s \
93-
agent \
94-
--server https://{{ api_endpoint }}:{{ api_port }} \
95-
{{ extra_agent_args }}
96-
97-
- name: Enable and check K3s agent service
98-
ansible.builtin.systemd:
90+
- name: Enable and start K3s agent
91+
ansible.builtin.service:
9992
name: k3s-agent
100-
daemon_reload: "{{ true if k3s_agent_service.changed else false }}"
101-
state: "{{ 'restarted' if (k3s_agent_service.changed or _agent_config_result.changed) else 'started' }}"
93+
state: "{{ 'restarted' if _agent_config_result.changed else 'started' }}"
10294
enabled: true

roles/k3s_upgrade/tasks/main.yml

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
# INSTALL_K3S_SKIP_START does work on upgrades, because the service is already installed and started.
3232
- name: Stop K3s service
3333
when: k3s_upgrade_current_version is version(k3s_version, '<')
34-
ansible.builtin.systemd:
34+
ansible.builtin.service:
3535
state: stopped
3636
name: "{{ (server_group in group_names) | ternary('k3s', 'k3s-agent') }}"
3737

@@ -44,23 +44,47 @@
4444
register: k3s_upgrade_old_token
4545
changed_when: false
4646

47-
- name: Install new K3s Version
47+
- name: Install new K3s Version [server]
4848
# For some reason, ansible-lint thinks using enviroment with command is an error
4949
# even though its valid https://ansible.readthedocs.io/projects/lint/rules/inline-env-var/#correct-code
5050
# Skip if only reconfiguring (no version change needed)
51-
when: k3s_upgrade_current_version is version(k3s_version, '<')
51+
when:
52+
- k3s_upgrade_current_version is version(k3s_version, '<')
53+
- server_group in group_names
5254
ansible.builtin.command: # noqa inline-env-var
5355
cmd: /usr/local/bin/k3s-install.sh
5456
environment: >-
5557
{{ extra_install_envs
5658
| combine({
5759
"INSTALL_K3S_SKIP_START": "true",
5860
"INSTALL_K3S_VERSION": k3s_version,
59-
"INSTALL_K3S_EXEC": ( "agent" if agent_group in group_names else "server" )
6061
})
6162
| combine(airgap_dir is defined and {"INSTALL_K3S_SKIP_DOWNLOAD": "true"} or {}) }}
6263
changed_when: true
6364

65+
- name: Install new K3s Version [agent]
66+
# For some reason, ansible-lint thinks using enviroment with command is an error
67+
# even though its valid https://ansible.readthedocs.io/projects/lint/rules/inline-env-var/#correct-code
68+
# Unlike server, we always run the install command, because we are using it to reconfigure the ENV and Args passed to k3s-agent.
69+
# Instead we just skip the download/replace if airgapped or no version change is needed.
70+
when:
71+
- agent_group in group_names
72+
# noqa var-naming[no-role-prefix]
73+
ansible.builtin.command:
74+
cmd: /usr/local/bin/k3s-install.sh
75+
environment: "{{ _install_envs }}"
76+
vars:
77+
_base_envs:
78+
INSTALL_K3S_SKIP_DOWNLOAD: "{{ (airgap_dir is defined or k3s_upgrade_current_version == k3s_version) | ternary('true', 'false') }}"
79+
INSTALL_K3S_SKIP_START: "true"
80+
INSTALL_K3S_SYSTEMD_DIR: "{{ systemd_dir }}"
81+
INSTALL_K3S_VERSION: "{{ k3s_version }}"
82+
INSTALL_K3S_EXEC: "agent --server https://{{ api_endpoint }}:{{ api_port }} {{ extra_agent_args }}"
83+
K3S_TOKEN: "{{ token if token is defined else k3s_upgrade_old_token.stdout }}"
84+
# We overrides the extra_install_envs with required keys from _base_envs on purpose
85+
_install_envs: "{{ extra_install_envs | default({}) | combine(_base_envs) }}"
86+
changed_when: true
87+
6488
- name: Regenerate K3s service file [server]
6589
when: server_group in group_names
6690
block:
@@ -125,23 +149,11 @@
125149
cluster_init: false
126150
join: true
127151

128-
- name: Regenerate K3s service file [agent]
129-
when:
130-
- agent_group in group_names
131-
- api_endpoint is defined
132-
ansible.builtin.replace:
133-
path: "{{ systemd_dir }}/k3s-agent.service"
134-
regexp: '^ExecStart=\/usr\/local\/bin\/k3s \\\n\s*agent.*(?:\n(?:[\t\s].*|$))*'
135-
replace: |
136-
ExecStart=/usr/local/bin/k3s \
137-
agent \
138-
--server https://{{ api_endpoint }}:{{ api_port }} \
139-
{{ extra_agent_args | default('') }}
140-
141-
- name: Add token to the environment
152+
- name: Add token to the environment [server]
153+
when: server_group in group_names
142154
no_log: true # avoid logging the server token
143155
ansible.builtin.lineinfile:
144-
path: "{{ systemd_dir }}/{{ (agent_group in group_names) | ternary('k3s-agent.service.env', 'k3s.service.env') }}"
156+
path: "{{ systemd_dir }}/k3s.service.env"
145157
regexp: '^K3S_TOKEN='
146158
line: "K3S_TOKEN={{ token is defined | ternary(token, k3s_upgrade_old_token.stdout) }}"
147159

@@ -154,7 +166,6 @@
154166

155167
- name: Restart K3s service [agent]
156168
when: agent_group in group_names
157-
ansible.builtin.systemd:
169+
ansible.builtin.service:
158170
state: restarted
159-
daemon_reload: true
160171
name: k3s-agent

0 commit comments

Comments
 (0)