From bf38deb03cbeba76bf289974afebe62f3c32496e Mon Sep 17 00:00:00 2001 From: Igor Korchynskyi Date: Wed, 3 Jul 2024 16:30:51 +0200 Subject: [PATCH] fix: allow to use persistent volume claims as additional volumes Signed-off-by: Igor Korchynskyi --- .../templates/opensearch-cluster-cr.yaml | 14 ++------ ...ensearch.opster.io_opensearchclusters.yaml | 28 ++++++++++++++++ .../api/v1/opensearch_types.go | 2 ++ .../api/v1/zz_generated.deepcopy.go | 5 +++ ...ensearch.opster.io_opensearchclusters.yaml | 32 +++++++++++++++++++ .../pkg/reconcilers/util/util.go | 9 ++++++ .../pkg/reconcilers/util/util_test.go | 14 ++++++++ 7 files changed, 92 insertions(+), 12 deletions(-) diff --git a/charts/opensearch-cluster/templates/opensearch-cluster-cr.yaml b/charts/opensearch-cluster/templates/opensearch-cluster-cr.yaml index e08132ca..15cb0953 100644 --- a/charts/opensearch-cluster/templates/opensearch-cluster-cr.yaml +++ b/charts/opensearch-cluster/templates/opensearch-cluster-cr.yaml @@ -67,12 +67,7 @@ spec: {{- end}} {{- if .Values.opensearchCluster.general.additionalVolumes }} additionalVolumes: - {{- range $key, $val := .Values.opensearchCluster.general.additionalVolumes }} - - name: {{ $val.name }} - path: {{ $val.path }} - secret: - secretName: {{ $val.secret.secretName }} - {{- end -}} + {{ toYaml .Values.opensearchCluster.general.additionalVolumes | nindent 6 }} {{- end }} {{- if .Values.opensearchCluster.general.additionalConfig }} additionalConfig: @@ -167,12 +162,7 @@ spec: {{- end}} {{- if .Values.opensearchCluster.dashboards.additionalVolumes }} additionalVolumes: - {{- range $key,$val := .Values.opensearchCluster.dashboards.additionalVolumes }} - - name: {{ $val.name }} - path: {{ $val.path }} - secret: - secretName: {{ $val.secretName.name }} - {{- end }} + {{ toYaml .Values.opensearchCluster.dashboards.additionalVolumes | nindent 6 }} {{- end }} {{- if .Values.opensearchCluster.dashboards.additionalConfig }} additionalConfig: diff --git a/charts/opensearch-operator/files/opensearch.opster.io_opensearchclusters.yaml b/charts/opensearch-operator/files/opensearch.opster.io_opensearchclusters.yaml index 1ea3615c..ba64d234 100644 --- a/charts/opensearch-operator/files/opensearch.opster.io_opensearchclusters.yaml +++ b/charts/opensearch-operator/files/opensearch.opster.io_opensearchclusters.yaml @@ -1098,6 +1098,20 @@ spec: description: Path in the container to mount the volume at. Required. type: string + persistentVolumeClaim: + description: PersistentVolumeClaim object to use to populate the volume + properties: + claimName: + description: |- + The name of a PersistentVolumeClaim in the same namespace as the pod using this volume. + More info: https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims + type: string + readOnly: + description: |- + Will force the ReadOnly setting in VolumeMounts. + Default false. + type: boolean + type: object projected: description: Projected object to use to populate the volume properties: @@ -3005,6 +3019,20 @@ spec: description: Path in the container to mount the volume at. Required. type: string + persistentVolumeClaim: + description: PersistentVolumeClaim object to use to populate the volume + properties: + claimName: + description: |- + The name of a PersistentVolumeClaim in the same namespace as the pod using this volume. + More info: https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims + type: string + readOnly: + description: |- + Will force the ReadOnly setting in VolumeMounts. + Default false. + type: boolean + type: object projected: description: Projected object to use to populate the volume properties: diff --git a/opensearch-operator/api/v1/opensearch_types.go b/opensearch-operator/api/v1/opensearch_types.go index 75e822ab..233c8778 100644 --- a/opensearch-operator/api/v1/opensearch_types.go +++ b/opensearch-operator/api/v1/opensearch_types.go @@ -296,6 +296,8 @@ type AdditionalVolume struct { EmptyDir *corev1.EmptyDirVolumeSource `json:"emptyDir,omitempty"` // CSI object to use to populate the volume CSI *corev1.CSIVolumeSource `json:"csi,omitempty"` + // Persistent object to use to populate the volume + PersistentVolumeClaim *corev1.PersistentVolumeClaimVolumeSource `json:"persistentVolumeClaim,omitempty"` // Projected object to use to populate the volume Projected *corev1.ProjectedVolumeSource `json:"projected,omitempty"` // Whether to restart the pods on content change diff --git a/opensearch-operator/api/v1/zz_generated.deepcopy.go b/opensearch-operator/api/v1/zz_generated.deepcopy.go index 055616a0..2f016e54 100644 --- a/opensearch-operator/api/v1/zz_generated.deepcopy.go +++ b/opensearch-operator/api/v1/zz_generated.deepcopy.go @@ -151,6 +151,11 @@ func (in *AdditionalVolume) DeepCopyInto(out *AdditionalVolume) { *out = new(corev1.CSIVolumeSource) (*in).DeepCopyInto(*out) } + if in.PersistentVolumeClaim != nil { + in, out := &in.PersistentVolumeClaim, &out.PersistentVolumeClaim + *out = new(corev1.PersistentVolumeClaimVolumeSource) + (*in).DeepCopyInto(*out) + } if in.Projected != nil { in, out := &in.Projected, &out.Projected *out = new(corev1.ProjectedVolumeSource) diff --git a/opensearch-operator/config/crd/bases/opensearch.opster.io_opensearchclusters.yaml b/opensearch-operator/config/crd/bases/opensearch.opster.io_opensearchclusters.yaml index 1ea3615c..bb953324 100644 --- a/opensearch-operator/config/crd/bases/opensearch.opster.io_opensearchclusters.yaml +++ b/opensearch-operator/config/crd/bases/opensearch.opster.io_opensearchclusters.yaml @@ -1098,6 +1098,22 @@ spec: description: Path in the container to mount the volume at. Required. type: string + persistentVolumeClaim: + description: Persistent object to use to populate the volume + properties: + claimName: + description: |- + claimName is the name of a PersistentVolumeClaim in the same namespace as the pod using this volume. + More info: https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims + type: string + readOnly: + description: |- + readOnly Will force the ReadOnly setting in VolumeMounts. + Default false. + type: boolean + required: + - claimName + type: object projected: description: Projected object to use to populate the volume properties: @@ -3005,6 +3021,22 @@ spec: description: Path in the container to mount the volume at. Required. type: string + persistentVolumeClaim: + description: Persistent object to use to populate the volume + properties: + claimName: + description: |- + claimName is the name of a PersistentVolumeClaim in the same namespace as the pod using this volume. + More info: https://kubernetes.io/docs/concepts/storage/persistent-volumes#persistentvolumeclaims + type: string + readOnly: + description: |- + readOnly Will force the ReadOnly setting in VolumeMounts. + Default false. + type: boolean + required: + - claimName + type: object projected: description: Projected object to use to populate the volume properties: diff --git a/opensearch-operator/pkg/reconcilers/util/util.go b/opensearch-operator/pkg/reconcilers/util/util.go index 345e937d..c81afbfd 100644 --- a/opensearch-operator/pkg/reconcilers/util/util.go +++ b/opensearch-operator/pkg/reconcilers/util/util.go @@ -135,6 +135,15 @@ func CreateAdditionalVolumes( }, }) } + if volumeConfig.PersistentVolumeClaim != nil { + readOnly = volumeConfig.PersistentVolumeClaim.ReadOnly + retVolumes = append(retVolumes, corev1.Volume{ + Name: volumeConfig.Name, + VolumeSource: corev1.VolumeSource{ + PersistentVolumeClaim: volumeConfig.PersistentVolumeClaim, + }, + }) + } if volumeConfig.Projected != nil { retVolumes = append(retVolumes, corev1.Volume{ Name: volumeConfig.Name, diff --git a/opensearch-operator/pkg/reconcilers/util/util_test.go b/opensearch-operator/pkg/reconcilers/util/util_test.go index ace524f5..b1892ed8 100644 --- a/opensearch-operator/pkg/reconcilers/util/util_test.go +++ b/opensearch-operator/pkg/reconcilers/util/util_test.go @@ -132,6 +132,20 @@ var _ = Describe("Additional volumes", func() { }) }) + When("PersistentVolumeClaim volume is added", func() { + It("Should have PersistentVolumeClaimVolumeSource fields", func() { + readOnly := true + volumeConfigs[0].PersistentVolumeClaim = &v1.PersistentVolumeClaimVolumeSource{ + ClaimName: "testClaim", + ReadOnly: readOnly, + } + + volume, _, _, _ := CreateAdditionalVolumes(mockClient, namespace, volumeConfigs) + Expect(volume[0].PersistentVolumeClaim.ClaimName).To(Equal("testClaim")) + Expect(volume[0].PersistentVolumeClaim.ReadOnly).Should(BeTrue()) + }) + }) + When("Projected volume is added", func() { It("Should have ProjectedVolumeSource fields", func() { volumeConfigs[0].Projected = &v1.ProjectedVolumeSource{