Skip to content

Commit

Permalink
judge if configmap exists
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbeeSo committed Nov 10, 2024
1 parent 78aa62d commit d43ab7a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
4 changes: 4 additions & 0 deletions deploy/chart/templates/rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 21 additions & 12 deletions pkg/mounter/ossfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{
Expand All @@ -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"}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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] != ""
}
37 changes: 37 additions & 0 deletions pkg/mounter/ossfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}
}

0 comments on commit d43ab7a

Please sign in to comment.