Skip to content

Commit 5a2f63c

Browse files
committed
OCPBUGS-33508: capi/aws: fix setting custom AMI
When I introduced the AMI copying functionality, I inadvertently broke the custom AMI logic, causing the code to always use the default RHCOS AMI. This PR fixes the issue and adds some comments to explain the AMI selection logic which spans multiple files and is not easy to follow.
1 parent c8febae commit 5a2f63c

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

pkg/infrastructure/aws/clusterapi/ami.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,15 @@ import (
1010
"github.com/sirupsen/logrus"
1111

1212
"github.com/openshift/installer/pkg/asset/installconfig"
13+
"github.com/openshift/installer/pkg/asset/rhcos"
1314
)
1415

1516
// copyAMIToRegion copies the AMI to the region configured in the installConfig if needed.
16-
func copyAMIToRegion(ctx context.Context, installConfig *installconfig.InstallConfig, infraID, rhcosImage string) (string, error) {
17-
osImage := strings.SplitN(rhcosImage, ",", 2)
18-
amiID := osImage[0]
19-
amiRegion := installConfig.Config.AWS.Region
20-
if len(osImage) > 1 {
21-
amiRegion = osImage[1]
22-
}
23-
24-
// Already in target region, nothing to do
25-
if amiRegion == installConfig.Config.AWS.Region {
26-
return amiID, nil
27-
}
17+
func copyAMIToRegion(ctx context.Context, installConfig *installconfig.InstallConfig, infraID string, rhcosImage *rhcos.Image) (string, error) {
18+
osImage := strings.SplitN(string(*rhcosImage), ",", 2)
19+
amiID, amiRegion := osImage[0], osImage[1]
2820

29-
logrus.Infof("Copying AMI to region %s", installConfig.AWS.Region)
21+
logrus.Infof("Copying AMI %s to region %s", amiID, installConfig.AWS.Region)
3022

3123
session, err := installConfig.AWS.Session(ctx)
3224
if err != nil {

pkg/infrastructure/aws/clusterapi/aws.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,29 @@ func (*Provider) PreProvision(ctx context.Context, in clusterapi.PreProvisionInp
4545
return fmt.Errorf("failed to create IAM roles: %w", err)
4646
}
4747

48-
amiID, err := copyAMIToRegion(ctx, in.InstallConfig, in.InfraID, string(*in.RhcosImage))
48+
// The AWSMachine manifests might already have the AMI ID set from the machine pool which takes into account the
49+
// ways in which the AMI can be specified: the default rhcos if already in the target region, a custom AMI ID set in
50+
// platform.aws.amiID, and a custom AMI ID specified in the controlPlane stanza. So we just get the value from the
51+
// first awsmachine manifest we find, instead of duplicating all the inheriting logic here.
52+
for i := range in.MachineManifests {
53+
if awsMachine, ok := in.MachineManifests[i].(*capa.AWSMachine); ok {
54+
// Default/custom AMI already in target region, nothing else to do
55+
if ptr.Deref(awsMachine.Spec.AMI.ID, "") != "" {
56+
return nil
57+
}
58+
}
59+
}
60+
61+
// Notice that we have to use the default RHCOS value because we set the AMI.ID to empty if the default RHCOS is not
62+
// in the target region and it needs to be copied over. See pkg/asset/machines/clusterapi.go
63+
amiID, err := copyAMIToRegion(ctx, in.InstallConfig, in.InfraID, in.RhcosImage)
4964
if err != nil {
5065
return fmt.Errorf("failed to copy AMI: %w", err)
5166
}
67+
// Update manifests with the new ID
5268
for i := range in.MachineManifests {
5369
if awsMachine, ok := in.MachineManifests[i].(*capa.AWSMachine); ok {
54-
awsMachine.Spec.AMI = capa.AMIReference{ID: ptr.To(amiID)}
70+
awsMachine.Spec.AMI.ID = ptr.To(amiID)
5571
}
5672
}
5773
return nil

0 commit comments

Comments
 (0)