Skip to content

Commit fe1f3bc

Browse files
committed
Update to make CLI backwards compatible with Kube versions < 1.25.
[sc-16942]
1 parent ab44919 commit fe1f3bc

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

internal/cmd/export.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ import (
2727
"github.com/spf13/cobra"
2828
appsv1 "k8s.io/api/apps/v1"
2929
batchv1 "k8s.io/api/batch/v1"
30+
batchv1beta1 "k8s.io/api/batch/v1beta1"
3031
corev1 "k8s.io/api/core/v1"
3132
policyv1 "k8s.io/api/policy/v1"
33+
policyv1beta1 "k8s.io/api/policy/v1beta1"
3234
apierrors "k8s.io/apimachinery/pkg/api/errors"
3335
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3436
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -90,6 +92,17 @@ var namespacedResources = []schema.GroupVersionResource{{
9092
Resource: "serviceaccounts",
9193
}}
9294

95+
// These "removed" GVRs are for making our CLI backwards compatible with older PGO versions.
96+
var removedNamespacedResources = []schema.GroupVersionResource{{
97+
Group: batchv1beta1.SchemeGroupVersion.Group,
98+
Version: batchv1beta1.SchemeGroupVersion.Version,
99+
Resource: "cronjobs",
100+
}, {
101+
Group: policyv1beta1.SchemeGroupVersion.Group,
102+
Version: policyv1beta1.SchemeGroupVersion.Version,
103+
Resource: "poddisruptionbudgets",
104+
}}
105+
93106
// newSupportCommand returns the support subcommand of the PGO plugin.
94107
func newSupportExportCommand(config *internal.Config) *cobra.Command {
95108
cmd := &cobra.Command{
@@ -388,6 +401,22 @@ func gatherNamespacedAPIResources(ctx context.Context,
388401
List(ctx, metav1.ListOptions{
389402
LabelSelector: "postgres-operator.crunchydata.com/cluster=" + clusterName,
390403
})
404+
// If the API returns an IsNotFound error, it is likely because the kube version in use
405+
// doesn't support the version of the resource we are attempting to use and there is an
406+
// earlier version we can use. This block will check the "removed" resources for a match
407+
// and use it if it exists.
408+
if apierrors.IsNotFound(err) {
409+
for _, bgvr := range removedNamespacedResources {
410+
if bgvr.Resource == gvr.Resource {
411+
gvr = bgvr
412+
list, err = client.Resource(gvr).Namespace(namespace).
413+
List(ctx, metav1.ListOptions{
414+
LabelSelector: "postgres-operator.crunchydata.com/cluster=" + clusterName,
415+
})
416+
break
417+
}
418+
}
419+
}
391420
if err != nil {
392421
if apierrors.IsForbidden(err) {
393422
cmd.Println(err.Error())

testing/kuttl/e2e/version/00--check-version.yaml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@ apiVersion: kuttl.dev/v1beta1
22
kind: TestStep
33
commands:
44
- script: |
5-
OPERATOR_VERSION=$(
6-
kubectl get crd postgresclusters.postgres-operator.crunchydata.com \
7-
-o yaml | \
8-
grep app.kubernetes.io/version | \
9-
awk '{print $2}'
10-
)
11-
125
VERSION_OUTPUT=$(kubectl pgo version)
136
CLI_VERSION=$(kubectl pgo version | awk '{print $3}')
147
158
# the CLI version isn't empty and the CLI version output follows the expected format
169
if [[ -z $CLI_VERSION || $VERSION_OUTPUT != *"Client Version: "* ]]; then
10+
echo "Version output is: "
11+
echo "'$VERSION_OUTPUT'"
1712
exit 1
1813
fi
14+
- script: |
15+
OPERATOR_VERSION=$(
16+
kubectl get crd postgresclusters.postgres-operator.crunchydata.com \
17+
-o go-template='{{ index .metadata.labels "app.kubernetes.io/version" }}'
18+
)
19+
20+
VERSION_OUTPUT=$(kubectl pgo version)
1921
2022
# the operator version isn't empty and the version output matches the CRD value
2123
if [[ -z $OPERATOR_VERSION || $VERSION_OUTPUT != *"Operator Version: v$OPERATOR_VERSION"* ]]; then
24+
echo "Version output is: "
25+
echo "$VERSION_OUTPUT"
26+
echo "Expected: v$OPERATOR_VERSION"
2227
exit 1
2328
fi

0 commit comments

Comments
 (0)