Skip to content

Commit 8db5719

Browse files
authored
Merge pull request kubernetes#135065 from eltrufas/automated-cherry-pick-of-#133599-upstream-release-1.33
Automated cherry pick of kubernetes#133599: Mark API server errors as transient in csi raw block driver
2 parents 8cd7fd6 + ae9cb84 commit 8db5719

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

pkg/volume/csi/csi_block.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ package csi
6868
import (
6969
"context"
7070
"errors"
71-
"fmt"
7271
"os"
7372
"path/filepath"
7473

@@ -171,8 +170,8 @@ func (m *csiBlockMapper) stageVolumeForBlock(
171170
if csiSource.NodeStageSecretRef != nil {
172171
nodeStageSecrets, err = getCredentialsFromSecret(m.k8s, csiSource.NodeStageSecretRef)
173172
if err != nil {
174-
return "", fmt.Errorf("failed to get NodeStageSecretRef %s/%s: %v",
175-
csiSource.NodeStageSecretRef.Namespace, csiSource.NodeStageSecretRef.Name, err)
173+
return "", volumetypes.NewTransientOperationFailure(log("failed to get NodeStageSecretRef %s/%s: %v",
174+
csiSource.NodeStageSecretRef.Namespace, csiSource.NodeStageSecretRef.Name, err))
176175
}
177176
}
178177

@@ -223,11 +222,11 @@ func (m *csiBlockMapper) publishVolumeForBlock(
223222
volAttribs := csiSource.VolumeAttributes
224223
podInfoEnabled, err := m.plugin.podInfoEnabled(string(m.driverName))
225224
if err != nil {
226-
return "", errors.New(log("blockMapper.publishVolumeForBlock failed to assemble volume attributes: %v", err))
225+
return "", volumetypes.NewTransientOperationFailure(log("blockMapper.publishVolumeForBlock failed to assemble volume attributes: %v", err))
227226
}
228227
volumeLifecycleMode, err := m.plugin.getVolumeLifecycleMode(m.spec)
229228
if err != nil {
230-
return "", errors.New(log("blockMapper.publishVolumeForBlock failed to get VolumeLifecycleMode: %v", err))
229+
return "", volumetypes.NewTransientOperationFailure(log("blockMapper.publishVolumeForBlock failed to get VolumeLifecycleMode: %v", err))
231230
}
232231
if podInfoEnabled {
233232
volAttribs = mergeMap(volAttribs, getPodInfoAttrs(m.pod, volumeLifecycleMode))
@@ -237,7 +236,7 @@ func (m *csiBlockMapper) publishVolumeForBlock(
237236
if csiSource.NodePublishSecretRef != nil {
238237
nodePublishSecrets, err = getCredentialsFromSecret(m.k8s, csiSource.NodePublishSecretRef)
239238
if err != nil {
240-
return "", errors.New(log("blockMapper.publishVolumeForBlock failed to get NodePublishSecretRef %s/%s: %v",
239+
return "", volumetypes.NewTransientOperationFailure(log("blockMapper.publishVolumeForBlock failed to get NodePublishSecretRef %s/%s: %v",
241240
csiSource.NodePublishSecretRef.Namespace, csiSource.NodePublishSecretRef.Name, err))
242241
}
243242
}
@@ -304,7 +303,7 @@ func (m *csiBlockMapper) SetUpDevice() (string, error) {
304303
attachID := getAttachmentName(csiSource.VolumeHandle, csiSource.Driver, nodeName)
305304
attachment, err = m.k8s.StorageV1().VolumeAttachments().Get(context.TODO(), attachID, meta.GetOptions{})
306305
if err != nil {
307-
return "", errors.New(log("blockMapper.SetupDevice failed to get volume attachment [id=%v]: %v", attachID, err))
306+
return "", volumetypes.NewTransientOperationFailure(log("blockMapper.SetupDevice failed to get volume attachment [id=%v]: %v", attachID, err))
308307
}
309308
}
310309

@@ -366,7 +365,7 @@ func (m *csiBlockMapper) MapPodDevice() (string, error) {
366365
attachID := getAttachmentName(csiSource.VolumeHandle, csiSource.Driver, nodeName)
367366
attachment, err = m.k8s.StorageV1().VolumeAttachments().Get(context.TODO(), attachID, meta.GetOptions{})
368367
if err != nil {
369-
return "", errors.New(log("blockMapper.MapPodDevice failed to get volume attachment [id=%v]: %v", attachID, err))
368+
return "", volumetypes.NewTransientOperationFailure(log("blockMapper.MapPodDevice failed to get volume attachment [id=%v]: %v", attachID, err))
370369
}
371370
}
372371

pkg/volume/csi/csi_block_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package csi
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
2223
"os"
2324
"path/filepath"
@@ -491,6 +492,46 @@ func TestBlockMapperMapPodDeviceNoClientError(t *testing.T) {
491492
}
492493
}
493494

495+
func TestBlockMapperMapPodDeviceGetStageSecretsError(t *testing.T) {
496+
transientError := volumetypes.NewTransientOperationFailure("")
497+
plug, tmpDir := newTestPlugin(t, nil)
498+
defer func() {
499+
if err := os.RemoveAll(tmpDir); err != nil {
500+
t.Error(err)
501+
}
502+
}()
503+
504+
csiMapper, _, pv, err := prepareBlockMapperTest(plug, "test-pv", t)
505+
if err != nil {
506+
t.Fatalf("Failed to make a new Mapper: %v", err)
507+
}
508+
509+
// set a stage secret for the pv
510+
pv.Spec.PersistentVolumeSource.CSI.NodePublishSecretRef = &api.SecretReference{
511+
Name: "foo",
512+
Namespace: "default",
513+
}
514+
pvName := pv.GetName()
515+
nodeName := string(plug.host.GetNodeName())
516+
517+
csiMapper.csiClient = setupClient(t, true)
518+
519+
attachID := getAttachmentName(csiMapper.volumeID, string(csiMapper.driverName), nodeName)
520+
attachment := makeTestAttachment(attachID, nodeName, pvName)
521+
attachment.Status.Attached = true
522+
if _, err = csiMapper.k8s.StorageV1().VolumeAttachments().Create(context.Background(), attachment, metav1.CreateOptions{}); err != nil {
523+
t.Fatalf("failed to setup VolumeAttachment: %v", err)
524+
}
525+
t.Log("created attachment ", attachID)
526+
527+
_, err = csiMapper.MapPodDevice()
528+
if err == nil {
529+
t.Errorf("test should fail, but no error occurred")
530+
} else if !errors.As(err, &transientError) {
531+
t.Errorf("expected a transient error but got %v", err)
532+
}
533+
}
534+
494535
func TestBlockMapperTearDownDevice(t *testing.T) {
495536
plug, tmpDir := newTestPlugin(t, nil)
496537
defer os.RemoveAll(tmpDir)

0 commit comments

Comments
 (0)