-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathmachines.go
129 lines (117 loc) · 4.32 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Package gcp generates Machine objects for gcp.
package gcp
import (
"fmt"
gcpprovider "github.com/openshift/cluster-api-provider-gcp/pkg/apis/gcpprovider/v1beta1"
machineapi "github.com/openshift/cluster-api/pkg/apis/machine/v1beta1"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
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/gcp"
)
// Machines returns a list of machines for a machinepool.
func Machines(clusterID string, config *types.InstallConfig, pool *types.MachinePool, osImage, role, userDataSecret string) ([]machineapi.Machine, error) {
if configPlatform := config.Platform.Name(); configPlatform != gcp.Name {
return nil, fmt.Errorf("non-GCP configuration: %q", configPlatform)
}
if poolPlatform := pool.Platform.Name(); poolPlatform != gcp.Name {
return nil, fmt.Errorf("non-GCP machine-pool: %q", poolPlatform)
}
platform := config.Platform.GCP
mpool := pool.Platform.GCP
azs := mpool.Zones
total := int64(1)
if pool.Replicas != nil {
total = *pool.Replicas
}
var machines []machineapi.Machine
for idx := int64(0); idx < total; idx++ {
azIndex := int(idx) % len(azs)
provider, err := provider(clusterID, platform, mpool, osImage, azIndex, role, userDataSecret)
if err != nil {
return nil, errors.Wrap(err, "failed to create provider")
}
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[:1], 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 operators.
},
}
machines = append(machines, machine)
}
return machines, nil
}
func provider(clusterID string, platform *gcp.Platform, mpool *gcp.MachinePool, osImage string, azIdx int, role, userDataSecret string) (*gcpprovider.GCPMachineProviderSpec, error) {
az := mpool.Zones[azIdx]
network, subnetwork, err := getNetworks(platform, clusterID, role)
if err != nil {
return nil, err
}
return &gcpprovider.GCPMachineProviderSpec{
TypeMeta: metav1.TypeMeta{
APIVersion: "gcpprovider.openshift.io/v1beta1",
Kind: "GCPMachineProviderSpec",
},
UserDataSecret: &corev1.LocalObjectReference{Name: userDataSecret},
CredentialsSecret: &corev1.LocalObjectReference{Name: "gcp-cloud-credentials"},
Disks: []*gcpprovider.GCPDisk{{
AutoDelete: true,
Boot: true,
SizeGb: 128,
Type: "pd-ssd",
Image: fmt.Sprintf("%s-rhcos-image", clusterID),
}},
NetworkInterfaces: []*gcpprovider.GCPNetworkInterface{{
Network: network,
Subnetwork: subnetwork,
}},
ServiceAccounts: []gcpprovider.GCPServiceAccount{{
Email: fmt.Sprintf("%s-%s@%s.iam.gserviceaccount.com", clusterID, role[0:1], platform.ProjectID),
Scopes: []string{"https://www.googleapis.com/auth/cloud-platform"},
}},
Tags: []string{fmt.Sprintf("%s-%s", clusterID, role)},
MachineType: mpool.InstanceType,
Region: platform.Region,
Zone: az,
ProjectID: platform.ProjectID,
}, nil
}
// ConfigMasters assigns a set of load balancers to the given machines
func ConfigMasters(machines []machineapi.Machine, clusterID string) {
for _, machine := range machines {
providerSpec := machine.Spec.ProviderSpec.Value.Object.(*gcpprovider.GCPMachineProviderSpec)
providerSpec.TargetPools = []string{
fmt.Sprintf("%s-api", clusterID),
}
}
}
func getNetworks(platform *gcp.Platform, clusterID, role string) (string, string, error) {
if platform.Network == "" {
return fmt.Sprintf("%s-network", clusterID), fmt.Sprintf("%s-%s-subnet", clusterID, role), nil
}
switch role {
case "worker":
return platform.Network, platform.ComputeSubnet, nil
case "master":
return platform.Network, platform.ControlPlaneSubnet, nil
default:
return "", "", fmt.Errorf("unrecognized machine role %s", role)
}
}