Skip to content

Commit

Permalink
feat: specify cachedirs in configmap (#1141)
Browse files Browse the repository at this point in the history
* feat: specify cachedirs in configmap

Signed-off-by: Xuhui zhang <[email protected]>

* fix unit test

Signed-off-by: Xuhui zhang <[email protected]>

---------

Signed-off-by: Xuhui zhang <[email protected]>
  • Loading branch information
zxh326 authored Oct 12, 2024
1 parent 8215440 commit ab2dcc0
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 17 deletions.
25 changes: 23 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,31 @@ type PVCSelector struct {
MatchName string `json:"matchName,omitempty"`
}

type MountPatchCacheDirType string

var (
MountPatchCacheDirTypeHostPath MountPatchCacheDirType = "HostPath"
MountPatchCacheDirTypePVC MountPatchCacheDirType = "PVC"
)

type MountPatchCacheDir struct {
Type MountPatchCacheDirType `json:"type,omitempty"`

// required for HostPath type
Path string `json:"path,omitempty"`

// required for PVC type
Name string `json:"name,omitempty"`
}

type MountPodPatch struct {
// used to specify the selector for the PVC that will be patched
// omit will patch for all PVC
PVCSelector *PVCSelector `json:"pvcSelector,omitempty"`

CEMountImage string `json:"ceMountImage,omitempty"`
EEMountImage string `json:"eeMountImage,omitempty"`
CEMountImage string `json:"ceMountImage,omitempty"`
EEMountImage string `json:"eeMountImage,omitempty"`
CacheDirs []MountPatchCacheDir `json:"cacheDirs,omitempty"`

Image string `json:"-"`
Labels map[string]string `json:"labels,omitempty"`
Expand Down Expand Up @@ -275,6 +293,9 @@ func (mpp *MountPodPatch) merge(mp MountPodPatch) {
if mp.MountOptions != nil {
mpp.MountOptions = mp.MountOptions
}
if mp.CacheDirs != nil {
mpp.CacheDirs = mp.CacheDirs
}
}

// TODO: migrate more config for here
Expand Down
19 changes: 18 additions & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ MountPodPatch:
- name: block-devices
persistentVolumeClaim:
claimName: block-pvc
- cacheDirs:
- type: PVC
name: cache-pvc
- type: HostPath
Path: /tmp
`)
err := os.WriteFile(configPath, testData, 0644)
if err != nil {
Expand All @@ -100,7 +105,7 @@ MountPodPatch:
}
defer GlobalConfig.Reset()
// Check the loaded config
assert.Equal(t, len(GlobalConfig.MountPodPatch), 9)
assert.Equal(t, len(GlobalConfig.MountPodPatch), 10)
assert.Equal(t, GlobalConfig.MountPodPatch[0], MountPodPatch{
CEMountImage: "juicedata/mount:ce-test",
EEMountImage: "juicedata/mount:ee-test",
Expand Down Expand Up @@ -200,6 +205,18 @@ MountPodPatch:
},
},
})
assert.Equal(t, GlobalConfig.MountPodPatch[9], MountPodPatch{
CacheDirs: []MountPatchCacheDir{
{
Type: "PVC",
Name: "cache-pvc",
},
{
Type: "HostPath",
Path: "/tmp",
},
},
})
}

func TestGenMountPodPatch(t *testing.T) {
Expand Down
40 changes: 30 additions & 10 deletions pkg/config/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type PodAttr struct {
VolumeDevices []corev1.VolumeDevice `json:"volumeDevices,omitempty"`
VolumeMounts []corev1.VolumeMount `json:"volumeMounts,omitempty"`
Env []corev1.EnvVar `json:"env,omitempty"`
CacheDirs []MountPatchCacheDir `json:"cacheDirs,omitempty"`

// inherit from csi
Image string
Expand Down Expand Up @@ -256,20 +257,29 @@ func genCacheDirs(jfsSetting *JfsSetting, volCtx map[string]string) error {
cacheDirsInContainer := []string{}
var err error
// parse pvc of cache
cachePVCs := []string{}
if volCtx != nil && volCtx[common.CachePVC] != "" {
cachePVCs := strings.Split(strings.TrimSpace(volCtx[common.CachePVC]), ",")
for i, pvc := range cachePVCs {
if pvc == "" {
continue
cachePVCs = strings.Split(strings.TrimSpace(volCtx[common.CachePVC]), ",")
}
if jfsSetting.Attr != nil {
for _, cacheDir := range jfsSetting.Attr.CacheDirs {
if cacheDir.Type == MountPatchCacheDirTypePVC {
cachePVCs = append(cachePVCs, cacheDir.Name)
}
volPath := fmt.Sprintf("/var/jfsCache-%d", i)
jfsSetting.CachePVCs = append(jfsSetting.CachePVCs, CachePVC{
PVCName: pvc,
Path: volPath,
})
cacheDirsInContainer = append(cacheDirsInContainer, volPath)
}
}
for i, pvc := range cachePVCs {
if pvc == "" {
continue
}
volPath := fmt.Sprintf("/var/jfsCache-%d", i)
jfsSetting.CachePVCs = append(jfsSetting.CachePVCs, CachePVC{
PVCName: pvc,
Path: volPath,
})
cacheDirsInContainer = append(cacheDirsInContainer, volPath)
}

// parse emptydir of cache
if volCtx != nil {
if _, ok := volCtx[common.CacheEmptyDir]; ok {
Expand Down Expand Up @@ -335,6 +345,15 @@ func genCacheDirs(jfsSetting *JfsSetting, volCtx map[string]string) error {
break
}
}
// parse hostPath dirs in setting attr
if jfsSetting.Attr != nil {
for _, cacheDir := range jfsSetting.Attr.CacheDirs {
if cacheDir.Type == MountPatchCacheDirTypeHostPath {
cacheDirsInContainer = append(cacheDirsInContainer, cacheDir.Path)
jfsSetting.CacheDirs = append(jfsSetting.CacheDirs, cacheDir.Path)
}
}
}
if len(cacheDirsInContainer) == 0 {
// set default cache dir
cacheDirsInOptions = []string{"/var/jfsCache"}
Expand Down Expand Up @@ -749,6 +768,7 @@ func applyConfigPatch(setting *JfsSetting) {
attr.VolumeMounts = patch.VolumeMounts
attr.Volumes = patch.Volumes
attr.Env = patch.Env
attr.CacheDirs = patch.CacheDirs

// merge or overwrite setting options
if setting.Options == nil {
Expand Down
55 changes: 51 additions & 4 deletions pkg/config/setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,12 +646,29 @@ func Test_genCacheDirs(t *testing.T) {
{
name: "test-cache-pvcs",
args: args{
JfsSetting: JfsSetting{},
volCtx: map[string]string{"juicefs/mount-cache-pvc": "abc,def"},
JfsSetting: JfsSetting{
Attr: &PodAttr{
CacheDirs: []MountPatchCacheDir{
{
Type: "PVC",
Name: "sss",
},
},
},
},
volCtx: map[string]string{"juicefs/mount-cache-pvc": "abc,def"},
},
want: JfsSetting{
CachePVCs: []CachePVC{{PVCName: "abc", Path: "/var/jfsCache-0"}, {PVCName: "def", Path: "/var/jfsCache-1"}},
Options: []string{"cache-dir=/var/jfsCache-0:/var/jfsCache-1"},
CachePVCs: []CachePVC{{PVCName: "abc", Path: "/var/jfsCache-0"}, {PVCName: "def", Path: "/var/jfsCache-1"}, {PVCName: "sss", Path: "/var/jfsCache-2"}},
Options: []string{"cache-dir=/var/jfsCache-0:/var/jfsCache-1:/var/jfsCache-2"},
Attr: &PodAttr{
CacheDirs: []MountPatchCacheDir{
{
Type: "PVC",
Name: "sss",
},
},
},
},
wantErr: false,
},
Expand Down Expand Up @@ -686,6 +703,36 @@ func Test_genCacheDirs(t *testing.T) {
},
wantErr: false,
},
{
name: "test-hostPath-with-pod-attr",
args: args{
JfsSetting: JfsSetting{
Attr: &PodAttr{
CacheDirs: []MountPatchCacheDir{
{
Type: "HostPath",
Path: "/abc",
},
},
},
},
},
want: JfsSetting{
Attr: &PodAttr{
CacheDirs: []MountPatchCacheDir{
{
Type: "HostPath",
Path: "/abc",
},
},
},
CacheDirs: []string{
"/abc",
},
Options: []string{"cache-dir=/abc"},
},
wantErr: false,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit ab2dcc0

Please sign in to comment.