github.com/openshift/installer@v1.4.17/pkg/asset/machines/nutanix/machinesets.go (about) 1 // Package nutanix generates Machine objects for nutanix.package nutanix 2 package nutanix 3 4 import ( 5 "fmt" 6 7 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 8 "k8s.io/apimachinery/pkg/runtime" 9 "k8s.io/utils/ptr" 10 11 machineapi "github.com/openshift/api/machine/v1beta1" 12 "github.com/openshift/installer/pkg/types" 13 "github.com/openshift/installer/pkg/types/nutanix" 14 ) 15 16 // MachineSets returns a list of machinesets for a machinepool. 17 func MachineSets(clusterID string, config *types.InstallConfig, pool *types.MachinePool, osImage, role, userDataSecret string) ([]*machineapi.MachineSet, error) { 18 if configPlatform := config.Platform.Name(); configPlatform != nutanix.Name { 19 return nil, fmt.Errorf("non nutanix configuration: %q", configPlatform) 20 } 21 if poolPlatform := pool.Platform.Name(); poolPlatform != nutanix.Name { 22 return nil, fmt.Errorf("non-nutanix machine-pool: %q", poolPlatform) 23 } 24 25 platform := config.Platform.Nutanix 26 mpool := pool.Platform.Nutanix 27 total := int32(0) 28 if pool.Replicas != nil { 29 total = int32(*pool.Replicas) 30 } 31 32 var machinesets []*machineapi.MachineSet 33 numOfFDs := int32(len(mpool.FailureDomains)) 34 numOfMachineSets := numOfFDs 35 if numOfMachineSets == 0 { 36 numOfMachineSets = 1 37 } else if numOfMachineSets > total { 38 numOfMachineSets = total 39 } 40 fdName2ReplicasMap := make(map[string]int32, numOfMachineSets) 41 fdName2FDsMap := make(map[string]*nutanix.FailureDomain, numOfFDs) 42 var fdName string 43 var idx, replica int32 44 45 if numOfFDs == 0 { 46 fdName2ReplicasMap[""] = total 47 } else { 48 // When failure domains is configured for the workers, evenly distribute 49 // the machineset replicas to the failure domains, based on order. 50 for _, fdName = range mpool.FailureDomains { 51 fd, err := platform.GetFailureDomainByName(fdName) 52 if err != nil { 53 return nil, err 54 } 55 fdName2FDsMap[fdName] = fd 56 } 57 58 for i := int32(0); i < total; i++ { 59 idx = i % numOfFDs 60 fdName = mpool.FailureDomains[idx] 61 replica = 1 62 if ra, ok := fdName2ReplicasMap[fdName]; ok { 63 replica = ra + 1 64 } 65 fdName2ReplicasMap[fdName] = replica 66 } 67 } 68 69 idx = 0 70 for fdName, replica = range fdName2ReplicasMap { 71 name := fmt.Sprintf("%s-%s", clusterID, pool.Name) 72 73 var failureDomain *nutanix.FailureDomain 74 if fdName != "" { 75 failureDomain = fdName2FDsMap[fdName] 76 name = fmt.Sprintf("%s-%v", name, idx) 77 } 78 79 provider, err := provider(clusterID, platform, mpool, osImage, userDataSecret, failureDomain) 80 if err != nil { 81 return nil, fmt.Errorf("failed to create provider: %w", err) 82 } 83 84 mset := &machineapi.MachineSet{ 85 TypeMeta: metav1.TypeMeta{ 86 APIVersion: "machine.openshift.io/v1beta1", 87 Kind: "MachineSet", 88 }, 89 ObjectMeta: metav1.ObjectMeta{ 90 Namespace: "openshift-machine-api", 91 Name: name, 92 Labels: map[string]string{ 93 "machine.openshift.io/cluster-api-cluster": clusterID, 94 }, 95 }, 96 Spec: machineapi.MachineSetSpec{ 97 Replicas: ptr.To[int32](replica), 98 Selector: metav1.LabelSelector{ 99 MatchLabels: map[string]string{ 100 "machine.openshift.io/cluster-api-machineset": name, 101 "machine.openshift.io/cluster-api-cluster": clusterID, 102 }, 103 }, 104 Template: machineapi.MachineTemplateSpec{ 105 ObjectMeta: machineapi.ObjectMeta{ 106 Labels: map[string]string{ 107 "machine.openshift.io/cluster-api-machineset": name, 108 "machine.openshift.io/cluster-api-cluster": clusterID, 109 "machine.openshift.io/cluster-api-machine-role": role, 110 "machine.openshift.io/cluster-api-machine-type": role, 111 }, 112 }, 113 Spec: machineapi.MachineSpec{ 114 ProviderSpec: machineapi.ProviderSpec{ 115 Value: &runtime.RawExtension{Object: provider}, 116 }, 117 // we don't need to set Versions, because we control those via cluster operators. 118 }, 119 }, 120 }, 121 } 122 machinesets = append(machinesets, mset) 123 idx++ 124 } 125 126 return machinesets, nil 127 }