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 Aug 3, 2022
1 parent dd437be commit 700c954
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
111 changes: 111 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,12 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"github.com/vmware-tanzu/tanzu-framework/apis/run/v1alpha1"
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 @@ -223,3 +227,110 @@ func E2ECommonSpec(ctx context.Context, inputGetter func() E2ECommonSpecInput) {
By("Test successful !")
})
}

func getNextAvailableTkrVersion(tkgctlClient tkgctl.TKGClient, currentTkrVersion string) (string, error) {
var foundVersion string

tkrs, err := tkgctlClient.GetTanzuKubernetesReleases("")
if err != nil {
return "", err
}

for i := range tkrs {
if !isCompatible(tkrs[i]) {
continue
}

if _, exists := tkrs[i].Labels[runv1alpha3.LabelDeactivated]; exists {
continue
}

specVersionIsNewer, err := isNewerVMwareVersion(tkrs[i].Spec.Version, currentTkrVersion)
if err != nil {
return "", err
}
if specVersionIsNewer {
// if we don't already have a foundVersion we take spec.version
foundVersion, err = pickOlderVMwareVersion(foundVersion, tkrs[i].Spec.Version)
if err != nil {
return "", err
}
}
}

if foundVersion == "" {
return "", fmt.Errorf("no TKR version available for upgrade")
}

return foundVersion, nil
}

func isCompatible(tkr v1alpha1.TanzuKubernetesRelease) bool {
var compatible string
for _, condition := range tkr.Status.Conditions {
if condition.Type == runv1alpha3.ConditionCompatible {
compatible = string(condition.Status)
break
}
}
if !strings.EqualFold(compatible, "true") {
return false
}
return true
}

func isNewerVMwareVersion(versionA, versionB string) (bool, error) {
compareResult, err := utils.CompareVMwareVersionStrings(versionB, versionA)
if err != nil {
return false, err
}
if compareResult < 0 {
return true, nil
}
return false, nil
}

func pickOlderVMwareVersion(tkrVersionA, tkrVersionB string) (string, error) {
var returnValue string
if tkrVersionA == "" {
returnValue = tkrVersionB
} else {
compareResult, err := utils.CompareVMwareVersionStrings(tkrVersionA, tkrVersionB)
if err != nil {
return "", err
}
if compareResult > 0 {
returnValue = tkrVersionB
}
}
return returnValue, 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,
})
Expect(err).ToNot(HaveOccurred())

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

. "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 @@ -54,6 +55,7 @@ var _ = Describe("TKGS ClusterClass based workload cluster tests", func() {

It("should successfully create a cluster", func() {
Expect(err).ToNot(HaveOccurred())
UpgradeClusterTest(tkgctlClient, clusterName, namespace)
})
})

Expand Down Expand Up @@ -81,6 +83,7 @@ var _ = Describe("TKGS ClusterClass based workload cluster tests", func() {

It("should successfully create a cluster", func() {
Expect(err).ToNot(HaveOccurred())
UpgradeClusterTest(tkgctlClient, clusterName, namespace)
})
})

Expand Down

0 comments on commit 700c954

Please # to comment.