diff --git a/changelogs/unreleased/4740-phuongatemc b/changelogs/unreleased/4740-phuongatemc new file mode 100644 index 000000000..0227a9665 --- /dev/null +++ b/changelogs/unreleased/4740-phuongatemc @@ -0,0 +1,2 @@ +Add PriorityClass to the AdditionalItems of Backup's PodAction and Restore's PodAction plugin to +backup and restore PriorityClass if it is used by a Pod. diff --git a/pkg/backup/pod_action.go b/pkg/backup/pod_action.go index be43c24ed..d0e834fbb 100644 --- a/pkg/backup/pod_action.go +++ b/pkg/backup/pod_action.go @@ -1,5 +1,5 @@ /* -Copyright 2017 the Velero contributors. +Copyright the Velero contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -56,12 +56,20 @@ func (a *PodAction) Execute(item runtime.Unstructured, backup *v1.Backup) (runti return nil, nil, errors.WithStack(err) } - if len(pod.Spec.Volumes) == 0 { - a.log.Info("pod has no volumes") - return item, nil, nil + var additionalItems []velero.ResourceIdentifier + if pod.Spec.PriorityClassName != "" { + a.log.Infof("Adding priorityclass %s to additionalItems", pod.Spec.PriorityClassName) + additionalItems = append(additionalItems, velero.ResourceIdentifier{ + GroupResource: kuberesource.PriorityClasses, + Name: pod.Spec.PriorityClassName, + }) + } + + if len(pod.Spec.Volumes) == 0 { + a.log.Info("pod has no volumes") + return item, additionalItems, nil } - var additionalItems []velero.ResourceIdentifier for _, volume := range pod.Spec.Volumes { if volume.PersistentVolumeClaim != nil && volume.PersistentVolumeClaim.ClaimName != "" { a.log.Infof("Adding pvc %s to additionalItems", volume.PersistentVolumeClaim.ClaimName) diff --git a/pkg/backup/pod_action_test.go b/pkg/backup/pod_action_test.go index c1a7f0e0a..1618b3089 100644 --- a/pkg/backup/pod_action_test.go +++ b/pkg/backup/pod_action_test.go @@ -1,5 +1,5 @@ /* -Copyright 2018 the Velero contributors. +Copyright the Velero contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -115,6 +115,25 @@ func TestPodActionExecute(t *testing.T) { {GroupResource: kuberesource.PersistentVolumeClaims, Namespace: "foo", Name: "claim2"}, }, }, + { + name: "test priority class", + pod: velerotest.UnstructuredOrDie(` + { + "apiVersion": "v1", + "kind": "Pod", + "metadata": { + "namespace": "foo", + "name": "bar" + }, + "spec": { + "priorityClassName": "testPriorityClass" + } + } + `), + expected: []velero.ResourceIdentifier{ + {GroupResource: kuberesource.PriorityClasses, Name: "testPriorityClass"}, + }, + }, } for _, test := range tests { diff --git a/pkg/kuberesource/kuberesource.go b/pkg/kuberesource/kuberesource.go index a515a70ff..c2c2d84ee 100644 --- a/pkg/kuberesource/kuberesource.go +++ b/pkg/kuberesource/kuberesource.go @@ -1,5 +1,5 @@ /* -Copyright 2018 the Velero contributors. +Copyright the Velero contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -34,4 +34,5 @@ var ( VolumeSnapshotClasses = schema.GroupResource{Group: "snapshot.storage.k8s.io", Resource: "volumesnapshotclasses"} VolumeSnapshots = schema.GroupResource{Group: "snapshot.storage.k8s.io", Resource: "volumesnapshots"} VolumeSnapshotContents = schema.GroupResource{Group: "snapshot.storage.k8s.io", Resource: "volumesnapshotcontents"} + PriorityClasses = schema.GroupResource{Group: "scheduling.k8s.io", Resource: "priorityclasses"} ) diff --git a/pkg/restore/pod_action.go b/pkg/restore/pod_action.go index 2086954ae..d4bdc1384 100644 --- a/pkg/restore/pod_action.go +++ b/pkg/restore/pod_action.go @@ -1,5 +1,5 @@ /* -Copyright 2017 the Velero contributors. +Copyright the Velero contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" + "github.com/vmware-tanzu/velero/pkg/kuberesource" "github.com/vmware-tanzu/velero/pkg/plugin/velero" ) @@ -85,6 +86,11 @@ func (a *PodAction) Execute(input *velero.RestoreItemActionExecuteInput) (*veler if err != nil { return nil, errors.WithStack(err) } - - return velero.NewRestoreItemActionExecuteOutput(&unstructured.Unstructured{Object: res}), nil + restoreExecuteOutput := velero.NewRestoreItemActionExecuteOutput(&unstructured.Unstructured{Object: res}) + if pod.Spec.PriorityClassName != "" { + a.logger.Infof("Adding priorityclass %s to AdditionalItems", pod.Spec.PriorityClassName) + restoreExecuteOutput.AdditionalItems = []velero.ResourceIdentifier{ + {GroupResource: kuberesource.PriorityClasses, Name: pod.Spec.PriorityClassName}} + } + return restoreExecuteOutput, nil } diff --git a/pkg/restore/pod_action_test.go b/pkg/restore/pod_action_test.go index 1d0165541..f1aa83c1a 100644 --- a/pkg/restore/pod_action_test.go +++ b/pkg/restore/pod_action_test.go @@ -1,5 +1,5 @@ /* -Copyright 2017, 2019 the Velero contributors. +Copyright the Velero contributors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" + "github.com/vmware-tanzu/velero/pkg/kuberesource" "github.com/vmware-tanzu/velero/pkg/plugin/velero" velerotest "github.com/vmware-tanzu/velero/pkg/test" ) @@ -34,10 +35,11 @@ func TestPodActionExecute(t *testing.T) { var priority int32 = 1 tests := []struct { - name string - obj corev1api.Pod - expectedErr bool - expectedRes corev1api.Pod + name string + obj corev1api.Pod + expectedErr bool + expectedRes corev1api.Pod + additionalItems []velero.ResourceIdentifier }{ { name: "nodeName (only) should be deleted from spec", @@ -189,6 +191,35 @@ func TestPodActionExecute(t *testing.T) { }, }, }, + { + name: "test priority class", + obj: corev1api.Pod{ + ObjectMeta: metav1.ObjectMeta{Name: "pod-1"}, + Spec: corev1api.PodSpec{ + ServiceAccountName: "foo", + PriorityClassName: "testPriorityClass", + Volumes: []corev1api.Volume{ + {Name: "foo"}, + {Name: "foo-token-foo"}, + }, + }, + }, + expectedRes: corev1api.Pod{ + ObjectMeta: metav1.ObjectMeta{Name: "pod-1"}, + Spec: corev1api.PodSpec{ + ServiceAccountName: "foo", + PriorityClassName: "testPriorityClass", + Volumes: []corev1api.Volume{ + {Name: "foo"}, + }, + }, + }, + additionalItems: []velero.ResourceIdentifier{ + {GroupResource: kuberesource.PriorityClasses, + Name: "testPriorityClass", + }, + }, + }, } for _, test := range tests { @@ -214,6 +245,7 @@ func TestPodActionExecute(t *testing.T) { require.NoError(t, runtime.DefaultUnstructuredConverter.FromUnstructured(res.UpdatedItem.UnstructuredContent(), &pod)) assert.Equal(t, test.expectedRes, pod) + assert.Equal(t, test.additionalItems, res.AdditionalItems) }) } }