@@ -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