Skip to content

Commit 0ffa8a2

Browse files
author
Yuvaraj Kakaraparthi
committedJul 29, 2019
setting hostname of machines created using cludinit
1 parent a0f94df commit 0ffa8a2

File tree

3 files changed

+62
-16
lines changed

3 files changed

+62
-16
lines changed
 

‎pkg/cloud/vsphere/services/userdata/controlplane.go

+36-12
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,24 @@ public-network = "{{ .Network }}"
5050
{{if .SSHAuthorizedKeys}}ssh_authorized_keys:{{range .SSHAuthorizedKeys}}
5151
- "{{.}}"{{end}}{{end}}
5252
53+
runcmd:
54+
- [hostname, {{HostNameLookup}}]
55+
5356
write_files:
57+
- path: /etc/hostname
58+
owner: root:root
59+
permissions: 0644
60+
content: |
61+
{{ HostNameLookup }}
62+
63+
- path: /etc/hosts
64+
owner: root:root
65+
permissions: 0644
66+
content: |
67+
::1 ipv6-localhost ipv6-loopback
68+
127.0.0.1 localhost
69+
127.0.0.1 {{HostNameLookup}}
70+
5471
- path: /etc/kubernetes/pki/ca.crt
5572
encoding: "base64"
5673
owner: root:root
@@ -132,7 +149,24 @@ kubeadm:
132149
{{if .SSHAuthorizedKeys}}ssh_authorized_keys:{{range .SSHAuthorizedKeys}}
133150
- "{{.}}"{{end}}{{end}}
134151
152+
runcmd:
153+
- [hostname, {{HostNameLookup}}]
154+
135155
write_files:
156+
- path: /etc/hostname
157+
owner: root:root
158+
permissions: 0644
159+
content: |
160+
{{ HostNameLookup }}
161+
162+
- path: /etc/hosts
163+
owner: root:root
164+
permissions: 0644
165+
content: |
166+
::1 ipv6-localhost ipv6-loopback
167+
127.0.0.1 localhost
168+
127.0.0.1 {{HostNameLookup}}
169+
136170
- path: /etc/kubernetes/pki/ca.crt
137171
encoding: "base64"
138172
owner: root:root
@@ -310,12 +344,7 @@ func NewControlPlane(input *ControlPlaneInput) (string, error) {
310344
return "", errors.Wrapf(err, "ControlPlaneInput is invalid")
311345
}
312346

313-
fMap := map[string]interface{}{
314-
"Base64Encode": templateBase64Encode,
315-
"Indent": templateYAMLIndent,
316-
}
317-
318-
userData, err := generateWithFuncs("controlplane", controlPlaneCloudInit, funcMap(fMap), input)
347+
userData, err := generateWithFuncs("controlplane", controlPlaneCloudInit, defaultFuncMap(), input)
319348
if err != nil {
320349
return "", errors.Wrapf(err, "failed to generate user data for new control plane machine")
321350
}
@@ -331,12 +360,7 @@ func JoinControlPlane(input *ContolPlaneJoinInput) (string, error) {
331360
return "", errors.Wrapf(err, "ControlPlaneInput is invalid")
332361
}
333362

334-
fMap := map[string]interface{}{
335-
"Base64Encode": templateBase64Encode,
336-
"Indent": templateYAMLIndent,
337-
}
338-
339-
userData, err := generateWithFuncs("controlplane", controlPlaneJoinCloudInit, funcMap(fMap), input)
363+
userData, err := generateWithFuncs("controlplane", controlPlaneJoinCloudInit, defaultFuncMap(), input)
340364
if err != nil {
341365
return "", errors.Wrapf(err, "failed to generate user data for machine joining control plane")
342366
}

‎pkg/cloud/vsphere/services/userdata/node.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,24 @@ const (
2121
{{if .SSHAuthorizedKeys}}ssh_authorized_keys:{{range .SSHAuthorizedKeys}}
2222
- "{{.}}"{{end}}{{end}}
2323
24+
runcmd:
25+
- [hostname, {{HostNameLookup}}]
26+
2427
write_files:
28+
- path: /etc/hostname
29+
owner: root:root
30+
permissions: 0644
31+
content: |
32+
{{ HostNameLookup }}
33+
34+
- path: /etc/hosts
35+
owner: root:root
36+
permissions: 0644
37+
content: |
38+
::1 ipv6-localhost ipv6-loopback
39+
127.0.0.1 localhost
40+
127.0.0.1 {{HostNameLookup}}
41+
2542
- path: /tmp/kubeadm-node.yaml
2643
owner: root:root
2744
permissions: '0640'
@@ -45,8 +62,5 @@ type NodeInput struct {
4562
// NewNode returns the user data string to be used on a node instance.
4663
func NewNode(input *NodeInput) (string, error) {
4764
input.Header = cloudConfigHeader
48-
fMap := map[string]interface{}{
49-
"Indent": templateYAMLIndent,
50-
}
51-
return generateWithFuncs("node", nodeCloudInit, fMap, input)
65+
return generateWithFuncs("node", nodeCloudInit, defaultFuncMap(), input)
5266
}

‎pkg/cloud/vsphere/services/userdata/userdata.go

+8
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,11 @@ func funcMap(funcs map[string]interface{}) template.FuncMap {
9090

9191
return funcMap
9292
}
93+
94+
func defaultFuncMap() template.FuncMap {
95+
return template.FuncMap{
96+
"Base64Encode": templateBase64Encode,
97+
"Indent": templateYAMLIndent,
98+
"HostNameLookup": func() string { return "{{ ds.meta_data.hostname }}" },
99+
}
100+
}

0 commit comments

Comments
 (0)
Please sign in to comment.