Merge pull request #4740 from phuongatemc/add-priorityclass-to-podaction-plugin

Add priorityclass to podaction plugin
pull/4766/head
Xun Jiang/Bruce Jiang 2022-03-22 19:43:04 +08:00 committed by GitHub
commit 9df0394359
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 83 additions and 15 deletions

View File

@ -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.

View File

@ -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)

View File

@ -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 {

View File

@ -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"}
)

View File

@ -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
}

View File

@ -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)
})
}
}