Skip to content

Commit 13dba4d

Browse files
authored
Merge pull request #141 from cloudymax/cloudbase-support
- Adds support for cloudbase-init syntax - fixes issue where wireguard configs supplied as secrets would not render - we now just grab the whole users[] array to avoid having to define every single possible option - fix default wireguard type declaration - remove some superfluous comments - add more reasonable default network data example Breaking Changes: - `ssh_import_id: []` and `ssh_authorized_keys: []` removed from default user values, should be commented our or removed when not in-use to avoid a templating error - new value `cloudbase: true/false` required
2 parents c9af7a8 + a991468 commit 13dba4d

File tree

7 files changed

+165
-109
lines changed

7 files changed

+165
-109
lines changed

charts/cloud-init/Chart.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ apiVersion: v2
22
name: cloud-init
33
description: A Helm chart that generates cloud-init config files
44
type: application
5-
version: 1.0.3
5+
version: 2.0.0
66
maintainers:
77
- name: cloudymax

charts/cloud-init/README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# cloud-init
22

3-
![Version: 1.0.2](https://img.shields.io/badge/Version-1.0.2-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square)
3+
![Version: 2.0.0](https://img.shields.io/badge/Version-2.0.0-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square)
44

55
A Helm chart that generates cloud-init config files
66

@@ -20,6 +20,7 @@ A Helm chart that generates cloud-init config files
2020
| argocd.syncString | string | `"Prune=false,Delete=false"` | String containing ArgoCD resource sync options |
2121
| boot_cmd | list | `[]` | Run arbitrary commands early in the boot process See https://cloudinit.readthedocs.io/en/latest/reference/modules.html#bootcmd |
2222
| ca_certs | list | `[]` | Add CA certificates See https://cloudinit.readthedocs.io/en/latest/reference/modules.html#ca-certificates |
23+
| cloudbase | bool | `false` | |
2324
| debug | bool | `false` | when enabled job sleeps to allow user to exec into the container |
2425
| disable_root | bool | `false` | Disable root login over ssh |
2526
| disk_setup | list | `[]` | |
@@ -32,9 +33,9 @@ A Helm chart that generates cloud-init config files
3233
| image | string | `"deserializeme/kv-cloud-init:1.0.0"` | image version |
3334
| mounts | list | `[]` | Set up mount points. mounts contains a list of lists. The inner list contains entries for an /etc/fstab line |
3435
| namespace | string | `"default"` | namespace in which to create resources |
35-
| network | object | `{"config":"disabled"}` | networking options |
36-
| network.config | string | `"disabled"` | disable cloud-init’s network configuration capability and rely on other methods such as embedded configuration or other customisations. |
37-
| networkData.content | string | `"renderer: networkd\nnetwork:\n version: 2\n ethernets:\n multus:\n match:\n macaddress: ${macaddress}\n dhcp4: false\n dhcp6: false\n addresses:\n - 192.168.100.100/24\n routes:\n - to: default\n via: 192.168.100.1\n mtu: 1500\n nameservers:\n addresses:\n - 192.168.100.1"` | |
36+
| network | object | `{"config":"enabled"}` | networking options |
37+
| network.config | string | `"enabled"` | disable cloud-init’s network configuration capability and rely on other methods such as embedded configuration or other customisations. |
38+
| networkData.content | string | `"network:\n version: 2\n renderer: networkd\n ethernets:\n enp1s0:\n dhcp4: true\n dhcp6: false\n"` | |
3839
| networkData.enabled | bool | `false` | |
3940
| package_reboot_if_required | bool | `false` | Update, upgrade, and install package See https://cloudinit.readthedocs.io/en/latest/reference/modules.html#package-update-upgrade-install |
4041
| package_update | bool | `true` | |
@@ -45,11 +46,9 @@ A Helm chart that generates cloud-init config files
4546
| secret_name | string | `"my-userdata"` | secret in which to save the user-data file, must be unique within namespace |
4647
| serviceAccount | object | `{"create":true,"existingServiceAccountName":"some-other-sa","name":"my-service-account"}` | Choose weather to create a service-account or not. Once a SA has been created you should set this to false on subsequent runs, or use a uniqne name per vm. |
4748
| swap | object | `{"enabled":false,"filename":"/swapfile","maxsize":"1G","size":"1G"}` | creates a swap file using human-readable values. |
48-
| users | list | `[{"groups":"users, admin, docker, sudo, kvm","lock_passwd":false,"name":"$USERNAME","password":"random","shell":"/bin/bash","ssh_authorized_keys":[],"ssh_import_id":[],"sudo":"ALL=(ALL) NOPASSWD:ALL"}]` | user configuration options See https://cloudinit.readthedocs.io/en/latest/reference/modules.html#users-and-groups do NOT use 'admin' as username - it conflicts with multiele cloud-images |
49-
| users[0].password | string | `"random"` | When set to 'random' a password will be generated for the user. |
50-
| users[0].ssh_authorized_keys | list | `[]` | provider user ssh pub key as plaintext |
51-
| users[0].ssh_import_id | list | `[]` | import user ssh public keys from github, gitlab, or launchpad See https://cloudinit.readthedocs.io/en/latest/reference/modules.html#ssh |
52-
| wireguard | list | `[]` | add wireguard configuration from existing secret or as plain-text See https://cloudinit.readthedocs.io/en/latest/reference/modules.html#wireguard |
49+
| users | list | `[{"groups":"users, admin, docker, sudo, kvm","lock_passwd":false,"name":"$USERNAME","passwd":"random","shell":"/bin/bash","sudo":"ALL=(ALL) NOPASSWD:ALL"}]` | user configuration options See https://cloudinit.readthedocs.io/en/latest/reference/modules.html#users-and-groups You are advised NOT to use 'admin' as username for linux systems because it conflicts with multiple cloud-images default user configurations When using with cloudbase-init syntax the use of "Admin" as a username is fine. |
50+
| users[0].passwd | string | `"random"` | When set to 'random' a password will be generated for the user. When empty "" we will look for an env-var named <$USERNAME>_PASSWORD Passing plain-text passwords is not supported. |
51+
| wireguard | object | `{"interfaces":[]}` | add wireguard configuration from existing secret or as plain-text See https://cloudinit.readthedocs.io/en/latest/reference/modules.html#wireguard |
5352
| write_files | list | `[]` | Write arbitrary files to disk. Files my be provided as plain-text or downloaded from a url See https://cloudinit.readthedocs.io/en/latest/reference/modules.html#write-files |
5453

5554
----------------------------------------------

charts/cloud-init/scripts/cigen.sh

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export ARGO_ENABLED="false"
3232
export ARGO_APP_NAME="none"
3333
export ARGO_SYNC="none"
3434
export ARGO_COMPARE="none"
35+
export CLOUDBASE="false"
3536

3637
# Parse and validate user inputs.
3738
parse_params() {
@@ -84,6 +85,10 @@ parse_params() {
8485
export ARGO_COMPARE="${2-}"
8586
shift
8687
;;
88+
-cb | --cloudbase)
89+
export CLOUDBASE="${2}"
90+
shift
91+
;;
8792
-?*) die "Unknown option: $1" ;;
8893
*) echo "${2-}" && break ;;
8994
esac
@@ -114,6 +119,8 @@ Available options:
114119
115120
-s, --salt Salt to use when encrypting passwords [string]
116121
122+
-cb, --cloudbase Use cloudbase-init syntax
123+
117124
Optional Kubernetes settings:
118125
119126
-k, --kubernetes Create kubernetes secrets from user and network data [true/false]
@@ -160,14 +167,23 @@ run_envsubst(){
160167
}
161168

162169
random_hostname(){
163-
CHECK=$(yq '.hostname' $USER_DATA_PATH | tr '[:lower:]' '[:upper:]')
170+
if [ "${CLOUDBASE}" == "true" ]; then
171+
CHECK=$(yq '.set_hostname' $USER_DATA_PATH | tr '[:lower:]' '[:upper:]')
172+
else
173+
CHECK=$(yq '.hostname' $USER_DATA_PATH | tr '[:lower:]' '[:upper:]')
174+
fi
164175

165-
if [ ${CHECK} == "RANDOM" ]; then
176+
if [ ${CHECK} == "RANDOM" ]; then
166177
log "Generating a random hostname."
167178
export HOSTNAME=$(golang-petname)
168-
yq -i '.hostname = env(HOSTNAME)' $USER_DATA_PATH
179+
180+
if [ "${CLOUDBASE}" == "true" ]; then
181+
yq -i '.set_hostname = env(HOSTNAME)' $USER_DATA_PATH
182+
else
183+
yq -i '.hostname = env(HOSTNAME)' $USER_DATA_PATH
184+
fi
169185
log "Nice to meet you, $HOSTNAME"
170-
fi
186+
fi
171187
}
172188

173189
# Hash and insert passwd field for each specified user
@@ -212,9 +228,16 @@ admin_password(){
212228
--argo-compare "${ARGO_COMPARE}"
213229
fi
214230

215-
log "Setting hashed password for user: $user"
216-
export HASHED_PASSWORD=$(mkpasswd --method=SHA-512 --rounds=4096 "${PASSWORD}" -s "${SALT}")
217-
yq -i '.users[env(COUNT)].passwd = env(HASHED_PASSWORD)' $USER_DATA_PATH
231+
# If not using cloudbse syntax, add the hashed password to the userdata
232+
if [ "${CLOUDBASE}" == "true" ]; then
233+
log "Setting password for user: $user"
234+
yq -i '.users[env(COUNT)].passwd = env(PASSWORD)' $USER_DATA_PATH
235+
else
236+
log "Setting hashed password for user: $user"
237+
export HASHED_PASSWORD=$(mkpasswd --method=SHA-512 --rounds=4096 "${PASSWORD}" -s "${SALT}")
238+
yq -i '.users[env(COUNT)].passwd = env(HASHED_PASSWORD)' $USER_DATA_PATH
239+
fi
240+
218241
export COUNT=$(($COUNT + 1))
219242
done
220243
}
@@ -297,6 +320,18 @@ wireguard(){
297320
yq -i '.wireguard.interfaces[env(COUNT)] |= (del(.path))' $USER_DATA_PATH
298321
fi
299322

323+
# if the config is from a secret
324+
if [ "$SOURCE" == "secret" ]; then
325+
export WG_NAME=$(yq '.wireguard.interfaces[env(COUNT)].name' "${USER_DATA_PATH}")
326+
export WG_PATH="/secrets/${WG_NAME}.conf"
327+
export OUTPUT=$(/bin/cat "${WG_PATH}")
328+
329+
log "Adding wireguard interface ${interface}"
330+
yq -i '.wireguard.interfaces[env(COUNT)].content = strenv(OUTPUT)' $USER_DATA_PATH
331+
yq -i '.wireguard.interfaces[env(COUNT)] |= (del(.source))' $USER_DATA_PATH
332+
yq -i '.wireguard.interfaces[env(COUNT)] |= (del(.path))' $USER_DATA_PATH
333+
fi
334+
300335
# if the config is in-line content
301336
if [ "$SOURCE" == "content" ]; then
302337
export CONTENT=$(yq '.wireguard.interfaces[env(COUNT)].content' "${USER_DATA_PATH}")
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{{- if .Values.cloudbase }}
2+
apiVersion: v1
3+
kind: ConfigMap
4+
metadata:
5+
name: {{ .Values.secret_name }}-userdata
6+
namespace: {{ .Values.namespace }}
7+
labels:
8+
{{- include "cloud-init.labels" . | nindent 4 }}
9+
data:
10+
{{- if .Values.networkData.enabled }}
11+
{{- $content := .Values.networkData.content }}
12+
network-data.yaml: |-
13+
{{- $content | nindent 4 }}
14+
{{- end }}
15+
user-data.yaml: |-
16+
#cloud-config
17+
set_hostname: {{ .Values.hostname }}
18+
{{- with .Values.groups }}
19+
groups:
20+
{{- toYaml . | nindent 4 }}
21+
{{- end }}
22+
{{- with .Values.users }}
23+
users:
24+
{{- toYaml . | nindent 6 }}
25+
{{- end }}
26+
{{- if gt (len .Values.write_files) 0 }}
27+
write_files:
28+
{{- range $reg, $props := .Values.write_files }}
29+
- path: {{ $props.path }}
30+
permissions: {{ $props.permissions | quote }}
31+
{{- if $props.url }}
32+
url: {{ $props.url | quote }}
33+
{{- end}}
34+
{{- if $props.encoding }}
35+
encoding: {{ $props.encoding | quote }}
36+
{{- end}}
37+
{{- if $props.content }}
38+
content: |-
39+
{{- with $props.content }}
40+
{{- . | nindent 10 }}
41+
{{- end }}
42+
{{- end }}
43+
{{- end }}
44+
{{- end }}
45+
{{- with .Values.runcmd }}
46+
runcmd:
47+
{{- toYaml . | nindent 6 }}
48+
{{- end }}
49+
{{- end }}

charts/cloud-init/templates/configmap.yaml

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{{- if not .Values.cloudbase }}
12
apiVersion: v1
23
kind: ConfigMap
34
metadata:
@@ -44,23 +45,10 @@ data:
4445
groups:
4546
{{- toYaml . | nindent 4 }}
4647
{{- end }}
48+
{{- with .Values.users }}
4749
users:
48-
{{- range $reg, $props := .Values.users }}
49-
- name: {{ $props.name }}
50-
groups: {{ $props.groups }}
51-
sudo: {{ $props.sudo }}
52-
shell: {{ $props.shell }}
53-
lock_passwd: {{ $props.lock_passwd }}
54-
passwd: {{ $props.password }}
55-
{{- with $props.ssh_import_id }}
56-
ssh_import_id:
57-
{{- toYaml . | nindent 10 }}
58-
{{- end }}
59-
{{- with $props.ssh_authorized_keys }}
60-
ssh_authorized_keys:
61-
{{- toYaml . | nindent 10 }}
62-
{{- end }}
63-
{{- end }}
50+
{{- toYaml . | nindent 6 }}
51+
{{- end }}
6452
{{- with .Values.boot_cmd }}
6553
bootcmd:
6654
{{- toYaml . | nindent 6 }}
@@ -98,11 +86,19 @@ data:
9886
runcmd:
9987
{{- toYaml . | nindent 6 }}
10088
{{- end }}
101-
{{- if .Values.wireguard }}
89+
{{- if .Values.wireguard.interfaces }}
10290
wireguard:
10391
interfaces:
10492
{{- range $reg, $props := .Values.wireguard.interfaces }}
10593
- name: {{ $props.name }}
10694
config_path: {{ $props.config_path }}
95+
source: {{ $props.source }}
96+
{{- if eq $props.source "content" }}
97+
content: |-
98+
{{- with $props.content }}
99+
{{- . | nindent 12 }}
100+
{{- end }}
101+
{{- end }}
107102
{{- end }}
108103
{{- end }}
104+
{{- end }}

charts/cloud-init/templates/job.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ spec:
3939
- "/bin/bash"
4040
- "/home/appuser/cigen.sh"
4141
- "--userdata /secrets/user-data.yaml"
42+
- "--cloudbase {{ .Values.cloudbase }}"
4243
{{- if .Values.networkData.enabled }}
4344
- "--networkdata /secrets/network-data.yaml"
4445
{{- end }}
@@ -79,11 +80,14 @@ spec:
7980
subPath: secretgen.sh
8081
{{- if .Values.wireguard }}
8182
{{- range $reg, $props := .Values.wireguard.interfaces }}
83+
{{- if $props.existingSecret }}
8284
- name: {{ $props.name }}
83-
mountPath: /secrets
85+
mountPath: /secrets/{{ $props.name }}.conf
86+
subPath: {{ $props.name }}.conf
8487
readOnly: true
8588
{{- end }}
8689
{{- end }}
90+
{{- end }}
8791
volumes:
8892
- name: userdata
8993
configMap:
@@ -113,7 +117,7 @@ spec:
113117
{{- end }}
114118
{{- if .Values.wireguard }}
115119
{{- range $reg, $props := .Values.wireguard.interfaces }}
116-
{{- if eq .props.source "secret" }}
120+
{{- if $props.existingSecret }}
117121
- name: {{ $props.name }}
118122
secret:
119123
secretName: {{ $props.existingSecret.name }}

0 commit comments

Comments
 (0)