|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/pkg/errors" |
| 9 | + corev1 "k8s.io/api/core/v1" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/utils/ptr" |
| 12 | + capa "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" |
| 13 | + |
| 14 | + "github.com/openshift/installer/pkg/asset" |
| 15 | + "github.com/openshift/installer/pkg/asset/installconfig" |
| 16 | + "github.com/openshift/installer/pkg/asset/manifests/capiutils" |
| 17 | +) |
| 18 | + |
| 19 | +// GenerateClusterAssets generates the manifests for the cluster-api. |
| 20 | +func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID *installconfig.ClusterID) (*capiutils.GenerateClusterAssetsOutput, error) { |
| 21 | + manifests := []*asset.RuntimeFile{} |
| 22 | + mainCIDR := capiutils.CIDRFromInstallConfig(installConfig) |
| 23 | + |
| 24 | + zones, err := installConfig.AWS.AvailabilityZones(context.TODO()) |
| 25 | + if err != nil { |
| 26 | + return nil, errors.Wrap(err, "failed to get availability zones") |
| 27 | + } |
| 28 | + |
| 29 | + awsCluster := &capa.AWSCluster{ |
| 30 | + ObjectMeta: metav1.ObjectMeta{ |
| 31 | + Name: clusterID.InfraID, |
| 32 | + Namespace: capiutils.Namespace, |
| 33 | + }, |
| 34 | + Spec: capa.AWSClusterSpec{ |
| 35 | + Region: installConfig.Config.AWS.Region, |
| 36 | + NetworkSpec: capa.NetworkSpec{ |
| 37 | + VPC: capa.VPCSpec{ |
| 38 | + CidrBlock: mainCIDR.String(), |
| 39 | + AvailabilityZoneUsageLimit: ptr.To(len(zones)), |
| 40 | + AvailabilityZoneSelection: &capa.AZSelectionSchemeOrdered, |
| 41 | + }, |
| 42 | + CNI: &capa.CNISpec{ |
| 43 | + CNIIngressRules: capa.CNIIngressRules{ |
| 44 | + { |
| 45 | + Description: "ICMP", |
| 46 | + Protocol: capa.SecurityGroupProtocolICMP, |
| 47 | + FromPort: -1, |
| 48 | + ToPort: -1, |
| 49 | + }, |
| 50 | + { |
| 51 | + Description: "Port 22 (TCP)", |
| 52 | + Protocol: capa.SecurityGroupProtocolTCP, |
| 53 | + FromPort: 22, |
| 54 | + ToPort: 22, |
| 55 | + }, |
| 56 | + { |
| 57 | + Description: "Port 4789 (UDP) for VXLAN", |
| 58 | + Protocol: capa.SecurityGroupProtocolUDP, |
| 59 | + FromPort: 4789, |
| 60 | + ToPort: 4789, |
| 61 | + }, |
| 62 | + { |
| 63 | + Description: "Port 6081 (UDP) for geneve", |
| 64 | + Protocol: capa.SecurityGroupProtocolUDP, |
| 65 | + FromPort: 6081, |
| 66 | + ToPort: 6081, |
| 67 | + }, |
| 68 | + { |
| 69 | + Description: "Port 500 (UDP) for IKE", |
| 70 | + Protocol: capa.SecurityGroupProtocolUDP, |
| 71 | + FromPort: 500, |
| 72 | + ToPort: 500, |
| 73 | + }, |
| 74 | + { |
| 75 | + Description: "Port 4500 (UDP) for IKE NAT", |
| 76 | + Protocol: capa.SecurityGroupProtocolUDP, |
| 77 | + FromPort: 4500, |
| 78 | + ToPort: 4500, |
| 79 | + }, |
| 80 | + { |
| 81 | + Description: "ESP", |
| 82 | + Protocol: capa.SecurityGroupProtocolESP, |
| 83 | + FromPort: -1, |
| 84 | + ToPort: -1, |
| 85 | + }, |
| 86 | + { |
| 87 | + Description: "Port 6441-6442 (TCP) for ovndb", |
| 88 | + Protocol: capa.SecurityGroupProtocolTCP, |
| 89 | + FromPort: 6441, |
| 90 | + ToPort: 6442, |
| 91 | + }, |
| 92 | + { |
| 93 | + Description: "Port 9000-9999 for node ports (TCP)", |
| 94 | + Protocol: capa.SecurityGroupProtocolTCP, |
| 95 | + FromPort: 9000, |
| 96 | + ToPort: 9999, |
| 97 | + }, |
| 98 | + { |
| 99 | + Description: "Port 9000-9999 for node ports (UDP)", |
| 100 | + Protocol: capa.SecurityGroupProtocolUDP, |
| 101 | + FromPort: 9000, |
| 102 | + ToPort: 9999, |
| 103 | + }, |
| 104 | + { |
| 105 | + Description: "Service node ports (TCP)", |
| 106 | + Protocol: capa.SecurityGroupProtocolTCP, |
| 107 | + FromPort: 30000, |
| 108 | + ToPort: 32767, |
| 109 | + }, |
| 110 | + { |
| 111 | + Description: "Service node ports (UDP)", |
| 112 | + Protocol: capa.SecurityGroupProtocolUDP, |
| 113 | + FromPort: 30000, |
| 114 | + ToPort: 32767, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + AdditionalControlPlaneIngressRules: []capa.IngressRule{ |
| 119 | + { |
| 120 | + Description: "MCS traffic from cluster network", |
| 121 | + Protocol: capa.SecurityGroupProtocolTCP, |
| 122 | + FromPort: 22623, |
| 123 | + ToPort: 22623, |
| 124 | + SourceSecurityGroupRoles: []capa.SecurityGroupRole{"node", "controlplane"}, |
| 125 | + }, |
| 126 | + { |
| 127 | + Description: "controller-manager", |
| 128 | + Protocol: capa.SecurityGroupProtocolTCP, |
| 129 | + FromPort: 10257, |
| 130 | + ToPort: 10257, |
| 131 | + SourceSecurityGroupRoles: []capa.SecurityGroupRole{"controlplane", "node"}, |
| 132 | + }, |
| 133 | + { |
| 134 | + Description: "kube-scheduler", |
| 135 | + Protocol: capa.SecurityGroupProtocolTCP, |
| 136 | + FromPort: 10259, |
| 137 | + ToPort: 10259, |
| 138 | + SourceSecurityGroupRoles: []capa.SecurityGroupRole{"controlplane", "node"}, |
| 139 | + }, |
| 140 | + { |
| 141 | + Description: "SSH everyone", |
| 142 | + Protocol: capa.SecurityGroupProtocolTCP, |
| 143 | + FromPort: 22, |
| 144 | + ToPort: 22, |
| 145 | + CidrBlocks: []string{"0.0.0.0/0"}, |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + S3Bucket: &capa.S3Bucket{ |
| 150 | + Name: fmt.Sprintf("openshift-bootstrap-data-%s", clusterID.InfraID), |
| 151 | + PresignedURLDuration: &metav1.Duration{Duration: 1 * time.Hour}, |
| 152 | + }, |
| 153 | + ControlPlaneLoadBalancer: &capa.AWSLoadBalancerSpec{ |
| 154 | + Name: ptr.To(clusterID.InfraID + "-ext"), |
| 155 | + LoadBalancerType: capa.LoadBalancerTypeNLB, |
| 156 | + Scheme: &capa.ELBSchemeInternetFacing, |
| 157 | + AdditionalListeners: []capa.AdditionalListenerSpec{ |
| 158 | + { |
| 159 | + Port: 22623, |
| 160 | + Protocol: capa.ELBProtocolTCP, |
| 161 | + }, |
| 162 | + }, |
| 163 | + }, |
| 164 | + }, |
| 165 | + } |
| 166 | + |
| 167 | + // If the install config has subnets, use them. |
| 168 | + if len(installConfig.AWS.Subnets) > 0 { |
| 169 | + privateSubnets, err := installConfig.AWS.PrivateSubnets(context.TODO()) |
| 170 | + if err != nil { |
| 171 | + return nil, errors.Wrap(err, "failed to get private subnets") |
| 172 | + } |
| 173 | + for _, subnet := range privateSubnets { |
| 174 | + awsCluster.Spec.NetworkSpec.Subnets = append(awsCluster.Spec.NetworkSpec.Subnets, capa.SubnetSpec{ |
| 175 | + ID: subnet.ID, |
| 176 | + CidrBlock: subnet.CIDR, |
| 177 | + AvailabilityZone: subnet.Zone.Name, |
| 178 | + IsPublic: subnet.Public, |
| 179 | + }) |
| 180 | + } |
| 181 | + publicSubnets, err := installConfig.AWS.PublicSubnets(context.TODO()) |
| 182 | + if err != nil { |
| 183 | + return nil, errors.Wrap(err, "failed to get public subnets") |
| 184 | + } |
| 185 | + |
| 186 | + for _, subnet := range publicSubnets { |
| 187 | + awsCluster.Spec.NetworkSpec.Subnets = append(awsCluster.Spec.NetworkSpec.Subnets, capa.SubnetSpec{ |
| 188 | + ID: subnet.ID, |
| 189 | + CidrBlock: subnet.CIDR, |
| 190 | + AvailabilityZone: subnet.Zone.Name, |
| 191 | + IsPublic: subnet.Public, |
| 192 | + }) |
| 193 | + } |
| 194 | + |
| 195 | + vpc, err := installConfig.AWS.VPC(context.TODO()) |
| 196 | + if err != nil { |
| 197 | + return nil, errors.Wrap(err, "failed to get VPC") |
| 198 | + } |
| 199 | + awsCluster.Spec.NetworkSpec.VPC = capa.VPCSpec{ |
| 200 | + ID: vpc, |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + manifests = append(manifests, &asset.RuntimeFile{ |
| 205 | + Object: awsCluster, |
| 206 | + File: asset.File{Filename: "02_infra-cluster.yaml"}, |
| 207 | + }) |
| 208 | + |
| 209 | + id := &capa.AWSClusterControllerIdentity{ |
| 210 | + ObjectMeta: metav1.ObjectMeta{ |
| 211 | + Name: "default", |
| 212 | + Namespace: capiutils.Namespace, |
| 213 | + }, |
| 214 | + Spec: capa.AWSClusterControllerIdentitySpec{ |
| 215 | + AWSClusterIdentitySpec: capa.AWSClusterIdentitySpec{ |
| 216 | + AllowedNamespaces: &capa.AllowedNamespaces{}, // Allow all namespaces. |
| 217 | + }, |
| 218 | + }, |
| 219 | + } |
| 220 | + manifests = append(manifests, &asset.RuntimeFile{ |
| 221 | + Object: id, |
| 222 | + File: asset.File{Filename: "01_aws-cluster-controller-identity-default.yaml"}, |
| 223 | + }) |
| 224 | + |
| 225 | + return &capiutils.GenerateClusterAssetsOutput{ |
| 226 | + Manifests: manifests, |
| 227 | + InfrastructureRef: &corev1.ObjectReference{ |
| 228 | + APIVersion: "infrastructure.cluster.x-k8s.io/v1beta2", |
| 229 | + Kind: "AWSCluster", |
| 230 | + Name: awsCluster.Name, |
| 231 | + Namespace: awsCluster.Namespace, |
| 232 | + }, |
| 233 | + }, nil |
| 234 | +} |
0 commit comments