forked from openshift/installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
master.go
165 lines (148 loc) · 4.93 KB
/
master.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package machines
import (
"fmt"
"github.com/ghodss/yaml"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
clusterapi "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
"github.com/openshift/installer/pkg/asset"
"github.com/openshift/installer/pkg/asset/ignition/machine"
"github.com/openshift/installer/pkg/asset/installconfig"
"github.com/openshift/installer/pkg/asset/machines/aws"
"github.com/openshift/installer/pkg/asset/machines/libvirt"
"github.com/openshift/installer/pkg/asset/machines/openstack"
"github.com/openshift/installer/pkg/asset/rhcos"
"github.com/openshift/installer/pkg/types"
awstypes "github.com/openshift/installer/pkg/types/aws"
libvirttypes "github.com/openshift/installer/pkg/types/libvirt"
nonetypes "github.com/openshift/installer/pkg/types/none"
openstacktypes "github.com/openshift/installer/pkg/types/openstack"
)
// Master generates the machines for the `master` machine pool.
type Master struct {
MachinesRaw []byte
UserDataSecretRaw []byte
}
var _ asset.Asset = (*Master)(nil)
// Name returns a human friendly name for the Master Asset.
func (m *Master) Name() string {
return "Master Machines"
}
// Dependencies returns all of the dependencies directly needed by the
// Master asset
func (m *Master) Dependencies() []asset.Asset {
return []asset.Asset{
&installconfig.ClusterID{},
&installconfig.InstallConfig{},
new(rhcos.Image),
&machine.Master{},
}
}
// Generate generates the Master asset.
func (m *Master) Generate(dependencies asset.Parents) error {
clusterID := &installconfig.ClusterID{}
installconfig := &installconfig.InstallConfig{}
rhcosImage := new(rhcos.Image)
mign := &machine.Master{}
dependencies.Get(clusterID, installconfig, rhcosImage, mign)
var err error
userDataMap := map[string][]byte{"master-user-data": mign.File.Data}
m.UserDataSecretRaw, err = userDataList(userDataMap)
if err != nil {
return errors.Wrap(err, "failed to create user-data secret for master machines")
}
ic := installconfig.Config
pool := masterPool(ic.Machines)
switch ic.Platform.Name() {
case awstypes.Name:
mpool := defaultAWSMachinePoolPlatform()
mpool.InstanceType = "m4.xlarge"
mpool.EC2RootVolume.Size = 120
mpool.Set(ic.Platform.AWS.DefaultMachinePlatform)
mpool.Set(pool.Platform.AWS)
if len(mpool.Zones) == 0 {
azs, err := aws.AvailabilityZones(ic.Platform.AWS.Region)
if err != nil {
return errors.Wrap(err, "failed to fetch availability zones")
}
mpool.Zones = azs
}
pool.Platform.AWS = &mpool
machines, err := aws.Machines(clusterID.ClusterID, ic, &pool, string(*rhcosImage), "master", "master-user-data")
if err != nil {
return errors.Wrap(err, "failed to create master machine objects")
}
aws.ConfigMasters(machines, ic.ObjectMeta.Name)
list := listFromMachines(machines)
raw, err := yaml.Marshal(list)
if err != nil {
return errors.Wrap(err, "failed to marshal")
}
m.MachinesRaw = raw
case libvirttypes.Name:
mpool := defaultLibvirtMachinePoolPlatform()
mpool.Set(ic.Platform.Libvirt.DefaultMachinePlatform)
mpool.Set(pool.Platform.Libvirt)
pool.Platform.Libvirt = &mpool
machines, err := libvirt.Machines(clusterID.ClusterID, ic, &pool, "master", "master-user-data")
if err != nil {
return errors.Wrap(err, "failed to create master machine objects")
}
list := listFromMachines(machines)
raw, err := yaml.Marshal(list)
if err != nil {
return errors.Wrap(err, "failed to marshal")
}
m.MachinesRaw = raw
case nonetypes.Name:
case openstacktypes.Name:
numOfMasters := int64(0)
if pool.Replicas != nil {
numOfMasters = *pool.Replicas
}
instances := []string{}
for i := 0; i < int(numOfMasters); i++ {
instances = append(instances, fmt.Sprintf("master-%d", i))
}
config := openstack.MasterConfig{
CloudName: ic.Platform.OpenStack.Cloud,
ClusterName: ic.ObjectMeta.Name,
Instances: instances,
Image: string(*rhcosImage),
Region: ic.Platform.OpenStack.Region,
Machine: defaultOpenStackMachinePoolPlatform(ic.Platform.OpenStack.FlavorName),
Trunk: trunkSupportBoolean(ic.Platform.OpenStack.TrunkSupport),
}
tags := map[string]string{
"openshiftClusterID": clusterID.ClusterID,
}
config.Tags = tags
config.Machine.Set(ic.Platform.OpenStack.DefaultMachinePlatform)
config.Machine.Set(pool.Platform.OpenStack)
m.MachinesRaw = applyTemplateData(openstack.MasterMachinesTmpl, config)
default:
return fmt.Errorf("invalid Platform")
}
return nil
}
func masterPool(pools []types.MachinePool) types.MachinePool {
for idx, pool := range pools {
if pool.Name == "master" {
return pools[idx]
}
}
return types.MachinePool{}
}
func listFromMachines(objs []clusterapi.Machine) *metav1.List {
list := &metav1.List{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "List",
},
}
for idx := range objs {
list.Items = append(list.Items, runtime.RawExtension{Object: &objs[idx]})
}
return list
}