Skip to content

Commit

Permalink
feat: add labels and annotations field to volumes
Browse files Browse the repository at this point in the history
Signed-off-by: Bence Csati <[email protected]>

feat: add labels and annotations field to volumes

Signed-off-by: Bence Csati <[email protected]>
  • Loading branch information
csatib02 committed Oct 10, 2024
1 parent f3d2b59 commit a7df9a1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
6 changes: 4 additions & 2 deletions pkg/volume/volume_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type KubernetesVolume struct {
type PersistentVolumeClaim struct {
PersistentVolumeClaimSpec corev1.PersistentVolumeClaimSpec `json:"spec,omitempty"`
PersistentVolumeSource corev1.PersistentVolumeClaimVolumeSource `json:"source,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
}

// `path` is the path in case the hostPath volume type is used and no path has been defined explicitly
Expand Down Expand Up @@ -108,12 +110,12 @@ func (v *KubernetesVolume) GetVolume(name string) (corev1.Volume, error) {
return volume, nil
}

func (v *KubernetesVolume) ApplyPVCForStatefulSet(containerName string, path string, spec *v1.StatefulSetSpec, meta func(name string) metav1.ObjectMeta) error {
func (v *KubernetesVolume) ApplyPVCForStatefulSet(containerName string, path string, spec *v1.StatefulSetSpec, meta func(name string, labels, annotations map[string]string) metav1.ObjectMeta) error {
if v.PersistentVolumeClaim == nil {
return errors.New("PVC definition is missing, unable to apply on statefulset")
}
pvc := corev1.PersistentVolumeClaim{
ObjectMeta: meta(v.PersistentVolumeClaim.PersistentVolumeSource.ClaimName),
ObjectMeta: meta(v.PersistentVolumeClaim.PersistentVolumeSource.ClaimName, v.PersistentVolumeClaim.Labels, v.PersistentVolumeClaim.Annotations),
Spec: v.PersistentVolumeClaim.PersistentVolumeClaimSpec,
Status: corev1.PersistentVolumeClaimStatus{
Phase: corev1.ClaimPending,
Expand Down
24 changes: 16 additions & 8 deletions pkg/volume/volume_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ import (

func TestKubernetesVolume_ApplyVolumeForPodSpec_FailNonExistingContainer(t *testing.T) {
vol := KubernetesVolume{
HostPath: &v1.HostPathVolumeSource{
},
HostPath: &v1.HostPathVolumeSource{},
}

spec := &v1.PodSpec{
Expand All @@ -47,8 +46,7 @@ func TestKubernetesVolume_ApplyVolumeForPodSpec_FailNonExistingContainer(t *test

func TestKubernetesVolume_ApplyVolumeForPodSpec(t *testing.T) {
vol := KubernetesVolume{
HostPath: &v1.HostPathVolumeSource{
},
HostPath: &v1.HostPathVolumeSource{},
}

spec := &v1.PodSpec{
Expand Down Expand Up @@ -93,11 +91,16 @@ func TestSecretKubernetesVolume_ApplyVolumeForPodSpec(t *testing.T) {
func TestKubernetesVolume_ApplyPVCForStatefulSet(t *testing.T) {
vol := KubernetesVolume{
PersistentVolumeClaim: &PersistentVolumeClaim{
PersistentVolumeClaimSpec: v1.PersistentVolumeClaimSpec{
},
PersistentVolumeClaimSpec: v1.PersistentVolumeClaimSpec{},
PersistentVolumeSource: v1.PersistentVolumeClaimVolumeSource{
ClaimName: "my-claim",
},
Labels: map[string]string{
"test": "label",
},
Annotations: map[string]string{
"test": "annotation",
},
},
}

Expand All @@ -113,15 +116,20 @@ func TestKubernetesVolume_ApplyPVCForStatefulSet(t *testing.T) {
},
}

err := vol.ApplyPVCForStatefulSet("test-container", "/there", sts, func(name string) metav1.ObjectMeta {
err := vol.ApplyPVCForStatefulSet("test-container", "/there", sts, func(name string, labels, annotations map[string]string) metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: "prefix-" + name,
Name: "prefix-" + name,
Labels: labels,
Annotations: annotations,
}
})

assert.Len(t, sts.Template.Spec.Volumes, 0)

assert.Equal(t, "prefix-my-claim", sts.Template.Spec.Containers[0].VolumeMounts[0].Name)
assert.Equal(t, vol.PersistentVolumeClaim.Labels, sts.VolumeClaimTemplates[0].Labels)
assert.Equal(t, vol.PersistentVolumeClaim.Annotations, sts.VolumeClaimTemplates[0].Annotations)

assert.Equal(t, "/there", sts.Template.Spec.Containers[0].VolumeMounts[0].MountPath)

assert.Equal(t, "prefix-my-claim", sts.VolumeClaimTemplates[0].Name)
Expand Down
14 changes: 14 additions & 0 deletions pkg/volume/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a7df9a1

Please sign in to comment.