Skip to content

Commit

Permalink
Bootstrap VM without userdata using Cloudinit
Browse files Browse the repository at this point in the history
  • Loading branch information
zyiyi11 committed Aug 30, 2023
1 parent 55c2dfa commit d6a76ed
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 9 deletions.
92 changes: 92 additions & 0 deletions pkg/vmprovider/providers/vsphere/vmprovider_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,98 @@ func vmTests() {
})
})
})

Context("Cloudinint Transport without userdata", func() {
var ec map[string]interface{}

JustBeforeEach(func() {
vm.Spec.VmMetadata = &vmopv1.VirtualMachineMetadata{
Transport: vmopv1.VirtualMachineMetadataCloudInitTransport,
}
vcVM, err := createOrUpdateAndGetVcVM(ctx, vm)
Expect(err).ToNot(HaveOccurred())

var o mo.VirtualMachine
Expect(vcVM.Properties(ctx, vcVM.Reference(), nil, &o)).To(Succeed())

ec = map[string]interface{}{}
for _, option := range o.Config.ExtraConfig {
if val := option.GetOptionValue(); val != nil {
ec[val.Key] = val.Value.(string)
}
}
})

AfterEach(func() {
ec = nil
})

It("Metadata data is included in ExtraConfig", func() {
By("Should include default keys and values", func() {
Expect(ec).To(HaveKeyWithValue("disk.enableUUID", "TRUE"))
Expect(ec).To(HaveKeyWithValue("vmware.tools.gosc.ignoretoolscheck", "TRUE"))
})

By("Should include guestinfo metadata and no guestinfo userdata", func() {
Expect(ec).To(HaveKey("guestinfo.metadata"))
Expect(ec).To(HaveKey("guestinfo.metadata.encoding"))
Expect(ec).ToNot(HaveKey("guestinfo.userdata"))
})
})
})

Context("Cloudinint Transport with userdata", func() {
var ec map[string]interface{}

JustBeforeEach(func() {
configMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "md-configmap-",
Namespace: vm.Namespace,
},
Data: map[string]string{
"user-data": "data",
},
}
Expect(ctx.Client.Create(ctx, configMap)).To(Succeed())

vm.Spec.VmMetadata = &vmopv1.VirtualMachineMetadata{
ConfigMapName: configMap.Name,
Transport: vmopv1.VirtualMachineMetadataCloudInitTransport,
}

vcVM, err := createOrUpdateAndGetVcVM(ctx, vm)
Expect(err).ToNot(HaveOccurred())

var o mo.VirtualMachine
Expect(vcVM.Properties(ctx, vcVM.Reference(), nil, &o)).To(Succeed())

ec = map[string]interface{}{}
for _, option := range o.Config.ExtraConfig {
if val := option.GetOptionValue(); val != nil {
ec[val.Key] = val.Value.(string)
}
}
})

AfterEach(func() {
ec = nil
})

It("Metadata data is included in ExtraConfig", func() {
By("Should include default keys and values", func() {
Expect(ec).To(HaveKeyWithValue("disk.enableUUID", "TRUE"))
Expect(ec).To(HaveKeyWithValue("vmware.tools.gosc.ignoretoolscheck", "TRUE"))
})

By("Should include guestinfo metadata and userdata", func() {
Expect(ec).To(HaveKey("guestinfo.metadata"))
Expect(ec).To(HaveKey("guestinfo.metadata.encoding"))
Expect(ec).To(HaveKey("guestinfo.userdata"))
Expect(ec).To(HaveKey("guestinfo.userdata.encoding"))
})
})
})
})

Context("Network", func() {
Expand Down
7 changes: 3 additions & 4 deletions pkg/vmprovider/providers/vsphere/vmprovider_vm_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,15 +232,16 @@ func GetVMMetadata(
return vmMD, errors.New("invalid VM Metadata: both ConfigMapName and SecretName are specified")
}

// ConfigMap and Secret can be both empty.
vmMD.Transport = metadata.Transport

if metadata.ConfigMapName != "" {
cm := &corev1.ConfigMap{}
err := k8sClient.Get(vmCtx, ctrlclient.ObjectKey{Name: metadata.ConfigMapName, Namespace: vmCtx.VM.Namespace}, cm)
if err != nil {
// TODO: Condition
return vmMD, errors.Wrap(err, "Failed to get VM Metadata ConfigMap")
}

vmMD.Transport = metadata.Transport
vmMD.Data = cm.Data
} else if metadata.SecretName != "" {
secret := &corev1.Secret{}
Expand All @@ -249,8 +250,6 @@ func GetVMMetadata(
// TODO: Condition
return vmMD, errors.Wrap(err, "Failed to get VM Metadata Secret")
}

vmMD.Transport = metadata.Transport
vmMD.Data = make(map[string]string)
for k, v := range secret.Data {
vmMD.Data[k] = string(v)
Expand Down
26 changes: 21 additions & 5 deletions pkg/vmprovider/providers/vsphere/vmprovider_vm_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/vmware-tanzu/vm-operator/pkg/lib"
"github.com/vmware-tanzu/vm-operator/pkg/vmprovider/providers/vsphere"
"github.com/vmware-tanzu/vm-operator/pkg/vmprovider/providers/vsphere/instancestorage"
"github.com/vmware-tanzu/vm-operator/pkg/vmprovider/providers/vsphere/session"
"github.com/vmware-tanzu/vm-operator/test/builder"
)

Expand Down Expand Up @@ -563,18 +562,32 @@ func vmUtilTests() {
})
})

When("neither ConfigMap nor Secret is specified", func() {
BeforeEach(func() {
vmCtx.VM.Spec.VmMetadata = &vmopv1.VirtualMachineMetadata{
Transport: vmopv1.VirtualMachineMetadataCloudInitTransport,
}
})
It("returns metadata transport", func() {
md, err := vsphere.GetVMMetadata(vmCtx, k8sClient)
Expect(err).ToNot(HaveOccurred())
Expect(md.Transport).To(Equal(vmopv1.VirtualMachineMetadataCloudInitTransport))
})
})

When("VM Metadata is specified via a ConfigMap", func() {
BeforeEach(func() {
vmCtx.VM.Spec.VmMetadata = &vmopv1.VirtualMachineMetadata{
ConfigMapName: vmMetaDataConfigMap.Name,
Transport: "transport",
Transport: vmopv1.VirtualMachineMetadataCloudInitTransport,
}
})

It("return an error when ConfigMap does not exist", func() {
md, err := vsphere.GetVMMetadata(vmCtx, k8sClient)
Expect(err).To(HaveOccurred())
Expect(md).To(Equal(session.VMMetadata{}))
Expect(md.Data).To(BeEmpty())
Expect(md.Transport).To(Equal(vmopv1.VirtualMachineMetadataCloudInitTransport))
})

When("ConfigMap exists", func() {
Expand All @@ -586,6 +599,7 @@ func vmUtilTests() {
md, err := vsphere.GetVMMetadata(vmCtx, k8sClient)
Expect(err).ToNot(HaveOccurred())
Expect(md.Data).To(Equal(vmMetaDataConfigMap.Data))
Expect(md.Transport).To(Equal(vmopv1.VirtualMachineMetadataCloudInitTransport))
})
})
})
Expand All @@ -594,14 +608,15 @@ func vmUtilTests() {
BeforeEach(func() {
vmCtx.VM.Spec.VmMetadata = &vmopv1.VirtualMachineMetadata{
SecretName: vmMetaDataSecret.Name,
Transport: "transport",
Transport: vmopv1.VirtualMachineMetadataCloudInitTransport,
}
})

It("returns an error when Secret does not exist", func() {
md, err := vsphere.GetVMMetadata(vmCtx, k8sClient)
Expect(err).To(HaveOccurred())
Expect(md).To(Equal(session.VMMetadata{}))
Expect(md.Data).To(BeEmpty())
Expect(md.Transport).To(Equal(vmopv1.VirtualMachineMetadataCloudInitTransport))
})

When("Secret exists", func() {
Expand All @@ -613,6 +628,7 @@ func vmUtilTests() {
md, err := vsphere.GetVMMetadata(vmCtx, k8sClient)
Expect(err).ToNot(HaveOccurred())
Expect(md.Data).ToNot(BeEmpty())
Expect(md.Transport).To(Equal(vmopv1.VirtualMachineMetadataCloudInitTransport))
})
})
})
Expand Down

0 comments on commit d6a76ed

Please sign in to comment.