diff --git a/pkg/v1/tkg/tkgconfigproviders/aws.go b/pkg/v1/tkg/tkgconfigproviders/aws.go index 942d65e91aa..649ac2b0f89 100644 --- a/pkg/v1/tkg/tkgconfigproviders/aws.go +++ b/pkg/v1/tkg/tkgconfigproviders/aws.go @@ -5,6 +5,7 @@ package tkgconfigproviders import ( "encoding/base64" + "github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/tkgconfigbom" "strconv" "github.com/pkg/errors" @@ -121,9 +122,7 @@ func (c *client) NewAWSConfig(params *models.AWSRegionalClusterParams, encodedCr amiID := "" - if val, ok := bomConfiguration.AMI[params.AwsAccountParams.Region]; ok { - amiID = val[0].ID - } + amiID = c.GetAMIId(bomConfiguration, params) if amiID == "" { return nil, errors.Errorf("No AMI found in region %s for TKr version %s", params.AwsAccountParams.Region, bomConfiguration.Release.Version) @@ -286,6 +285,24 @@ func (c *client) NewAWSConfig(params *models.AWSRegionalClusterParams, encodedCr return res, nil } +func (c *client) GetAMIId(bomConfiguration *tkgconfigbom.BOMConfiguration, params *models.AWSRegionalClusterParams) string { + amiID := "" + if amis, ok := bomConfiguration.AMI[params.AwsAccountParams.Region]; ok { + if params.Os != nil && params.Os.OsInfo != nil { + for _, ami := range amis { + if ami.OSInfo.Name == params.Os.OsInfo.Name { + amiID = ami.ID + break + } + } + } else { + amiID = amis[0].ID + } + } + + return amiID +} + // AppendSubnets append subnet information in providerConfig to paramsVpc func appendSubnets(paramsVpc *models.AWSVpc, highAvailability bool) ([]*newSubnetPair, error) { ProdSubnetCount := 6 diff --git a/pkg/v1/tkg/tkgconfigproviders/client.go b/pkg/v1/tkg/tkgconfigproviders/client.go index 1f71b87cde8..8ed649c4293 100644 --- a/pkg/v1/tkg/tkgconfigproviders/client.go +++ b/pkg/v1/tkg/tkgconfigproviders/client.go @@ -34,6 +34,7 @@ type Client interface { NewAzureConfig(params *models.AzureRegionalClusterParams) (*AzureConfig, error) NewVSphereConfig(params *models.VsphereRegionalClusterParams) (*VSphereConfig, error) NewDockerConfig(params *models.DockerRegionalClusterParams) (*DockerConfig, error) + GetAMIId(bomConfiguration *tkgconfigbom.BOMConfiguration, params *models.AWSRegionalClusterParams) string } // TKGConfigReaderWriter returns tkgConfigReaderWriter client diff --git a/pkg/v1/tkg/tkgconfigproviders/tkgconfig_test.go b/pkg/v1/tkg/tkgconfigproviders/tkgconfig_test.go index 5c7801a2fe2..ff5440cbc5b 100644 --- a/pkg/v1/tkg/tkgconfigproviders/tkgconfig_test.go +++ b/pkg/v1/tkg/tkgconfigproviders/tkgconfig_test.go @@ -117,6 +117,89 @@ var _ = Describe("Azure VM Images", func() { }) }) +var _ = FDescribe("Test AWS AMIID", func() { + var ( + vpcConfig *models.AWSVpc + params *models.AWSRegionalClusterParams + flavor string + client Client + bomConfiguration *tkgconfigbom.BOMConfiguration + ) + + JustBeforeEach(func() { + params = &models.AWSRegionalClusterParams{ + Vpc: vpcConfig, + AwsAccountParams: &models.AWSAccountParams{ + Region: fakeRegion, + }, + ControlPlaneFlavor: flavor, + Networking: &models.TKGNetwork{ + ClusterPodCIDR: "10.0.0.4/15", + }, + KubernetesVersion: "v1.18.0+vmware.1", + IdentityManagement: &models.IdentityManagementConfig{ + IdmType: swag.String("oidc"), + OidcClaimMappings: map[string]string{"groups": "group", "username": "usr"}, + OidcClientID: "client-id", + OidcClientSecret: "clientsecret", + OidcProviderName: "my-provider", + OidcProviderURL: "http:0.0.0.0", + OidcScope: "email", + OidcSkipVerifyCert: true, + }, + } + setupBomFile("../fakes/config/bom/tkr-bom-v1.18.0+vmware.1-tkg.2.yaml", testingDir) + client = newForTesting("../fakes/config/config.yaml", testingDir, defaultBoMFilepath) + + bomConfiguration = &tkgconfigbom.BOMConfiguration{ + Release: &tkgconfigbom.ReleaseInfo{ + Version: "v1.18.0+vmware.1-tkg.2", + }, + AMI: map[string][]tkgconfigbom.AMIInfo{ + fakeRegion: { + { + ID: "ami-123456", + OSInfo: tkgconfigbom.OSInfo{ + Name: "amazon", + Version: "2", + Arch: "amd64", + }, + }, + { + ID: "ami-567890", + OSInfo: tkgconfigbom.OSInfo{ + Name: "ubuntu", + Version: "2", + Arch: "amd64", + }, + }, + }, + }, + } + + }) + + It("When OS is specified in the parameters", func() { + os := &models.AWSVirtualMachine{ + Name: "ubuntu", + OsInfo: &models.OSInfo{ + Arch: "amd64", + Name: "ubuntu", + Version: "2", + }, + } + + params.Os = os + amiId := client.GetAMIId(bomConfiguration,params) + Expect(amiId).To(Equal("ami-567890")) + }) + + It("When OS is not specified in the paramters", func() { + amiId := client.GetAMIId(bomConfiguration,params) + Expect(amiId).To(Equal("ami-123456")) + }) +}) + var _ = Describe("EnsureNewVPCAWSConfig", func() { var ( err error