-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathcluster.go
301 lines (285 loc) · 9.88 KB
/
cluster.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package aws
import (
"context"
"fmt"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
capa "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
"github.com/openshift/installer/pkg/asset"
"github.com/openshift/installer/pkg/asset/installconfig"
"github.com/openshift/installer/pkg/asset/machines/aws"
"github.com/openshift/installer/pkg/asset/manifests/capiutils"
awstypes "github.com/openshift/installer/pkg/types/aws"
)
// BootstrapSSHDescription is the description for the
// ingress rule that provides SSH access to the bootstrap node
// & identifies the rule for removal during bootstrap destroy.
const BootstrapSSHDescription = "Bootstrap SSH Access"
// GenerateClusterAssets generates the manifests for the cluster-api.
func GenerateClusterAssets(ic *installconfig.InstallConfig, clusterID *installconfig.ClusterID) (*capiutils.GenerateClusterAssetsOutput, error) {
manifests := []*asset.RuntimeFile{}
tags, err := aws.CapaTagsFromUserTags(clusterID.InfraID, ic.Config.AWS.UserTags)
if err != nil {
return nil, fmt.Errorf("failed to get user tags: %w", err)
}
sshRuleCidr := []string{"0.0.0.0/0"}
if !ic.Config.PublicAPI() {
sshRuleCidr = []string{capiutils.CIDRFromInstallConfig(ic).String()}
}
awsCluster := &capa.AWSCluster{
ObjectMeta: metav1.ObjectMeta{
Name: clusterID.InfraID,
Namespace: capiutils.Namespace,
},
Spec: capa.AWSClusterSpec{
Region: ic.Config.AWS.Region,
NetworkSpec: capa.NetworkSpec{
CNI: &capa.CNISpec{
CNIIngressRules: capa.CNIIngressRules{
{
Description: "ICMP",
Protocol: capa.SecurityGroupProtocolICMP,
FromPort: -1,
ToPort: -1,
},
{
Description: "Port 22 (TCP)",
Protocol: capa.SecurityGroupProtocolTCP,
FromPort: 22,
ToPort: 22,
},
{
Description: "Port 4789 (UDP) for VXLAN",
Protocol: capa.SecurityGroupProtocolUDP,
FromPort: 4789,
ToPort: 4789,
},
{
Description: "Port 6081 (UDP) for geneve",
Protocol: capa.SecurityGroupProtocolUDP,
FromPort: 6081,
ToPort: 6081,
},
{
Description: "Port 500 (UDP) for IKE",
Protocol: capa.SecurityGroupProtocolUDP,
FromPort: 500,
ToPort: 500,
},
{
Description: "Port 4500 (UDP) for IKE NAT",
Protocol: capa.SecurityGroupProtocolUDP,
FromPort: 4500,
ToPort: 4500,
},
{
Description: "ESP",
Protocol: capa.SecurityGroupProtocolESP,
FromPort: -1,
ToPort: -1,
},
{
Description: "Port 6441-6442 (TCP) for ovndb",
Protocol: capa.SecurityGroupProtocolTCP,
FromPort: 6441,
ToPort: 6442,
},
{
Description: "Port 9000-9999 for node ports (TCP)",
Protocol: capa.SecurityGroupProtocolTCP,
FromPort: 9000,
ToPort: 9999,
},
{
Description: "Port 9000-9999 for node ports (UDP)",
Protocol: capa.SecurityGroupProtocolUDP,
FromPort: 9000,
ToPort: 9999,
},
{
Description: "Service node ports (TCP)",
Protocol: capa.SecurityGroupProtocolTCP,
FromPort: 30000,
ToPort: 32767,
},
{
Description: "Service node ports (UDP)",
Protocol: capa.SecurityGroupProtocolUDP,
FromPort: 30000,
ToPort: 32767,
},
},
},
AdditionalControlPlaneIngressRules: []capa.IngressRule{
{
Description: "MCS traffic from cluster network",
Protocol: capa.SecurityGroupProtocolTCP,
FromPort: 22623,
ToPort: 22623,
SourceSecurityGroupRoles: []capa.SecurityGroupRole{"node", "controlplane", "apiserver-lb"},
},
{
Description: "controller-manager",
Protocol: capa.SecurityGroupProtocolTCP,
FromPort: 10257,
ToPort: 10257,
SourceSecurityGroupRoles: []capa.SecurityGroupRole{"controlplane", "node"},
},
{
Description: "kube-scheduler",
Protocol: capa.SecurityGroupProtocolTCP,
FromPort: 10259,
ToPort: 10259,
SourceSecurityGroupRoles: []capa.SecurityGroupRole{"controlplane", "node"},
},
{
Description: BootstrapSSHDescription,
Protocol: capa.SecurityGroupProtocolTCP,
FromPort: 22,
ToPort: 22,
CidrBlocks: sshRuleCidr,
},
},
NodePortIngressRuleCidrBlocks: []string{capiutils.CIDRFromInstallConfig(ic).String()},
},
S3Bucket: &capa.S3Bucket{
Name: GetIgnitionBucketName(clusterID.InfraID),
PresignedURLDuration: &metav1.Duration{Duration: 1 * time.Hour},
BestEffortDeleteObjects: ptr.To(ic.Config.AWS.BestEffortDeleteIgnition),
},
ControlPlaneLoadBalancer: &capa.AWSLoadBalancerSpec{
Name: ptr.To(clusterID.InfraID + "-int"),
LoadBalancerType: capa.LoadBalancerTypeNLB,
Scheme: &capa.ELBSchemeInternal,
CrossZoneLoadBalancing: true,
HealthCheckProtocol: &capa.ELBProtocolHTTPS,
HealthCheck: &capa.TargetGroupHealthCheckAPISpec{
IntervalSeconds: ptr.To[int64](10),
TimeoutSeconds: ptr.To[int64](10),
ThresholdCount: ptr.To[int64](2),
UnhealthyThresholdCount: ptr.To[int64](2),
},
AdditionalListeners: []capa.AdditionalListenerSpec{
{
Port: 22623,
Protocol: capa.ELBProtocolTCP,
HealthCheck: &capa.TargetGroupHealthCheckAdditionalSpec{
Protocol: ptr.To[string](capa.ELBProtocolHTTPS.String()),
Port: ptr.To[string]("22623"),
Path: ptr.To[string]("/healthz"),
IntervalSeconds: ptr.To[int64](10),
TimeoutSeconds: ptr.To[int64](10),
ThresholdCount: ptr.To[int64](2),
UnhealthyThresholdCount: ptr.To[int64](2),
},
},
},
IngressRules: []capa.IngressRule{
{
Description: "Machine Config Server internal traffic from cluster",
Protocol: capa.SecurityGroupProtocolTCP,
FromPort: 22623,
ToPort: 22623,
CidrBlocks: []string{capiutils.CIDRFromInstallConfig(ic).String()},
},
},
},
AdditionalTags: tags,
},
}
awsCluster.SetGroupVersionKind(capa.GroupVersion.WithKind("AWSCluster"))
if ic.Config.PublicAPI() {
awsCluster.Spec.SecondaryControlPlaneLoadBalancer = &capa.AWSLoadBalancerSpec{
Name: ptr.To(clusterID.InfraID + "-ext"),
LoadBalancerType: capa.LoadBalancerTypeNLB,
Scheme: &capa.ELBSchemeInternetFacing,
CrossZoneLoadBalancing: true,
HealthCheckProtocol: &capa.ELBProtocolHTTPS,
HealthCheck: &capa.TargetGroupHealthCheckAPISpec{
IntervalSeconds: ptr.To[int64](10),
TimeoutSeconds: ptr.To[int64](10),
ThresholdCount: ptr.To[int64](2),
UnhealthyThresholdCount: ptr.To[int64](2),
},
IngressRules: []capa.IngressRule{
{
Description: "Kubernetes API Server traffic for public access",
Protocol: capa.SecurityGroupProtocolTCP,
FromPort: 6443,
ToPort: 6443,
CidrBlocks: []string{"0.0.0.0/0"},
},
},
}
} else {
awsCluster.Spec.ControlPlaneLoadBalancer.IngressRules = append(
awsCluster.Spec.ControlPlaneLoadBalancer.IngressRules,
capa.IngressRule{
Description: "Kubernetes API Server traffic",
Protocol: capa.SecurityGroupProtocolTCP,
FromPort: 6443,
ToPort: 6443,
CidrBlocks: []string{"0.0.0.0/0"},
},
)
}
// Set the NetworkSpec.Subnets from VPC and zones (managed)
// or subnets (BYO VPC) based in the install-config.yaml.
err = setSubnets(context.TODO(), &zonesInput{
InstallConfig: ic,
ClusterID: clusterID,
Cluster: awsCluster,
})
if err != nil {
return nil, fmt.Errorf("failed to set cluster zones or subnets: %w", err)
}
// Enable BYO Public IPv4 when defined on install-config.yaml
if len(ic.Config.Platform.AWS.PublicIpv4Pool) > 0 {
awsCluster.Spec.NetworkSpec.VPC.ElasticIPPool = &capa.ElasticIPPool{
PublicIpv4Pool: ptr.To(ic.Config.Platform.AWS.PublicIpv4Pool),
PublicIpv4PoolFallBackOrder: ptr.To(capa.PublicIpv4PoolFallbackOrderAmazonPool),
}
}
if awstypes.IsPublicOnlySubnetsEnabled() {
// If we don't set the subnets for the internal LB, CAPA will try to use private subnets but there aren't any in
// public-only mode.
awsCluster.Spec.ControlPlaneLoadBalancer.Subnets = awsCluster.Spec.NetworkSpec.Subnets.IDs()
}
manifests = append(manifests, &asset.RuntimeFile{
Object: awsCluster,
File: asset.File{Filename: "02_infra-cluster.yaml"},
})
id := &capa.AWSClusterControllerIdentity{
ObjectMeta: metav1.ObjectMeta{
Name: "default",
Namespace: capiutils.Namespace,
},
Spec: capa.AWSClusterControllerIdentitySpec{
AWSClusterIdentitySpec: capa.AWSClusterIdentitySpec{
AllowedNamespaces: &capa.AllowedNamespaces{}, // Allow all namespaces.
},
},
}
id.SetGroupVersionKind(capa.GroupVersion.WithKind("AWSClusterControllerIdentity"))
manifests = append(manifests, &asset.RuntimeFile{
Object: id,
File: asset.File{Filename: "01_aws-cluster-controller-identity-default.yaml"},
})
return &capiutils.GenerateClusterAssetsOutput{
Manifests: manifests,
InfrastructureRefs: []*corev1.ObjectReference{
{
APIVersion: capa.GroupVersion.String(),
Kind: "AWSCluster",
Name: awsCluster.Name,
Namespace: awsCluster.Namespace,
},
},
}, nil
}
// GetIgnitionBucketName returns the name of the bucket for the given cluster.
func GetIgnitionBucketName(infraID string) string {
return fmt.Sprintf("openshift-bootstrap-data-%s", infraID)
}