forked from openshift/installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
machines.go
80 lines (73 loc) · 2.79 KB
/
machines.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Package libvirt generates Machine objects for libvirt.
package libvirt
import (
"fmt"
libvirtprovider "github.com/openshift/cluster-api-provider-libvirt/pkg/apis/libvirtproviderconfig/v1alpha1"
machineapi "github.com/openshift/cluster-api/pkg/apis/machine/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"github.com/openshift/installer/pkg/types"
"github.com/openshift/installer/pkg/types/libvirt"
)
// Machines returns a list of machines for a machinepool.
func Machines(clusterID string, config *types.InstallConfig, pool *types.MachinePool, role, userDataSecret string) ([]machineapi.Machine, error) {
if configPlatform := config.Platform.Name(); configPlatform != libvirt.Name {
return nil, fmt.Errorf("non-Libvirt configuration: %q", configPlatform)
}
if poolPlatform := pool.Platform.Name(); poolPlatform != libvirt.Name {
return nil, fmt.Errorf("non-Libvirt machine-pool: %q", poolPlatform)
}
platform := config.Platform.Libvirt
total := int64(1)
if pool.Replicas != nil {
total = *pool.Replicas
}
provider := provider(clusterID, config.Networking.MachineCIDR.String(), platform, userDataSecret)
var machines []machineapi.Machine
for idx := int64(0); idx < total; idx++ {
machine := machineapi.Machine{
TypeMeta: metav1.TypeMeta{
APIVersion: "machine.openshift.io/v1beta1",
Kind: "Machine",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "openshift-machine-api",
Name: fmt.Sprintf("%s-%s-%d", clusterID, pool.Name, idx),
Labels: map[string]string{
"machine.openshift.io/cluster-api-cluster": clusterID,
"machine.openshift.io/cluster-api-machine-role": role,
"machine.openshift.io/cluster-api-machine-type": role,
},
},
Spec: machineapi.MachineSpec{
ProviderSpec: machineapi.ProviderSpec{
Value: &runtime.RawExtension{Object: provider},
},
// we don't need to set Versions, because we control those via cluster operators.
},
}
machines = append(machines, machine)
}
return machines, nil
}
func provider(clusterID string, networkInterfaceAddress string, platform *libvirt.Platform, userDataSecret string) *libvirtprovider.LibvirtMachineProviderConfig {
return &libvirtprovider.LibvirtMachineProviderConfig{
TypeMeta: metav1.TypeMeta{
APIVersion: "libvirtproviderconfig.k8s.io/v1alpha1",
Kind: "LibvirtMachineProviderConfig",
},
DomainMemory: 4096,
DomainVcpu: 2,
Ignition: &libvirtprovider.Ignition{
UserDataSecret: userDataSecret,
},
Volume: &libvirtprovider.Volume{
PoolName: "default",
BaseVolumeID: fmt.Sprintf("/var/lib/libvirt/images/%s-base", clusterID),
},
NetworkInterfaceName: clusterID,
NetworkInterfaceAddress: networkInterfaceAddress,
Autostart: false,
URI: platform.URI,
}
}