diff --git a/pkg/volume/volume_types.go b/pkg/volume/volume_types.go index 5e54655..fa7e24b 100644 --- a/pkg/volume/volume_types.go +++ b/pkg/volume/volume_types.go @@ -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" @@ -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 @@ -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, diff --git a/pkg/volume/volume_types_test.go b/pkg/volume/volume_types_test.go index 4e18893..95003bc 100644 --- a/pkg/volume/volume_types_test.go +++ b/pkg/volume/volume_types_test.go @@ -25,8 +25,7 @@ import ( func TestKubernetesVolume_ApplyVolumeForPodSpec_FailNonExistingContainer(t *testing.T) { vol := KubernetesVolume{ - HostPath: &v1.HostPathVolumeSource{ - }, + HostPath: &v1.HostPathVolumeSource{}, } spec := &v1.PodSpec{ @@ -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{ @@ -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", + }, }, } @@ -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) diff --git a/pkg/volume/zz_generated.deepcopy.go b/pkg/volume/zz_generated.deepcopy.go index 036ec36..07bf16b 100644 --- a/pkg/volume/zz_generated.deepcopy.go +++ b/pkg/volume/zz_generated.deepcopy.go @@ -72,6 +72,20 @@ func (in *PersistentVolumeClaim) DeepCopyInto(out *PersistentVolumeClaim) { *out = *in in.PersistentVolumeClaimSpec.DeepCopyInto(&out.PersistentVolumeClaimSpec) out.PersistentVolumeSource = in.PersistentVolumeSource + if in.Labels != nil { + in, out := &in.Labels, &out.Labels + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + if in.Annotations != nil { + in, out := &in.Annotations, &out.Annotations + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new PersistentVolumeClaim.