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

Commit

Permalink
cc_cluster lifecycle upgrade test - upgrade
Browse files Browse the repository at this point in the history
adds the required code to execute an upgrade test on a cluster
  • Loading branch information
Adolfo Duarte committed Jul 28, 2022
1 parent 4efc259 commit dcfb314
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 6 deletions.
102 changes: 102 additions & 0 deletions pkg/v1/tkg/test/tkgctl/shared/e2e_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"math/rand"
"os"
"path/filepath"
"strings"
"time"

"sigs.k8s.io/cluster-api/util"
Expand All @@ -18,9 +19,11 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

runv1alpha3 "github.com/vmware-tanzu/tanzu-framework/apis/run/v1alpha3"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/constants"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/test/framework"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/tkgctl"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/utils"
)

type E2ECommonSpecInput struct {
Expand Down Expand Up @@ -199,6 +202,21 @@ func E2ECommonSpec(ctx context.Context, inputGetter func() E2ECommonSpecInput) {
By(fmt.Sprintf("Get management cluster resources created by addons-manager for workload cluster %q on management cluster %q", clusterName, input.E2EConfig.ManagementClusterName))
clusterResources, err = getManagementClusterResources(ctx, mngclient, mngDynamicClient, mngAggregatedAPIResourcesClient, mngDiscoveryClient, namespace, clusterName, infrastructureName)
Expect(err).NotTo(HaveOccurred())

UpgradeClusterTest(tkgCtlClient, clusterName, namespace)

By(fmt.Sprintf("Get k8s client for upgraded workload cluster %q", clusterName))
wlcClient, _, _, _, err = getClients(ctx, tempFilePath)
Expect(err).NotTo(HaveOccurred())

By(fmt.Sprintf("Verify addon packages on workload cluster %q matches clusterBootstrap info on management cluster %q", clusterName, input.E2EConfig.ManagementClusterName))
err = checkClusterCB(ctx, mngClient, wlcClient, input.E2EConfig.ManagementClusterName, constants.TkgNamespace, clusterName, namespace, infrastructureName, false)
Expect(err).To(BeNil())

By(fmt.Sprintf("Get management cluster resources created by addons-manager for workload cluster %q on management cluster %q", clusterName, input.E2EConfig.ManagementClusterName))
clusterResources, err = getManagementClusterResources(ctx, mngClient, mngDynamicClient, mngAggregatedAPIResourcesClient, mngDiscoveryClient, namespace, clusterName, infrastructureName)
Expect(err).NotTo(HaveOccurred())

}
}

Expand All @@ -223,3 +241,87 @@ func E2ECommonSpec(ctx context.Context, inputGetter func() E2ECommonSpecInput) {
By("Test successful !")
})
}

func getNextAvailableTkrVersion(tkgctlClient tkgctl.TKGClient, currentTkrVersion string) (string, error) {
tkrs, err := tkgctlClient.GetTanzuKubernetesReleases("")
if err != nil {
return "", err
}

availableVersions := make(map[string]string)
for i := range tkrs {
compatible := ""
for _, condition := range tkrs[i].Status.Conditions {
if condition.Type == runv1alpha3.ConditionCompatible {
compatible = string(condition.Status)
}
}
labels := tkrs[i].Labels
activeStatus := "True"
if labels != nil {
if _, exists := labels[runv1alpha3.LabelDeactivated]; exists {
activeStatus = "False"
}
}
if !strings.EqualFold(compatible, "true") || !strings.EqualFold(activeStatus, "true") {
continue
}

compareResult, err := utils.CompareVMwareVersionStrings(currentTkrVersion, tkrs[i].Spec.Version)
if err != nil {
return "", err
}

if compareResult < 0 {
availableVersions[tkrs[i].Spec.Version] = tkrs[i].Name
}

}
var tkrVersions []string
for version := range availableVersions {
tkrVersions = append(tkrVersions, version)
}
if len(tkrVersions) < 1 {
return "", fmt.Errorf("no TKR version available for upgrade")
}
nextAvailableVersion := tkrVersions[0]
for _, newVersion := range tkrVersions {
compareResult, err := utils.CompareVMwareVersionStrings(nextAvailableVersion, newVersion)
if err != nil {
return "", err
}
if compareResult > 0 {
nextAvailableVersion = newVersion
}
}
return nextAvailableVersion, nil
}
func getTkrVersion(tkgctlClient tkgctl.TKGClient, clusterName, clusterNamespace string) (string, error) {
cluster, err := tkgctlClient.GetClusters(tkgctl.ListTKGClustersOptions{Namespace: clusterNamespace,
ClusterName: clusterName})
if err != nil {
return "", err
}
if len(cluster) < 1 {
return "", nil
}
return cluster[0].K8sVersion, nil
}

// UpgradeClusterTest tests upgrading a workload cluster to next available tkr
func UpgradeClusterTest(tkgctlClient tkgctl.TKGClient, clusterName, namespace string) {
By(fmt.Sprintf("Upgrade workload cluster %q in namespace %q", clusterName, namespace))
currentTkrVersion, err := getTkrVersion(tkgctlClient, clusterName, namespace)
Expect(err).ToNot(HaveOccurred())
nextAvailableTkrVersion, err := getNextAvailableTkrVersion(tkgctlClient, currentTkrVersion)
Expect(err).ToNot(HaveOccurred())
err = tkgctlClient.UpgradeCluster(tkgctl.UpgradeClusterOptions{
ClusterName: clusterName,
Namespace: namespace,
TkrVersion: nextAvailableTkrVersion,
SkipPrompt: true,
Timeout: 15 * time.Minute,
})
Expect(err).ToNot(HaveOccurred())

}
25 changes: 19 additions & 6 deletions pkg/v1/tkg/test/tkgctl/tkgs/tkgs_workload_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/constants"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/test/framework"
. "github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/test/tkgctl/shared"
"github.com/vmware-tanzu/tanzu-framework/pkg/v1/tkg/tkgctl"
)

Expand Down Expand Up @@ -137,7 +138,9 @@ var _ = Describe("TKGS - Create workload cluster use cases", func() {
Expect(err).To(BeNil())
})
It("should return success or error based on the ClusterClass feature-gate status on the Supervisor", func() {
createClusterClassBasedClusterTest(tkgctlClient, deleteClusterOptions, true, clusterName, namespace)
createClusterClassBasedCluster(tkgctlClient, true, clusterName, namespace)
UpgradeClusterTest(tkgctlClient, clusterName, namespace)
deleteClusterClassBasedCluster(tkgctlClient, deleteClusterOptions, clusterName, namespace)
})
})
When("cluster class cli feature flag (features.global.package-based-lcm-beta) is set to false", func() {
Expand All @@ -148,21 +151,20 @@ var _ = Describe("TKGS - Create workload cluster use cases", func() {
Expect(err).To(BeNil())
})
It("should return success or error based on the ClusterClass feature-gate status on the Supervisor", func() {
createClusterClassBasedClusterTest(tkgctlClient, deleteClusterOptions, false, clusterName, namespace)
createClusterClassBasedCluster(tkgctlClient, false, clusterName, namespace)
UpgradeClusterTest(tkgctlClient, clusterName, namespace)
deleteClusterClassBasedCluster(tkgctlClient, deleteClusterOptions, clusterName, namespace)
})
})
})
})

// createClusterClassBasedClusterTest creates and deletes (if created successfully) workload cluster
func createClusterClassBasedClusterTest(tkgctlClient tkgctl.TKGClient, deleteClusterOptions tkgctl.DeleteClustersOptions, cliFlag bool, clusterName, namespace string) {
func createClusterClassBasedCluster(tkgctlClient tkgctl.TKGClient, cliFlag bool, clusterName, namespace string) {
if isClusterClassFeatureActivated {
By(fmt.Sprintf("creating Cluster class based workload cluster, ClusterClass feature-gate is activated and cli feature flag set %v", cliFlag))
err = tkgctlClient.CreateCluster(clusterOptions)
Expect(err).To(BeNil())
By(fmt.Sprintf("deleting cluster class based workload cluster %v in namespace: %v", clusterName, namespace))
err = tkgctlClient.DeleteCluster(deleteClusterOptions)
Expect(err).To(BeNil())
} else {
By(fmt.Sprintf("creating Cluster class based workload cluster, ClusterClass feature-gate is deactivated and cli feature flag set %v", cliFlag))
err = tkgctlClient.CreateCluster(clusterOptions)
Expand All @@ -171,6 +173,17 @@ func createClusterClassBasedClusterTest(tkgctlClient tkgctl.TKGClient, deleteClu
}
}

// deleteClusterClassBasedCluster deletes workload cluster
func deleteClusterClassBasedCluster(tkgctlClient tkgctl.TKGClient, deleteClusterOptions tkgctl.DeleteClustersOptions, clusterName, namespace string) {
if isClusterClassFeatureActivated {
By(fmt.Sprintf("deleting cluster class based workload cluster %v in namespace: %v", clusterName, namespace))
err = tkgctlClient.DeleteCluster(deleteClusterOptions)
Expect(err).To(BeNil())
} else {
// TODO: Delete cluster when feature is not activated
}
}

// createLegacyClusterTest creates and deletes (if created successfully) workload cluster
func createLegacyClusterTest(tkgctlClient tkgctl.TKGClient, deleteClusterOptions tkgctl.DeleteClustersOptions, cliFlag bool, clusterName, namespace string) {
if isTKCAPIFeatureActivated {
Expand Down

0 comments on commit dcfb314

Please # to comment.