Skip to content

Commit

Permalink
Update to make CLI backwards compatible with Kube versions < 1.25.
Browse files Browse the repository at this point in the history
[sc-16942]
  • Loading branch information
dsessler7 committed Dec 8, 2022
1 parent ab44919 commit fe1f3bc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
29 changes: 29 additions & 0 deletions internal/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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())
Expand Down
19 changes: 12 additions & 7 deletions testing/kuttl/e2e/version/00--check-version.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fe1f3bc

Please sign in to comment.