Skip to content

Commit

Permalink
Start using the new machinespec definition for creating the instance …
Browse files Browse the repository at this point in the history
…on vsphere (kubernetes-sigs#72)

The code now acts accordingly based on the availability of the new cloud-init datasource
"VMwareGuestInfo" inside the template used. This availability can be set by the users in
the spec using the flag "vsphereCloudInit" set to true. For usual upstream version of
ubuntu cloud image that has been used so far, this flag need not be set at all and the
controller will utilize the OVF properties available on the ubuntu cloud image to inject
the cloud-init data.
Note: The new static IP support is only available with the templates that have the vsphere
cloud-init datasource. DHCP based IPs will continue to work as they have in the past.

Going forward we prefer users creating a custom ubuntu cloud image that also has this new
cloud-init datasource pre-installed. As that will give them the ability to use static IPs
as well if they want.

See https://github.com/akutz/cloud-init-vmware-guestinfo for more info on the new cloud-init
datasource

* Adds support for multiple nics
* Adds support for static IP

Resolves kubernetes-sigs#24
Resolves kubernetes-sigs#28

Change-Id: Id2dee12b382d57b6591f840dadd77a1c287667c4
  • Loading branch information
sidharthsurana authored and k8s-ci-robot committed Oct 25, 2018
1 parent 2e910e9 commit 4e0bb12
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 98 deletions.
90 changes: 84 additions & 6 deletions pkg/cloud/vsphere/provisioner/common/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package common

import (
"bytes"
"encoding/base64"
"fmt"
"text/template"

vsphereconfig "sigs.k8s.io/cluster-api-provider-vsphere/pkg/apis/vsphereproviderconfig"
vsphereutils "sigs.k8s.io/cluster-api-provider-vsphere/pkg/cloud/vsphere/utils"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
)
Expand Down Expand Up @@ -86,10 +88,12 @@ func preloadScript(t *template.Template, version string, dockerImages []string)
}

var (
nodeStartupScriptTemplate *template.Template
masterStartupScriptTemplate *template.Template
cloudInitUserDataTemplate *template.Template
cloudProviderConfigTemplate *template.Template
nodeStartupScriptTemplate *template.Template
masterStartupScriptTemplate *template.Template
cloudInitUserDataTemplate *template.Template
cloudProviderConfigTemplate *template.Template
cloudInitMetaDataNetworkTemplate *template.Template
cloudInitMetaDataTemplate *template.Template
)

func init() {
Expand All @@ -108,8 +112,30 @@ func init() {
nodeStartupScriptTemplate = template.Must(nodeStartupScriptTemplate.Parse(genericTemplates))
masterStartupScriptTemplate = template.Must(template.New("masterStartupScript").Funcs(funcMap).Parse(masterStartupScript))
masterStartupScriptTemplate = template.Must(masterStartupScriptTemplate.Parse(genericTemplates))
cloudInitUserDataTemplate = template.Must(template.New("cloudInitUserData").Parse(cloudinit))
cloudInitUserDataTemplate = template.Must(template.New("cloudInitUserData").Parse(cloudInitUserData))
cloudProviderConfigTemplate = template.Must(template.New("cloudProviderConfig").Parse(cloudProviderConfig))
cloudInitMetaDataNetworkTemplate = template.Must(template.New("cloudInitMetaDataNetwork").Parse(networkSpec))
cloudInitMetaDataTemplate = template.Must(template.New("cloudInitMetaData").Parse(cloudInitMetaData))
}

// Returns the startup script for the nodes.
func GetCloudInitMetaData(name string, params *vsphereconfig.VsphereMachineProviderConfig) (string, error) {
var buf bytes.Buffer
param := CloudInitMetadataNetworkTemplate{
Networks: params.MachineSpec.Networks,
}
if err := cloudInitMetaDataNetworkTemplate.Execute(&buf, param); err != nil {
return "", err
}
param2 := CloudInitMetadataTemplate{
NetworkSpec: base64.StdEncoding.EncodeToString(buf.Bytes()),
Hostname: name,
}
buf.Reset()
if err := cloudInitMetaDataTemplate.Execute(&buf, param2); err != nil {
return "", err
}
return buf.String(), nil
}

// Returns the startup script for the nodes.
Expand Down Expand Up @@ -147,10 +173,62 @@ type CloudInitTemplate struct {
Script string
IsMaster bool
CloudProviderConfig string
SSHPublicKey string
}

type CloudInitMetadataNetworkTemplate struct {
Networks []vsphereconfig.NetworkSpec
}
type CloudInitMetadataTemplate struct {
NetworkSpec string
Hostname string
}

const cloudInitMetaData = `
{
"network": "{{ .NetworkSpec }}",
"network.encoding": "base64",
"local-hostname": "{{ .Hostname }}"
}
`

const networkSpec = `
version: 1
config:
{{- range $index, $network := .Networks}}
- type: physical
name: eth{{ $index }}
subnets:
{{- if eq $network.IPConfig.NetworkType "static" }}
- type: static
address: {{ $network.IPConfig.IP }}
{{- if $network.IPConfig.Gateway }}
gateway: {{ $network.IPConfig.Gateway }}
{{- end }}
{{- if $network.IPConfig.Netmask }}
netmask: {{ $network.IPConfig.Netmask }}
{{- end }}
{{- if $network.IPConfig.Dns }}
dns_nameservers:
{{- range $network.IPConfig.Dns }}
- {{ . }}
{{- end }}
{{- end }}
{{- else }}
- type: dhcp
{{- end }}
{{- end }}
`

const cloudinit = `
const cloudInitUserData = `
#cloud-config
users:
- name: ubuntu
ssh_authorized_keys:
- {{ .SSHPublicKey }}
sudo: ALL=(ALL) NOPASSWD:ALL
groups: sudo
shell: /bin/bash
write_files:
- path: /tmp/boot.sh
content: |
Expand Down
Loading

0 comments on commit 4e0bb12

Please sign in to comment.