From ab2dcc01910c7ea9f4174e3b3a5070dd4609c955 Mon Sep 17 00:00:00 2001 From: Zzde Date: Sat, 12 Oct 2024 11:17:49 +0800 Subject: [PATCH] feat: specify cachedirs in configmap (#1141) * feat: specify cachedirs in configmap Signed-off-by: Xuhui zhang * fix unit test Signed-off-by: Xuhui zhang --------- Signed-off-by: Xuhui zhang --- pkg/config/config.go | 25 +++++++++++++++-- pkg/config/config_test.go | 19 ++++++++++++- pkg/config/setting.go | 40 ++++++++++++++++++++------- pkg/config/setting_test.go | 55 +++++++++++++++++++++++++++++++++++--- 4 files changed, 122 insertions(+), 17 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index a2f69e9578..8d6d9835e9 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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"` @@ -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 diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 09bea12801..dff0be093c 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -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 { @@ -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", @@ -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) { diff --git a/pkg/config/setting.go b/pkg/config/setting.go index edb7c97eb0..190d71ebc5 100644 --- a/pkg/config/setting.go +++ b/pkg/config/setting.go @@ -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 @@ -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 { @@ -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"} @@ -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 { diff --git a/pkg/config/setting_test.go b/pkg/config/setting_test.go index fc780177b6..975aad4e7f 100644 --- a/pkg/config/setting_test.go +++ b/pkg/config/setting_test.go @@ -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, }, @@ -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 {