diff --git a/pkg/apis/storage/fuzzer/fuzzer.go b/pkg/apis/storage/fuzzer/fuzzer.go index 124e800335a..2081ffe846f 100644 --- a/pkg/apis/storage/fuzzer/fuzzer.go +++ b/pkg/apis/storage/fuzzer/fuzzer.go @@ -42,6 +42,10 @@ var Funcs = func(codecs runtimeserializer.CodecFactory) []interface{} { obj.Spec.AttachRequired = new(bool) *(obj.Spec.AttachRequired) = true } + if obj.Spec.PodInfoOnMount == nil { + obj.Spec.PodInfoOnMount = new(bool) + *(obj.Spec.PodInfoOnMount) = false + } }, } } diff --git a/pkg/apis/storage/types.go b/pkg/apis/storage/types.go index c7c8b638e06..daf23d61139 100644 --- a/pkg/apis/storage/types.go +++ b/pkg/apis/storage/types.go @@ -278,13 +278,15 @@ type CSIDriverSpec struct { // If set to true, podInfoOnMount indicates this CSI volume driver // requires additional pod information (like podName, podUID, etc.) during // mount operations. - // If not set or set to false, pod information will not be passed on mount. + // If set to false, pod information will not be passed on mount. + // Default is false. // The CSI driver specifies podInfoOnMount as part of driver deployment. // If true, Kubelet will pass pod information as VolumeContext in the CSI // NodePublishVolume() calls. // The CSI driver is responsible for parsing and validating the information // passed in as VolumeContext. - // The following VolumeConext will be passed if podInfoOnMount is set to true: + // The following VolumeConext will be passed if podInfoOnMount is set to true. + // This list might grow, but the prefix will be used. // "csi.storage.k8s.io/pod.name": pod.Name // "csi.storage.k8s.io/pod.namespace": pod.Namespace // "csi.storage.k8s.io/pod.uid": string(pod.UID) @@ -318,7 +320,7 @@ type CSINode struct { // CSINodeSpec holds information about the specification of all CSI drivers installed on a node type CSINodeSpec struct { // drivers is a list of information of all CSI Drivers existing on a node. - // It can be empty on initialization. + // If all drivers in the list are uninstalled, this can become empty. // +patchMergeKey=name // +patchStrategy=merge Drivers []CSINodeDriver diff --git a/pkg/apis/storage/v1beta1/defaults.go b/pkg/apis/storage/v1beta1/defaults.go index 69a43689706..30803281659 100644 --- a/pkg/apis/storage/v1beta1/defaults.go +++ b/pkg/apis/storage/v1beta1/defaults.go @@ -43,4 +43,8 @@ func SetDefaults_CSIDriver(obj *storagev1beta1.CSIDriver) { obj.Spec.AttachRequired = new(bool) *(obj.Spec.AttachRequired) = true } + if obj.Spec.PodInfoOnMount == nil { + obj.Spec.PodInfoOnMount = new(bool) + *(obj.Spec.PodInfoOnMount) = false + } } diff --git a/pkg/apis/storage/v1beta1/defaults_test.go b/pkg/apis/storage/v1beta1/defaults_test.go index f4092891b3d..d1cebcf7b90 100644 --- a/pkg/apis/storage/v1beta1/defaults_test.go +++ b/pkg/apis/storage/v1beta1/defaults_test.go @@ -65,12 +65,19 @@ func TestSetDefaultAttachRequired(t *testing.T) { driver := &storagev1beta1.CSIDriver{} // field should be defaulted - defaultMode := true + defaultAttach := true + defaultPodInfo := false output := roundTrip(t, runtime.Object(driver)).(*storagev1beta1.CSIDriver) - outMode := output.Spec.AttachRequired - if outMode == nil { - t.Errorf("Expected AttachRequired to be defaulted to: %+v, got: nil", defaultMode) - } else if *outMode != defaultMode { - t.Errorf("Expected AttachRequired to be defaulted to: %+v, got: %+v", defaultMode, outMode) + outAttach := output.Spec.AttachRequired + if outAttach == nil { + t.Errorf("Expected AttachRequired to be defaulted to: %+v, got: nil", defaultAttach) + } else if *outAttach != defaultAttach { + t.Errorf("Expected AttachRequired to be defaulted to: %+v, got: %+v", defaultAttach, outAttach) + } + outPodInfo := output.Spec.PodInfoOnMount + if outPodInfo == nil { + t.Errorf("Expected PodInfoOnMount to be defaulted to: %+v, got: nil", defaultPodInfo) + } else if *outPodInfo != defaultPodInfo { + t.Errorf("Expected PodInfoOnMount to be defaulted to: %+v, got: %+v", defaultPodInfo, outPodInfo) } } diff --git a/pkg/apis/storage/validation/validation.go b/pkg/apis/storage/validation/validation.go index 44bc8d2881d..518e04a9de3 100644 --- a/pkg/apis/storage/validation/validation.go +++ b/pkg/apis/storage/validation/validation.go @@ -324,7 +324,7 @@ func validateCSINodeDriverNodeID(nodeID string, fldPath *field.Path) field.Error allErrs = append(allErrs, field.Required(fldPath, nodeID)) } if len(nodeID) > csiNodeIDMaxLength { - allErrs = append(allErrs, field.Invalid(fldPath, nodeID, fmt.Sprintf("nodeID must be %d characters or less", csiNodeIDMaxLength))) + allErrs = append(allErrs, field.Invalid(fldPath, nodeID, fmt.Sprintf("must be %d characters or less", csiNodeIDMaxLength))) } return allErrs } @@ -387,6 +387,7 @@ func validateCSIDriverSpec( spec *storage.CSIDriverSpec, fldPath *field.Path) field.ErrorList { allErrs := field.ErrorList{} allErrs = append(allErrs, validateAttachRequired(spec.AttachRequired, fldPath.Child("attachedRequired"))...) + allErrs = append(allErrs, validatePodInfoOnMount(spec.PodInfoOnMount, fldPath.Child("podInfoOnMount"))...) return allErrs } @@ -399,3 +400,13 @@ func validateAttachRequired(attachRequired *bool, fldPath *field.Path) field.Err return allErrs } + +// validatePodInfoOnMount tests if podInfoOnMount is set for CSIDriver. +func validatePodInfoOnMount(podInfoOnMount *bool, fldPath *field.Path) field.ErrorList { + allErrs := field.ErrorList{} + if podInfoOnMount == nil { + allErrs = append(allErrs, field.Required(fldPath, "")) + } + + return allErrs +} diff --git a/pkg/apis/storage/validation/validation_test.go b/pkg/apis/storage/validation/validation_test.go index 79fd77c0d16..8c9c4c26886 100644 --- a/pkg/apis/storage/validation/validation_test.go +++ b/pkg/apis/storage/validation/validation_test.go @@ -1471,6 +1471,22 @@ func TestCSIDriverValidation(t *testing.T) { PodInfoOnMount: ¬PodInfoOnMount, }, }, + { + // AttachRequired not set + ObjectMeta: metav1.ObjectMeta{Name: driverName}, + Spec: storage.CSIDriverSpec{ + AttachRequired: nil, + PodInfoOnMount: &podInfoOnMount, + }, + }, + { + // AttachRequired not set + ObjectMeta: metav1.ObjectMeta{Name: driverName}, + Spec: storage.CSIDriverSpec{ + AttachRequired: &attachNotRequired, + PodInfoOnMount: nil, + }, + }, } for _, csiDriver := range errorCases { diff --git a/staging/src/k8s.io/api/storage/v1beta1/types.go b/staging/src/k8s.io/api/storage/v1beta1/types.go index d82e58f9039..49005791202 100644 --- a/staging/src/k8s.io/api/storage/v1beta1/types.go +++ b/staging/src/k8s.io/api/storage/v1beta1/types.go @@ -272,13 +272,15 @@ type CSIDriverSpec struct { // If set to true, podInfoOnMount indicates this CSI volume driver // requires additional pod information (like podName, podUID, etc.) during // mount operations. - // If not set or set to false, pod information will not be passed on mount. + // If set to false, pod information will not be passed on mount. + // Default is false. // The CSI driver specifies podInfoOnMount as part of driver deployment. // If true, Kubelet will pass pod information as VolumeContext in the CSI // NodePublishVolume() calls. // The CSI driver is responsible for parsing and validating the information // passed in as VolumeContext. - // The following VolumeConext will be passed if podInfoOnMount is set to true: + // The following VolumeConext will be passed if podInfoOnMount is set to true. + // This list might grow, but the prefix will be used. // "csi.storage.k8s.io/pod.name": pod.Name // "csi.storage.k8s.io/pod.namespace": pod.Namespace // "csi.storage.k8s.io/pod.uid": string(pod.UID) @@ -312,7 +314,7 @@ type CSINode struct { // CSINodeSpec holds information about the specification of all CSI drivers installed on a node type CSINodeSpec struct { // drivers is a list of information of all CSI Drivers existing on a node. - // It can be empty on initialization. + // If all drivers in the list are uninstalled, this can become empty. // +patchMergeKey=name // +patchStrategy=merge Drivers []CSINodeDriver `json:"drivers" patchStrategy:"merge" patchMergeKey:"name" protobuf:"bytes,1,rep,name=drivers"`