Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -10288,6 +10288,9 @@ spec:
upgrade to apply changes to Kubernetes objects. Default is the latest
version.
type: string
x-kubernetes-validations:
- message: CRVersion must be a valid semantic version
rule: self == "" || self.matches('^[0-9]+\\.[0-9]+\\.[0-9]+(-[a-zA-Z0-9.+-]+)?$')
dataSource:
description: Specifies a data source for bootstrapping the PostgreSQL
cluster.
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 @@ -10983,6 +10983,9 @@ spec:
upgrade to apply changes to Kubernetes objects. Default is the latest
version.
type: string
x-kubernetes-validations:
- message: CRVersion must be a valid semantic version
rule: self == "" || self.matches('^[0-9]+\\.[0-9]+\\.[0-9]+(-[a-zA-Z0-9.+-]+)?$')
dataSource:
description: Specifies a data source for bootstrapping the PostgreSQL
cluster.
Expand Down
2 changes: 2 additions & 0 deletions deploy/bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29774,6 +29774,8 @@ spec:
and is true
rule: '!has(self.users) || self.postgresVersion >= 15 || self.users.all(u,
!has(u.grantPublicSchemaAccess) || !u.grantPublicSchemaAccess)'
- message: CRVersion must be a valid semantic version
rule: '!has(self.crVersion) || self.crVersion == "" || self.crVersion.matches(''^[0-9]+\\.[0-9]+\\.[0-9]+(-[a-zA-Z0-9.+-]+)?$'')'
status:
properties:
conditions:
Expand Down
3 changes: 3 additions & 0 deletions deploy/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11280,6 +11280,9 @@ spec:
upgrade to apply changes to Kubernetes objects. Default is the latest
version.
type: string
x-kubernetes-validations:
- message: CRVersion must be a valid semantic version
rule: self == "" || self.matches('^[0-9]+\\.[0-9]+\\.[0-9]+(-[a-zA-Z0-9.+-]+)?$')
dataSource:
description: Specifies a data source for bootstrapping the PostgreSQL
cluster.
Expand Down
2 changes: 2 additions & 0 deletions deploy/cw-bundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29774,6 +29774,8 @@ spec:
and is true
rule: '!has(self.users) || self.postgresVersion >= 15 || self.users.all(u,
!has(u.grantPublicSchemaAccess) || !u.grantPublicSchemaAccess)'
- message: CRVersion must be a valid semantic version
rule: '!has(self.crVersion) || self.crVersion == "" || self.crVersion.matches(''^[0-9]+\\.[0-9]+\\.[0-9]+(-[a-zA-Z0-9.+-]+)?$'')'
status:
properties:
conditions:
Expand Down
7 changes: 6 additions & 1 deletion pkg/apis/pgv2.percona.com/v2/perconapgcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type PerconaPGClusterSpec struct {
// upgrade to apply changes to Kubernetes objects. Default is the latest
// version.
// +optional
// +kubebuilder:validation:XValidation:rule="self == \"\" || self.matches('^[0-9]+\\\\.[0-9]+\\\\.[0-9]+(-[a-zA-Z0-9.+-]+)?$')",message="CRVersion must be a valid semantic version"
CRVersion string `json:"crVersion,omitempty"`
Comment on lines 60 to 64

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed


InitContainer *crunchyv1beta1.InitContainerSpec `json:"initContainer,omitempty"`
Expand Down Expand Up @@ -517,7 +518,11 @@ func (cr *PerconaPGCluster) ToCrunchy(ctx context.Context, postgresCluster *crun
}

func (cr *PerconaPGCluster) Version() *gover.Version {
return gover.Must(gover.NewVersion(cr.Spec.CRVersion))
crVersion := cr.Spec.CRVersion
if crVersion == "" {
crVersion = version.Version()
}
Comment on lines +521 to +524

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This test case would only work on e2e tests

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we don't need to test ShouldCheckStandbyLag regression but it'd be great to add a simple unit test that covers PerconaPGCluster.Version() behavior

return gover.Must(gover.NewVersion(crVersion))
Comment on lines +521 to +525

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, this only falls back with the assumption that the opeator will reconcile the cluster and set the version asynchronously. Falling back to version.Version() on non empty can introduce hard to find bugs. But a way to do it, would actually be a validation pattern on the CRD

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i agree

}

func (cr *PerconaPGCluster) CompareVersion(ver string) int {
Expand Down
19 changes: 19 additions & 0 deletions pkg/apis/pgv2.percona.com/v2/perconapgcluster_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ func TestPerconaPGCluster_Validate(t *testing.T) {
})
}

func TestPerconaPGCluster_Version(t *testing.T) {
t.Run("empty CRVersion does not crash", func(t *testing.T) {
cr := new(PerconaPGCluster)
// cr.Version() should not panic when CRVersion is empty
ver := cr.Version()
require.NotNil(t, ver)
// Should return the default operator version
assert.Equal(t, version.Version(), ver.String())
})

t.Run("valid CRVersion is parsed correctly", func(t *testing.T) {
cr := new(PerconaPGCluster)
cr.Spec.CRVersion = "2.5.0"
ver := cr.Version()
require.NotNil(t, ver)
assert.Equal(t, "2.5.0", ver.String())
})
}

func TestPerconaPGCluster_Proxy(t *testing.T) {
t.Run("Proxy is nil, CRVersion < 2.9.0", func(t *testing.T) {
cr := new(PerconaPGCluster)
Expand Down
Loading