Skip to content

Commit f823042

Browse files
committed
now uses bootstrap tokens instead of cloud-init metadata
1 parent 25eea00 commit f823042

File tree

17 files changed

+88
-71
lines changed

17 files changed

+88
-71
lines changed

ansible/bootstrap.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,4 @@
312312
tasks:
313313
- ansible.builtin.include_role:
314314
name: k3s
315-
tasks_from: install.yml
315+
tasks_from: "{{ 'install.yml' if 'builder' in group_names else 'runtime.yml' }}"

ansible/roles/k3s/defaults/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ k3s_version: "v1.31.0+k3s1"
33
k3s_selinux_release: v1.6.latest.1
44
k3s_selinux_rpm_version: 1.6-1
55
k3s_helm_version: v3.11.0
6+
k3s_bootstrap_token_expiry: 5m

ansible/roles/k3s/files/start_k3s.yml

Lines changed: 0 additions & 44 deletions
This file was deleted.

ansible/roles/k3s/tasks/install.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,3 @@
7171
ansible.builtin.lineinfile:
7272
path: /etc/environment
7373
line: "KUBECONFIG=/etc/rancher/k3s/k3s.yaml"
74-
75-
- name: Install ansible-init playbook for k3s agent or server activation
76-
copy:
77-
src: start_k3s.yml
78-
dest: /etc/ansible-init/playbooks/0-start-k3s.yml

ansible/roles/k3s/tasks/runtime.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
- name: Check if k3s agents are already connected
3+
service_facts:
4+
register: services_state
5+
6+
- name: Initialise and authenticate k3s server and agents
7+
vars:
8+
k3s_server_name: "{{ hostvars[groups['k3s_server'].0].ansible_host }}"
9+
access_ip: "{{ ansible_default_ipv4.address }}"
10+
services_states: > # getting list of all unique agent service states
11+
groups['k3s_agent']
12+
| map('extract', hostvars, ['services', 'k3s-agent.service', 'state'])
13+
| unique
14+
when: not (services_state | length == 1 and services_state[0] == 'running')
15+
block:
16+
- name: Initialise server and generate bootstrap tokens
17+
when: inventory_hostname in groups['k3s_server']
18+
block:
19+
- name: Template k3s env file
20+
ansible.builtin.template:
21+
dest: /etc/systemd/system/k3s.service.env
22+
src: k3s.service.env.j2
23+
24+
- name: Start k3s server
25+
ansible.builtin.systemd:
26+
name: k3s
27+
daemon_reload: true
28+
state: started
29+
enabled: true
30+
31+
- name: Generate bootstrap token
32+
no_log: true
33+
shell:
34+
cmd: "k3s token create --ttl {{ k3s_bootstrap_token_expiry }}"
35+
register: _token_output
36+
37+
- name: Initialise agents
38+
when: inventory_hostname in groups['k3s_agent']
39+
block:
40+
- name: Template k3s agent env file
41+
ansible.builtin.template:
42+
dest: /etc/systemd/system/k3s-agent.service.env
43+
src: k3s-agent.service.env.j2
44+
45+
- name: Ensure password directory exists
46+
ansible.builtin.file:
47+
path: "/etc/rancher/node"
48+
state: directory
49+
50+
- name: Write node password
51+
ansible.builtin.copy:
52+
dest: /etc/rancher/node/password
53+
content: "{{ vault_k3s_node_password }}"
54+
owner: root
55+
group: root
56+
mode: 640 # normal k3s install is 644 but that doesn't feel right
57+
58+
- name: Start k3s agent
59+
ansible.builtin.systemd:
60+
name: k3s-agent
61+
daemon_reload: true
62+
state: started
63+
enabled: true
64+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
K3S_NODE_IP={{ access_ip }}
2+
K3S_TOKEN={{ hostvars[groups['control'] | first]._token_output.stdout }}
3+
K3S_URL=https://{{ k3s_server_name }}:6443
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
K3S_NODE_IP={{ access_ip }}

ansible/roles/passwords/defaults/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ slurm_appliance_secrets:
88
vault_openhpc_mungekey: "{{ secrets_openhpc_mungekey | default(vault_openhpc_mungekey | default(secrets_openhpc_mungekey_default)) }}"
99
vault_freeipa_ds_password: "{{ vault_freeipa_ds_password | default(lookup('password', '/dev/null')) }}"
1010
vault_freeipa_admin_password: "{{ vault_freeipa_admin_password | default(lookup('password', '/dev/null')) }}"
11-
vault_k3s_token: "{{ vault_k3s_token | default(lookup('ansible.builtin.password', '/dev/null', length=64)) }}"
11+
vault_k3s_node_password: "{{ vault_k3s_node_password | default(lookup('ansible.builtin.password', '/dev/null', length=64)) }}"
1212
vault_pulp_admin_password: "{{ vault_pulp_admin_password | default(lookup('password', '/dev/null', chars=['ascii_letters', 'digits'])) }}"
1313
vault_demo_user_password: "{{ vault_demo_user_password | default(lookup('password', '/dev/null')) }}"
1414

ansible/roles/passwords/templates/k3s-token.auto.tfvars.json.j2

Lines changed: 0 additions & 3 deletions
This file was deleted.

environments/common/inventory/groups

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,16 @@ freeipa_client
145145
[compute_init]
146146
# EXPERIMENTAL: Compute hosts to enable joining cluster on boot on
147147

148-
[k3s]
148+
[k3s:children]
149149
# Hosts to run k3s server/agent
150+
k3s_server
151+
k3s_agent
152+
153+
[k3s_server]
154+
# Hosts to run k3s server (should only be single node i.e control node)
155+
156+
[k3s_agent]
157+
# Hosts to run k3s agent
150158

151159
[k9s]
152160
# Hosts to install k9s on

environments/common/layouts/everything

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,14 @@ cluster
9797
# EXPERIMENTAL: Compute hosts to enable joining cluster on boot on
9898
compute
9999

100-
[k3s:children]
101-
# Hosts to run k3s server/agent
102-
openhpc
100+
[k3s_server:children]
101+
# Hosts to run k3s server (should only be single node i.e control node)
102+
control
103+
104+
[k3s_agent:children]
105+
# Hosts to run k3s agent
106+
compute
107+
login
103108

104109
[k9s:children]
105110
# Hosts to install k9s on

environments/skeleton/{{cookiecutter.environment}}/tofu/compute.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ module "compute" {
2929
availability_zone = lookup(each.value, "availability_zone", "nova")
3030

3131
# computed
32-
k3s_token = local.k3s_token
3332
# not using openstack_compute_instance_v2.control.access_ip_v4 to avoid
3433
# updates to node metadata on deletion/recreation of the control node:
3534
control_address = openstack_networking_port_v2.control[var.cluster_networks[0].network].all_fixed_ips[0]

environments/skeleton/{{cookiecutter.environment}}/tofu/control.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ resource "openstack_compute_instance_v2" "control" {
5959

6060
metadata = {
6161
environment_root = var.environment_root
62-
k3s_token = local.k3s_token
6362
access_ip = openstack_networking_port_v2.control[var.cluster_networks[0].network].all_fixed_ips[0]
6463
}
6564

environments/skeleton/{{cookiecutter.environment}}/tofu/login.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ module "login" {
3333
ignore_image_changes = false
3434

3535
# computed
36-
k3s_token = local.k3s_token
3736
# not using openstack_compute_instance_v2.control.access_ip_v4 to avoid
3837
# updates to node metadata on deletion/recreation of the control node:
3938
control_address = openstack_networking_port_v2.control[var.cluster_networks[0].network].all_fixed_ips[0]

environments/skeleton/{{cookiecutter.environment}}/tofu/node_group/nodes.tf

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ resource "openstack_compute_instance_v2" "compute_fixed_image" {
8585
metadata = merge(
8686
{
8787
environment_root = var.environment_root
88-
k3s_token = var.k3s_token
8988
control_address = var.control_address
9089
access_ip = openstack_networking_port_v2.compute["${each.key}-${var.networks[0].network}"].all_fixed_ips[0]
9190
},
@@ -139,7 +138,6 @@ resource "openstack_compute_instance_v2" "compute" {
139138
metadata = merge(
140139
{
141140
environment_root = var.environment_root
142-
k3s_token = var.k3s_token
143141
control_address = var.control_address
144142
access_ip = openstack_networking_port_v2.compute["${each.key}-${var.networks[0].network}"].all_fixed_ips[0]
145143
},

environments/skeleton/{{cookiecutter.environment}}/tofu/node_group/variables.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ variable "security_group_ids" {
7070
type = list
7171
}
7272

73-
variable "k3s_token" {
74-
type = string
75-
}
76-
7773
variable "control_address" {
7874
description = "Name/address of control node"
7975
type = string

environments/skeleton/{{cookiecutter.environment}}/tofu/variables.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,3 @@ variable "inventory_secrets_path" {
189189
type = string
190190
default = ""
191191
}
192-
193-
locals {
194-
k3s_token = data.external.inventory_secrets.result["vault_k3s_token"]
195-
}

0 commit comments

Comments
 (0)