Skip to content

Commit

Permalink
add host path type for apiserver
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyu.jiang committed Jul 25, 2022
1 parent 09c7da3 commit 4052748
Show file tree
Hide file tree
Showing 5 changed files with 404 additions and 185 deletions.
21 changes: 19 additions & 2 deletions apiserver/pkg/util/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,23 @@ func buildWorkerPodTemplate(cluster *api.Cluster, spec *api.WorkerGroupSpec, com
}

func buildVolumeMounts(apiVolumes []*api.Volume) []v1.VolumeMount {
var volMounts []v1.VolumeMount
var (
volMounts []v1.VolumeMount
hostToContainer = v1.MountPropagationHostToContainer
bidirectonal = v1.MountPropagationBidirectional
)
for _, vol := range apiVolumes {
volMount := v1.VolumeMount{
Name: vol.Name,
ReadOnly: vol.ReadOnly,
MountPath: vol.MountPath,
}
switch vol.MountPropagationMode {
case api.Volume_HOSTTOCONTAINER:
volMount.MountPropagation = &hostToContainer
case api.Volume_BIDIRECTIONAL:
volMount.MountPropagation = &bidirectonal
}
volMounts = append(volMounts, volMount)
}
return volMounts
Expand All @@ -354,10 +364,17 @@ func buildVols(apiVolumes []*api.Volume) []v1.Volume {
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: rayVol.Source,
Type: newHostPathType(string(v1.HostPathDirectory)),
},
},
}
switch rayVol.HostPathType {
case api.Volume_DIRECTORY:
vol.VolumeSource.HostPath.Type = newHostPathType(string(v1.HostPathDirectory))
case api.Volume_FILE:
vol.VolumeSource.HostPath.Type = newHostPathType(string(v1.HostPathFile))
default:
vol.VolumeSource.HostPath.Type = newHostPathType(string(v1.HostPathDirectory))
}
vols = append(vols, vol)
}
// TODO(Jeffwan@): handle PVC in the future
Expand Down
39 changes: 34 additions & 5 deletions apiserver/pkg/util/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ var testVolume = &api.Volume{
ReadOnly: true,
}

func TestBuildVolumeMounts(t *testing.T) {
// There is only an fake case for test both MountPropagationMode and file type
// in real case hostToContainer mode may only valid for directory
var testFileVolume = &api.Volume{
Name: "test-file",
VolumeType: api.Volume_HOST_PATH,
MountPropagationMode: api.Volume_HOSTTOCONTAINER,
Source: "/root/proc/stat",
MountPath: "/proc/stat",
HostPathType: api.Volume_FILE,
ReadOnly: true,
}

func TestBuildVolumes(t *testing.T) {
targetVolume := v1.Volume{
Name: testVolume.Name,
VolumeSource: v1.VolumeSource{
Expand All @@ -26,7 +38,15 @@ func TestBuildVolumeMounts(t *testing.T) {
},
},
}

targetFileVolume := v1.Volume{
Name: testFileVolume.Name,
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: testFileVolume.Source,
Type: newHostPathType(string(v1.HostPathFile)),
},
},
}
tests := []struct {
name string
apiVolume []*api.Volume
Expand All @@ -35,9 +55,9 @@ func TestBuildVolumeMounts(t *testing.T) {
{
"normal test",
[]*api.Volume{
testVolume,
testVolume, testFileVolume,
},
[]v1.Volume{targetVolume},
[]v1.Volume{targetVolume, targetFileVolume},
},
}
for _, tt := range tests {
Expand All @@ -50,12 +70,19 @@ func TestBuildVolumeMounts(t *testing.T) {
}
}

func TestBuildVolumes(t *testing.T) {
func TestBuildVolumeMounts(t *testing.T) {
hostToContainer := v1.MountPropagationHostToContainer
targetVolumeMount := v1.VolumeMount{
Name: testVolume.Name,
ReadOnly: testVolume.ReadOnly,
MountPath: testVolume.MountPath,
}
targetFileVolumeMount := v1.VolumeMount{
Name: testFileVolume.Name,
ReadOnly: testFileVolume.ReadOnly,
MountPath: testFileVolume.MountPath,
MountPropagation: &hostToContainer,
}
tests := []struct {
name string
apiVolume []*api.Volume
Expand All @@ -65,9 +92,11 @@ func TestBuildVolumes(t *testing.T) {
"normal test",
[]*api.Volume{
testVolume,
testFileVolume,
},
[]v1.VolumeMount{
targetVolumeMount,
targetFileVolumeMount,
},
},
}
Expand Down
15 changes: 15 additions & 0 deletions proto/cluster.proto
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,21 @@ message Volume {
string name = 3;
string source = 4;
bool read_only = 5;

// If indicate hostpath, we need to let user indicate which type
// they would like to use.
enum HostPathType {
DIRECTORY = 0;
FILE = 1;
}
HostPathType host_path_type = 6;

enum MountPropagationMode {
NONE = 0;
HOSTTOCONTAINER = 1;
BIDIRECTIONAL = 2;
}
MountPropagationMode mount_propagation_mode = 7;
}

message HeadGroupSpec {
Expand Down
Loading

0 comments on commit 4052748

Please sign in to comment.