github.com/openshift/installer@v1.4.17/pkg/asset/machines/ovirt/machines.go (about) 1 // Package ovirt generates Machine objects for ovirt. 2 package ovirt 3 4 import ( 5 "fmt" 6 7 corev1 "k8s.io/api/core/v1" 8 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 9 "k8s.io/apimachinery/pkg/runtime" 10 11 machineapi "github.com/openshift/api/machine/v1beta1" 12 ovirtprovider "github.com/openshift/cluster-api-provider-ovirt/pkg/apis/ovirtprovider/v1beta1" 13 "github.com/openshift/installer/pkg/types" 14 "github.com/openshift/installer/pkg/types/ovirt" 15 ) 16 17 // Machines returns a list of machines for a machinepool. 18 func Machines(clusterID string, config *types.InstallConfig, pool *types.MachinePool, osImage, role, userDataSecret string) ([]machineapi.Machine, error) { 19 if configPlatform := config.Platform.Name(); configPlatform != ovirt.Name { 20 return nil, fmt.Errorf("non-ovirt configuration: %q", configPlatform) 21 } 22 if poolPlatform := pool.Platform.Name(); poolPlatform != ovirt.Name { 23 return nil, fmt.Errorf("non-ovirt machine-pool: %q", poolPlatform) 24 } 25 platform := config.Platform.Ovirt 26 27 total := int64(1) 28 if pool.Replicas != nil { 29 total = *pool.Replicas 30 } 31 provider := provider(platform, pool, userDataSecret, clusterID, osImage) 32 var machines []machineapi.Machine 33 for idx := int64(0); idx < total; idx++ { 34 machine := machineapi.Machine{ 35 TypeMeta: metav1.TypeMeta{ 36 APIVersion: "machine.openshift.io/v1beta1", 37 Kind: "Machine", 38 }, 39 ObjectMeta: metav1.ObjectMeta{ 40 Namespace: "openshift-machine-api", 41 Name: fmt.Sprintf("%s-%s-%d", clusterID, pool.Name, idx), 42 Labels: map[string]string{ 43 "machine.openshift.io/cluster-api-cluster": clusterID, 44 "machine.openshift.io/cluster-api-machine-role": role, 45 "machine.openshift.io/cluster-api-machine-type": role, 46 }, 47 }, 48 Spec: machineapi.MachineSpec{ 49 ProviderSpec: machineapi.ProviderSpec{ 50 Value: &runtime.RawExtension{Object: provider}, 51 }, 52 // we don't need to set Versions, because we control those via cluster operators. 53 }, 54 } 55 machines = append(machines, machine) 56 } 57 58 return machines, nil 59 } 60 61 func provider(platform *ovirt.Platform, pool *types.MachinePool, userDataSecret string, clusterID string, osImage string) *ovirtprovider.OvirtMachineProviderSpec { 62 spec := ovirtprovider.OvirtMachineProviderSpec{ 63 TypeMeta: metav1.TypeMeta{ 64 APIVersion: "ovirtproviderconfig.machine.openshift.io/v1beta1", 65 Kind: "OvirtMachineProviderSpec", 66 }, 67 UserDataSecret: &corev1.LocalObjectReference{Name: userDataSecret}, 68 CredentialsSecret: &corev1.LocalObjectReference{Name: "ovirt-credentials"}, 69 TemplateName: osImage, 70 ClusterId: platform.ClusterID, 71 InstanceTypeId: pool.Platform.Ovirt.InstanceTypeID, 72 MemoryMB: pool.Platform.Ovirt.MemoryMB, 73 VMType: string(pool.Platform.Ovirt.VMType), 74 AutoPinningPolicy: string(pool.Platform.Ovirt.AutoPinningPolicy), 75 Hugepages: int32(pool.Platform.Ovirt.Hugepages), 76 Clone: pool.Platform.Ovirt.Clone, 77 Sparse: pool.Platform.Ovirt.Sparse, 78 Format: pool.Platform.Ovirt.Format, 79 } 80 uniqueNewAG := make(map[string]ovirt.AffinityGroup) 81 for _, ag := range platform.AffinityGroups { 82 uniqueNewAG[ag.Name] = ag 83 } 84 ags := make([]string, len(pool.Platform.Ovirt.AffinityGroupsNames)) 85 for i, agName := range pool.Platform.Ovirt.AffinityGroupsNames { 86 if _, ok := uniqueNewAG[agName]; ok { 87 // add the cluster name only if the affinity group is created by the installer 88 ags[i] = clusterID + "-" + agName 89 } else { 90 ags[i] = agName 91 } 92 } 93 spec.AffinityGroupsNames = ags 94 if pool.Platform.Ovirt.CPU != nil { 95 spec.CPU = &ovirtprovider.CPU{ 96 Cores: pool.Platform.Ovirt.CPU.Cores, 97 Sockets: pool.Platform.Ovirt.CPU.Sockets, 98 Threads: 1, 99 } 100 } 101 if pool.Platform.Ovirt.OSDisk != nil { 102 spec.OSDisk = &ovirtprovider.Disk{ 103 SizeGB: pool.Platform.Ovirt.OSDisk.SizeGB, 104 } 105 } 106 return &spec 107 }