Skip to content

Commit

Permalink
Merge pull request #32 from chengyu-l/csi-spec-v0.3.0
Browse files Browse the repository at this point in the history
Feature: Using different user's name when create a new volume, it aviod that all S3 user use same AK and SK
  • Loading branch information
awzhgw authored Sep 21, 2020
2 parents 2064487 + cf33f4a commit 98a4cf5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 96 deletions.
3 changes: 1 addition & 2 deletions build/build_cfs_client.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#!/bin/sh

RootPath=$(cd "$(dirname $0)";pwd)
CfsVersion=2.0.0
test -e ${RootPath}/bin/cfs-client && exit 0
test -d ${RootPath}/chubaofs && rm -rf ${RootPath}/chubaofs
git clone https://github.com/chubaofs/chubaofs.git ${RootPath}/chubaofs && cd ${RootPath}/chubaofs && git checkout v${CfsVersion}
git clone https://github.com/chubaofs/chubaofs.git ${RootPath}/chubaofs && cd ${RootPath}/chubaofs
ChubaoFSSrcPath=${RootPath}/chubaofs
Out=`docker run -it --rm --privileged -v ${ChubaoFSSrcPath}:/root/go/src/github.com/chubaofs/chubaofs chubaofs/cfs-base:1.0.1 \
/bin/bash -c 'cd /root/go/src/github.com/chubaofs/chubaofs/build && bash ./build.sh > build.out 2>&1 && echo 0 || echo 1'`
Expand Down
2 changes: 1 addition & 1 deletion deploy/csi-controller-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ spec:
- name: kube-config
mountPath: /etc/kubernetes
- name: cfs-driver
image: chubaofs/cfs-csi-driver:2.1.3
image: chubaofs/cfs-csi-driver:2.2.1.030.0
imagePullPolicy: IfNotPresent
securityContext:
privileged: true
Expand Down
2 changes: 1 addition & 1 deletion deploy/csi-node-daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ spec:
- name: kube-config
mountPath: /etc/kubernetes
- name: cfs-driver
image: chubaofs/cfs-csi-driver:2.1.3
image: chubaofs/cfs-csi-driver:2.2.1.030.0
imagePullPolicy: IfNotPresent
securityContext:
privileged: true
Expand Down
135 changes: 45 additions & 90 deletions pkg/chubaofs/cfs_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,27 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
csicommon "github.com/chubaofs/chubaofs-csi/pkg/csi-common"
"github.com/golang/glog"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
)

const (
KVolumeName = "volName"
KMasterAddr = "masterAddr"
KlogLevel = "logLevel"
KLogLevel = "logLevel"
KLogDir = "logDir"
KOwner = "owner"
KMountPoint = "mountPoint"
KExporterPort = "exporterPort"
KProfPort = "profPort"
KCrossZone = "crossZone"
KEnableToken = "enableToken"
KZoneName = "zoneName"
Expand Down Expand Up @@ -65,39 +72,8 @@ const (
)

type cfsServer struct {
masterAddr string
volName string
owner string
clientConfFile string
crossZone string
enableToken string
zoneName string
clientConf *cfsClientConf
}

type cfsClientConf struct {
MasterAddr string `json:"masterAddr"`
VolName string `json:"volName"`
Owner string `json:"owner"`
LogDir string `json:"logDir"`
MountPoint string `json:"mountPoint"`
LogLevel string `json:"logLevel"`
ConsulAddr string `json:"consulAddr,omitempty"`
ExporterPort int `json:"exporterPort"`
ProfPort int `json:"profPort"`
LookupValid string `json:"lookupValid,omitempty"`
AttrValid string `json:"attrValid,omitempty"`
IcacheTimeout string `json:"icacheTimeout,omitempty"`
EnSyncWrite string `json:"enSyncWrite,omitempty"`
AutoInvalData string `json:"autoInvalData,omitempty"`
Rdonly string `json:"rdonly,omitempty"`
Writecache string `json:"writecache,omitempty"`
Keepcache string `json:"keepcache,omitempty"`
Authenticate string `json:"authenticate,omitempty"`
TicketHosts string `json:"ticketHost,omitempty"`
EnableHTTPS string `json:"enableHTTPS,omitempty"`
AccessKey string `json:"accessKey,omitempty"`
SecretKey string `json:"secretKey,omitempty"`
clientConf map[string]string
}

// Create and Delete Volume Response
Expand All @@ -115,57 +91,34 @@ func newCfsServer(volName string, param map[string]string) (cs *cfsServer, err e

newVolName := getValueWithDefault(param, KVolumeName, volName)
clientConfFile := defaultClientConfPath + newVolName + jsonFileSuffix
logDir := defaultLogDir + newVolName
owner := getValueWithDefault(param, KOwner, defaultOwner)
crossZone := getValueWithDefault(param, KCrossZone, "false")
enableToken := getValueWithDefault(param, KEnableToken, "false")
zoneName := getValue(param, KZoneName)

logLevel := getValueWithDefault(param, KlogLevel, defaultLogLevel)
consulAddr := getValue(param, KConsulAddr)
lookupValid := getValue(param, KLookupValid)
attrValid := getValue(param, KAttrValid)
icacheTimeout := getValue(param, KIcacheTimeout)
enSyncWrite := getValue(param, KEnSyncWrite)
autoInvalData := getValue(param, KAutoInvalData)
rdonly := getValueWithDefault(param, KRdonly, "false")
writecache := getValue(param, KWritecache)
keepcache := getValue(param, KKeepcache)
authenticate := getValueWithDefault(param, KAuthenticate, "false")
ticketHost := getValue(param, KTicketHosts)
enableHTTPS := getValueWithDefault(param, KEnableHTTPS, "false")
accessKey := getValue(param, KAccessKey)
secretKey := getValue(param, KSecretKey)
newOwner := csicommon.ShortenString(fmt.Sprintf("csi_%d", time.Now().UnixNano()), 20)
clientConf := make(map[string]string)
clientConf[KMasterAddr] = masterAddr
clientConf[KVolumeName] = newVolName
clientConf[KOwner] = getValueWithDefault(param, KOwner, newOwner)
clientConf[KLogLevel] = getValueWithDefault(param, KLogLevel, defaultLogLevel)
clientConf[KLogDir] = defaultLogDir + newVolName
clientConf[KZoneName] = getValue(param, KZoneName)
clientConf[KCrossZone] = getValueWithDefault(param, KCrossZone, "false")
clientConf[KConsulAddr] = getValue(param, KConsulAddr)
clientConf[KEnableToken] = getValueWithDefault(param, KEnableToken, "false")
clientConf[KLookupValid] = getValue(param, KLookupValid)
clientConf[KAttrValid] = getValue(param, KAttrValid)
clientConf[KIcacheTimeout] = getValue(param, KIcacheTimeout)
clientConf[KEnSyncWrite] = getValue(param, KEnSyncWrite)
clientConf[KAutoInvalData] = getValue(param, KAutoInvalData)
clientConf[KRdonly] = getValueWithDefault(param, KRdonly, "false")
clientConf[KWritecache] = getValue(param, KWritecache)
clientConf[KKeepcache] = getValue(param, KKeepcache)
clientConf[KAuthenticate] = getValueWithDefault(param, KAuthenticate, "false")
clientConf[KTicketHosts] = getValue(param, KTicketHosts)
clientConf[KEnableHTTPS] = getValueWithDefault(param, KEnableHTTPS, "false")
clientConf[KAccessKey] = getValue(param, KAccessKey)
clientConf[KSecretKey] = getValue(param, KSecretKey)

return &cfsServer{
masterAddr: masterAddr,
volName: newVolName,
owner: owner,
crossZone: crossZone,
enableToken: enableToken,
zoneName: zoneName,
clientConfFile: clientConfFile,
clientConf: &cfsClientConf{
MasterAddr: masterAddr,
VolName: newVolName,
Owner: owner,
LogLevel: logLevel,
ConsulAddr: consulAddr,
LogDir: logDir,
LookupValid: lookupValid,
AttrValid: attrValid,
IcacheTimeout: icacheTimeout,
EnSyncWrite: enSyncWrite,
AutoInvalData: autoInvalData,
Rdonly: rdonly,
Writecache: writecache,
Keepcache: keepcache,
Authenticate: authenticate,
TicketHosts: ticketHost,
EnableHTTPS: enableHTTPS,
AccessKey: accessKey,
SecretKey: secretKey,
},
clientConf: clientConf,
}, err
}

Expand All @@ -183,24 +136,26 @@ func getValueWithDefault(param map[string]string, key string, defaultValue strin
}

func (cs *cfsServer) persistClientConf(mountPoint string) error {
cs.clientConf.MountPoint = mountPoint
cs.clientConf.ExporterPort, _ = getFreePort(defaultExporterPort)
cs.clientConf.ProfPort, _ = getFreePort(defaultProfPort)
_ = os.Mkdir(cs.clientConf.LogDir, 0777)
exporterPort, _ := getFreePort(defaultExporterPort)
profPort, _ := getFreePort(defaultProfPort)
cs.clientConf[KMountPoint] = mountPoint
cs.clientConf[KExporterPort] = strconv.Itoa(exporterPort)
cs.clientConf[KProfPort] = strconv.Itoa(profPort)
_ = os.Mkdir(cs.clientConf[KLogDir], 0777)
clientConfBytes, _ := json.Marshal(cs.clientConf)
err := ioutil.WriteFile(cs.clientConfFile, clientConfBytes, 0444)
if err != nil {
return status.Errorf(codes.Internal, "create client config file fail. err: %v", err.Error())
}

glog.V(0).Infof("create client config file success, volumeId:%v", cs.volName)
glog.V(0).Infof("create client config file success, volumeId:%v", cs.clientConf[KVolumeName])
return nil
}

func (cs *cfsServer) createVolume(capacityGB int64) error {
masterAddr := strings.Split(cs.masterAddr, ",")[0]
masterAddr := strings.Split(cs.clientConf[KMasterAddr], ",")[0]
url := fmt.Sprintf("http://%s/admin/createVol?name=%s&capacity=%v&owner=%v&crossZone=%v&enableToken=%v&zoneName=%v",
masterAddr, cs.volName, capacityGB, cs.owner, cs.crossZone, cs.enableToken, cs.zoneName)
masterAddr, cs.clientConf[KVolumeName], capacityGB, cs.clientConf[KOwner], cs.clientConf[KCrossZone], cs.clientConf[KEnableToken], cs.clientConf[KZoneName])
glog.Infof("createVol url: %v", url)
resp, err := cs.executeRequest(url)
if err != nil {
Expand All @@ -221,11 +176,11 @@ func (cs *cfsServer) createVolume(capacityGB int64) error {

func (cs *cfsServer) deleteVolume() error {
key := md5.New()
if _, err := key.Write([]byte(cs.owner)); err != nil {
if _, err := key.Write([]byte(cs.clientConf[KOwner])); err != nil {
return status.Errorf(codes.Internal, "deleteVolume failed to get md5 sum, err(%v)", err)
}

url := fmt.Sprintf("http://%s/vol/delete?name=%s&authKey=%v", cs.masterAddr, cs.volName, hex.EncodeToString(key.Sum(nil)))
url := fmt.Sprintf("http://%s/vol/delete?name=%s&authKey=%v", cs.clientConf[KMasterAddr], cs.clientConf[KVolumeName], hex.EncodeToString(key.Sum(nil)))
glog.Infof("deleteVol url: %v", url)
resp, err := cs.executeRequest(url)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/chubaofs/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package chubaofs

import (
"github.com/chubaofs/chubaofs-csi/pkg/csi-common"
csicommon "github.com/chubaofs/chubaofs-csi/pkg/csi-common"
"github.com/container-storage-interface/spec/lib/go/csi/v0"
"github.com/golang/glog"
"golang.org/x/net/context"
Expand Down Expand Up @@ -60,7 +60,7 @@ func (cs *controllerServer) CreateVolume(ctx context.Context, req *csi.CreateVol
Volume: &csi.Volume{
Id: volName,
CapacityBytes: capacity,
Attributes: req.GetParameters(),
Attributes: cfsServer.clientConf,
},
}, nil
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/csi-common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ func logGRPC(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, h
}
return resp, err
}

// ShortenString returns the first N slice of a string.
func ShortenString(str string, n int) string {
if len(str) <= n {
return str
}
return str[:n]
}

0 comments on commit 98a4cf5

Please sign in to comment.