37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
utils.go 3.23 KB
一键复制 编辑 原始数据 按行查看 历史
Dan Ramich 提交于 2019-06-12 13:38 . goimport linting changes
package nodedriver
import (
"fmt"
"net/rpc"
"reflect"
"strconv"
"strings"
"github.com/docker/machine/libmachine/drivers/plugin/localbinary"
rpcdriver "github.com/docker/machine/libmachine/drivers/rpc"
cli "github.com/docker/machine/libmachine/mcnflag"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/sirupsen/logrus"
)
var (
// secretFields lists all the hard-coded fields that should hidden as password
secretFields = map[string]struct{}{
// ec2
"secretKey": {},
// digitalOcean
"accessToken": {},
// azure
"clientSecret": {},
// packet, rackspace, softlayer
"apiKey": {},
// vSphere, openstack, vmwarevcloudair
"password": {},
// exoscale
"apiSecretKey": {},
// otc
"accessKeySecret": {},
}
)
func FlagToField(flag cli.Flag) (string, v3.Field, error) {
field := v3.Field{
Create: true,
Update: true,
Type: "string",
}
name, err := ToLowerCamelCase(flag.String())
if err != nil {
return name, field, err
}
switch v := flag.(type) {
case *cli.StringFlag:
if isPassword(name) {
field.Type = "password"
}
field.Description = v.Usage
field.Default.StringValue = v.Value
case *cli.IntFlag:
// This will make the int flag appear as a string field in the rancher API, but we are doing this to maintain
// backward compatibility, at least until we fix a bug that prevents nodeDriver schemas from updating upon
// a Rancher upgrade
field.Description = v.Usage
field.Default.StringValue = strconv.Itoa(v.Value)
case *cli.BoolFlag:
field.Type = "boolean"
field.Description = v.Usage
case *cli.StringSliceFlag:
field.Type = "array[string]"
field.Description = v.Usage
field.Default.StringSliceValue = v.Value
case *BoolPointerFlag:
field.Type = "boolean"
field.Description = v.Usage
default:
return name, field, fmt.Errorf("unknown type of flag %v: %v", flag, reflect.TypeOf(flag))
}
return name, field, nil
}
func isPassword(key string) bool {
_, ok := secretFields[key]
return ok
}
func ToLowerCamelCase(nodeFlagName string) (string, error) {
parts := strings.SplitN(nodeFlagName, "-", 2)
if len(parts) != 2 {
return "", fmt.Errorf("parameter %s does not follow expected naming convention [DRIVER]-[FLAG-NAME]", nodeFlagName)
}
flagNameParts := strings.Split(parts[1], "-")
flagName := flagNameParts[0]
for _, flagNamePart := range flagNameParts[1:] {
flagName = flagName + strings.ToUpper(flagNamePart[:1]) + flagNamePart[1:]
}
return flagName, nil
}
func getCreateFlagsForDriver(driver string) ([]cli.Flag, error) {
logrus.Debug("Starting binary ", driver)
p, err := localbinary.NewPlugin(driver)
if err != nil {
return nil, err
}
go func() {
err := p.Serve()
if err != nil {
logrus.Debugf("Error serving plugin server for driver=%s, err=%v", driver, err)
}
}()
defer p.Close()
addr, err := p.Address()
if err != nil {
return nil, err
}
rpcclient, err := rpc.DialHTTP("tcp", addr)
if err != nil {
return nil, fmt.Errorf("Error dialing to plugin server's address(%v), err=%v", addr, err)
}
defer rpcclient.Close()
c := rpcdriver.NewInternalClient(rpcclient)
var flags []cli.Flag
if err := c.Call(".GetCreateFlags", struct{}{}, &flags); err != nil {
return nil, fmt.Errorf("Error getting flags err=%v", err)
}
return flags, nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.7

搜索帮助