|
| 1 | +/******************************************************************************* |
| 2 | +* IBM Confidential |
| 3 | +* OCO Source Materials |
| 4 | +* IBM Cloud Kubernetes Service, 5737-D43 |
| 5 | +* (C) Copyright IBM Corp. 2023 All Rights Reserved. |
| 6 | +* The source code for this program is not published or otherwise divested of |
| 7 | +* its trade secrets, irrespective of what has been deposited with |
| 8 | +* the U.S. Copyright Office. |
| 9 | +******************************************************************************/ |
| 10 | + |
| 11 | +// Package mounter |
| 12 | +package mounter |
| 13 | + |
| 14 | +import ( |
| 15 | + "crypto/sha256" |
| 16 | + "fmt" |
| 17 | + "os" |
| 18 | + "path" |
| 19 | + |
| 20 | + // "github.com/IBM/ibm-object-csi-driver/pkg/constants" |
| 21 | + "github.com/IBM/ibm-object-csi-driver/pkg/mounter/utils" |
| 22 | + "k8s.io/klog/v2" |
| 23 | +) |
| 24 | + |
| 25 | +// Mounter interface defined in mounter.go |
| 26 | +// mntS3Mounter Implements Mounter |
| 27 | +type mntS3Mounter struct { |
| 28 | + bucketName string //From Secret in SC |
| 29 | + objPath string //From Secret in SC |
| 30 | + endPoint string //From Secret in SC |
| 31 | + locConstraint string //From Secret in SC |
| 32 | + authType string |
| 33 | + accessKey string |
| 34 | + secretKey string |
| 35 | + mountOptions []string |
| 36 | + MounterUtils utils.MounterUtils |
| 37 | +} |
| 38 | + |
| 39 | +func NewMntS3Mounter(secretMap map[string]string, mountOptions []string, mounterUtils utils.MounterUtils) Mounter { |
| 40 | + klog.Info("-newMntS3Mounter-") |
| 41 | + |
| 42 | + var ( |
| 43 | + val string |
| 44 | + check bool |
| 45 | + mounter *mntS3Mounter |
| 46 | + ) |
| 47 | + |
| 48 | + mounter = &mntS3Mounter{} |
| 49 | + |
| 50 | + if val, check = secretMap["cosEndpoint"]; check { |
| 51 | + mounter.endPoint = val |
| 52 | + } |
| 53 | + if val, check = secretMap["locationConstraint"]; check { |
| 54 | + mounter.locConstraint = val |
| 55 | + } |
| 56 | + if val, check = secretMap["bucketName"]; check { |
| 57 | + mounter.bucketName = val |
| 58 | + } |
| 59 | + if val, check = secretMap["objPath"]; check { |
| 60 | + mounter.objPath = val |
| 61 | + } |
| 62 | + if val, check = secretMap["accessKey"]; check { |
| 63 | + mounter.accessKey = val |
| 64 | + } |
| 65 | + if val, check = secretMap["secretKey"]; check { |
| 66 | + mounter.secretKey = val |
| 67 | + } |
| 68 | + |
| 69 | + klog.Infof("newMntS3Mounter args:\n\tbucketName: [%s]\n\tobjPath: [%s]\n\tendPoint: [%s]\n\tlocationConstraint: [%s]\n\tauthType: [%s]", |
| 70 | + mounter.bucketName, mounter.objPath, mounter.endPoint, mounter.locConstraint, mounter.authType) |
| 71 | + |
| 72 | + mounter.mountOptions = mountOptions |
| 73 | + mounter.MounterUtils = mounterUtils |
| 74 | + |
| 75 | + return mounter |
| 76 | +} |
| 77 | + |
| 78 | +const ( |
| 79 | + mntS3Cmd = "mount-s3" |
| 80 | + metaRootMntS3 = "/var/lib/ibmc-mntS3" |
| 81 | +) |
| 82 | + |
| 83 | +func (mntS3 *mntS3Mounter) Stage(stagePath string) error { |
| 84 | + return nil |
| 85 | +} |
| 86 | +func (mntS3 *mntS3Mounter) Unstage(stagePath string) error { |
| 87 | + return nil |
| 88 | +} |
| 89 | +func (mntS3 *mntS3Mounter) Mount(source string, target string) error { |
| 90 | + klog.Info("-MntS3Mounter Mount-") |
| 91 | + klog.Infof("Mount args:\n\tsource: <%s>\n\ttarget: <%s>", source, target) |
| 92 | + var pathExist bool |
| 93 | + var err error |
| 94 | + metaPath := path.Join(metaRootMntS3, fmt.Sprintf("%x", sha256.Sum256([]byte(target)))) |
| 95 | + |
| 96 | + if pathExist, err = checkPath(metaPath); err != nil { |
| 97 | + klog.Errorf("MntS3Mounter Mount: Cannot stat directory %s: %v", metaPath, err) |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + if !pathExist { |
| 102 | + if err = os.MkdirAll(metaPath, 0755); // #nosec G301: used for mntS3 |
| 103 | + err != nil { |
| 104 | + klog.Errorf("MntS3Mounter Mount: Cannot create directory %s: %v", metaPath, err) |
| 105 | + return err |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + os.Setenv("AWS_ACCESS_KEY_ID", mntS3.accessKey) |
| 110 | + os.Setenv("AWS_SECRET_ACCESS_KEY", mntS3.secretKey) |
| 111 | + |
| 112 | + args := []string{ |
| 113 | + fmt.Sprintf("--endpoint-url=%v", mntS3.endPoint), |
| 114 | + mntS3.bucketName, |
| 115 | + target, |
| 116 | + } |
| 117 | + |
| 118 | + if mntS3.objPath != "" { |
| 119 | + args = append(args, fmt.Sprintf("--prefix %s", mntS3.objPath)) |
| 120 | + } |
| 121 | + return mntS3.MounterUtils.FuseMount(target, mntS3Cmd, args) |
| 122 | +} |
| 123 | +func (mntS3 *mntS3Mounter) Unmount(target string) error { |
| 124 | + klog.Info("-MntS3Mounter Unmount-") |
| 125 | + metaPath := path.Join(metaRootMntS3, fmt.Sprintf("%x", sha256.Sum256([]byte(target)))) |
| 126 | + err := os.RemoveAll(metaPath) |
| 127 | + if err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + |
| 131 | + return mntS3.MounterUtils.FuseUnmount(target) |
| 132 | +} |
0 commit comments