Skip to content

Commit 0c3c52e

Browse files
committed
fix: user-data template
Fix functions used in the template framework. Signed-off-by: Serge Logvinov <[email protected]>
1 parent 0f34c30 commit 0c3c52e

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

docs/functions.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ The Cloud-Init provider supports several template functions that can be used to
8787
{{ trimPrefix "hello" "he" }} -> llo
8888
```
8989

90+
* `split` - the function to split the string by the given separator and return a slice of strings.
91+
92+
```yaml
93+
{{ split "a,b,c" "," }} -> [a b c]
94+
```
95+
96+
* `join` - the function to join a slice of strings with the given separator.
97+
98+
```yaml
99+
{{ join (list "a" "b" "c") "," }} -> a,b,c
100+
```
101+
90102
* `replace` - the function to replace all occurrences of the old string with the new string.
91103

92104
```yaml

pkg/providers/instance/cloudinit/functions.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ var genericMap = map[string]interface{}{
5050
"trim": strings.TrimSpace,
5151
"trimSuffix": func(a, b string) string { return strings.TrimSuffix(b, a) },
5252
"trimPrefix": func(a, b string) string { return strings.TrimPrefix(b, a) },
53+
"split": strings.Split,
54+
"join": strings.Join,
5355

5456
"replace": func(o, n, s string) string { return strings.ReplaceAll(s, o, n) },
5557
"regexFind": regexFind,
@@ -191,7 +193,7 @@ func toYamlPretty(v any) string {
191193
}
192194

193195
// hasKey returns true if the given map has the given key.
194-
func hasKey(m map[string]interface{}, key string) bool {
196+
func hasKey(m map[string]any, key string) bool {
195197
if empty(m) {
196198
return false
197199
}
@@ -271,7 +273,7 @@ func regexFind(regex string, s string) (string, error) {
271273
}
272274

273275
// get returns the value for the given key in the given map, or an empty string if the key does not exist.
274-
func get(m map[string]interface{}, key string) interface{} {
276+
func get(m map[string]string, key string) string {
275277
if val, ok := m[key]; ok {
276278
return val
277279
}

pkg/providers/instance/cloudinit/userdata.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ users:
2929
shell: /bin/bash
3030
{{- with get .Values "SSHAuthorizedKeys" }}
3131
ssh_authorized_keys:
32-
{{- toYaml . | nindent 6 }}
32+
{{- toYaml (split . ",") | nindent 6 }}
3333
{{- end }}
3434
3535
write_files:

pkg/providers/instance/cloudinit/userdata_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ write_files:
4444
permissions: 0o600
4545
defer: true
4646
content: |
47-
{{- .Metadata.Tags | toYamlPretty | nindent 6 }}
47+
{{- join .Metadata.Tags "," | nindent 6 }}
4848
{{- end }}
4949
`
5050
)
@@ -57,7 +57,7 @@ func TestUserData(t *testing.T) {
5757
data := struct {
5858
Metadata cloudinit.MetaData
5959
KubeletConfiguration *instance.KubeletConfiguration
60-
Values map[string]interface{}
60+
Values map[string]string
6161
}{
6262
Metadata: cloudinit.MetaData{
6363
Hostname: "hostname-1",
@@ -74,8 +74,8 @@ func TestUserData(t *testing.T) {
7474
TopologyManagerPolicy: "best-effort",
7575
ProviderID: provider.GetProviderID(region, 100),
7676
},
77-
Values: map[string]interface{}{
78-
"SSHAuthorizedKeys": []string{"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCu..."},
77+
Values: map[string]string{
78+
"SSHAuthorizedKeys": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCu...,ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDk...",
7979
},
8080
}
8181

@@ -101,6 +101,7 @@ users:
101101
shell: /bin/bash
102102
ssh_authorized_keys:
103103
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCu...
104+
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDk...
104105
105106
write_files:
106107
- path: /etc/karpenter.yaml
@@ -124,8 +125,7 @@ write_files:
124125
- kernel.shmmax
125126
providerID: proxmox://test-region/100
126127
values:
127-
SSHAuthorizedKeys:
128-
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCu...
128+
SSHAuthorizedKeys: ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCu...,ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDk...
129129
owner: root:root
130130
`,
131131
},
@@ -153,8 +153,7 @@ write_files:
153153
permissions: 0o600
154154
defer: true
155155
content: |
156-
- tag1
157-
- tag2
156+
tag1,tag2
158157
`,
159158
},
160159
}

pkg/providers/instance/instance_cloudinit.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/luthermonson/go-proxmox"
2727

28+
goproxmox "github.com/sergelogvinov/go-proxmox"
2829
"github.com/sergelogvinov/karpenter-provider-proxmox/pkg/apis/v1alpha1"
2930
"github.com/sergelogvinov/karpenter-provider-proxmox/pkg/providers/cloudcapacity"
3031
"github.com/sergelogvinov/karpenter-provider-proxmox/pkg/providers/instance/cloudinit"
@@ -205,10 +206,14 @@ func (p *DefaultProvider) generateCloudInitVars(
205206
return "", "", "", "", fmt.Errorf("failed to create bootstrap token: %v", err)
206207
}
207208

209+
smbios1 := goproxmox.VMSMBIOS{}
210+
smbios1.UnmarshalString(vm.VirtualMachineConfig.SMBios1)
211+
208212
metadataValues := cloudinit.MetaData{
209213
Hostname: nodeClaim.Name,
210214
InstanceID: fmt.Sprintf("%d", vm.VMID),
211215
InstanceType: instanceType.Name,
216+
InstanceUUID: smbios1.UUID,
212217
ProviderID: provider.GetProviderID(region, int(vm.VMID)),
213218
Region: region,
214219
Zone: zone,

0 commit comments

Comments
 (0)