Skip to content

Commit

Permalink
Add mapped selected-node existence check. (#5806)
Browse files Browse the repository at this point in the history
Signed-off-by: Xun Jiang <blackpiglet@gmail.com>
  • Loading branch information
blackpiglet committed Feb 6, 2023
1 parent a761111 commit 218fd76
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelogs/unreleased/5806-blackpiglet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add mapped selected-node existence check
9 changes: 9 additions & 0 deletions pkg/restore/change_pvc_node_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ func (p *ChangePVCNodeSelectorAction) Execute(input *velero.RestoreItemActionExe
}

if len(newNode) != 0 {
// Check whether the mapped node exists first.
exists, err := isNodeExist(p.nodeClient, newNode)
if err != nil {
return nil, errors.Wrapf(err, "error checking %s's mapped node %s existence", node, newNode)
}
if !exists {
log.Warnf("Selected-node's mapped node doesn't exist: source: %s, dest: %s. Please check the ConfigMap with label velero.io/change-pvc-node-selector.", node, newNode)
}

// set node selector
// We assume that node exist for node-mapping
annotations["volume.kubernetes.io/selected-node"] = newNode
Expand Down
17 changes: 16 additions & 1 deletion pkg/restore/change_pvc_node_selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ limitations under the License.
package restore

import (
"bytes"
"context"
"fmt"
"strings"
"testing"

"github.com/sirupsen/logrus"
Expand All @@ -44,6 +46,7 @@ func TestChangePVCNodeSelectorActionExecute(t *testing.T) {
pvc *corev1api.PersistentVolumeClaim
configMap *corev1api.ConfigMap
node *corev1api.Node
newNode *corev1api.Node
want *corev1api.PersistentVolumeClaim
wantErr error
}{
Expand All @@ -57,6 +60,7 @@ func TestChangePVCNodeSelectorActionExecute(t *testing.T) {
ObjectMeta(builder.WithLabels("velero.io/plugin-config", "true", "velero.io/change-pvc-node-selector", "RestoreItemAction")).
Data("source-node", "dest-node").
Result(),
newNode: builder.ForNode("dest-node").Result(),
want: builder.ForPersistentVolumeClaim("source-ns", "pvc-1").
ObjectMeta(
builder.WithAnnotations("volume.kubernetes.io/selected-node", "dest-node"),
Expand Down Expand Up @@ -127,8 +131,11 @@ func TestChangePVCNodeSelectorActionExecute(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
clientset := fake.NewSimpleClientset()
logger := logrus.StandardLogger()
buf := bytes.Buffer{}
logrus.SetOutput(&buf)
a := NewChangePVCNodeSelectorAction(
logrus.StandardLogger(),
logger,
clientset.CoreV1().ConfigMaps("velero"),
clientset.CoreV1().Nodes(),
)
Expand All @@ -143,6 +150,10 @@ func TestChangePVCNodeSelectorActionExecute(t *testing.T) {
_, err := clientset.CoreV1().Nodes().Create(context.TODO(), tc.node, metav1.CreateOptions{})
require.NoError(t, err)
}
if tc.newNode != nil {
_, err := clientset.CoreV1().Nodes().Create(context.TODO(), tc.newNode, metav1.CreateOptions{})
require.NoError(t, err)
}
unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(tc.pvc)
require.NoError(t, err)

Expand All @@ -155,6 +166,10 @@ func TestChangePVCNodeSelectorActionExecute(t *testing.T) {
// execute method under test
res, err := a.Execute(input)

// Make sure mapped selected-node exists.
log_output := buf.String()
assert.Equal(t, strings.Contains(log_output, "Selected-node's mapped node doesn't exist"), false)

// validate for both error and non-error cases
switch {
case tc.wantErr != nil:
Expand Down

0 comments on commit 218fd76

Please sign in to comment.