Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Correcting AMI ID being sent from the UI
Browse files Browse the repository at this point in the history
Signed-off-by: Sudarshan <asudarshan@vmware.com>
  • Loading branch information
saji-pivotal committed Sep 29, 2021
1 parent 5b764f3 commit d8eb68d
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 5 deletions.
24 changes: 21 additions & 3 deletions pkg/v1/tkg/tkgconfigproviders/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"encoding/base64"
"strconv"

"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/tkgconfigbom"

"github.com/pkg/errors"

"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/constants"
Expand Down Expand Up @@ -121,9 +123,7 @@ func (c *client) NewAWSConfig(params *models.AWSRegionalClusterParams, encodedCr

amiID := ""

if val, ok := bomConfiguration.AMI[params.AwsAccountParams.Region]; ok {
amiID = val[0].ID
}
amiID = 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)
Expand Down Expand Up @@ -286,6 +286,24 @@ func (c *client) NewAWSConfig(params *models.AWSRegionalClusterParams, encodedCr
return res, nil
}

func 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
Expand Down
84 changes: 82 additions & 2 deletions pkg/v1/tkg/tkgconfigproviders/tkgconfig_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2021 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package tkgconfigproviders_test
package tkgconfigproviders

import (
"fmt"
Expand All @@ -19,7 +19,6 @@ import (
fakehelper "github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/fakes/helper"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/tkgconfigbom"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/tkgconfigpaths"
. "github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/tkgconfigproviders"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/tkgconfigreaderwriter"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/utils"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/web/server/models"
Expand Down Expand Up @@ -117,6 +116,87 @@ var _ = Describe("Azure VM Images", func() {
})
})

var _ = Describe("Test AWS AMIID", func() {
var (
vpcConfig *models.AWSVpc
params *models.AWSRegionalClusterParams
flavor string
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)

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 := getAMIId(bomConfiguration, params)
Expect(amiID).To(Equal("ami-567890"))
})

It("When OS is not specified in the parameters", func() {
amiID := getAMIId(bomConfiguration, params)
Expect(amiID).To(Equal("ami-123456"))
})
})

var _ = Describe("EnsureNewVPCAWSConfig", func() {
var (
err error
Expand Down

0 comments on commit d8eb68d

Please # to comment.