Skip to content

Commit

Permalink
Merge pull request #263 from csatib02/feat/add-labels-and-annotations…
Browse files Browse the repository at this point in the history
…-field-to-volumes

feat: add labels and annotations field to volumes
  • Loading branch information
pepov authored Oct 11, 2024
2 parents 4973eea + 678229c commit 539087c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
10 changes: 9 additions & 1 deletion pkg/volume/volume_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
v1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)

// +docName:"Kubernetes volume abstraction"
Expand Down Expand Up @@ -54,6 +55,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 @@ -112,8 +115,13 @@ func (v *KubernetesVolume) ApplyPVCForStatefulSet(containerName string, path str
if v.PersistentVolumeClaim == nil {
return errors.New("PVC definition is missing, unable to apply on statefulset")
}

m := meta(v.PersistentVolumeClaim.PersistentVolumeSource.ClaimName)
m.Labels = labels.Merge(m.Labels, v.PersistentVolumeClaim.Labels)
m.Annotations = labels.Merge(m.Annotations, v.PersistentVolumeClaim.Annotations)

pvc := corev1.PersistentVolumeClaim{
ObjectMeta: meta(v.PersistentVolumeClaim.PersistentVolumeSource.ClaimName),
ObjectMeta: m,
Spec: v.PersistentVolumeClaim.PersistentVolumeClaimSpec,
Status: corev1.PersistentVolumeClaimStatus{
Phase: corev1.ClaimPending,
Expand Down
18 changes: 12 additions & 6 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 @@ -122,6 +125,9 @@ func TestKubernetesVolume_ApplyPVCForStatefulSet(t *testing.T) {
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 539087c

Please sign in to comment.