Skip to content

Commit

Permalink
Refactor the aks provisioning and import methods to not write to conf…
Browse files Browse the repository at this point in the history
…ig file

Signed-off-by: Parthvi <parthvi.vala@suse.com>
  • Loading branch information
valaparthvi committed May 13, 2024
1 parent 2891727 commit 54aca22
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 156 deletions.
117 changes: 65 additions & 52 deletions hosted/aks/helper/helper_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,6 @@ var (
subscriptionID = os.Getenv("AKS_SUBSCRIPTION_ID")
)

func GetTags() map[string]string {
aksConfig := new(management.AKSClusterConfigSpec)
config.LoadConfig(aks.AKSClusterConfigConfigurationFileKey, aksConfig)
tags := helpers.GetCommonMetadataLabels()
for key, value := range aksConfig.Tags {
tags[key] = value
}
return tags
}

// UpgradeClusterKubernetesVersion upgrades the k8s version to the value defined by upgradeToVersion.
func UpgradeClusterKubernetesVersion(cluster *management.Cluster, upgradeToVersion *string, client *rancher.Client) (*management.Cluster, error) {
upgradedCluster := new(management.Cluster)
Expand Down Expand Up @@ -68,6 +58,57 @@ func UpgradeNodeKubernetesVersion(cluster *management.Cluster, upgradeToVersion
return cluster, nil
}

func CreateAKSHostedCluster(client *rancher.Client, cloudCredentialID, clusterName, k8sVersion, location string, tags map[string]string) (*management.Cluster, error) {
var aksClusterConfig aks.ClusterConfig
config.LoadConfig(aks.AKSClusterConfigConfigurationFileKey, &aksClusterConfig)
var aksNodePools []management.AKSNodePool
for _, aksNodePoolConfig := range *aksClusterConfig.NodePools {
aksNodePool := management.AKSNodePool{
AvailabilityZones: aksNodePoolConfig.AvailabilityZones,
Count: aksNodePoolConfig.NodeCount,
EnableAutoScaling: aksNodePoolConfig.EnableAutoScaling,
MaxPods: aksNodePoolConfig.MaxPods,
MaxCount: aksNodePoolConfig.MaxCount,
MinCount: aksNodePoolConfig.MinCount,
Mode: aksNodePoolConfig.Mode,
Name: aksNodePoolConfig.Name,
OrchestratorVersion: &k8sVersion,
OsDiskSizeGB: aksNodePoolConfig.OsDiskSizeGB,
OsDiskType: aksNodePoolConfig.OsDiskType,
OsType: aksNodePoolConfig.OsType,
VMSize: aksNodePoolConfig.VMSize,
}
aksNodePools = append(aksNodePools, aksNodePool)
}

cluster := &management.Cluster{
AKSConfig: &management.AKSClusterConfigSpec{
AzureCredentialSecret: cloudCredentialID,
ClusterName: clusterName,
DNSPrefix: pointer.String(clusterName + "-dns"),
Imported: false,
KubernetesVersion: &k8sVersion,
LinuxAdminUsername: aksClusterConfig.LinuxAdminUsername,
LoadBalancerSKU: aksClusterConfig.LoadBalancerSKU,
NetworkPlugin: aksClusterConfig.NetworkPlugin,
NodePools: aksNodePools,
PrivateCluster: aksClusterConfig.PrivateCluster,
ResourceGroup: clusterName,
ResourceLocation: location,
Tags: tags,
},
DockerRootDir: "/var/lib/docker",
Name: clusterName,
}

clusterResp, err := client.Management.Cluster.Create(cluster)
if err != nil {
return nil, err
}

return clusterResp, err
}

// DeleteAKSHostCluster deletes the AKS cluster
func DeleteAKSHostCluster(cluster *management.Cluster, client *rancher.Client) error {
return client.Management.Cluster.Delete(cluster)
Expand Down Expand Up @@ -112,10 +153,9 @@ func AddNodePool(cluster *management.Cluster, increaseBy int, client *rancher.Cl
upgradedCluster := new(management.Cluster)
upgradedCluster.Name = cluster.Name
upgradedCluster.AKSConfig = cluster.AKSConfig
nodeConfig := AksHostNodeConfig()

for i := 1; i <= increaseBy; i++ {
for _, np := range nodeConfig {
for _, np := range cluster.AKSConfig.NodePools {
newNodepool := management.AKSNodePool{
Count: pointer.Int64(1),
VMSize: np.VMSize,
Expand Down Expand Up @@ -179,8 +219,7 @@ func ListAKSAvailableVersions(client *rancher.Client, clusterID string) ([]strin
}

// Create Azure AKS cluster using AZ CLI
func CreateAKSClusterOnAzure(location string, clusterName string, k8sVersion string, nodes string) error {
tags := GetTags()
func CreateAKSClusterOnAzure(location string, clusterName string, k8sVersion string, nodes string, tags map[string]string) error {
formattedTags := convertMapToAKSString(tags)
fmt.Println("Creating AKS resource group ...")
rgargs := []string{"group", "create", "--location", location, "--resource-group", clusterName, "--subscription", subscriptionID}
Expand Down Expand Up @@ -231,17 +270,19 @@ func DeleteAKSClusteronAzure(clusterName string) error {
return nil
}

func ImportAKSHostedCluster(client *rancher.Client, displayName, cloudCredentialID string, enableClusterAlerting, enableClusterMonitoring, enableNetworkPolicy, windowsPreferedCluster bool, labels map[string]string) (*management.Cluster, error) {
aksHostCluster := AksHostClusterConfig(displayName, cloudCredentialID)
// ImportAKSHostedCluster imports an AKS cluster to Rancher
func ImportAKSHostedCluster(client *rancher.Client, clusterName, cloudCredentialID, location string, tags map[string]string) (*management.Cluster, error) {
cluster := &management.Cluster{
DockerRootDir: "/var/lib/docker",
AKSConfig: aksHostCluster,
Name: displayName,
EnableClusterAlerting: enableClusterAlerting,
EnableClusterMonitoring: enableClusterMonitoring,
EnableNetworkPolicy: &enableNetworkPolicy,
Labels: labels,
WindowsPreferedCluster: windowsPreferedCluster,
DockerRootDir: "/var/lib/docker",
AKSConfig: &management.AKSClusterConfigSpec{
AzureCredentialSecret: cloudCredentialID,
ClusterName: clusterName,
Imported: true,
ResourceLocation: location,
ResourceGroup: clusterName,
Tags: tags,
},
Name: clusterName,
}

clusterResp, err := client.Management.Cluster.Create(cluster)
Expand All @@ -251,34 +292,6 @@ func ImportAKSHostedCluster(client *rancher.Client, displayName, cloudCredential
return clusterResp, err
}

func AksHostClusterConfig(displayName, cloudCredentialID string) *management.AKSClusterConfigSpec {
var aksClusterConfig ImportClusterConfig
config.LoadConfig("aksClusterConfig", &aksClusterConfig)

return &management.AKSClusterConfigSpec{
AzureCredentialSecret: cloudCredentialID,
ClusterName: displayName,
Imported: aksClusterConfig.Imported,
ResourceLocation: aksClusterConfig.ResourceLocation,
ResourceGroup: aksClusterConfig.ResourceGroup,
}
}

func AksHostNodeConfig() []management.AKSNodePool {
var nodeConfig management.AKSClusterConfigSpec
config.LoadConfig("aksClusterConfig", &nodeConfig)

return nodeConfig.NodePools
}

type ImportClusterConfig struct {
ResourceGroup string `json:"resourceGroup" yaml:"resourceGroup"`
ResourceLocation string `json:"resourceLocation" yaml:"resourceLocation"`
Tags map[string]string `json:"tags,omitempty" yaml:"tags,omitempty"`
Imported bool `json:"imported" yaml:"imported"`
NodePools []*management.AKSNodePool `json:"nodePools" yaml:"nodePools"`
}

// defaultAKS returns the default AKS version used by Rancher; if forUpgrade is true, it returns the second-highest minor k8s version
func defaultAKS(client *rancher.Client, cloudCredentialID, region string, forUpgrade bool) (defaultAKS string, err error) {
url := fmt.Sprintf("%s://%s/meta/aksVersions", "https", client.RancherConfig.Host)
Expand Down
15 changes: 2 additions & 13 deletions hosted/aks/k8s_chart_support/k8s_chart_support_import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
management "github.com/rancher/shepherd/clients/rancher/generated/management/v3"
"github.com/rancher/shepherd/extensions/clusters/aks"
"github.com/rancher/shepherd/pkg/config"

"github.com/rancher/hosted-providers-e2e/hosted/aks/helper"
"github.com/rancher/hosted-providers-e2e/hosted/helpers"
Expand All @@ -17,18 +15,9 @@ var _ = Describe("K8sChartSupportImport", func() {
var cluster *management.Cluster

BeforeEach(func() {
var err error
err = helper.CreateAKSClusterOnAzure(location, clusterName, k8sVersion, "1")
err := helper.CreateAKSClusterOnAzure(location, clusterName, k8sVersion, "1", helpers.GetCommonMetadataLabels())
Expect(err).To(BeNil())

aksConfig := new(helper.ImportClusterConfig)
config.LoadAndUpdateConfig(aks.AKSClusterConfigConfigurationFileKey, aksConfig, func() {
aksConfig.ResourceGroup = clusterName
aksConfig.ResourceLocation = location
aksConfig.Tags = helper.GetTags()
})

cluster, err = helper.ImportAKSHostedCluster(ctx.RancherClient, clusterName, ctx.CloudCred.ID, false, false, false, false, map[string]string{})
cluster, err = helper.ImportAKSHostedCluster(ctx.RancherClient, clusterName, ctx.CloudCred.ID, location, helpers.GetCommonMetadataLabels())
Expect(err).To(BeNil())
cluster, err = helpers.WaitUntilClusterIsReady(cluster, ctx.RancherClient)
Expect(err).To(BeNil())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
management "github.com/rancher/shepherd/clients/rancher/generated/management/v3"
"github.com/rancher/shepherd/extensions/clusters/aks"
"github.com/rancher/shepherd/pkg/config"

"github.com/rancher/hosted-providers-e2e/hosted/aks/helper"
"github.com/rancher/hosted-providers-e2e/hosted/helpers"
Expand All @@ -19,16 +17,7 @@ var _ = Describe("K8sChartSupportProvisioning", func() {
)
BeforeEach(func() {
var err error
aksConfig := new(aks.ClusterConfig)
config.LoadAndUpdateConfig(aks.AKSClusterConfigConfigurationFileKey, aksConfig, func() {
aksConfig.ResourceGroup = clusterName
dnsPrefix := clusterName + "-dns"
aksConfig.DNSPrefix = &dnsPrefix
aksConfig.ResourceLocation = location
aksConfig.Tags = helper.GetTags()
aksConfig.KubernetesVersion = &k8sVersion
})
cluster, err = aks.CreateAKSHostedCluster(ctx.RancherClient, clusterName, ctx.CloudCred.ID, false, false, false, false, map[string]string{})
cluster, err = helper.CreateAKSHostedCluster(ctx.RancherClient, ctx.CloudCred.ID, clusterName, k8sVersion, location, helpers.GetCommonMetadataLabels())
Expect(err).To(BeNil())
cluster, err = helpers.WaitUntilClusterIsReady(cluster, ctx.RancherClient)
Expect(err).To(BeNil())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
management "github.com/rancher/shepherd/clients/rancher/generated/management/v3"
"github.com/rancher/shepherd/extensions/clusters/aks"
"github.com/rancher/shepherd/pkg/config"

"github.com/rancher/hosted-providers-e2e/hosted/aks/helper"
"github.com/rancher/hosted-providers-e2e/hosted/helpers"
Expand All @@ -17,18 +15,9 @@ var _ = Describe("K8sChartSupportUpgradeImport", func() {
var cluster *management.Cluster

BeforeEach(func() {
var err error
err = helper.CreateAKSClusterOnAzure(location, clusterName, k8sVersion, "1")
err := helper.CreateAKSClusterOnAzure(location, clusterName, k8sVersion, "1", helpers.GetCommonMetadataLabels())
Expect(err).To(BeNil())

aksConfig := new(helper.ImportClusterConfig)
config.LoadAndUpdateConfig(aks.AKSClusterConfigConfigurationFileKey, aksConfig, func() {
aksConfig.ResourceGroup = clusterName
aksConfig.ResourceLocation = location
aksConfig.Tags = helper.GetTags()
})

cluster, err = helper.ImportAKSHostedCluster(ctx.RancherClient, clusterName, ctx.CloudCred.ID, false, false, false, false, map[string]string{})
cluster, err = helper.ImportAKSHostedCluster(ctx.RancherClient, clusterName, ctx.CloudCred.ID, location, helpers.GetCommonMetadataLabels())
Expect(err).To(BeNil())
cluster, err = helpers.WaitUntilClusterIsReady(cluster, ctx.RancherClient)
Expect(err).To(BeNil())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
management "github.com/rancher/shepherd/clients/rancher/generated/management/v3"
"github.com/rancher/shepherd/extensions/clusters/aks"
"github.com/rancher/shepherd/pkg/config"

"github.com/rancher/hosted-providers-e2e/hosted/aks/helper"
"github.com/rancher/hosted-providers-e2e/hosted/helpers"
Expand All @@ -19,16 +17,7 @@ var _ = Describe("K8sChartSupportUpgradeProvisioning", func() {
)
BeforeEach(func() {
var err error
aksConfig := new(aks.ClusterConfig)
config.LoadAndUpdateConfig(aks.AKSClusterConfigConfigurationFileKey, aksConfig, func() {
aksConfig.ResourceGroup = clusterName
dnsPrefix := clusterName + "-dns"
aksConfig.DNSPrefix = &dnsPrefix
aksConfig.ResourceLocation = location
aksConfig.Tags = helper.GetTags()
aksConfig.KubernetesVersion = &k8sVersion
})
cluster, err = aks.CreateAKSHostedCluster(ctx.RancherClient, clusterName, ctx.CloudCred.ID, false, false, false, false, map[string]string{})
cluster, err = helper.CreateAKSHostedCluster(ctx.RancherClient, ctx.CloudCred.ID, clusterName, k8sVersion, location, helpers.GetCommonMetadataLabels())
Expect(err).To(BeNil())
cluster, err = helpers.WaitUntilClusterIsReady(cluster, ctx.RancherClient)
Expect(err).To(BeNil())
Expand Down
14 changes: 2 additions & 12 deletions hosted/aks/p0/p0_importing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
. "github.com/onsi/gomega"
"github.com/rancher/shepherd/clients/rancher"
management "github.com/rancher/shepherd/clients/rancher/generated/management/v3"
"github.com/rancher/shepherd/extensions/clusters/aks"
"github.com/rancher/shepherd/pkg/config"

"github.com/rancher/hosted-providers-e2e/hosted/aks/helper"
"github.com/rancher/hosted-providers-e2e/hosted/helpers"
Expand Down Expand Up @@ -57,17 +55,9 @@ var _ = Describe("P0Importing", func() {
Expect(err).NotTo(HaveOccurred())
GinkgoLogr.Info("Using K8s version: " + k8sVersion)

err = helper.CreateAKSClusterOnAzure(location, clusterName, k8sVersion, "1")
err = helper.CreateAKSClusterOnAzure(location, clusterName, k8sVersion, "1", helpers.GetCommonMetadataLabels())
Expect(err).To(BeNil())

aksConfig := new(helper.ImportClusterConfig)
config.LoadAndUpdateConfig(aks.AKSClusterConfigConfigurationFileKey, aksConfig, func() {
aksConfig.ResourceGroup = clusterName
aksConfig.ResourceLocation = location
aksConfig.Tags = helper.GetTags()
})

cluster, err = helper.ImportAKSHostedCluster(ctx.RancherClient, clusterName, ctx.CloudCred.ID, false, false, false, false, map[string]string{})
cluster, err = helper.ImportAKSHostedCluster(ctx.RancherClient, clusterName, ctx.CloudCred.ID, location, helpers.GetCommonMetadataLabels())
Expect(err).To(BeNil())
cluster, err = helpers.WaitUntilClusterIsReady(cluster, ctx.RancherClient)
Expect(err).To(BeNil())
Expand Down
15 changes: 1 addition & 14 deletions hosted/aks/p0/p0_provisioning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ import (
"github.com/rancher/shepherd/clients/rancher"

management "github.com/rancher/shepherd/clients/rancher/generated/management/v3"
"github.com/rancher/shepherd/pkg/config"

"github.com/rancher/shepherd/extensions/clusters/aks"

"github.com/rancher/hosted-providers-e2e/hosted/aks/helper"
"github.com/rancher/hosted-providers-e2e/hosted/helpers"
Expand Down Expand Up @@ -59,17 +56,7 @@ var _ = Describe("P0Provisioning", func() {
Expect(err).NotTo(HaveOccurred())
GinkgoLogr.Info("Using K8s version: " + k8sVersion)

aksConfig := new(aks.ClusterConfig)
config.LoadAndUpdateConfig(aks.AKSClusterConfigConfigurationFileKey, aksConfig, func() {
aksConfig.ResourceGroup = clusterName
dnsPrefix := clusterName + "-dns"
aksConfig.DNSPrefix = &dnsPrefix
aksConfig.ResourceLocation = location
aksConfig.Tags = helper.GetTags()
aksConfig.KubernetesVersion = &k8sVersion
})

cluster, err = aks.CreateAKSHostedCluster(ctx.RancherClient, clusterName, ctx.CloudCred.ID, false, false, false, false, map[string]string{})
cluster, err = helper.CreateAKSHostedCluster(ctx.RancherClient, ctx.CloudCred.ID, clusterName, k8sVersion, location, helpers.GetCommonMetadataLabels())
Expect(err).To(BeNil())
cluster, err = helpers.WaitUntilClusterIsReady(cluster, ctx.RancherClient)
Expect(err).To(BeNil())
Expand Down
2 changes: 1 addition & 1 deletion hosted/aks/p0/p0_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func p0upgradeK8sVersionCheck(cluster *management.Cluster, client *rancher.Clien
Expect(err).To(BeNil())
Expect(versions).ToNot(BeEmpty())
upgradeToVersion := &versions[0]
GinkgoLogr.Info(fmt.Sprintf("Upgrading cluster to GKE version %s", *upgradeToVersion))
GinkgoLogr.Info(fmt.Sprintf("Upgrading cluster to AKS version %s", *upgradeToVersion))

By("upgrading the ControlPlane", func() {
cluster, err = helper.UpgradeClusterKubernetesVersion(cluster, upgradeToVersion, client)
Expand Down
13 changes: 2 additions & 11 deletions hosted/aks/support_matrix/support_matrix_importing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"fmt"

management "github.com/rancher/shepherd/clients/rancher/generated/management/v3"
"github.com/rancher/shepherd/extensions/clusters/aks"
"github.com/rancher/shepherd/pkg/config"
namegen "github.com/rancher/shepherd/pkg/namegenerator"

"github.com/rancher/hosted-providers-e2e/hosted/aks/helper"
Expand All @@ -41,16 +39,9 @@ var _ = Describe("SupportMatrixImporting", func() {
)
BeforeEach(func() {
clusterName = namegen.AppendRandomString(helpers.ClusterNamePrefix)
var err error
err = helper.CreateAKSClusterOnAzure(location, clusterName, version, "1")
err := helper.CreateAKSClusterOnAzure(location, clusterName, version, "1", helpers.GetCommonMetadataLabels())
Expect(err).To(BeNil())
aksConfig := new(helper.ImportClusterConfig)
config.LoadAndUpdateConfig(aks.AKSClusterConfigConfigurationFileKey, aksConfig, func() {
aksConfig.ResourceGroup = clusterName
aksConfig.ResourceLocation = location
aksConfig.Tags = helper.GetTags()
})
cluster, err = helper.ImportAKSHostedCluster(ctx.RancherClient, clusterName, ctx.CloudCred.ID, false, false, false, false, map[string]string{})
cluster, err = helper.ImportAKSHostedCluster(ctx.RancherClient, clusterName, ctx.CloudCred.ID, location, helpers.GetCommonMetadataLabels())
Expect(err).To(BeNil())
cluster, err = helpers.WaitUntilClusterIsReady(cluster, ctx.RancherClient)
Expect(err).To(BeNil())
Expand Down
13 changes: 1 addition & 12 deletions hosted/aks/support_matrix/support_matrix_provisioning_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"fmt"

management "github.com/rancher/shepherd/clients/rancher/generated/management/v3"
"github.com/rancher/shepherd/extensions/clusters/aks"
"github.com/rancher/shepherd/pkg/config"
namegen "github.com/rancher/shepherd/pkg/namegenerator"

"github.com/rancher/hosted-providers-e2e/hosted/aks/helper"
Expand All @@ -42,16 +40,7 @@ var _ = Describe("SupportMatrixProvisioning", func() {
BeforeEach(func() {
clusterName = namegen.AppendRandomString(helpers.ClusterNamePrefix)
var err error
aksConfig := new(aks.ClusterConfig)
config.LoadAndUpdateConfig(aks.AKSClusterConfigConfigurationFileKey, aksConfig, func() {
aksConfig.ResourceGroup = clusterName
dnsPrefix := clusterName + "-dns"
aksConfig.DNSPrefix = &dnsPrefix
aksConfig.ResourceLocation = location
aksConfig.KubernetesVersion = &version
aksConfig.Tags = helper.GetTags()
})
cluster, err = aks.CreateAKSHostedCluster(ctx.RancherClient, clusterName, ctx.CloudCred.ID, false, false, false, false, map[string]string{})
cluster, err = helper.CreateAKSHostedCluster(ctx.RancherClient, ctx.CloudCred.ID, clusterName, version, location, helpers.GetCommonMetadataLabels())
Expect(err).To(BeNil())
cluster, err = helpers.WaitUntilClusterIsReady(cluster, ctx.RancherClient)
Expect(err).To(BeNil())
Expand Down
Loading

0 comments on commit 54aca22

Please sign in to comment.