diff --git a/pkg/api/fuzzer/fuzzer.go b/pkg/api/fuzzer/fuzzer.go index d6da2708219..045952d9cb1 100644 --- a/pkg/api/fuzzer/fuzzer.go +++ b/pkg/api/fuzzer/fuzzer.go @@ -357,8 +357,8 @@ var Funcs = func(codecs runtimeserializer.CodecFactory) []interface{} { }, func(obj *api.HostPathVolumeSource, c fuzz.Continue) { c.FuzzNoCustom(obj) - types := []api.HostPathType{api.HostPathDirectoryOrCreate, api.HostPathDirectory, api.HostPathFileOrCreate, - api.HostPathFile, api.HostPathSocket, api.HostPathCharDev, api.HostPathBlockDev} + types := []api.HostPathType{api.HostPathUnset, api.HostPathDirectoryOrCreate, api.HostPathDirectory, + api.HostPathFileOrCreate, api.HostPathFile, api.HostPathSocket, api.HostPathCharDev, api.HostPathBlockDev} typeVol := types[c.Rand.Intn(len(types))] if obj.Path != "" && obj.Type == nil { obj.Type = &typeVol diff --git a/pkg/api/types.go b/pkg/api/types.go index cd0a48fa865..57a3f74d730 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -600,6 +600,8 @@ const ( type HostPathType string const ( + // For backwards compatible, leave it empty if unset + HostPathUnset HostPathType = "" // If nothing exists at the given path, an empty directory will be created there // as needed with file mode 0755, having the same group and ownership with Kubelet. HostPathDirectoryOrCreate HostPathType = "DirectoryOrCreate" @@ -623,7 +625,7 @@ const ( type HostPathVolumeSource struct { // If the path is a symlink, it will follow the link to the real path. Path string - // Defaults to DirectoryOrCreate + // Defaults to "" Type *HostPathType } diff --git a/pkg/api/v1/defaults.go b/pkg/api/v1/defaults.go index 83bcf150930..8b062695f93 100644 --- a/pkg/api/v1/defaults.go +++ b/pkg/api/v1/defaults.go @@ -371,7 +371,7 @@ func SetDefaults_ScaleIOVolumeSource(obj *v1.ScaleIOVolumeSource) { } func SetDefaults_HostPathVolumeSource(obj *v1.HostPathVolumeSource) { - typeVol := v1.HostPathDirectoryOrCreate + typeVol := v1.HostPathUnset if obj.Type == nil { obj.Type = &typeVol } diff --git a/pkg/api/v1/defaults_test.go b/pkg/api/v1/defaults_test.go index 0e39bd55d9f..6b7027d1ae5 100644 --- a/pkg/api/v1/defaults_test.go +++ b/pkg/api/v1/defaults_test.go @@ -1277,3 +1277,25 @@ func TestSetDefaultSchedulerName(t *testing.T) { t.Errorf("Expected scheduler name: %+v\ngot: %+v\n", v1.DefaultSchedulerName, output.Spec.SchedulerName) } } + +func TestSetDefaultHostPathVolumeSource(t *testing.T) { + s := v1.PodSpec{} + s.Volumes = []v1.Volume{ + { + VolumeSource: v1.VolumeSource{ + HostPath: &v1.HostPathVolumeSource{Path: "foo"}, + }, + }, + } + pod := &v1.Pod{ + Spec: s, + } + output := roundTrip(t, runtime.Object(pod)) + pod2 := output.(*v1.Pod) + defaultType := pod2.Spec.Volumes[0].VolumeSource.HostPath.Type + expectedType := v1.HostPathUnset + + if defaultType == nil || *defaultType != expectedType { + t.Errorf("Expected v1.HostPathVolumeSource default type %v, got %v", expectedType, defaultType) + } +} diff --git a/pkg/api/validation/validation.go b/pkg/api/validation/validation.go index fcd2af9126f..f6511a280fd 100644 --- a/pkg/api/validation/validation.go +++ b/pkg/api/validation/validation.go @@ -976,6 +976,7 @@ func validateProjectedVolumeSource(projection *api.ProjectedVolumeSource, fldPat } var supportedHostPathTypes = sets.NewString( + string(api.HostPathUnset), string(api.HostPathDirectoryOrCreate), string(api.HostPathDirectory), string(api.HostPathFileOrCreate), diff --git a/pkg/volume/host_path/host_path.go b/pkg/volume/host_path/host_path.go index 57853918608..e00fc8e5125 100644 --- a/pkg/volume/host_path/host_path.go +++ b/pkg/volume/host_path/host_path.go @@ -214,6 +214,9 @@ func (b *hostPathMounter) SetUp(fsGroup *int64) error { return fmt.Errorf("invalid HostPath `%s`: %v", b.GetPath(), err) } + if *b.pathType == v1.HostPathUnset { + return nil + } return checkType(b.GetPath(), b.pathType) } diff --git a/pkg/volume/host_path/host_path_test.go b/pkg/volume/host_path/host_path_test.go index c588099773f..d7761dc890a 100644 --- a/pkg/volume/host_path/host_path_test.go +++ b/pkg/volume/host_path/host_path_test.go @@ -554,7 +554,7 @@ func TestHostPathTypeCheckerInternal(t *testing.T) { isFile: false, exists: false, validpathType: newHostPathTypeList(string(v1.HostPathFileOrCreate)), - invalidpathType: newHostPathTypeList(string(v1.HostPathFile), string(v1.HostPathDirectory), + invalidpathType: newHostPathTypeList(string(v1.HostPathDirectory), string(v1.HostPathSocket), string(v1.HostPathCharDev), string(v1.HostPathBlockDev)), }, { diff --git a/staging/src/k8s.io/api/core/v1/types.go b/staging/src/k8s.io/api/core/v1/types.go index 210b06c4e6c..17c7f35b8ee 100644 --- a/staging/src/k8s.io/api/core/v1/types.go +++ b/staging/src/k8s.io/api/core/v1/types.go @@ -684,6 +684,8 @@ const ( type HostPathType string const ( + // For backwards compatible, leave it empty if unset + HostPathUnset HostPathType = "" // If nothing exists at the given path, an empty directory will be created there // as needed with file mode 0755, having the same group and ownership with Kubelet. HostPathDirectoryOrCreate HostPathType = "DirectoryOrCreate" @@ -710,7 +712,7 @@ type HostPathVolumeSource struct { // More info: https://kubernetes.io/docs/concepts/storage/volumes#hostpath Path string `json:"path" protobuf:"bytes,1,opt,name=path"` // Type for HostPath Volume - // Defaults to DirectoryOrCreate + // Defaults to "" // More info: https://kubernetes.io/docs/concepts/storage/volumes#hostpath // +optional Type *HostPathType `json:"type,omitempty" protobuf:"bytes,2,opt,name=type"`