Skip to content

Commit 8a80457

Browse files
committed
capi/aws: reuse GenerateMachines for bootstrap
Instead of duplicating most of the code.
1 parent 6489222 commit 8a80457

File tree

2 files changed

+47
-95
lines changed

2 files changed

+47
-95
lines changed

pkg/asset/machines/aws/awsmachines.go

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
capi "sigs.k8s.io/cluster-api/api/v1beta1"
1313

1414
"github.com/openshift/installer/pkg/asset"
15+
"github.com/openshift/installer/pkg/asset/manifests/capiutils"
1516
"github.com/openshift/installer/pkg/types"
1617
"github.com/openshift/installer/pkg/types/aws"
1718
)
@@ -21,7 +22,7 @@ type MachineInput struct {
2122
Role string
2223
Pool *types.MachinePool
2324
Subnets map[string]string
24-
Tags map[string]string
25+
Tags capa.Tags
2526
PublicIP bool
2627
}
2728

@@ -37,29 +38,28 @@ func GenerateMachines(clusterID string, in *MachineInput) ([]*asset.RuntimeFile,
3738
total = *in.Pool.Replicas
3839
}
3940

40-
tags, err := CapaTagsFromUserTags(clusterID, in.Tags)
41-
if err != nil {
42-
return nil, fmt.Errorf("failed to create CAPA tags from UserTags: %w", err)
43-
}
44-
4541
var result []*asset.RuntimeFile
4642

4743
for idx := int64(0); idx < total; idx++ {
48-
zone := mpool.Zones[int(idx)%len(mpool.Zones)]
49-
subnetID, ok := in.Subnets[zone]
50-
if len(in.Subnets) > 0 && !ok {
51-
return nil, fmt.Errorf("no subnet for zone %s", zone)
52-
}
53-
subnet := &capa.AWSResourceReference{}
54-
if subnetID == "" {
55-
subnet.Filters = []capa.Filter{
56-
{
57-
Name: "tag:Name",
58-
Values: []string{fmt.Sprintf("%s-subnet-private-%s", clusterID, zone)},
59-
},
44+
var subnet *capa.AWSResourceReference
45+
// By not setting subnets for the machine, we let CAPA choose one for us
46+
if len(in.Subnets) > 0 {
47+
zone := mpool.Zones[int(idx)%len(mpool.Zones)]
48+
subnetID, ok := in.Subnets[zone]
49+
if len(in.Subnets) > 0 && !ok {
50+
return nil, fmt.Errorf("no subnet for zone %s", zone)
51+
}
52+
subnet = &capa.AWSResourceReference{}
53+
if subnetID == "" {
54+
subnet.Filters = []capa.Filter{
55+
{
56+
Name: "tag:Name",
57+
Values: []string{fmt.Sprintf("%s-subnet-private-%s", clusterID, zone)},
58+
},
59+
}
60+
} else {
61+
subnet.ID = ptr.To(subnetID)
6062
}
61-
} else {
62-
subnet.ID = ptr.To(subnetID)
6363
}
6464

6565
awsMachine := &capa.AWSMachine{
@@ -77,7 +77,7 @@ func GenerateMachines(clusterID string, in *MachineInput) ([]*asset.RuntimeFile,
7777
SSHKeyName: ptr.To(""),
7878
IAMInstanceProfile: fmt.Sprintf("%s-master-profile", clusterID),
7979
Subnet: subnet,
80-
AdditionalTags: tags,
80+
AdditionalTags: in.Tags,
8181
RootVolume: &capa.Volume{
8282
Size: int64(mpool.EC2RootVolume.Size),
8383
Type: capa.VolumeType(mpool.EC2RootVolume.Type),
@@ -89,6 +89,11 @@ func GenerateMachines(clusterID string, in *MachineInput) ([]*asset.RuntimeFile,
8989
}
9090
awsMachine.SetGroupVersionKind(capa.GroupVersion.WithKind("AWSMachine"))
9191

92+
if in.Role == "bootstrap" {
93+
awsMachine.Name = capiutils.GenerateBoostrapMachineName(clusterID)
94+
awsMachine.Labels["install.openshift.io/bootstrap"] = ""
95+
}
96+
9297
// Handle additional security groups.
9398
for _, sg := range mpool.AdditionalSecurityGroupIDs {
9499
awsMachine.Spec.AdditionalSecurityGroups = append(

pkg/asset/machines/clusterapi.go

Lines changed: 21 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@ import (
88

99
"github.com/pkg/errors"
1010
"github.com/sirupsen/logrus"
11-
v1 "k8s.io/api/core/v1"
12-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1311
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1412
"k8s.io/utils/ptr"
15-
capa "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
16-
capi "sigs.k8s.io/cluster-api/api/v1beta1"
1713
"sigs.k8s.io/controller-runtime/pkg/client"
1814
"sigs.k8s.io/yaml"
1915

@@ -31,6 +27,7 @@ import (
3127
"github.com/openshift/installer/pkg/asset/rhcos"
3228
"github.com/openshift/installer/pkg/clusterapi"
3329
rhcosutils "github.com/openshift/installer/pkg/rhcos"
30+
"github.com/openshift/installer/pkg/types"
3431
awstypes "github.com/openshift/installer/pkg/types/aws"
3532
awsdefaults "github.com/openshift/installer/pkg/types/aws/defaults"
3633
azuretypes "github.com/openshift/installer/pkg/types/azure"
@@ -145,89 +142,39 @@ func (c *ClusterAPI) Generate(dependencies asset.Parents) error {
145142
}
146143
}
147144

145+
tags, err := aws.CapaTagsFromUserTags(clusterID.InfraID, installConfig.Config.Platform.AWS.UserTags)
146+
if err != nil {
147+
return fmt.Errorf("failed to create CAPA tags from UserTags: %w", err)
148+
}
149+
148150
pool.Platform.AWS = &mpool
149151
awsMachines, err := aws.GenerateMachines(clusterID.InfraID, &aws.MachineInput{
150152
Role: "master",
151153
Pool: &pool,
152154
Subnets: subnets,
153-
Tags: installConfig.Config.AWS.UserTags,
155+
Tags: tags,
154156
PublicIP: false,
155-
},
156-
)
157+
})
157158
if err != nil {
158159
return errors.Wrap(err, "failed to create master machine objects")
159160
}
160161
c.FileList = append(c.FileList, awsMachines...)
161162

162-
// TODO(vincepri): The following code is almost duplicated from aws.AWSMachines.
163-
// Refactor and generalize around a bootstrap pool, with a single machine and
164-
// a custom openshift label to determine the bootstrap machine role, so we can
165-
// delete the machine when the stage is complete.
166-
bootstrapAWSMachine := &capa.AWSMachine{
167-
ObjectMeta: metav1.ObjectMeta{
168-
Name: capiutils.GenerateBoostrapMachineName(clusterID.InfraID),
169-
Labels: map[string]string{
170-
"cluster.x-k8s.io/control-plane": "",
171-
"install.openshift.io/bootstrap": "",
172-
},
173-
},
174-
Spec: capa.AWSMachineSpec{
175-
Ignition: &capa.Ignition{Version: "3.2"},
176-
UncompressedUserData: ptr.To(true),
177-
InstanceType: mpool.InstanceType,
178-
AMI: capa.AMIReference{ID: ptr.To(mpool.AMIID)},
179-
SSHKeyName: ptr.To(""),
180-
IAMInstanceProfile: fmt.Sprintf("%s-master-profile", clusterID.InfraID),
181-
PublicIP: ptr.To(true),
182-
RootVolume: &capa.Volume{
183-
Size: int64(mpool.EC2RootVolume.Size),
184-
Type: capa.VolumeType(mpool.EC2RootVolume.Type),
185-
IOPS: int64(mpool.EC2RootVolume.IOPS),
186-
Encrypted: ptr.To(true),
187-
EncryptionKey: mpool.KMSKeyARN,
188-
},
189-
},
190-
}
191-
bootstrapAWSMachine.SetGroupVersionKind(capa.GroupVersion.WithKind("AWSMachine"))
192-
193-
// Handle additional security groups.
194-
for _, sg := range mpool.AdditionalSecurityGroupIDs {
195-
bootstrapAWSMachine.Spec.AdditionalSecurityGroups = append(
196-
bootstrapAWSMachine.Spec.AdditionalSecurityGroups,
197-
capa.AWSResourceReference{ID: ptr.To(sg)},
198-
)
199-
}
200-
201-
c.FileList = append(c.FileList, &asset.RuntimeFile{
202-
File: asset.File{Filename: fmt.Sprintf("10_inframachine_%s.yaml", bootstrapAWSMachine.Name)},
203-
Object: bootstrapAWSMachine,
163+
pool := *ic.ControlPlane
164+
pool.Name = "bootstrap"
165+
pool.Replicas = ptr.To[int64](1)
166+
pool.Platform.AWS = &mpool
167+
bootstrapAWSMachine, err := aws.GenerateMachines(clusterID.InfraID, &aws.MachineInput{
168+
Role: "bootstrap",
169+
Subnets: nil, // let CAPA pick one
170+
Pool: &pool,
171+
Tags: tags,
172+
PublicIP: installConfig.Config.Publish == types.ExternalPublishingStrategy,
204173
})
205-
206-
bootstrapMachine := &capi.Machine{
207-
ObjectMeta: metav1.ObjectMeta{
208-
Name: bootstrapAWSMachine.Name,
209-
Labels: map[string]string{
210-
"cluster.x-k8s.io/control-plane": "",
211-
},
212-
},
213-
Spec: capi.MachineSpec{
214-
ClusterName: clusterID.InfraID,
215-
Bootstrap: capi.Bootstrap{
216-
DataSecretName: ptr.To(fmt.Sprintf("%s-%s", clusterID.InfraID, "bootstrap")),
217-
},
218-
InfrastructureRef: v1.ObjectReference{
219-
APIVersion: capa.GroupVersion.String(),
220-
Kind: "AWSMachine",
221-
Name: bootstrapAWSMachine.Name,
222-
},
223-
},
174+
if err != nil {
175+
return fmt.Errorf("failed to create bootstrap machine object: %w", err)
224176
}
225-
bootstrapMachine.SetGroupVersionKind(capi.GroupVersion.WithKind("Machine"))
226-
227-
c.FileList = append(c.FileList, &asset.RuntimeFile{
228-
File: asset.File{Filename: fmt.Sprintf("10_machine_%s.yaml", bootstrapMachine.Name)},
229-
Object: bootstrapMachine,
230-
})
177+
c.FileList = append(c.FileList, bootstrapAWSMachine...)
231178
case azuretypes.Name:
232179
mpool := defaultAzureMachinePoolPlatform()
233180
mpool.InstanceType = azuredefaults.ControlPlaneInstanceType(

0 commit comments

Comments
 (0)