From fe1f3bcdbef8efaa5c4d6d6faebd576e1b84249d Mon Sep 17 00:00:00 2001 From: Drew Sessler Date: Wed, 7 Dec 2022 23:55:44 +0000 Subject: [PATCH] Update to make CLI backwards compatible with Kube versions < 1.25. [sc-16942] --- internal/cmd/export.go | 29 +++++++++++++++++++ .../kuttl/e2e/version/00--check-version.yaml | 19 +++++++----- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/internal/cmd/export.go b/internal/cmd/export.go index a1273eba..591fd455 100644 --- a/internal/cmd/export.go +++ b/internal/cmd/export.go @@ -27,8 +27,10 @@ import ( "github.com/spf13/cobra" appsv1 "k8s.io/api/apps/v1" batchv1 "k8s.io/api/batch/v1" + batchv1beta1 "k8s.io/api/batch/v1beta1" corev1 "k8s.io/api/core/v1" policyv1 "k8s.io/api/policy/v1" + policyv1beta1 "k8s.io/api/policy/v1beta1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -90,6 +92,17 @@ var namespacedResources = []schema.GroupVersionResource{{ Resource: "serviceaccounts", }} +// These "removed" GVRs are for making our CLI backwards compatible with older PGO versions. +var removedNamespacedResources = []schema.GroupVersionResource{{ + Group: batchv1beta1.SchemeGroupVersion.Group, + Version: batchv1beta1.SchemeGroupVersion.Version, + Resource: "cronjobs", +}, { + Group: policyv1beta1.SchemeGroupVersion.Group, + Version: policyv1beta1.SchemeGroupVersion.Version, + Resource: "poddisruptionbudgets", +}} + // newSupportCommand returns the support subcommand of the PGO plugin. func newSupportExportCommand(config *internal.Config) *cobra.Command { cmd := &cobra.Command{ @@ -388,6 +401,22 @@ func gatherNamespacedAPIResources(ctx context.Context, List(ctx, metav1.ListOptions{ LabelSelector: "postgres-operator.crunchydata.com/cluster=" + clusterName, }) + // If the API returns an IsNotFound error, it is likely because the kube version in use + // doesn't support the version of the resource we are attempting to use and there is an + // earlier version we can use. This block will check the "removed" resources for a match + // and use it if it exists. + if apierrors.IsNotFound(err) { + for _, bgvr := range removedNamespacedResources { + if bgvr.Resource == gvr.Resource { + gvr = bgvr + list, err = client.Resource(gvr).Namespace(namespace). + List(ctx, metav1.ListOptions{ + LabelSelector: "postgres-operator.crunchydata.com/cluster=" + clusterName, + }) + break + } + } + } if err != nil { if apierrors.IsForbidden(err) { cmd.Println(err.Error()) diff --git a/testing/kuttl/e2e/version/00--check-version.yaml b/testing/kuttl/e2e/version/00--check-version.yaml index c061f5a3..5bdd803d 100644 --- a/testing/kuttl/e2e/version/00--check-version.yaml +++ b/testing/kuttl/e2e/version/00--check-version.yaml @@ -2,22 +2,27 @@ apiVersion: kuttl.dev/v1beta1 kind: TestStep commands: - script: | - OPERATOR_VERSION=$( - kubectl get crd postgresclusters.postgres-operator.crunchydata.com \ - -o yaml | \ - grep app.kubernetes.io/version | \ - awk '{print $2}' - ) - VERSION_OUTPUT=$(kubectl pgo version) CLI_VERSION=$(kubectl pgo version | awk '{print $3}') # the CLI version isn't empty and the CLI version output follows the expected format if [[ -z $CLI_VERSION || $VERSION_OUTPUT != *"Client Version: "* ]]; then + echo "Version output is: " + echo "'$VERSION_OUTPUT'" exit 1 fi +- script: | + OPERATOR_VERSION=$( + kubectl get crd postgresclusters.postgres-operator.crunchydata.com \ + -o go-template='{{ index .metadata.labels "app.kubernetes.io/version" }}' + ) + + VERSION_OUTPUT=$(kubectl pgo version) # the operator version isn't empty and the version output matches the CRD value if [[ -z $OPERATOR_VERSION || $VERSION_OUTPUT != *"Operator Version: v$OPERATOR_VERSION"* ]]; then + echo "Version output is: " + echo "$VERSION_OUTPUT" + echo "Expected: v$OPERATOR_VERSION" exit 1 fi