From 75323d0b300f346762f43802306d26cc3be6c110 Mon Sep 17 00:00:00 2001 From: Yanhua Li Date: Thu, 26 Jan 2023 15:05:50 -0500 Subject: [PATCH] Changes to support new optional fields of NutanixMachineProviderConfig --- pkg/actuators/machine/actuator_test.go | 4 + pkg/actuators/machine/machine_scope.go | 3 + pkg/actuators/machine/reconciler.go | 10 +- pkg/actuators/machine/utils.go | 20 +- pkg/actuators/machine/vm.go | 223 ++++++++++++++++---- pkg/actuators/machineset/controller_test.go | 3 +- pkg/actuators/machineset/suite_test.go | 6 +- pkg/client/state.go | 2 +- 8 files changed, 210 insertions(+), 61 deletions(-) diff --git a/pkg/actuators/machine/actuator_test.go b/pkg/actuators/machine/actuator_test.go index fb00c926..8dd3bede 100644 --- a/pkg/actuators/machine/actuator_test.go +++ b/pkg/actuators/machine/actuator_test.go @@ -164,6 +164,8 @@ func TestMachineEvents(t *testing.T) { pspec.VCPUSockets = 0 pspec.MemorySize = resource.MustParse("1.5Gi") pspec.SystemDiskSize = resource.MustParse("18Gi") + pspec.BootType = "invalid-boottype" + pspec.Project.Type = "uuid" return pspec }(), operation: func(actuator *Actuator, machine *machinev1beta1.Machine) { @@ -176,6 +178,8 @@ func TestMachineEvents(t *testing.T) { "The minimum vCPU sockets of the VM is 1", "The minimum memorySize is 2Gi bytes", "The minimum systemDiskSize is 20Gi bytes", + "Invalid bootType, the valid bootType values are: \"\", \"Legacy\", \"UEFI\", \"SecureBoot\"", + "Missing project uuid", }, }, { diff --git a/pkg/actuators/machine/machine_scope.go b/pkg/actuators/machine/machine_scope.go index a73330c1..16e6491b 100644 --- a/pkg/actuators/machine/machine_scope.go +++ b/pkg/actuators/machine/machine_scope.go @@ -49,6 +49,9 @@ type machineScope struct { machineToBePatched runtimeclient.Patch providerSpec *machinev1.NutanixMachineProviderConfig providerStatus *machinev1.NutanixMachineProviderStatus + // For Machine vm create/update use, take a copy of the providerSpec that we can mutate. + // This must never be written back to the Machine itself. + providerSpecValidated *machinev1.NutanixMachineProviderConfig } func newMachineScope(params machineScopeParams) (*machineScope, error) { diff --git a/pkg/actuators/machine/reconciler.go b/pkg/actuators/machine/reconciler.go index ce3bbf25..268b0790 100644 --- a/pkg/actuators/machine/reconciler.go +++ b/pkg/actuators/machine/reconciler.go @@ -267,11 +267,13 @@ func (r *Reconciler) setProviderID(vmUUID *string) error { klog.Infof("%s: ProviderID set at machine.spec: %s", r.machine.Name, providerID) } - // update the corresponding node.Spec.ProviderID - var nodeName string - if r.machine.Status.NodeRef != nil { - nodeName = r.machine.Status.NodeRef.Name + if r.machine.Status.NodeRef == nil { + klog.Infof("%s: the Machine node is not ready yet.", r.machine.Name) + return nil } + + // update the corresponding node.Spec.ProviderID + nodeName := r.machine.Status.NodeRef.Name if len(nodeName) == 0 { nodeName = r.machine.Name } diff --git a/pkg/actuators/machine/utils.go b/pkg/actuators/machine/utils.go index 58d7d314..5b43bebc 100644 --- a/pkg/actuators/machine/utils.go +++ b/pkg/actuators/machine/utils.go @@ -95,10 +95,20 @@ const ( NutanixCategoryValue = "owned" ) -// Add the category for installer clueanup the Machine VM at cluster torn-down time -// if the category exists in PC. -func addCategory(mscp *machineScope, vmMetadata *nutanixClientV3.Metadata) error { +// addCategories adds the category for installer clueanup the Machine VM +// at cluster torn-down time if the category exists in PC. +// Add the addtional categories to the Machine VM if configured +func addCategories(mscp *machineScope, vmMetadata *nutanixClientV3.Metadata) error { var err error + if vmMetadata.Categories == nil { + vmMetadata.Categories = make(map[string]string, len(mscp.providerSpec.Categories)+1) + } + + // The addtional categories are already verified + for _, category := range mscp.providerSpec.Categories { + vmMetadata.Categories[category.Key] = category.Value + } + clusterID, ok := getClusterID(mscp.machine) if !ok || clusterID == "" { err = fmt.Errorf("%s: failed to get the clusterID", mscp.machine.Name) @@ -113,10 +123,6 @@ func addCategory(mscp *machineScope, vmMetadata *nutanixClientV3.Metadata) error return err } - // Add the category to the vm metadata - if vmMetadata.Categories == nil { - vmMetadata.Categories = make(map[string]string, 1) - } vmMetadata.Categories[categoryKey] = NutanixCategoryValue return nil diff --git a/pkg/actuators/machine/vm.go b/pkg/actuators/machine/vm.go index a8deece4..23ead9b2 100644 --- a/pkg/actuators/machine/vm.go +++ b/pkg/actuators/machine/vm.go @@ -21,6 +21,9 @@ func validateVMConfig(mscp *machineScope) field.ErrorList { fldPath := field.NewPath("spec", "providerSpec", "value") var errMsg string + // Will use the parameters in providerSpecValidated to create the VM + mscp.providerSpecValidated = mscp.providerSpec.DeepCopy() + // verify the cluster configuration if fldErr := validateClusterConfig(mscp, fldPath); fldErr != nil { errList = append(errList, fldErr) @@ -34,46 +37,70 @@ func validateVMConfig(mscp *machineScope) field.ErrorList { // verify the subnets configuration // Currently we only allow and support one subnet per VM // We may extend to allow and support more than one subnets per VM, in the future release - if len(mscp.providerSpec.Subnets) == 0 { + if len(mscp.providerSpecValidated.Subnets) == 0 { errList = append(errList, field.Required(fldPath.Child("subnets"), "Missing subnets")) - } else if len(mscp.providerSpec.Subnets) > 1 { + } else if len(mscp.providerSpecValidated.Subnets) > 1 { errMsg = "Currently we only allow and support one subnet per VM, but more than one subnets are configured." - errList = append(errList, field.Invalid(fldPath.Child("subnets"), len(mscp.providerSpec.Subnets), errMsg)) + errList = append(errList, field.Invalid(fldPath.Child("subnets"), len(mscp.providerSpecValidated.Subnets), errMsg)) } else { - subnet := &mscp.providerSpec.Subnets[0] + subnet := &mscp.providerSpecValidated.Subnets[0] if fldErr := validateSubnetConfig(mscp, subnet, fldPath); fldErr != nil { errList = append(errList, fldErr) } } // verify the vcpusPerSocket configuration - if mscp.providerSpec.VCPUsPerSocket < 1 { + if mscp.providerSpecValidated.VCPUsPerSocket < 1 { errMsg = "The minimum number of vCPUs per socket of the VM is 1." - errList = append(errList, field.Invalid(fldPath.Child("vcpusPerSocket"), mscp.providerSpec.VCPUsPerSocket, errMsg)) + errList = append(errList, field.Invalid(fldPath.Child("vcpusPerSocket"), mscp.providerSpecValidated.VCPUsPerSocket, errMsg)) } // verify the vcpuSockets configuration - if mscp.providerSpec.VCPUSockets < 1 { + if mscp.providerSpecValidated.VCPUSockets < 1 { errMsg = "The minimum vCPU sockets of the VM is 1." - errList = append(errList, field.Invalid(fldPath.Child("vcpuSockets"), mscp.providerSpec.VCPUSockets, errMsg)) + errList = append(errList, field.Invalid(fldPath.Child("vcpuSockets"), mscp.providerSpecValidated.VCPUSockets, errMsg)) } // verify the memorySize configuration // The minimum memorySize is 2Gi bytes - memSizeMib := GetMibValueOfQuantity(mscp.providerSpec.MemorySize) + memSizeMib := GetMibValueOfQuantity(mscp.providerSpecValidated.MemorySize) if memSizeMib < 2*1024 { errList = append(errList, field.Invalid(fldPath.Child("memorySize"), fmt.Sprintf("%vMib", memSizeMib), "The minimum memorySize is 2Gi bytes")) } // verify the systemDiskSize configuration // The minimum systemDiskSize is 20Gi bytes - diskSizeMib := GetMibValueOfQuantity(mscp.providerSpec.SystemDiskSize) + diskSizeMib := GetMibValueOfQuantity(mscp.providerSpecValidated.SystemDiskSize) if diskSizeMib < 20*1024 { errList = append(errList, field.Invalid(fldPath.Child("systemDiskSize"), fmt.Sprintf("%vGib", diskSizeMib/1024), "The minimum systemDiskSize is 20Gi bytes")) } + // verify the bootType configurations + // Type bootType field is optional, and valid values include: "", Legacy, UEFI, SecureBoot + switch mscp.providerSpecValidated.BootType { + case "", machinev1.NutanixLegacyBoot, machinev1.NutanixUEFIBoot, machinev1.NutanixSecureBoot: + // valid bootType + default: + errMsg = fmt.Sprintf("Invalid bootType, the valid bootType values are: \"\", %q, %q, %q.", + machinev1.NutanixLegacyBoot, machinev1.NutanixUEFIBoot, machinev1.NutanixSecureBoot) + errList = append(errList, field.Invalid(fldPath.Child("bootType"), mscp.providerSpecValidated.BootType, errMsg)) + } + + // verify the project configuration + if fldErr := validateProjectConfig(mscp, fldPath); fldErr != nil { + errList = append(errList, fldErr) + } + + // verify the categories configuration + if len(mscp.providerSpecValidated.Categories) > 0 { + fldErrs := validateCategoriesConfig(mscp, fldPath) + for _, fldErr := range fldErrs { + errList = append(errList, fldErr) + } + } + return errList } @@ -81,26 +108,26 @@ func validateClusterConfig(mscp *machineScope, fldPath *field.Path) *field.Error var err error var errMsg string - switch mscp.providerSpec.Cluster.Type { + switch mscp.providerSpecValidated.Cluster.Type { case machinev1.NutanixIdentifierName: - if mscp.providerSpec.Cluster.Name == nil || *mscp.providerSpec.Cluster.Name == "" { + if mscp.providerSpecValidated.Cluster.Name == nil || *mscp.providerSpecValidated.Cluster.Name == "" { return field.Required(fldPath.Child("cluster", "name"), "Missing cluster name") } else { - clusterName := *mscp.providerSpec.Cluster.Name + clusterName := *mscp.providerSpecValidated.Cluster.Name clusterRefUuidPtr, err := findClusterUuidByName(mscp.nutanixClient, clusterName) if err != nil { errMsg = fmt.Sprintf("Failed to find cluster with name %q. error: %v", clusterName, err) return field.Invalid(fldPath.Child("cluster", "name"), clusterName, errMsg) } else { - mscp.providerSpec.Cluster.Type = machinev1.NutanixIdentifierUUID - mscp.providerSpec.Cluster.UUID = clusterRefUuidPtr + mscp.providerSpecValidated.Cluster.Type = machinev1.NutanixIdentifierUUID + mscp.providerSpecValidated.Cluster.UUID = clusterRefUuidPtr } } case machinev1.NutanixIdentifierUUID: - if mscp.providerSpec.Cluster.UUID == nil || *mscp.providerSpec.Cluster.UUID == "" { + if mscp.providerSpecValidated.Cluster.UUID == nil || *mscp.providerSpecValidated.Cluster.UUID == "" { return field.Required(fldPath.Child("cluster", "uuid"), "Missing cluster uuid") } else { - clusterUUID := *mscp.providerSpec.Cluster.UUID + clusterUUID := *mscp.providerSpecValidated.Cluster.UUID if _, err = mscp.nutanixClient.V3.GetCluster(clusterUUID); err != nil { errMsg = fmt.Sprintf("Failed to find cluster with uuid %v. error: %v", clusterUUID, err) return field.Invalid(fldPath.Child("cluster", "uuid"), clusterUUID, errMsg) @@ -108,7 +135,7 @@ func validateClusterConfig(mscp *machineScope, fldPath *field.Path) *field.Error } default: errMsg = fmt.Sprintf("Invalid cluster identifier type, valid types are: %q, %q.", machinev1.NutanixIdentifierName, machinev1.NutanixIdentifierUUID) - return field.Invalid(fldPath.Child("cluster", "type"), mscp.providerSpec.Cluster.Type, errMsg) + return field.Invalid(fldPath.Child("cluster", "type"), mscp.providerSpecValidated.Cluster.Type, errMsg) } return nil @@ -118,26 +145,26 @@ func validateImageConfig(mscp *machineScope, fldPath *field.Path) *field.Error { var err error var errMsg string - switch mscp.providerSpec.Image.Type { + switch mscp.providerSpecValidated.Image.Type { case machinev1.NutanixIdentifierName: - if mscp.providerSpec.Image.Name == nil || *mscp.providerSpec.Image.Name == "" { + if mscp.providerSpecValidated.Image.Name == nil || *mscp.providerSpecValidated.Image.Name == "" { return field.Required(fldPath.Child("image", "name"), "Missing image name") } else { - imageName := *mscp.providerSpec.Image.Name + imageName := *mscp.providerSpecValidated.Image.Name imageRefUuidPtr, err := findImageUuidByName(mscp.nutanixClient, imageName) if err != nil { errMsg = fmt.Sprintf("Failed to find image with name %q. error: %v", imageName, err) return field.Invalid(fldPath.Child("image", "name"), imageName, errMsg) } else { - mscp.providerSpec.Image.Type = machinev1.NutanixIdentifierUUID - mscp.providerSpec.Image.UUID = imageRefUuidPtr + mscp.providerSpecValidated.Image.Type = machinev1.NutanixIdentifierUUID + mscp.providerSpecValidated.Image.UUID = imageRefUuidPtr } } case machinev1.NutanixIdentifierUUID: - if mscp.providerSpec.Image.UUID == nil || *mscp.providerSpec.Image.UUID == "" { + if mscp.providerSpecValidated.Image.UUID == nil || *mscp.providerSpecValidated.Image.UUID == "" { return field.Required(fldPath.Child("image", "uuid"), "Missing image uuid") } else { - imageUUID := *mscp.providerSpec.Image.UUID + imageUUID := *mscp.providerSpecValidated.Image.UUID if _, err = mscp.nutanixClient.V3.GetImage(imageUUID); err != nil { errMsg = fmt.Sprintf("Failed to find image with uuid %v. error: %v", imageUUID, err) return field.Invalid(fldPath.Child("image", "uuid"), imageUUID, errMsg) @@ -145,7 +172,7 @@ func validateImageConfig(mscp *machineScope, fldPath *field.Path) *field.Error { } default: errMsg = fmt.Sprintf("Invalid image identifier type, valid types are: %q, %q.", machinev1.NutanixIdentifierName, machinev1.NutanixIdentifierUUID) - return field.Invalid(fldPath.Child("image", "type"), mscp.providerSpec.Image.Type, errMsg) + return field.Invalid(fldPath.Child("image", "type"), mscp.providerSpecValidated.Image.Type, errMsg) } return nil @@ -186,6 +213,58 @@ func validateSubnetConfig(mscp *machineScope, subnet *machinev1.NutanixResourceI return nil } +func validateProjectConfig(mscp *machineScope, fldPath *field.Path) *field.Error { + var err error + var errMsg string + + switch mscp.providerSpecValidated.Project.Type { + case "": + // ignore if not configured + return nil + case machinev1.NutanixIdentifierName: + if mscp.providerSpecValidated.Project.Name == nil || *mscp.providerSpecValidated.Project.Name == "" { + return field.Required(fldPath.Child("project", "name"), "Missing projct name") + } else { + projectName := *mscp.providerSpecValidated.Project.Name + projectRefUuidPtr, err := findProjectUuidByName(mscp.nutanixClient, projectName) + if err != nil { + errMsg = fmt.Sprintf("Failed to find project with name %q. error: %v", projectName, err) + return field.Invalid(fldPath.Child("project", "name"), projectName, errMsg) + } else { + mscp.providerSpecValidated.Project.Type = machinev1.NutanixIdentifierUUID + mscp.providerSpecValidated.Project.UUID = projectRefUuidPtr + } + } + case machinev1.NutanixIdentifierUUID: + if mscp.providerSpecValidated.Project.UUID == nil || *mscp.providerSpecValidated.Project.UUID == "" { + return field.Required(fldPath.Child("project", "uuid"), "Missing project uuid") + } else { + projectUUID := *mscp.providerSpecValidated.Project.UUID + if _, err = mscp.nutanixClient.V3.GetProject(projectUUID); err != nil { + errMsg = fmt.Sprintf("Failed to find project with uuid %v. error: %v", projectUUID, err) + return field.Invalid(fldPath.Child("project", "uuid"), projectUUID, errMsg) + } + } + default: + errMsg = fmt.Sprintf("Invalid project identifier type, valid types are: %q, %q.", machinev1.NutanixIdentifierName, machinev1.NutanixIdentifierUUID) + return field.Invalid(fldPath.Child("project", "type"), mscp.providerSpecValidated.Project.Type, errMsg) + } + + return nil +} + +func validateCategoriesConfig(mscp *machineScope, fldPath *field.Path) (fldErrs []*field.Error) { + for _, category := range mscp.providerSpecValidated.Categories { + _, err := mscp.nutanixClient.V3.GetCategoryValue(category.Key, category.Value) + if err != nil { + errMsg := fmt.Sprintf("Failed to find the category with key %q and value %q. error: %v", category.Key, category.Value, err) + fldErrs = append(fldErrs, field.Invalid(fldPath.Child("categories"), category, errMsg)) + } + } + + return fldErrs +} + // CreateVM creates a VM and is invoked by the NutanixMachineReconciler func createVM(mscp *machineScope, userData []byte) (*nutanixClientV3.VMIntentResponse, error) { @@ -209,12 +288,12 @@ func createVM(mscp *machineScope, userData []byte) (*nutanixClientV3.VMIntentRes userdataEncoded := base64.StdEncoding.EncodeToString(userData) // Create the VM - klog.V(5).Infof("To create VM with name %q, and providerSpec: %+v", vmName, *mscp.providerSpec) + klog.V(5).Infof("To create VM with name %q, and providerSpec: %+v", vmName, *mscp.providerSpecValidated) vmInput := nutanixClientV3.VMIntentInput{} vmSpec := nutanixClientV3.VM{Name: utils.StringPtr(vmName)} nicList := []*nutanixClientV3.VMNic{} - for _, subnet := range mscp.providerSpec.Subnets { + for _, subnet := range mscp.providerSpecValidated.Subnets { vmNic := &nutanixClientV3.VMNic{ SubnetReference: &nutanixClientV3.Reference{ Kind: utils.StringPtr("subnet"), @@ -228,9 +307,9 @@ func createVM(mscp *machineScope, userData []byte) (*nutanixClientV3.VMIntentRes diskList = append(diskList, &nutanixClientV3.VMDisk{ DataSourceReference: &nutanixClientV3.Reference{ Kind: utils.StringPtr("image"), - UUID: mscp.providerSpec.Image.UUID, + UUID: mscp.providerSpecValidated.Image.UUID, }, - DiskSizeMib: utils.Int64Ptr(GetMibValueOfQuantity(mscp.providerSpec.SystemDiskSize)), + DiskSizeMib: utils.Int64Ptr(GetMibValueOfQuantity(mscp.providerSpecValidated.SystemDiskSize)), }) vmMetadata := nutanixClientV3.Metadata{ @@ -238,20 +317,27 @@ func createVM(mscp *machineScope, userData []byte) (*nutanixClientV3.VMIntentRes SpecVersion: utils.Int64Ptr(1), } - // Add the category for installer clueanup the VM at cluster torn-down time - // if the category exists in PC - if err := addCategory(mscp, &vmMetadata); err != nil { - klog.Warningf("Failed to add category to the vm %q. %v", vmName, err) + // Add the projectReference if project is configured + if mscp.providerSpecValidated.Project.Type == machinev1.NutanixIdentifierUUID { + vmMetadata.ProjectReference = &nutanixClientV3.Reference{ + Kind: utils.StringPtr("project"), + UUID: mscp.providerSpecValidated.Project.UUID, + } + } + + // Add the categories + if err := addCategories(mscp, &vmMetadata); err != nil { + klog.Warningf("Failed to add categories to the vm %q. %v", vmName, err) } else { - klog.Infof("%s: added the category to the vm %q: %v", mscp.machine.Name, vmName, vmMetadata.Categories) + klog.Infof("%s: added the categories to the vm %q: %v", mscp.machine.Name, vmName, vmMetadata.Categories) } vmSpec.Resources = &nutanixClientV3.VMResources{ PowerState: utils.StringPtr("ON"), HardwareClockTimezone: utils.StringPtr("UTC"), - NumVcpusPerSocket: utils.Int64Ptr(int64(mscp.providerSpec.VCPUsPerSocket)), - NumSockets: utils.Int64Ptr(int64(mscp.providerSpec.VCPUSockets)), - MemorySizeMib: utils.Int64Ptr(GetMibValueOfQuantity(mscp.providerSpec.MemorySize)), + NumVcpusPerSocket: utils.Int64Ptr(int64(mscp.providerSpecValidated.VCPUsPerSocket)), + NumSockets: utils.Int64Ptr(int64(mscp.providerSpecValidated.VCPUSockets)), + MemorySizeMib: utils.Int64Ptr(GetMibValueOfQuantity(mscp.providerSpecValidated.MemorySize)), NicList: nicList, DiskList: diskList, GuestCustomization: &nutanixClientV3.GuestCustomization{ @@ -262,7 +348,30 @@ func createVM(mscp *machineScope, userData []byte) (*nutanixClientV3.VMIntentRes // Set cluster/PE reference vmSpec.ClusterReference = &nutanixClientV3.Reference{ Kind: utils.StringPtr("cluster"), - UUID: mscp.providerSpec.Cluster.UUID, + UUID: mscp.providerSpecValidated.Cluster.UUID, + } + + // Set boot_type if configured in the machine's providerSpec + switch mscp.providerSpecValidated.BootType { + case machinev1.NutanixUEFIBoot: + vmSpec.Resources.BootConfig = &nutanixClientV3.VMBootConfig{ + BootType: utils.StringPtr("UEFI"), + } + case machinev1.NutanixSecureBoot: + vmSpec.Resources.BootConfig = &nutanixClientV3.VMBootConfig{ + BootType: utils.StringPtr("SECURE_BOOT"), + } + case machinev1.NutanixLegacyBoot: + bootDeviceOrderList := make([]*string, 0) + bootDeviceOrderList = append(bootDeviceOrderList, utils.StringPtr("CDROM")) + bootDeviceOrderList = append(bootDeviceOrderList, utils.StringPtr("DISK")) + bootDeviceOrderList = append(bootDeviceOrderList, utils.StringPtr("NETWORK")) + vmSpec.Resources.BootConfig = &nutanixClientV3.VMBootConfig{ + BootType: utils.StringPtr("LEGACY"), + BootDeviceOrderList: bootDeviceOrderList, + } + default: + // The vm uses the default boot type } vmInput.Spec = &vmSpec @@ -287,7 +396,7 @@ func createVM(mscp *machineScope, userData []byte) (*nutanixClientV3.VMIntentRes } vm, err = findVMByUUID(mscp.nutanixClient, vmUuid) - for err != nil { + if err != nil { klog.Errorf("Failed to find the vm with UUID %s. %v", vmUuid, err) return nil, err } @@ -380,7 +489,9 @@ func findClusterUuidByName(ntnxclient *nutanixClientV3.Client, clusterName strin } if len(res.Entities) > 1 { - klog.Warningf("Found more than one (%v) clusters with name %q.", len(res.Entities), clusterName) + err = fmt.Errorf("Found more than one (%v) clusters with name %q.", len(res.Entities), clusterName) + klog.Errorf(err.Error()) + return nil, err } return res.Entities[0].Metadata.UUID, nil @@ -401,7 +512,9 @@ func findImageUuidByName(ntnxclient *nutanixClientV3.Client, imageName string) ( } if len(res.Entities) > 1 { - klog.Warningf("Found more than one (%v) images with name %q.", len(res.Entities), imageName) + err = fmt.Errorf("Found more than one (%v) images with name %q.", len(res.Entities), imageName) + klog.Errorf(err.Error()) + return nil, err } return res.Entities[0].Metadata.UUID, nil @@ -422,7 +535,31 @@ func findSubnetUuidByName(ntnxclient *nutanixClientV3.Client, subnetName string) } if len(res.Entities) > 1 { - klog.Warningf("Found more than one (%v) subnets with name %q.", len(res.Entities), subnetName) + err = fmt.Errorf("Found more than one (%v) subnets with name %q.", len(res.Entities), subnetName) + klog.Errorf(err.Error()) + return nil, err + } + + return res.Entities[0].Metadata.UUID, nil +} + +// findProjectUuidByName retrieves the project uuid by the given project name +func findProjectUuidByName(ntnxclient *nutanixClientV3.Client, projectName string) (*string, error) { + klog.Infof("Checking if project with name %q exists.", projectName) + + res, err := ntnxclient.V3.ListProject(&nutanixClientV3.DSMetadata{ + Filter: utils.StringPtr(fmt.Sprintf("name==%s", projectName)), + }) + if err != nil || len(res.Entities) == 0 { + e1 := fmt.Errorf("Failed to find project by name %q. error: %w", projectName, err) + klog.Errorf(e1.Error()) + return nil, e1 + } + + if len(res.Entities) > 1 { + err = fmt.Errorf("Found more than one (%v) projects with name %q.", len(res.Entities), projectName) + klog.Errorf(err.Error()) + return nil, err } return res.Entities[0].Metadata.UUID, nil diff --git a/pkg/actuators/machineset/controller_test.go b/pkg/actuators/machineset/controller_test.go index 12d831c1..ca5cc4c0 100644 --- a/pkg/actuators/machineset/controller_test.go +++ b/pkg/actuators/machineset/controller_test.go @@ -20,8 +20,7 @@ import ( "fmt" "testing" - . "github.com/onsi/ginkgo" - . "github.com/onsi/ginkgo/extensions/table" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" gtypes "github.com/onsi/gomega/types" machinev1 "github.com/openshift/api/machine/v1" diff --git a/pkg/actuators/machineset/suite_test.go b/pkg/actuators/machineset/suite_test.go index baeeeac2..6a461c8d 100644 --- a/pkg/actuators/machineset/suite_test.go +++ b/pkg/actuators/machineset/suite_test.go @@ -21,7 +21,7 @@ import ( "testing" "time" - . "github.com/onsi/ginkgo" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" machinev1 "github.com/openshift/api/machine/v1beta1" "k8s.io/client-go/kubernetes/scheme" @@ -71,9 +71,7 @@ var _ = BeforeSuite(func(done Done) { cfg, err = testEnv.Start() Expect(err).ToNot(HaveOccurred()) Expect(cfg).ToNot(BeNil()) - - close(done) -}, 60) +}) var _ = AfterSuite(func() { By("tearing down the test environment") diff --git a/pkg/client/state.go b/pkg/client/state.go index 6055e29e..f2ee6b3f 100644 --- a/pkg/client/state.go +++ b/pkg/client/state.go @@ -5,7 +5,7 @@ import ( "math" "time" - "k8s.io/klog" + "k8s.io/klog/v2" nutanixClientV3 "github.com/nutanix-cloud-native/prism-go-client/pkg/nutanix/v3" )