Skip to content

Commit 18453b0

Browse files
committed
Tolerate apiserver being older than controller-manager
1 parent 9bbf768 commit 18453b0

File tree

2 files changed

+67
-13
lines changed

2 files changed

+67
-13
lines changed

pkg/controller/volume/persistentvolume/index.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -259,22 +259,23 @@ func findMatchingVolume(
259259
}
260260

261261
// checkVolumeModeMismatches is a convenience method that checks volumeMode for PersistentVolume
262-
// and PersistentVolumeClaims along with making sure that the feature gate BlockVolume is enabled.
262+
// and PersistentVolumeClaims
263263
func checkVolumeModeMismatches(pvcSpec *v1.PersistentVolumeClaimSpec, pvSpec *v1.PersistentVolumeSpec) (bool, error) {
264-
if utilfeature.DefaultFeatureGate.Enabled(features.BlockVolume) {
265-
if pvSpec.VolumeMode != nil && pvcSpec.VolumeMode != nil {
266-
requestedVolumeMode := *pvcSpec.VolumeMode
267-
pvVolumeMode := *pvSpec.VolumeMode
268-
return requestedVolumeMode != pvVolumeMode, nil
269-
} else {
270-
// This also should return an error, this means that
271-
// the defaulting has failed.
272-
return true, fmt.Errorf("api defaulting for volumeMode failed")
273-
}
274-
} else {
275-
// feature gate is disabled
264+
if !utilfeature.DefaultFeatureGate.Enabled(features.BlockVolume) {
276265
return false, nil
277266
}
267+
268+
// In HA upgrades, we cannot guarantee that the apiserver is on a version >= controller-manager.
269+
// So we default a nil volumeMode to filesystem
270+
requestedVolumeMode := v1.PersistentVolumeFilesystem
271+
if pvcSpec.VolumeMode != nil {
272+
requestedVolumeMode = *pvcSpec.VolumeMode
273+
}
274+
pvVolumeMode := v1.PersistentVolumeFilesystem
275+
if pvSpec.VolumeMode != nil {
276+
pvVolumeMode = *pvSpec.VolumeMode
277+
}
278+
return requestedVolumeMode != pvVolumeMode, nil
278279
}
279280

280281
// findBestMatchForClaim is a convenience method that finds a volume by the claim's AccessModes and requests for Storage

pkg/controller/volume/persistentvolume/index_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,29 @@ func createVolumeModeFilesystemTestVolume() *v1.PersistentVolume {
10381038
}
10391039
}
10401040

1041+
func createVolumeModeNilTestVolume() *v1.PersistentVolume {
1042+
return &v1.PersistentVolume{
1043+
ObjectMeta: metav1.ObjectMeta{
1044+
UID: "local-1",
1045+
Name: "nil-mode",
1046+
},
1047+
Spec: v1.PersistentVolumeSpec{
1048+
Capacity: v1.ResourceList{
1049+
v1.ResourceName(v1.ResourceStorage): resource.MustParse("10G"),
1050+
},
1051+
PersistentVolumeSource: v1.PersistentVolumeSource{
1052+
Local: &v1.LocalVolumeSource{},
1053+
},
1054+
AccessModes: []v1.PersistentVolumeAccessMode{
1055+
v1.ReadWriteOnce,
1056+
},
1057+
},
1058+
Status: v1.PersistentVolumeStatus{
1059+
Phase: v1.VolumeAvailable,
1060+
},
1061+
}
1062+
}
1063+
10411064
func createTestVolOrderedIndex(pv *v1.PersistentVolume) persistentVolumeOrderedIndex {
10421065
volFile := newPersistentVolumeOrderedIndex()
10431066
volFile.store.Add(pv)
@@ -1081,6 +1104,36 @@ func TestVolumeModeCheck(t *testing.T) {
10811104
pvc: makeVolumeModePVC("8G", &filesystemMode, nil),
10821105
enableBlock: true,
10831106
},
1107+
"feature enabled - pvc filesystem and pv nil": {
1108+
isExpectedMismatch: false,
1109+
vol: createVolumeModeNilTestVolume(),
1110+
pvc: makeVolumeModePVC("8G", &filesystemMode, nil),
1111+
enableBlock: true,
1112+
},
1113+
"feature enabled - pvc nil and pv filesytem": {
1114+
isExpectedMismatch: false,
1115+
vol: createVolumeModeFilesystemTestVolume(),
1116+
pvc: makeVolumeModePVC("8G", nil, nil),
1117+
enableBlock: true,
1118+
},
1119+
"feature enabled - pvc nil and pv nil": {
1120+
isExpectedMismatch: false,
1121+
vol: createVolumeModeNilTestVolume(),
1122+
pvc: makeVolumeModePVC("8G", nil, nil),
1123+
enableBlock: true,
1124+
},
1125+
"feature enabled - pvc nil and pv block": {
1126+
isExpectedMismatch: true,
1127+
vol: createVolumeModeBlockTestVolume(),
1128+
pvc: makeVolumeModePVC("8G", nil, nil),
1129+
enableBlock: true,
1130+
},
1131+
"feature enabled - pvc block and pv nil": {
1132+
isExpectedMismatch: true,
1133+
vol: createVolumeModeNilTestVolume(),
1134+
pvc: makeVolumeModePVC("8G", &blockMode, nil),
1135+
enableBlock: true,
1136+
},
10841137
"feature disabled - pvc block and pv filesystem": {
10851138
isExpectedMismatch: false,
10861139
vol: createVolumeModeFilesystemTestVolume(),

0 commit comments

Comments
 (0)