Skip to content

Commit

Permalink
Feature(fs): support configure environment variables for client conta…
Browse files Browse the repository at this point in the history
…iner.
  • Loading branch information
Wine93 committed Feb 15, 2023
1 parent 6640f82 commit 8bbe40c
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 15 deletions.
27 changes: 15 additions & 12 deletions internal/configure/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const (
KEY_CURVEBS_LISTEN_MDS_ADDRS = "mds.listen.addr"
KEY_CURVEFS_LISTEN_MDS_ADDRS = "mdsOpt.rpcRetryOpt.addrs"
KEY_CONTAINER_PID = "container_pid"
KEY_ENVIRONMENT = "env"

KEY_CLIENT_S3_ACCESS_KEY = "s3.ak"
KEY_CLIENT_S3_SECRET_KEY = "s3.sk"
Expand All @@ -66,6 +67,7 @@ var (
KEY_DATA_DIR: true,
KEY_CORE_DIR: true,
KEY_CONTAINER_PID: true,
KEY_ENVIRONMENT: true,
}

LAYOUT_CURVEBS_ROOT_DIR = topology.GetCurveBSProjectLayout().ProjectRootDir
Expand Down Expand Up @@ -173,18 +175,19 @@ func (cc *ClientConfig) getBool(key string) bool {
return v.(bool)
}

func (cc *ClientConfig) GetKind() string { return cc.getString(KEY_KIND) }
func (cc *ClientConfig) GetDataDir() string { return cc.getString(KEY_DATA_DIR) }
func (cc *ClientConfig) GetLogDir() string { return cc.getString(KEY_LOG_DIR) }
func (cc *ClientConfig) GetCoreDir() string { return cc.getString(KEY_CORE_DIR) }
func (cc *ClientConfig) GetS3AccessKey() string { return cc.getString(KEY_CLIENT_S3_ACCESS_KEY) }
func (cc *ClientConfig) GetS3SecretKey() string { return cc.getString(KEY_CLIENT_S3_SECRET_KEY) }
func (cc *ClientConfig) GetS3Address() string { return cc.getString(KEY_CLIENT_S3_ADDRESS) }
func (cc *ClientConfig) GetS3BucketName() string { return cc.getString(KEY_CLIENT_S3_BUCKET_NAME) }
func (c *ClientConfig) GetContainerPid() string { return c.getString(KEY_CONTAINER_PID) }
func (cc *ClientConfig) GetCoreLocateDir() string { return DEFAULT_CORE_LOCATE_DIR }
func (c *ClientConfig) GetServiceConfig() map[string]string { return c.serviceConfig }
func (cc *ClientConfig) GetVariables() *variable.Variables { return cc.variables }
func (cc *ClientConfig) GetKind() string { return cc.getString(KEY_KIND) }
func (cc *ClientConfig) GetDataDir() string { return cc.getString(KEY_DATA_DIR) }
func (cc *ClientConfig) GetLogDir() string { return cc.getString(KEY_LOG_DIR) }
func (cc *ClientConfig) GetCoreDir() string { return cc.getString(KEY_CORE_DIR) }
func (cc *ClientConfig) GetS3AccessKey() string { return cc.getString(KEY_CLIENT_S3_ACCESS_KEY) }
func (cc *ClientConfig) GetS3SecretKey() string { return cc.getString(KEY_CLIENT_S3_SECRET_KEY) }
func (cc *ClientConfig) GetS3Address() string { return cc.getString(KEY_CLIENT_S3_ADDRESS) }
func (cc *ClientConfig) GetS3BucketName() string { return cc.getString(KEY_CLIENT_S3_BUCKET_NAME) }
func (cc *ClientConfig) GetContainerPid() string { return cc.getString(KEY_CONTAINER_PID) }
func (cc *ClientConfig) GetEnvironments() string { return cc.getString(KEY_ENVIRONMENT) }
func (cc *ClientConfig) GetCoreLocateDir() string { return DEFAULT_CORE_LOCATE_DIR }
func (cc *ClientConfig) GetServiceConfig() map[string]string { return cc.serviceConfig }
func (cc *ClientConfig) GetVariables() *variable.Variables { return cc.variables }
func (cc *ClientConfig) GetContainerImage() string {
containerImage := cc.getString(KEY_CONTAINER_IMAGE)
if len(containerImage) == 0 {
Expand Down
17 changes: 14 additions & 3 deletions internal/task/task/fs/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ func newCurveBSMutate(cc *configure.ClientConfig, delimiter string) step.Mutate
func newToolsMutate(cc *configure.ClientConfig, delimiter string) step.Mutate {
clientConfig := cc.GetServiceConfig()
tools2client := map[string]string{
"mdsAddr": "mdsOpt.rpcRetryOpt.addrs",
"volumeCluster" : KEY_CURVEBS_CLUSTER,
"mdsAddr": "mdsOpt.rpcRetryOpt.addrs",
"volumeCluster": KEY_CURVEBS_CLUSTER,
}
return func(in, key, value string) (out string, err error) {
if len(key) == 0 {
Expand Down Expand Up @@ -225,6 +225,17 @@ func checkMountStatus(mountPoint string, out *string) step.LambdaType {
}
}

func getEnvironments(cc *configure.ClientConfig) []string {
envs := []string{
"LD_PRELOAD=/usr/local/lib/libjemalloc.so",
}
env := cc.GetEnvironments()
if len(env) > 0 {
envs = append(envs, strings.Split(env, " ")...)
}
return envs
}

func (s *step2InsertClient) Execute(ctx *context.Context) error {
config := s.config
curveadm := s.curveadm
Expand Down Expand Up @@ -310,7 +321,7 @@ func NewMountFSTask(curveadm *cli.CurveAdm, cc *configure.ClientConfig) (*task.T
Image: cc.GetContainerImage(),
Command: getMountCommand(cc, mountFSName, mountFSType, mountPoint),
Entrypoint: "/bin/bash",
Envs: []string{"LD_PRELOAD=/usr/local/lib/libjemalloc.so"},
Envs: getEnvironments(cc),
Init: true,
Name: mountPoint2ContainerName(mountPoint),
Mount: fmt.Sprintf(FORMAT_MOUNT_OPTION, mountPoint, containerMountPath),
Expand Down
60 changes: 60 additions & 0 deletions internal/task/task/fs/mount_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package fs

import (
"strings"
"testing"

"github.com/opencurve/curveadm/internal/configure"
"github.com/stretchr/testify/assert"
)

var (
KEY_KIND = strings.ToLower(configure.KEY_KIND)
KEY_ADDRS = strings.ToLower(configure.KEY_CURVEFS_LISTEN_MDS_ADDRS)
KEY_ENV = strings.ToLower(configure.KEY_ENVIRONMENT)
)

func run(t *testing.T, config map[string]interface{}, envs []string) {
assert := assert.New(t)
cc, err := configure.NewClientConfig(config)
assert.Nil(err)
assert.Equal(envs, getEnvironments(cc))
}

func TestConfigureEnv_Basic(t *testing.T) {
run(t, map[string]interface{}{
KEY_KIND: "curvefs",
KEY_ADDRS: "1.1.1.1",
}, []string{
"LD_PRELOAD=/usr/local/lib/libjemalloc.so",
})

run(t, map[string]interface{}{
KEY_KIND: "curvefs",
KEY_ADDRS: "1.1.1.1",
KEY_ENV: "MALLOC_CONF=prof:true,lg_prof_interval:26,prof_prefix:/curvefs/client/logs/jeprof.out",
}, []string{
"LD_PRELOAD=/usr/local/lib/libjemalloc.so",
"MALLOC_CONF=prof:true,lg_prof_interval:26,prof_prefix:/curvefs/client/logs/jeprof.out",
})

run(t, map[string]interface{}{
KEY_KIND: "curvefs",
KEY_ADDRS: "1.1.1.1",
KEY_ENV: "NAME=jack AGE=18 FROM=china",
}, []string{
"LD_PRELOAD=/usr/local/lib/libjemalloc.so",
"NAME=jack",
"AGE=18",
"FROM=china",
})

run(t, map[string]interface{}{
KEY_KIND: "curvefs",
KEY_ADDRS: "1.1.1.1",
KEY_ENV: "LD_PRELOAD=",
}, []string{
"LD_PRELOAD=/usr/local/lib/libjemalloc.so",
"LD_PRELOAD=",
})
}

0 comments on commit 8bbe40c

Please sign in to comment.