Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add labels and annotations field to volumes #263

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{
pepov marked this conversation as resolved.
Show resolved Hide resolved
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.

Loading