From d43ab7a68143cc77519a874a6d615d8e536f4839 Mon Sep 17 00:00:00 2001 From: AlbeeSo Date: Sun, 10 Nov 2024 20:56:08 +0800 Subject: [PATCH] judge if configmap exists --- deploy/chart/templates/rbac.yaml | 4 ++++ pkg/mounter/ossfs.go | 33 +++++++++++++++++----------- pkg/mounter/ossfs_test.go | 37 ++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 12 deletions(-) diff --git a/deploy/chart/templates/rbac.yaml b/deploy/chart/templates/rbac.yaml index 2c1b43b7f..65d65cd37 100644 --- a/deploy/chart/templates/rbac.yaml +++ b/deploy/chart/templates/rbac.yaml @@ -63,6 +63,10 @@ rules: - apiGroups: ["storage.alibabacloud.com"] resources: ["containernetworkfilesystems"] verbs: ["get","list", "watch"] + - apiGroups: [""] + resources: ["configmaps"] + resourceNames: ["ossfs-mime-config"] + verbs: ["get"] --- kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 diff --git a/pkg/mounter/ossfs.go b/pkg/mounter/ossfs.go index 38ee8c58b..5ba6b9a15 100644 --- a/pkg/mounter/ossfs.go +++ b/pkg/mounter/ossfs.go @@ -171,7 +171,7 @@ func (f *fuseOssfs) buildPodSpec(c *FusePodContext, target string) (spec corev1. } *mimeFileVolume.ConfigMap.Optional = true - spec.Volumes = []corev1.Volume{targetDirVolume, metricsDirVolume, etcDirVolume, mimeFileVolume} + spec.Volumes = []corev1.Volume{targetDirVolume, metricsDirVolume, etcDirVolume} bidirectional := corev1.MountPropagationBidirectional socketPath := GetOssfsMountProxySocketPath(c.VolumeId) @@ -190,9 +190,6 @@ func (f *fuseOssfs) buildPodSpec(c *FusePodContext, target string) (spec corev1. }, { Name: etcDirVolume.Name, MountPath: etcDirVolume.HostPath.Path, - }, { - Name: mimeFileVolume.Name, - MountPath: "/csi", }, }, SecurityContext: &corev1.SecurityContext{ @@ -211,6 +208,14 @@ func (f *fuseOssfs) buildPodSpec(c *FusePodContext, target string) (spec corev1. }, } + if hasSetMimeConfigMap(c.VolumeId) { + spec.Volumes = append(spec.Volumes, mimeFileVolume) + container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{ + Name: mimeFileVolume.Name, + MountPath: "/csi", + }) + } + buildAuthSpec(c, target, &spec, &container) container.Args = []string{"-socket=" + socketPath, "-v=4"} @@ -257,14 +262,7 @@ func (f *fuseOssfs) AddDefaultMountOptions(options []string, volumeId string) [] func (f *fuseOssfs) getMimeOption(volumeId string) string { // if config has set for specified volume, use it - cfg := options.MustGetRestConfig() - clientset := kubernetes.NewForConfigOrDie(cfg) - - configmap, err := clientset.CoreV1().ConfigMaps(FusePodNamespace).Get(context.Background(), OssfsCsiMimeConfigMap, metav1.GetOptions{}) - if err != nil && !apierrors.IsNotFound(err) { - klog.Errorf("failed to get configmap %s/%s: %v", FusePodNamespace, OssfsCsiMimeConfigMap, err) - } - if configmap != nil && configmap.Data != nil && configmap.Data[volumeId] != "" { + if hasSetMimeConfigMap(volumeId) { return fmt.Sprintf("mime=%s", OssfsCsiMimeTypesFilePath) } @@ -445,3 +443,14 @@ func getPasswdSecretVolume(secretRef string) (secret *corev1.SecretVolumeSource) } return } + +func hasSetMimeConfigMap(volumeId string) bool { + cfg := options.MustGetRestConfig() + clientset := kubernetes.NewForConfigOrDie(cfg) + + configmap, err := clientset.CoreV1().ConfigMaps(FusePodNamespace).Get(context.Background(), OssfsCsiMimeConfigMap, metav1.GetOptions{}) + if err != nil && !apierrors.IsNotFound(err) { + klog.Errorf("failed to get configmap %s/%s: %v", FusePodNamespace, OssfsCsiMimeConfigMap, err) + } + return configmap != nil && configmap.Data != nil && configmap.Data[volumeId] != "" +} diff --git a/pkg/mounter/ossfs_test.go b/pkg/mounter/ossfs_test.go index 05141f670..b54ee3c3e 100644 --- a/pkg/mounter/ossfs_test.go +++ b/pkg/mounter/ossfs_test.go @@ -267,3 +267,40 @@ func Test_getMimeOption(t *testing.T) { }) } } + +func Test_hasSetMimeConfigMap(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + prepareFakeK8sContext() + tests := []struct { + name string + volumeId string + configMapExist bool + want bool + }{ + { + name: "ConfigMap exists with matched volumeId", + volumeId: "volumeId", + configMapExist: true, + want: true, + }, + { + name: "ConfigMap not exists", + volumeId: "volumeId", + configMapExist: false, + want: false, + }, + { + name: "ConfigMap exists without matched volumeId", + volumeId: "fakeId", + configMapExist: true, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + registerConfigMapResponder(tt.configMapExist) + assert.Equal(t, tt.want, hasSetMimeConfigMap(tt.volumeId)) + }) + } +}