Skip to content

K8SPG-777 set crVersion #1210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5237,6 +5237,9 @@ spec:
Version of the operator. Update this to new version after operator
upgrade to apply changes to Kubernetes objects. Default is the latest
version.
Version is the application version in the format X.Y.Z (e.g., "2.7.0").
example: 2.7.0
pattern: ^$|^\d+\.\d+\.\d+$
type: string
dataSource:
description: Specifies a data source for bootstrapping the PostgreSQL
Expand Down
3 changes: 3 additions & 0 deletions config/crd/bases/pgv2.percona.com_perconapgclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5644,6 +5644,9 @@ spec:
Version of the operator. Update this to new version after operator
upgrade to apply changes to Kubernetes objects. Default is the latest
version.
Version is the application version in the format X.Y.Z (e.g., "2.7.0").
example: 2.7.0
pattern: ^$|^\d+\.\d+\.\d+$
type: string
dataSource:
description: Specifies a data source for bootstrapping the PostgreSQL
Expand Down
3 changes: 3 additions & 0 deletions deploy/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5941,6 +5941,9 @@ spec:
Version of the operator. Update this to new version after operator
upgrade to apply changes to Kubernetes objects. Default is the latest
version.
Version is the application version in the format X.Y.Z (e.g., "2.7.0").
example: 2.7.0
pattern: ^$|^\d+\.\d+\.\d+$
type: string
dataSource:
description: Specifies a data source for bootstrapping the PostgreSQL
Expand Down
2 changes: 1 addition & 1 deletion deploy/cr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ metadata:
# - percona.com/delete-ssl
# - percona.com/delete-backups
spec:
crVersion: 2.7.0
crVersion: "2.7.0"
# initContainer:
# image: perconalab/percona-postgresql-operator:main
# resources:
Expand Down
3 changes: 3 additions & 0 deletions deploy/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5941,6 +5941,9 @@ spec:
Version of the operator. Update this to new version after operator
upgrade to apply changes to Kubernetes objects. Default is the latest
version.
Version is the application version in the format X.Y.Z (e.g., "2.7.0").
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I use 2.6.0 with this CRD?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it was like en example.
how can we make it better?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

example: 2.7.0
pattern: ^$|^\d+\.\d+\.\d+$
type: string
dataSource:
description: Specifies a data source for bootstrapping the PostgreSQL
Expand Down
3 changes: 3 additions & 0 deletions deploy/cw-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5941,6 +5941,9 @@ spec:
Version of the operator. Update this to new version after operator
upgrade to apply changes to Kubernetes objects. Default is the latest
version.
Version is the application version in the format X.Y.Z (e.g., "2.7.0").
example: 2.7.0
pattern: ^$|^\d+\.\d+\.\d+$
type: string
dataSource:
description: Specifies a data source for bootstrapping the PostgreSQL
Expand Down
22 changes: 22 additions & 0 deletions percona/controller/pgcluster/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"reflect"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[goimports-reviser] reported by reviewdog 🐶

Suggested change
logf "sigs.k8s.io/controller-runtime/pkg/log"

"slices"
"strings"
"time"
Expand Down Expand Up @@ -49,6 +50,7 @@ import (
"github.com/percona/percona-postgresql-operator/percona/pmm"
perconaPG "github.com/percona/percona-postgresql-operator/percona/postgres"
"github.com/percona/percona-postgresql-operator/percona/utils/registry"
"github.com/percona/percona-postgresql-operator/percona/version"
"github.com/percona/percona-postgresql-operator/percona/watcher"
v2 "github.com/percona/percona-postgresql-operator/pkg/apis/pgv2.percona.com/v2"
"github.com/percona/percona-postgresql-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
Expand Down Expand Up @@ -201,6 +203,9 @@ func (r *PGClusterReconciler) Reconcile(ctx context.Context, request reconcile.R
return ctrl.Result{}, errors.Wrap(err, "get PerconaPGCluster")
}

if err := r.setCRVersion(ctx, cr); err != nil {
return reconcile.Result{}, errors.Wrap(err, "set CR version")
}
cr.Default()

if cr.Spec.OpenShift == nil {
Expand Down Expand Up @@ -964,3 +969,20 @@ func (r *PGClusterReconciler) ensureFinalizers(ctx context.Context, cr *v2.Perco

return nil
}

func (r *PGClusterReconciler) setCRVersion(ctx context.Context, cr *v2.PerconaPGCluster) error {
if len(cr.Spec.CRVersion) > 0 {
return nil
}

orig := cr.DeepCopy()
cr.Spec.CRVersion = version.Version()

if err := r.Client.Patch(ctx, cr, client.MergeFrom(orig)); err != nil {
return errors.Wrap(err, "patch CR")
}

logf.FromContext(ctx).Info("Set CR version", "version", cr.Spec.CRVersion)

return nil
}
77 changes: 76 additions & 1 deletion percona/controller/pgcluster/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/percona/percona-postgresql-operator/internal/feature"
"github.com/percona/percona-postgresql-operator/internal/naming"
pNaming "github.com/percona/percona-postgresql-operator/percona/naming"
"github.com/percona/percona-postgresql-operator/percona/version"
v2 "github.com/percona/percona-postgresql-operator/pkg/apis/pgv2.percona.com/v2"
"github.com/percona/percona-postgresql-operator/pkg/apis/postgres-operator.crunchydata.com/v1beta1"
)
Expand Down Expand Up @@ -686,7 +687,7 @@ var _ = Describe("Version labels", Ordered, func() {
})

cr, err := readDefaultCR(crName, ns)
It("should read defautl cr.yaml", func() {
It("should read default cr.yaml", func() {
Expect(err).NotTo(HaveOccurred())
})

Expand Down Expand Up @@ -2265,3 +2266,77 @@ var _ = Describe("Init Container", Ordered, func() {
})
})
})

var _ = Describe("CR Version Management", Ordered, func() {
ctx := context.Background()
const crName = "cr-version"
const ns = crName

namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: crName,
Namespace: ns,
},
}

BeforeAll(func() {
By("Creating the Namespace for CR version tests")
err := k8sClient.Create(ctx, namespace)
Expect(err).To(Not(HaveOccurred()))
})

AfterAll(func() {
By("Deleting the Namespace after CR version tests")
_ = k8sClient.Delete(ctx, namespace)
})

Context("setCRVersion logic", Ordered, func() {
When("the CRVersion is already set", func() {
It("should not change the CRVersion", func() {
cr, err := readDefaultCR("cr-version-1", ns)
Expect(err).NotTo(HaveOccurred())

cr.Spec.CRVersion = "2.7.0"
Expect(k8sClient.Create(ctx, cr)).Should(Succeed())

reconciler := &PGClusterReconciler{Client: k8sClient}
err = reconciler.setCRVersion(ctx, cr)
Expect(err).NotTo(HaveOccurred())
Expect(cr.Spec.CRVersion).To(Equal("2.7.0"))
})
})

When("the CRVersion is empty", func() {
It("should set CRVersion and patch the resource", func() {
cr, err := readDefaultCR("cr-version-2", ns)
Expect(err).NotTo(HaveOccurred())

cr.Spec.CRVersion = ""
Expect(k8sClient.Create(ctx, cr)).Should(Succeed())

reconciler := &PGClusterReconciler{Client: k8sClient}
err = reconciler.setCRVersion(ctx, cr)
Expect(err).NotTo(HaveOccurred())

// Fetch the CR again to verify the patch was applied in the cluster
updated := &v2.PerconaPGCluster{}
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: cr.Name, Namespace: cr.Namespace}, updated)).Should(Succeed())
Expect(updated.Spec.CRVersion).To(Equal(version.Version()))
})
})

When("the patch operation fails", func() {
It("should return an error", func() {
cr, err := readDefaultCR("cr-version-3", ns)
Expect(err).NotTo(HaveOccurred())
cr.Spec.CRVersion = ""

// Do NOT create the CR in k8s, so Patch will fail (object does not exist)
reconciler := &PGClusterReconciler{Client: k8sClient}
err = reconciler.setCRVersion(ctx, cr)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("patch CR"))
})
})
})
})
3 changes: 3 additions & 0 deletions pkg/apis/pgv2.percona.com/v2/perconapgcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ type PerconaPGClusterSpec struct {
// upgrade to apply changes to Kubernetes objects. Default is the latest
// version.
// +optional
// Version is the application version in the format X.Y.Z (e.g., "2.7.0").
// +kubebuilder:validation:Pattern=`^$|^\d+\.\d+\.\d+$`
// +kubebuilder:example="2.7.0"
CRVersion string `json:"crVersion,omitempty"`

InitContainer *crunchyv1beta1.InitContainerSpec `json:"initContainer,omitempty"`
Expand Down
Loading