Skip to content

Commit 498b58e

Browse files
authored
Merge pull request kubernetes#86982 from gnufied/add-pvc-mount-failure-events
Add events to PV when mount fails on filesystem mismatch
2 parents 4c08717 + c058073 commit 498b58e

File tree

6 files changed

+88
-1
lines changed

6 files changed

+88
-1
lines changed

pkg/kubelet/events/event.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const (
7272
SandboxChanged = "SandboxChanged"
7373
FailedCreatePodSandBox = "FailedCreatePodSandBox"
7474
FailedStatusPodSandBox = "FailedPodSandBoxStatus"
75+
FailedMountOnFilesystemMismatch = "FailedMountOnFilesystemMismatch"
7576
)
7677

7778
// Image manager event reason list

pkg/volume/local/local.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ func (dm *deviceMounter) mountLocalBlockDevice(spec *volume.Spec, devicePath str
342342
if rmErr := os.Remove(deviceMountPath); rmErr != nil {
343343
klog.Warningf("local: failed to remove %s: %v", deviceMountPath, rmErr)
344344
}
345-
return fmt.Errorf("local: failed to mount device %s at %s (fstype: %s), error %v", devicePath, deviceMountPath, fstype, err)
345+
return fmt.Errorf("local: failed to mount device %s at %s (fstype: %s), error %w", devicePath, deviceMountPath, fstype, err)
346346
}
347347
klog.V(3).Infof("local: successfully mount device %s at %s (fstype: %s)", devicePath, deviceMountPath, fstype)
348348
return nil

pkg/volume/util/operationexecutor/operation_generator.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,7 @@ func (og *operationGenerator) GenerateMountVolumeFunc(
581581
devicePath,
582582
deviceMountPath)
583583
if err != nil {
584+
og.checkForFailedMount(volumeToMount, err)
584585
og.markDeviceErrorState(volumeToMount, devicePath, deviceMountPath, err, actualStateOfWorld)
585586
// On failure, return error. Caller will log and retry.
586587
return volumeToMount.GenerateError("MountVolume.MountDevice failed", err)
@@ -635,6 +636,7 @@ func (og *operationGenerator) GenerateMountVolumeFunc(
635636
VolumeMountState: VolumeMounted,
636637
}
637638
if mountErr != nil {
639+
og.checkForFailedMount(volumeToMount, mountErr)
638640
og.markVolumeErrorState(volumeToMount, markOpts, mountErr, actualStateOfWorld)
639641
// On failure, return error. Caller will log and retry.
640642
return volumeToMount.GenerateError("MountVolume.SetUp failed", mountErr)
@@ -684,6 +686,18 @@ func (og *operationGenerator) GenerateMountVolumeFunc(
684686
}
685687
}
686688

689+
func (og *operationGenerator) checkForFailedMount(volumeToMount VolumeToMount, mountError error) {
690+
pv := volumeToMount.VolumeSpec.PersistentVolume
691+
if pv == nil {
692+
return
693+
}
694+
695+
if volumetypes.IsFilesystemMismatchError(mountError) {
696+
simpleMsg, _ := volumeToMount.GenerateMsg("MountVolume failed", mountError.Error())
697+
og.recorder.Eventf(pv, v1.EventTypeWarning, kevents.FailedMountOnFilesystemMismatch, simpleMsg)
698+
}
699+
}
700+
687701
func (og *operationGenerator) markDeviceErrorState(volumeToMount VolumeToMount, devicePath, deviceMountPath string, mountError error, actualStateOfWorld ActualStateOfWorldMounterUpdater) {
688702
if volumetypes.IsOperationFinishedError(mountError) &&
689703
actualStateOfWorld.GetDeviceMountState(volumeToMount.VolumeName) == DeviceMountUncertain {

pkg/volume/util/types/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package(default_visibility = ["//visibility:public"])
33
load(
44
"@io_bazel_rules_go//go:def.bzl",
55
"go_library",
6+
"go_test",
67
)
78

89
go_library(
@@ -12,6 +13,7 @@ go_library(
1213
deps = [
1314
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
1415
"//staging/src/k8s.io/apimachinery/pkg/util/runtime:go_default_library",
16+
"//vendor/k8s.io/utils/mount:go_default_library",
1517
],
1618
)
1719

@@ -27,3 +29,10 @@ filegroup(
2729
srcs = [":package-srcs"],
2830
tags = ["automanaged"],
2931
)
32+
33+
go_test(
34+
name = "go_default_test",
35+
srcs = ["types_test.go"],
36+
embed = [":go_default_library"],
37+
deps = ["//vendor/k8s.io/utils/mount:go_default_library"],
38+
)

pkg/volume/util/types/types.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ limitations under the License.
1818
package types
1919

2020
import (
21+
"errors"
22+
2123
"k8s.io/apimachinery/pkg/types"
2224
"k8s.io/apimachinery/pkg/util/runtime"
25+
"k8s.io/utils/mount"
2326
)
2427

2528
// UniquePodName defines the type to key pods off of
@@ -93,6 +96,16 @@ func IsOperationFinishedError(err error) bool {
9396
return true
9497
}
9598

99+
// IsFilesystemMismatchError checks if mount failed because requested filesystem
100+
// on PVC and actual filesystem on disk did not match
101+
func IsFilesystemMismatchError(err error) bool {
102+
mountError := &mount.MountError{}
103+
if errors.As(err, &mountError) && mountError.Type == mount.FilesystemMismatch {
104+
return true
105+
}
106+
return false
107+
}
108+
96109
// IsUncertainProgressError checks if given error is of type that indicates
97110
// operation might be in-progress in background.
98111
func IsUncertainProgressError(err error) bool {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package types
18+
19+
import (
20+
"fmt"
21+
"testing"
22+
23+
"k8s.io/utils/mount"
24+
)
25+
26+
func TestIsFilesystemMismatchError(t *testing.T) {
27+
tests := []struct {
28+
mountError error
29+
expectError bool
30+
}{
31+
{
32+
mount.NewMountError(mount.FilesystemMismatch, "filesystem mismatch"),
33+
true,
34+
},
35+
{
36+
mount.NewMountError(mount.FormatFailed, "filesystem mismatch"),
37+
false,
38+
},
39+
{
40+
fmt.Errorf("mount failed %w", mount.NewMountError(mount.FilesystemMismatch, "filesystem mismatch")),
41+
true,
42+
},
43+
}
44+
for _, test := range tests {
45+
ok := IsFilesystemMismatchError(test.mountError)
46+
if ok != test.expectError {
47+
t.Errorf("expected filesystem mismatch to be %v but got %v", test.expectError, ok)
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)