forked from openshift/installer
/
machines.go
137 lines (124 loc) · 4.56 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
130
131
132
133
134
135
136
137
// Package openstack generates Machine objects for openstack.
package openstack
import (
"fmt"
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"
openstackprovider "sigs.k8s.io/cluster-api-provider-openstack/pkg/apis/openstackproviderconfig/v1alpha1"
"github.com/openshift/installer/pkg/types"
"github.com/openshift/installer/pkg/types/openstack"
)
const (
// TODO(flaper87): We're choosing to hardcode these values to make
// the environment more predictable. We expect there to a secret
// named `openstack-credentials` and a cloud named `openstack` in
// the clouds file stored in this secret.
cloudsSecret = "openstack-credentials"
cloudsSecretNamespace = "kube-system"
// CloudName is a constant containing the name of the cloud used in the internal cloudsSecret
CloudName = "openstack"
)
// 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 != openstack.Name {
return nil, fmt.Errorf("non-OpenStack configuration: %q", configPlatform)
}
if poolPlatform := pool.Platform.Name(); poolPlatform != openstack.Name {
return nil, fmt.Errorf("non-OpenStack machine-pool: %q", poolPlatform)
}
platform := config.Platform.OpenStack
mpool := pool.Platform.OpenStack
total := int64(1)
if pool.Replicas != nil {
total = *pool.Replicas
}
var machines []machineapi.Machine
for idx := int64(0); idx < total; idx++ {
az := ""
trunk := config.Platform.OpenStack.TrunkSupport
provider, err := provider(clusterID, platform, mpool, osImage, az, role, userDataSecret, trunk)
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, 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 *openstack.Platform, mpool *openstack.MachinePool, osImage string, az string, role, userDataSecret string, trunk string) (*openstackprovider.OpenstackProviderSpec, error) {
return &openstackprovider.OpenstackProviderSpec{
TypeMeta: metav1.TypeMeta{
APIVersion: "openstackproviderconfig.k8s.io/v1alpha1",
Kind: "OpenstackProviderSpec",
},
Flavor: mpool.FlavorName,
/*RootVolume: openstackprovider.RootVolume{
VolumeType: pointer.StringPtr(mpool.Type),
Size: pointer.Int64Ptr(int64(mpool.Size)),
},*/
Image: osImage,
CloudName: CloudName,
CloudsSecret: &corev1.SecretReference{Name: cloudsSecret, Namespace: cloudsSecretNamespace},
UserDataSecret: &corev1.SecretReference{Name: userDataSecret},
Networks: []openstackprovider.NetworkParam{
{
Subnets: []openstackprovider.SubnetParam{
{
Filter: openstackprovider.SubnetFilter{
Name: fmt.Sprintf("%s-nodes", clusterID),
Tags: fmt.Sprintf("%s=%s", "openshiftClusterID", clusterID),
},
},
},
},
},
AvailabilityZone: az,
SecurityGroups: []openstackprovider.SecurityGroupParam{
{
Name: fmt.Sprintf("%s-%s", clusterID, role),
},
},
Trunk: trunkSupportBoolean(trunk),
ServerMetadata: map[string]string{
"Name": fmt.Sprintf("%s-%s", clusterID, role),
"openshiftClusterID": clusterID,
},
}, nil
}
func trunkSupportBoolean(trunkSupport string) (result bool) {
if trunkSupport == "1" {
result = true
} else {
result = false
}
return
}
// ConfigMasters sets the PublicIP flag and 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.(*openstackprovider.OpenstackProviderSpec)
}*/
}