Skip to content

Commit 9b14c15

Browse files
committed
Use user data secret in template if available
1 parent de57c7b commit 9b14c15

4 files changed

Lines changed: 55 additions & 23 deletions

File tree

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ MAKEFLAGS += --no-builtin-variables
1111
PROJECT_ROOT_DIR = .
1212
include Makefile.vars.mk
1313

14+
JSONNET_FILES ?= $(shell find . -type f -not -path './vendor/*' \( -name '*.*jsonnet' -or -name '*.libsonnet' \))
15+
1416
.PHONY: help
1517
help: ## Show this help
1618
@grep -E -h '\s##\s' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
@@ -37,6 +39,7 @@ generate: ## Generate e.g. CRD, RBAC etc.
3739
.PHONY: fmt
3840
fmt: ## Run go fmt against code
3941
go fmt ./...
42+
go tool github.com/google/go-jsonnet/cmd/jsonnetfmt -i -- $(JSONNET_FILES)
4043

4144
.PHONY: vet
4245
vet: ## Run go vet against code

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,5 @@ require (
124124
sigs.k8s.io/randfill v1.0.0 // indirect
125125
sigs.k8s.io/structured-merge-diff/v6 v6.3.0 // indirect
126126
)
127+
128+
tool github.com/google/go-jsonnet/cmd/jsonnetfmt

pkg/machine/actuator_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func Test_Actuator_Create_ComplexMachineE2E(t *testing.T) {
103103
},
104104
Data: map[string][]byte{
105105
"ignitionCA": []byte("CADATA"),
106-
"userData": []byte("{ca: std.extVar('context').data.ignitionCA, udsecrets: std.map(function(s) s.metadata.name,std.extVar('context').secrets)}"),
106+
"userData": []byte("{ca: std.extVar('context').data.ignitionCA, udsecrets: std.map(function(s) [s.metadata.name,s.data],std.extVar('context').secrets)}"),
107107
},
108108
}
109109

@@ -167,7 +167,7 @@ func Test_Actuator_Create_ComplexMachineE2E(t *testing.T) {
167167
SSHKeys: []string{},
168168
UseIPV6: providerSpec.UseIPV6,
169169
ServerGroups: []string{"created-server-group-uuid"},
170-
UserData: "{\"ca\":\"CADATA\",\"udsecrets\":[\"user-data-managed\"]}",
170+
UserData: "{\"ca\":\"CADATA\",\"udsecrets\":[[\"user-data-managed\",{\"userData\":\"e30=\"}]]}",
171171
}),
172172
).DoAndReturn(cloudscaleServerFromServerRequest(func(s *cloudscale.Server) {
173173
s.UUID = "created-server-uuid"
Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,63 @@
11
local context = std.extVar('context');
22

3-
{
4-
ignition: {
5-
version: '3.1.0',
6-
config: {
7-
merge: [ {
8-
source: 'https://%s:22623/config/%s' % [ context.data.ignitionHost, std.get(context.data, 'ignitionConfigName', 'worker') ],
9-
} ],
10-
},
11-
security: {
12-
tls: {
13-
certificateAuthorities: [ {
14-
source: 'data:text/plain;charset=utf-8;base64,%s' % [ std.base64(context.data.ignitionCA) ],
15-
} ],
3+
// Tries to load user data from a secret specific to the machineset.
4+
// The variable contains `null` if the secret is not found.
5+
// The secret is expected to be named `<machineset>-user-data-managed`.
6+
// The machine set name is read from the machine labels.
7+
local userData =
8+
local machineSet =
9+
std.get(
10+
std.get(context.machine.metadata, 'labels', {}),
11+
'machine.openshift.io/cluster-api-machineset'
12+
);
13+
if machineSet != null then
14+
local secretName = std.trace("Looking for '%s-user-data-managed'", '%s-user-data-managed' % machineSet);
15+
local uds = std.filter(function(s) s.metadata.name == secretName, context.secrets);
16+
if std.length(uds) == 1 && std.objectHas(uds[0].data, 'userData') then
17+
std.trace("Found user data secret for machineset '%s'",
18+
machineSet,
19+
std.parseJson(std.decodeUTF8(std.base64DecodeBytes(uds[0].data.userData))))
20+
else
21+
std.trace("No user data secret found for machineset '%s'", machineSet, null)
22+
;
23+
24+
(
25+
if userData != null then
26+
userData
27+
else
28+
{
29+
ignition: {
30+
version: '3.1.0',
31+
config: {
32+
merge: [{
33+
source: 'https://%s:22623/config/%s' % [context.data.ignitionHost, std.get(context.data, 'ignitionConfigName', 'worker')],
34+
}],
35+
},
36+
security: {
37+
tls: {
38+
certificateAuthorities: [{
39+
source: 'data:text/plain;charset=utf-8;base64,%s' % [std.base64(context.data.ignitionCA)],
40+
}],
41+
},
42+
},
1643
},
17-
},
18-
},
19-
systemd: {
20-
units: [ {
44+
}
45+
) {
46+
systemd+: {
47+
units+: [{
2148
name: 'cloudscale-hostkeys.service',
2249
enabled: true,
2350
contents: "[Unit]\nDescription=Print SSH Public Keys to tty\nAfter=sshd-keygen.target\n\n[Install]\nWantedBy=multi-user.target\n\n[Service]\nType=oneshot\nStandardOutput=tty\nTTYPath=/dev/ttyS0\nExecStart=/bin/sh -c \"echo '-----BEGIN SSH HOST KEY KEYS-----'; cat /etc/ssh/ssh_host_*key.pub; echo '-----END SSH HOST KEY KEYS-----'\"",
24-
} ],
51+
}],
2552
},
26-
storage: {
27-
files: [ {
53+
storage+: {
54+
files+: [{
2855
filesystem: 'root',
2956
path: '/etc/hostname',
3057
mode: 420,
3158
contents: {
3259
source: 'data:,%s' % context.machine.metadata.name,
3360
},
34-
} ],
61+
}],
3562
},
3663
}

0 commit comments

Comments
 (0)