diff --git a/pkg/vmprovider/providers/vsphere/vmprovider_vm_test.go b/pkg/vmprovider/providers/vsphere/vmprovider_vm_test.go index 20447ba38..b0a6d9479 100644 --- a/pkg/vmprovider/providers/vsphere/vmprovider_vm_test.go +++ b/pkg/vmprovider/providers/vsphere/vmprovider_vm_test.go @@ -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() { diff --git a/pkg/vmprovider/providers/vsphere/vmprovider_vm_utils.go b/pkg/vmprovider/providers/vsphere/vmprovider_vm_utils.go index f1a3bf4e8..fb9e791bb 100644 --- a/pkg/vmprovider/providers/vsphere/vmprovider_vm_utils.go +++ b/pkg/vmprovider/providers/vsphere/vmprovider_vm_utils.go @@ -232,6 +232,9 @@ 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) @@ -239,8 +242,6 @@ func GetVMMetadata( // 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{} @@ -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) diff --git a/pkg/vmprovider/providers/vsphere/vmprovider_vm_utils_test.go b/pkg/vmprovider/providers/vsphere/vmprovider_vm_utils_test.go index 638bd10d5..8d8acde4f 100644 --- a/pkg/vmprovider/providers/vsphere/vmprovider_vm_utils_test.go +++ b/pkg/vmprovider/providers/vsphere/vmprovider_vm_utils_test.go @@ -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" ) @@ -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() { @@ -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)) }) }) }) @@ -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() { @@ -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)) }) }) })