Skip to content
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

Define a struct to share validation rules for PVC specs #4131

Merged
merged 1 commit into from
Mar 11, 2025
Merged
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
@@ -1560,6 +1560,12 @@ spec:
backing this claim.
type: string
type: object
x-kubernetes-map-type: atomic
x-kubernetes-validations:
- message: missing accessModes
rule: 0 < size(self.accessModes)
- message: missing storage request
rule: has(self.resources.requests.storage)
image:
description: The image name to use for pgAdmin instance.
type: string
Original file line number Diff line number Diff line change
@@ -3197,13 +3197,12 @@ spec:
to the PersistentVolume backing this claim.
type: string
type: object
x-kubernetes-map-type: atomic
x-kubernetes-validations:
- message: missing accessModes
rule: has(self.accessModes) && size(self.accessModes)
> 0
rule: 0 < size(self.accessModes)
- message: missing storage request
rule: has(self.resources) && has(self.resources.requests)
&& has(self.resources.requests.storage)
rule: has(self.resources.requests.storage)
required:
- volumeClaimSpec
type: object
@@ -6524,13 +6523,12 @@ spec:
to the PersistentVolume backing this claim.
type: string
type: object
x-kubernetes-map-type: atomic
x-kubernetes-validations:
- message: missing accessModes
rule: has(self.accessModes) && size(self.accessModes)
> 0
rule: 0 < size(self.accessModes)
- message: missing storage request
rule: has(self.resources) && has(self.resources.requests)
&& has(self.resources.requests.storage)
rule: has(self.resources.requests.storage)
required:
- volumeClaimSpec
type: object
@@ -10412,12 +10410,12 @@ spec:
PersistentVolume backing this claim.
type: string
type: object
x-kubernetes-map-type: atomic
x-kubernetes-validations:
- message: missing accessModes
rule: has(self.accessModes) && size(self.accessModes) > 0
rule: 0 < size(self.accessModes)
- message: missing storage request
rule: has(self.resources) && has(self.resources.requests)
&& has(self.resources.requests.storage)
rule: has(self.resources.requests.storage)
metadata:
description: Metadata contains metadata for custom resources
properties:
@@ -10797,13 +10795,12 @@ spec:
the PersistentVolume backing this claim.
type: string
type: object
x-kubernetes-map-type: atomic
x-kubernetes-validations:
- message: missing accessModes
rule: has(self.accessModes) && size(self.accessModes)
> 0
rule: 0 < size(self.accessModes)
- message: missing storage request
rule: has(self.resources) && has(self.resources.requests)
&& has(self.resources.requests.storage)
rule: has(self.resources.requests.storage)
name:
description: |-
The name for the tablespace, used as the path name for the volume.
@@ -11238,12 +11235,12 @@ spec:
PersistentVolume backing this claim.
type: string
type: object
x-kubernetes-map-type: atomic
x-kubernetes-validations:
- message: missing accessModes
rule: has(self.accessModes) && size(self.accessModes) > 0
rule: 0 < size(self.accessModes)
- message: missing storage request
rule: has(self.resources) && has(self.resources.requests)
&& has(self.resources.requests.storage)
rule: has(self.resources.requests.storage)
required:
- dataVolumeClaimSpec
type: object
@@ -17328,6 +17325,12 @@ spec:
PersistentVolume backing this claim.
type: string
type: object
x-kubernetes-map-type: atomic
x-kubernetes-validations:
- message: missing accessModes
rule: 0 < size(self.accessModes)
- message: missing storage request
rule: has(self.resources.requests.storage)
image:
description: |-
Name of a container image that can run pgAdmin 4. Changing this value causes
4 changes: 2 additions & 2 deletions internal/controller/postgrescluster/helpers_test.go
Original file line number Diff line number Diff line change
@@ -90,9 +90,9 @@ func setupNamespace(t testing.TB, cc client.Client) *corev1.Namespace {
return require.Namespace(t, cc)
}

func testVolumeClaimSpec() corev1.PersistentVolumeClaimSpec {
func testVolumeClaimSpec() v1beta1.VolumeClaimSpec {
// Defines a volume claim spec that can be used to create instances
return corev1.PersistentVolumeClaimSpec{
return v1beta1.VolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
Resources: corev1.VolumeResourceRequirements{
Requests: map[corev1.ResourceName]resource.Quantity{
8 changes: 4 additions & 4 deletions internal/controller/postgrescluster/instance_test.go
Original file line number Diff line number Diff line change
@@ -280,7 +280,7 @@ func TestStoreDesiredRequest(t *testing.T) {
InstanceSets: []v1beta1.PostgresInstanceSetSpec{{
Name: "red",
Replicas: initialize.Int32(1),
DataVolumeClaimSpec: corev1.PersistentVolumeClaimSpec{
DataVolumeClaimSpec: v1beta1.VolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
Resources: corev1.VolumeResourceRequirements{
Limits: map[corev1.ResourceName]resource.Quantity{
@@ -1850,7 +1850,7 @@ func TestFindAvailableInstanceNames(t *testing.T) {
expectedInstanceNames: []string{"instance1-def"},
}, {
set: v1beta1.PostgresInstanceSetSpec{Name: "instance1",
WALVolumeClaimSpec: &corev1.PersistentVolumeClaimSpec{}},
WALVolumeClaimSpec: &v1beta1.VolumeClaimSpec{}},
fakeObservedInstances: newObservedInstances(
&v1beta1.PostgresCluster{Spec: v1beta1.PostgresClusterSpec{
InstanceSets: []v1beta1.PostgresInstanceSetSpec{{Name: "instance1"}},
@@ -1877,7 +1877,7 @@ func TestFindAvailableInstanceNames(t *testing.T) {
expectedInstanceNames: []string{},
}, {
set: v1beta1.PostgresInstanceSetSpec{Name: "instance1",
WALVolumeClaimSpec: &corev1.PersistentVolumeClaimSpec{}},
WALVolumeClaimSpec: &v1beta1.VolumeClaimSpec{}},
fakeObservedInstances: newObservedInstances(
&v1beta1.PostgresCluster{Spec: v1beta1.PostgresClusterSpec{
InstanceSets: []v1beta1.PostgresInstanceSetSpec{{Name: "instance1"}},
@@ -1901,7 +1901,7 @@ func TestFindAvailableInstanceNames(t *testing.T) {
expectedInstanceNames: []string{"instance1-def"},
}, {
set: v1beta1.PostgresInstanceSetSpec{Name: "instance1",
WALVolumeClaimSpec: &corev1.PersistentVolumeClaimSpec{}},
WALVolumeClaimSpec: &v1beta1.VolumeClaimSpec{}},
fakeObservedInstances: newObservedInstances(
&v1beta1.PostgresCluster{Spec: v1beta1.PostgresClusterSpec{
InstanceSets: []v1beta1.PostgresInstanceSetSpec{{Name: "instance1"}},
2 changes: 1 addition & 1 deletion internal/controller/postgrescluster/pgadmin.go
Original file line number Diff line number Diff line change
@@ -405,7 +405,7 @@ func (r *Reconciler) reconcilePGAdminDataVolume(
cluster.Spec.Metadata.GetLabelsOrNil(),
labelMap,
)
pvc.Spec = cluster.Spec.UserInterface.PGAdmin.DataVolumeClaimSpec
pvc.Spec = cluster.Spec.UserInterface.PGAdmin.DataVolumeClaimSpec.AsPersistentVolumeClaimSpec()

err := errors.WithStack(r.setControllerReference(cluster, pvc))

4 changes: 2 additions & 2 deletions internal/controller/postgrescluster/pgadmin_test.go
Original file line number Diff line number Diff line change
@@ -853,7 +853,7 @@ func pgAdminTestCluster(ns corev1.Namespace) *v1beta1.PostgresCluster {
Repos: []v1beta1.PGBackRestRepo{{
Name: "repo1",
Volume: &v1beta1.RepoPVC{
VolumeClaimSpec: corev1.PersistentVolumeClaimSpec{
VolumeClaimSpec: v1beta1.VolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
Resources: corev1.VolumeResourceRequirements{
Requests: corev1.ResourceList{
@@ -868,7 +868,7 @@ func pgAdminTestCluster(ns corev1.Namespace) *v1beta1.PostgresCluster {
UserInterface: &v1beta1.UserInterfaceSpec{
PGAdmin: &v1beta1.PGAdminPodSpec{
Image: "test-image",
DataVolumeClaimSpec: corev1.PersistentVolumeClaimSpec{
DataVolumeClaimSpec: v1beta1.VolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
Resources: corev1.VolumeResourceRequirements{
Requests: corev1.ResourceList{
3 changes: 2 additions & 1 deletion internal/controller/postgrescluster/pgbackrest.go
Original file line number Diff line number Diff line change
@@ -2628,7 +2628,8 @@ func (r *Reconciler) reconcileRepos(ctx context.Context,
if repo.Volume == nil {
continue
}
repo, err := r.applyRepoVolumeIntent(ctx, postgresCluster, repo.Volume.VolumeClaimSpec,
repo, err := r.applyRepoVolumeIntent(ctx, postgresCluster,
repo.Volume.VolumeClaimSpec.AsPersistentVolumeClaimSpec(),
repo.Name, repoResources)
if err != nil {
log.Error(err, errMsg)
8 changes: 4 additions & 4 deletions internal/controller/postgrescluster/pgbackrest_test.go
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ func fakePostgresCluster(clusterName, namespace, clusterUID string,
Image: "example.com/crunchy-postgres-ha:test",
InstanceSets: []v1beta1.PostgresInstanceSetSpec{{
Name: "instance1",
DataVolumeClaimSpec: corev1.PersistentVolumeClaimSpec{
DataVolumeClaimSpec: v1beta1.VolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteMany},
Resources: corev1.VolumeResourceRequirements{
Requests: corev1.ResourceList{
@@ -115,7 +115,7 @@ func fakePostgresCluster(clusterName, namespace, clusterUID string,
postgresCluster.Spec.Backups.PGBackRest.Repos[0] = v1beta1.PGBackRestRepo{
Name: "repo1",
Volume: &v1beta1.RepoPVC{
VolumeClaimSpec: corev1.PersistentVolumeClaimSpec{
VolumeClaimSpec: v1beta1.VolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteMany},
Resources: corev1.VolumeResourceRequirements{
Requests: map[corev1.ResourceName]resource.Quantity{
@@ -2268,7 +2268,7 @@ func TestCopyConfigurationResources(t *testing.T) {
Image: "example.com/crunchy-postgres-ha:test",
InstanceSets: []v1beta1.PostgresInstanceSetSpec{{
Name: "instance1",
DataVolumeClaimSpec: corev1.PersistentVolumeClaimSpec{
DataVolumeClaimSpec: v1beta1.VolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteMany},
Resources: corev1.VolumeResourceRequirements{
Requests: corev1.ResourceList{
@@ -2320,7 +2320,7 @@ func TestCopyConfigurationResources(t *testing.T) {
},
InstanceSets: []v1beta1.PostgresInstanceSetSpec{{
Name: "instance1",
DataVolumeClaimSpec: corev1.PersistentVolumeClaimSpec{
DataVolumeClaimSpec: v1beta1.VolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteMany},
Resources: corev1.VolumeResourceRequirements{
Requests: corev1.ResourceList{
6 changes: 3 additions & 3 deletions internal/controller/postgrescluster/postgres.go
Original file line number Diff line number Diff line change
@@ -747,7 +747,7 @@ func (r *Reconciler) reconcilePostgresDataVolume(
labelMap,
)

pvc.Spec = instanceSpec.DataVolumeClaimSpec
pvc.Spec = instanceSpec.DataVolumeClaimSpec.AsPersistentVolumeClaimSpec()

// If a source cluster was provided and VolumeSnapshots are turned on in the source cluster and
// there is a VolumeSnapshot available for the source cluster that is ReadyToUse, use it as the
@@ -910,7 +910,7 @@ func (r *Reconciler) reconcileTablespaceVolumes(
labelMap,
)

pvc.Spec = vol.DataVolumeClaimSpec
pvc.Spec = vol.DataVolumeClaimSpec.AsPersistentVolumeClaimSpec()

if err == nil {
err = r.handlePersistentVolumeClaimError(cluster,
@@ -1017,7 +1017,7 @@ func (r *Reconciler) reconcilePostgresWALVolume(
labelMap,
)

pvc.Spec = *instanceSpec.WALVolumeClaimSpec
pvc.Spec = instanceSpec.WALVolumeClaimSpec.AsPersistentVolumeClaimSpec()

if err == nil {
err = r.handlePersistentVolumeClaimError(cluster,
20 changes: 10 additions & 10 deletions internal/controller/postgrescluster/postgres_test.go
Original file line number Diff line number Diff line change
@@ -883,7 +883,7 @@ func TestSetVolumeSize(t *testing.T) {
instanceSetSpec := func(request, limit string) *v1beta1.PostgresInstanceSetSpec {
return &v1beta1.PostgresInstanceSetSpec{
Name: "some-instance",
DataVolumeClaimSpec: corev1.PersistentVolumeClaimSpec{
DataVolumeClaimSpec: v1beta1.VolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
Resources: corev1.VolumeResourceRequirements{
Requests: map[corev1.ResourceName]resource.Quantity{
@@ -911,7 +911,7 @@ func TestSetVolumeSize(t *testing.T) {

pvc := &corev1.PersistentVolumeClaim{ObjectMeta: naming.InstancePostgresDataVolume(instance)}
spec := instanceSetSpec("4Gi", "3Gi")
pvc.Spec = spec.DataVolumeClaimSpec
pvc.Spec = spec.DataVolumeClaimSpec.AsPersistentVolumeClaimSpec()

reconciler.setVolumeSize(ctx, &cluster, pvc, spec.Name)

@@ -948,7 +948,7 @@ resources:
}},
}

pvc.Spec = spec.DataVolumeClaimSpec
pvc.Spec = spec.DataVolumeClaimSpec.AsPersistentVolumeClaimSpec()

reconciler.setVolumeSize(ctx, &cluster, pvc, spec.Name)

@@ -984,14 +984,14 @@ resources:
pvc := &corev1.PersistentVolumeClaim{ObjectMeta: naming.InstancePostgresDataVolume(instance)}
spec := &v1beta1.PostgresInstanceSetSpec{
Name: "some-instance",
DataVolumeClaimSpec: corev1.PersistentVolumeClaimSpec{
DataVolumeClaimSpec: v1beta1.VolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce},
Resources: corev1.VolumeResourceRequirements{
Requests: map[corev1.ResourceName]resource.Quantity{
corev1.ResourceStorage: resource.MustParse("1Gi"),
}}}}
cluster.Status = desiredStatus("2Gi")
pvc.Spec = spec.DataVolumeClaimSpec
pvc.Spec = spec.DataVolumeClaimSpec.AsPersistentVolumeClaimSpec()

reconciler.setVolumeSize(ctx, &cluster, pvc, spec.Name)

@@ -1016,7 +1016,7 @@ resources:

pvc := &corev1.PersistentVolumeClaim{ObjectMeta: naming.InstancePostgresDataVolume(instance)}
spec := instanceSetSpec("1Gi", "2Gi")
pvc.Spec = spec.DataVolumeClaimSpec
pvc.Spec = spec.DataVolumeClaimSpec.AsPersistentVolumeClaimSpec()

reconciler.setVolumeSize(ctx, &cluster, pvc, spec.Name)

@@ -1041,7 +1041,7 @@ resources:
pvc := &corev1.PersistentVolumeClaim{ObjectMeta: naming.InstancePostgresDataVolume(instance)}
spec := instanceSetSpec("1Gi", "3Gi")
cluster.Status = desiredStatus("NotAValidValue")
pvc.Spec = spec.DataVolumeClaimSpec
pvc.Spec = spec.DataVolumeClaimSpec.AsPersistentVolumeClaimSpec()

reconciler.setVolumeSize(ctx, &cluster, pvc, spec.Name)

@@ -1068,7 +1068,7 @@ resources:
pvc := &corev1.PersistentVolumeClaim{ObjectMeta: naming.InstancePostgresDataVolume(instance)}
spec := instanceSetSpec("1Gi", "3Gi")
cluster.Status = desiredStatus("2Gi")
pvc.Spec = spec.DataVolumeClaimSpec
pvc.Spec = spec.DataVolumeClaimSpec.AsPersistentVolumeClaimSpec()

reconciler.setVolumeSize(ctx, &cluster, pvc, spec.Name)

@@ -1093,7 +1093,7 @@ resources:
pvc := &corev1.PersistentVolumeClaim{ObjectMeta: naming.InstancePostgresDataVolume(instance)}
spec := instanceSetSpec("1Gi", "2Gi")
cluster.Status = desiredStatus("2Gi")
pvc.Spec = spec.DataVolumeClaimSpec
pvc.Spec = spec.DataVolumeClaimSpec.AsPersistentVolumeClaimSpec()

reconciler.setVolumeSize(ctx, &cluster, pvc, spec.Name)

@@ -1122,7 +1122,7 @@ resources:
pvc := &corev1.PersistentVolumeClaim{ObjectMeta: naming.InstancePostgresDataVolume(instance)}
spec := instanceSetSpec("4Gi", "5Gi")
cluster.Status = desiredStatus("10Gi")
pvc.Spec = spec.DataVolumeClaimSpec
pvc.Spec = spec.DataVolumeClaimSpec.AsPersistentVolumeClaimSpec()

reconciler.setVolumeSize(ctx, &cluster, pvc, spec.Name)

2 changes: 1 addition & 1 deletion internal/controller/postgrescluster/snapshots.go
Original file line number Diff line number Diff line change
@@ -313,7 +313,7 @@ func (r *Reconciler) createDedicatedSnapshotVolume(ctx context.Context,
return pvc, err
}

pvc.Spec = instanceSpec.DataVolumeClaimSpec
pvc.Spec = instanceSpec.DataVolumeClaimSpec.AsPersistentVolumeClaimSpec()

// Set the snapshot volume to the same size as the pgdata volume. The size should scale with auto-grow.
r.setVolumeSize(ctx, cluster, pvc, instanceSpec.Name)
3 changes: 2 additions & 1 deletion internal/controller/postgrescluster/snapshots_test.go
Original file line number Diff line number Diff line change
@@ -388,8 +388,9 @@ func TestReconcileDedicatedSnapshotVolume(t *testing.T) {
naming.LabelData: naming.DataPostgres,
},
},
Spec: testVolumeClaimSpec(),
}
spec := testVolumeClaimSpec()
pvc.Spec = spec.AsPersistentVolumeClaimSpec()
assert.NilError(t, r.setControllerReference(cluster, pvc))
assert.NilError(t, r.apply(ctx, pvc))

6 changes: 3 additions & 3 deletions internal/controller/postgrescluster/volumes.go
Original file line number Diff line number Diff line change
@@ -254,7 +254,7 @@ func (r *Reconciler) configureExistingPGVolumes(
Name: volName,
Namespace: cluster.Namespace,
},
Spec: cluster.Spec.InstanceSets[0].DataVolumeClaimSpec,
Spec: cluster.Spec.InstanceSets[0].DataVolumeClaimSpec.AsPersistentVolumeClaimSpec(),
}

volume.ObjectMeta.Labels = map[string]string{
@@ -307,7 +307,7 @@ func (r *Reconciler) configureExistingPGWALVolume(
Name: volName,
Namespace: cluster.Namespace,
},
Spec: cluster.Spec.InstanceSets[0].DataVolumeClaimSpec,
Spec: cluster.Spec.InstanceSets[0].DataVolumeClaimSpec.AsPersistentVolumeClaimSpec(),
}

volume.ObjectMeta.Labels = map[string]string{
@@ -362,7 +362,7 @@ func (r *Reconciler) configureExistingRepoVolumes(
cluster.Spec.Backups.PGBackRest.Repos[0].Name),
},
Spec: cluster.Spec.Backups.PGBackRest.Repos[0].Volume.
VolumeClaimSpec,
VolumeClaimSpec.AsPersistentVolumeClaimSpec(),
}

//volume.ObjectMeta = naming.PGBackRestRepoVolume(cluster, cluster.Spec.Backups.PGBackRest.Repos[0].Name)
Loading
Oops, something went wrong.
Loading
Oops, something went wrong.