Skip to content

Commit

Permalink
Merge pull request #474 from ykakarap/fix-hostname-setup
Browse files Browse the repository at this point in the history
setting hostname of machines using cloud-init
  • Loading branch information
k8s-ci-robot authored Aug 8, 2019
2 parents 64c4c4d + 0ffa8a2 commit 6bae52c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
48 changes: 36 additions & 12 deletions pkg/cloud/vsphere/services/userdata/controlplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,24 @@ const (
{{if .SSHAuthorizedKeys}}ssh_authorized_keys:{{range .SSHAuthorizedKeys}}
- "{{.}}"{{end}}{{end}}
runcmd:
- [hostname, {{HostNameLookup}}]
write_files:
- path: /etc/hostname
owner: root:root
permissions: 0644
content: |
{{ HostNameLookup }}
- path: /etc/hosts
owner: root:root
permissions: 0644
content: |
::1 ipv6-localhost ipv6-loopback
127.0.0.1 localhost
127.0.0.1 {{HostNameLookup}}
- path: /etc/kubernetes/pki/ca.crt
encoding: "base64"
owner: root:root
Expand Down Expand Up @@ -110,7 +127,24 @@ kubeadm:
{{if .SSHAuthorizedKeys}}ssh_authorized_keys:{{range .SSHAuthorizedKeys}}
- "{{.}}"{{end}}{{end}}
runcmd:
- [hostname, {{HostNameLookup}}]
write_files:
- path: /etc/hostname
owner: root:root
permissions: 0644
content: |
{{ HostNameLookup }}
- path: /etc/hosts
owner: root:root
permissions: 0644
content: |
::1 ipv6-localhost ipv6-loopback
127.0.0.1 localhost
127.0.0.1 {{HostNameLookup}}
- path: /etc/kubernetes/pki/ca.crt
encoding: "base64"
owner: root:root
Expand Down Expand Up @@ -275,12 +309,7 @@ func NewControlPlane(input *ControlPlaneInput) (string, error) {
return "", errors.Wrapf(err, "ControlPlaneInput is invalid")
}

fMap := map[string]interface{}{
"Base64Encode": templateBase64Encode,
"Indent": templateYAMLIndent,
}

userData, err := generateWithFuncs("controlplane", controlPlaneCloudInit, funcMap(fMap), input)
userData, err := generateWithFuncs("controlplane", controlPlaneCloudInit, defaultFuncMap(), input)
if err != nil {
return "", errors.Wrapf(err, "failed to generate user data for new control plane machine")
}
Expand All @@ -296,12 +325,7 @@ func JoinControlPlane(input *ContolPlaneJoinInput) (string, error) {
return "", errors.Wrapf(err, "ControlPlaneInput is invalid")
}

fMap := map[string]interface{}{
"Base64Encode": templateBase64Encode,
"Indent": templateYAMLIndent,
}

userData, err := generateWithFuncs("controlplane", controlPlaneJoinCloudInit, funcMap(fMap), input)
userData, err := generateWithFuncs("controlplane", controlPlaneJoinCloudInit, defaultFuncMap(), input)
if err != nil {
return "", errors.Wrapf(err, "failed to generate user data for machine joining control plane")
}
Expand Down
22 changes: 18 additions & 4 deletions pkg/cloud/vsphere/services/userdata/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,24 @@ const (
{{if .SSHAuthorizedKeys}}ssh_authorized_keys:{{range .SSHAuthorizedKeys}}
- "{{.}}"{{end}}{{end}}
runcmd:
- [hostname, {{HostNameLookup}}]
write_files:
- path: /etc/hostname
owner: root:root
permissions: 0644
content: |
{{ HostNameLookup }}
- path: /etc/hosts
owner: root:root
permissions: 0644
content: |
::1 ipv6-localhost ipv6-loopback
127.0.0.1 localhost
127.0.0.1 {{HostNameLookup}}
- path: /tmp/kubeadm-node.yaml
owner: root:root
permissions: '0640'
Expand All @@ -45,8 +62,5 @@ type NodeInput struct {
// NewNode returns the user data string to be used on a node instance.
func NewNode(input *NodeInput) (string, error) {
input.Header = cloudConfigHeader
fMap := map[string]interface{}{
"Indent": templateYAMLIndent,
}
return generateWithFuncs("node", nodeCloudInit, fMap, input)
return generateWithFuncs("node", nodeCloudInit, defaultFuncMap(), input)
}
8 changes: 8 additions & 0 deletions pkg/cloud/vsphere/services/userdata/userdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,11 @@ func funcMap(funcs map[string]interface{}) template.FuncMap {

return funcMap
}

func defaultFuncMap() template.FuncMap {
return template.FuncMap{
"Base64Encode": templateBase64Encode,
"Indent": templateYAMLIndent,
"HostNameLookup": func() string { return "{{ ds.meta_data.hostname }}" },
}
}

0 comments on commit 6bae52c

Please sign in to comment.