Skip to content

Commit

Permalink
chore: review, adds unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Armando Ruocco <armando.ruocco@enterprisedb.com>
  • Loading branch information
armru committed Jan 17, 2024
1 parent 9ff1b4e commit 6565c68
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
4 changes: 2 additions & 2 deletions api/v1/cluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -1479,10 +1479,10 @@ func (r *Cluster) validateWalStorageSize() field.ErrorList {
func (r *Cluster) validateEphemeralVolumeSource() field.ErrorList {
var result field.ErrorList

if r.Spec.EphemeralVolumeSource != nil && (r.Spec.EphemeralVolumesSizeLimit != nil && r.Spec.EphemeralVolumesSizeLimit.TemporaryData != nil) {
if r.Spec.EphemeralVolumeSource != nil && r.Spec.EphemeralVolumesSizeLimit != nil {
result = append(result, field.Duplicate(
field.NewPath("spec", "ephemeralVolumeSource"),
"ephemeralVolumeSource and ephemeralVolumesSizeLimit.temporaryData are in conflict, set one only",
"Conflicting settings: provide either ephemeralVolumeSource or ephemeralVolumesSizeLimit, not both.",
))
}

Expand Down
15 changes: 6 additions & 9 deletions docs/src/cluster_conf.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ CloudNativePG relies on [ephemeral volumes](https://kubernetes.io/docs/concepts/
for part of the internal activities. Ephemeral volumes exist for the sole
duration of a pod's life, without persisting across pod restarts.

### Volume for temporary storage
# Volume Claim Template for Temporary Storage

An ephemeral volume used for temporary storage. By default an `emptyDir` volume is used. To configure
the volume use the `.spec.ephemeralVolumeSource` field in the cluster spec.
The operator uses by default an `emptyDir` volume, which can be customized by using the `.spec.ephemeralVolumesSizeLimit field`.
This can be overridden by specifying a volume claim template in the `.spec.ephemeralVolumeSource` field.

This simple example shows how to set a `1Gi` ephemeral volume. Set the `storageClassName` to something
available on your Kubernetes cluster.
In the following example, a `1Gi` ephemeral volume is set.

```yaml
apiVersion: postgresql.cnpg.io/v1
Expand All @@ -66,16 +65,14 @@ spec:
volumeClaimTemplate:
spec:
accessModes: ["ReadWriteOnce"]
# example storageClassName, replace with one existing in your Kubernetes cluster
storageClassName: "scratch-storage-class"
resources:
requests:
storage: 1Gi
```

Alternatively you can configure an upper bound on the default volume size using the `.spec.ephemeralVolumesSizeLimit.temporaryData`
field in the cluster spec.

Both `.spec.emphemeralVolumeSource` and `.spec.ephemeralVolumesSizeLimit.temporaryData` cannot be set.
Both `.spec.emphemeralVolumeSource` and `.spec.ephemeralVolumesSizeLimit.temporaryData` cannot be specified simultaneously.

### Volume for shared memory

Expand Down
46 changes: 46 additions & 0 deletions pkg/specs/volumes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package specs

import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/utils/ptr"

apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"

Expand Down Expand Up @@ -472,3 +474,47 @@ var _ = DescribeTable("test creation of volumes",
},
}),
)

var _ = Describe("createEphemeralVolume", func() {
var cluster apiv1.Cluster

BeforeEach(func() {
cluster = apiv1.Cluster{}
})

It("should create an emptyDir volume by default", func() {
ephemeralVolume := createEphemeralVolume(cluster)
Expect(ephemeralVolume.Name).To(Equal("scratch-data"))
Expect(ephemeralVolume.VolumeSource.EmptyDir).NotTo(BeNil())
})

It("should create an ephemeral volume when specified in the cluster", func() {
const storageClass = "test-storageclass"
cluster.Spec.EphemeralVolumeSource = &corev1.EphemeralVolumeSource{
VolumeClaimTemplate: &corev1.PersistentVolumeClaimTemplate{
Spec: corev1.PersistentVolumeClaimSpec{
StorageClassName: ptr.To(storageClass),
},
},
}

ephemeralVolume := createEphemeralVolume(cluster)

Expect(ephemeralVolume.Name).To(Equal("scratch-data"))
Expect(ephemeralVolume.EmptyDir).To(BeNil())
Expect(ephemeralVolume.VolumeSource.Ephemeral).NotTo(BeNil())
Expect(*ephemeralVolume.VolumeSource.Ephemeral.VolumeClaimTemplate.Spec.StorageClassName).To(Equal(storageClass))
})

It("should set size limit when specified in the cluster", func() {
quantity := resource.MustParse("1Gi")
cluster.Spec.EphemeralVolumesSizeLimit = &apiv1.EphemeralVolumesSizeLimitConfiguration{
TemporaryData: &quantity,
}

ephemeralVolume := createEphemeralVolume(cluster)

Expect(ephemeralVolume.Name).To(Equal("scratch-data"))
Expect(*ephemeralVolume.VolumeSource.EmptyDir.SizeLimit).To(Equal(quantity))
})
})

0 comments on commit 6565c68

Please sign in to comment.