From c39c043eec4fda3a2468b8a81381887cc8d5c23f Mon Sep 17 00:00:00 2001 From: zhouqunjie <450705171@qq.com> Date: Tue, 5 Apr 2022 16:20:05 +0800 Subject: [PATCH] Add tencent create Signed-off-by: zhouqunjie <450705171@qq.com> --- adaptor/pod_adaptor/service/poder/ali_eci.go | 6 +- adaptor/pod_adaptor/service/poder/poder.go | 2 +- .../pod_adaptor/service/poder/tencent_eks.go | 169 +++++++++++------- idl/pbpod/pod.proto | 8 +- lan_trans/idl/pbpod/pod.pb.go | 32 ++-- .../openapiv2/idl/pbpod/pod.swagger.json | 12 +- 6 files changed, 135 insertions(+), 94 deletions(-) diff --git a/adaptor/pod_adaptor/service/poder/ali_eci.go b/adaptor/pod_adaptor/service/poder/ali_eci.go index 5102011b..729e4156 100644 --- a/adaptor/pod_adaptor/service/poder/ali_eci.go +++ b/adaptor/pod_adaptor/service/poder/ali_eci.go @@ -5,6 +5,7 @@ import ( "gitlink.org.cn/JCCE/PCM/common/tenanter" "gitlink.org.cn/JCCE/PCM/lan_trans/idl/pbpod" "gitlink.org.cn/JCCE/PCM/lan_trans/idl/pbtenant" + "strconv" "sync" alieci "github.com/aliyun/alibaba-cloud-sdk-go/services/eci" @@ -27,7 +28,6 @@ func newAliEciClient(region tenanter.Region, tenant tenanter.Tenanter) (Poder, e switch t := tenant.(type) { case *tenanter.AccessKeyTenant: - // 阿里云的sdk有一个 map 的并发问题,go test 加上-race 能检测出来,所以这里加一个锁 aliClientMutex.Lock() client, err = alieci.NewClientWithAccessKey(region.GetName(), t.GetId(), t.GetSecret()) aliClientMutex.Unlock() @@ -90,8 +90,8 @@ func (eci *AliEci) ListPodDetail(ctx context.Context, req *pbpod.ListPodDetailRe RegionId: 0, ContainerImage: v.Containers[k].Image, ContainerName: v.Containers[k].Name, - CpuPod: v.Cpu, - MemoryPod: v.Memory, + CpuPod: strconv.FormatFloat(float64(v.Cpu), 'f', 6, 64), + MemoryPod: strconv.FormatFloat(float64(v.Memory), 'f', 6, 64), SecurityGroupId: v.SecurityGroupId, SubnetId: v.InternetIp, VpcId: v.VpcId, diff --git a/adaptor/pod_adaptor/service/poder/poder.go b/adaptor/pod_adaptor/service/poder/poder.go index 9f03f5e4..4d4502fa 100644 --- a/adaptor/pod_adaptor/service/poder/poder.go +++ b/adaptor/pod_adaptor/service/poder/poder.go @@ -33,7 +33,7 @@ func NewPodClient(provider pbtenant.CloudProvider, region tenanter.Region, tenan case pbtenant.CloudProvider_ali: return newAliEciClient(region, tenant) case pbtenant.CloudProvider_tencent: - return nil, nil + return newTencentEksClient(region, tenant) case pbtenant.CloudProvider_huawei: return nil, nil //TODO aws diff --git a/adaptor/pod_adaptor/service/poder/tencent_eks.go b/adaptor/pod_adaptor/service/poder/tencent_eks.go index 37723ed3..a485f66c 100644 --- a/adaptor/pod_adaptor/service/poder/tencent_eks.go +++ b/adaptor/pod_adaptor/service/poder/tencent_eks.go @@ -1,67 +1,112 @@ package poder -// -//import ( -// "fmt" -// "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" -// "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors" -// "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" -// tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525" -// "gitlink.org.cn/JCCE/PCM/adaptor/pod_adaptor/common/auth/aksk" -// pcmCommon "gitlink.org.cn/JCCE/PCM/adaptor/pod_adaptor/common/config" -//) -// -//func CreateEksInstance(cloudStack string, akskPath string, configPath string) { -// -// configCommon, _ := pcmCommon.PCMconfig(configPath) -// configAksk, _ := aksk.AkskConfig(cloudStack, akskPath) -// -// credential := common.NewCredential( -// configAksk.AccessKey, -// configAksk.SecretKey, -// ) -// cpf := profile.NewClientProfile() -// cpf.HttpProfile.Endpoint = "tke.tencentcloudapi.com" -// client, _ := tke.NewClient(credential, configCommon.RegionId, cpf) -// -// request := tke.NewCreateEKSContainerInstancesRequest() -// -// eksCiName := &configCommon.ContainerGroupName -// containerName := &configCommon.ContainerName -// containerImage := &configCommon.ContainerImage -// eksCpu := &configCommon.CpuPodFloat -// eksMemory := &configCommon.MemoryPodFloat -// securityGroupId := &configCommon.SecurityGroupId -// securityGroupIds := make([]*string, 1) -// securityGroupIds[0] = securityGroupId -// subNetId := &configCommon.SubnetId -// vpcId := &configCommon.VpcId -// -// request.EksCiName = eksCiName -// container := make([]*tke.Container, 1) -// container[0] = new(tke.Container) -// container[0].Name = containerName -// container[0].Image = containerImage -// //container[0].Cpu = containerCpuPt -// //container[0].Memory = containerMemoryPt -// -// request.Containers = container -// request.Cpu = eksCpu -// request.Memory = eksMemory -// request.SecurityGroupIds = securityGroupIds -// request.SubnetId = subNetId -// request.VpcId = vpcId -// -// response, err := client.CreateEKSContainerInstances(request) -// if _, ok := err.(*errors.TencentCloudSDKError); ok { -// fmt.Printf("An API error has returned: %s", err) -// return -// } -// if err != nil { -// panic(err) -// } -// fmt.Printf("%s", response.ToJsonString()) -//} +import ( + "context" + "github.com/pkg/errors" + "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common" + "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile" + tencenteks "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525" + "gitlink.org.cn/JCCE/PCM/common/tenanter" + "gitlink.org.cn/JCCE/PCM/lan_trans/idl/pbpod" + "strconv" + "sync" +) + +var tencentClientMutex sync.Mutex + +type TencentEks struct { + cli *tencenteks.Client + region tenanter.Region + tenanter tenanter.Tenanter +} + +func newTencentEksClient(region tenanter.Region, tenant tenanter.Tenanter) (Poder, error) { + var ( + client *tencenteks.Client + err error + ) + + switch t := tenant.(type) { + case *tenanter.AccessKeyTenant: + tencentClientMutex.Lock() + + credential := common.NewCredential( + t.GetId(), + t.GetSecret(), + ) + cpf := profile.NewClientProfile() + client, err = tencenteks.NewClient(credential, region.GetName(), cpf) + tencentClientMutex.Unlock() + default: + } + + if err != nil { + return nil, errors.Wrap(err, "init ali ecs client error") + } + + return &TencentEks{ + cli: client, + region: region, + tenanter: tenant, + }, nil +} + +func (eks TencentEks) CreatePod(ctx context.Context, req *pbpod.CreatePodReq) (*pbpod.CreatePodResp, error) { + + request := tencenteks.NewCreateEKSContainerInstancesRequest() + + eksCiName := req.PodName + containerName := req.ContainerName + containerImage := req.ContainerImage + eksCpu := req.CpuPod + eksMemory := req.MemoryPod + securityGroupId := req.SecurityGroupId + securityGroupIds := make([]*string, 1) + securityGroupIds[0] = &securityGroupId + subNetId := req.SubnetId + vpcId := req.VpcId + + request.EksCiName = &eksCiName + container := make([]*tencenteks.Container, 1) + container[0] = new(tencenteks.Container) + container[0].Name = &containerName + container[0].Image = &containerImage + //container[0].Cpu = containerCpuPt + //container[0].Memory = containerMemoryPt + + request.Containers = container + eksCpu64, err := strconv.ParseFloat(eksCpu, 64) + eksMemory64, err := strconv.ParseFloat(eksMemory, 64) + request.Cpu = &eksCpu64 + request.Memory = &eksMemory64 + request.SecurityGroupIds = securityGroupIds + request.SubnetId = &subNetId + request.VpcId = &vpcId + + resp, err := eks.cli.CreateEKSContainerInstances(request) + if err != nil { + return nil, errors.Wrap(err, "Tencent CreatePod error") + } + + isFinished := false + + if resp.Response.RequestId != nil { + isFinished = true + } + + return &pbpod.CreatePodResp{ + Pods: nil, + Finished: isFinished, + RequestId: *resp.Response.RequestId, + }, nil + +} + +func (eks TencentEks) ListPodDetail(ctx context.Context, req *pbpod.ListPodDetailReq) (resp *pbpod.ListPodDetailResp, err error) { + //TODO implement me + panic("implement me") +} + // //func ListEksInstance(cloudStack string, akskPath string, configPath string) { // diff --git a/idl/pbpod/pod.proto b/idl/pbpod/pod.proto index da73f608..e3f7d153 100644 --- a/idl/pbpod/pod.proto +++ b/idl/pbpod/pod.proto @@ -22,9 +22,9 @@ message PodInstance { // 容器名称 string container_name = 7; // vcpu数 - float cpu_pod = 8; + string cpu_pod = 8; // 内存MB - float memory_pod = 9; + string memory_pod = 9; //安全组ID 对应腾讯 SecurityGroupIds(腾讯必需) string security_group_id = 10; @@ -54,9 +54,9 @@ message CreatePodReq { // 容器名称 string container_name = 7; // v cpu数 - float cpu_pod = 8; + string cpu_pod = 8; // 内存MB - float memory_pod = 9; + string memory_pod = 9; //安全组ID 对应腾讯 SecurityGroupIds(腾讯必需) string security_group_id = 10; //子网ID 对应腾讯 SubnetId(腾讯必需) diff --git a/lan_trans/idl/pbpod/pod.pb.go b/lan_trans/idl/pbpod/pod.pb.go index 3209d876..59218b22 100644 --- a/lan_trans/idl/pbpod/pod.pb.go +++ b/lan_trans/idl/pbpod/pod.pb.go @@ -42,9 +42,9 @@ type PodInstance struct { // 容器名称 ContainerName string `protobuf:"bytes,7,opt,name=container_name,json=containerName,proto3" json:"container_name,omitempty"` // vcpu数 - CpuPod float32 `protobuf:"fixed32,8,opt,name=cpu_pod,json=cpuPod,proto3" json:"cpu_pod,omitempty"` + CpuPod string `protobuf:"bytes,8,opt,name=cpu_pod,json=cpuPod,proto3" json:"cpu_pod,omitempty"` // 内存MB - MemoryPod float32 `protobuf:"fixed32,9,opt,name=memory_pod,json=memoryPod,proto3" json:"memory_pod,omitempty"` + MemoryPod string `protobuf:"bytes,9,opt,name=memory_pod,json=memoryPod,proto3" json:"memory_pod,omitempty"` //安全组ID 对应腾讯 SecurityGroupIds(腾讯必需) SecurityGroupId string `protobuf:"bytes,10,opt,name=security_group_id,json=securityGroupId,proto3" json:"security_group_id,omitempty"` //子网ID 对应腾讯 SubnetId(腾讯必需) @@ -136,18 +136,18 @@ func (x *PodInstance) GetContainerName() string { return "" } -func (x *PodInstance) GetCpuPod() float32 { +func (x *PodInstance) GetCpuPod() string { if x != nil { return x.CpuPod } - return 0 + return "" } -func (x *PodInstance) GetMemoryPod() float32 { +func (x *PodInstance) GetMemoryPod() string { if x != nil { return x.MemoryPod } - return 0 + return "" } func (x *PodInstance) GetSecurityGroupId() string { @@ -198,9 +198,9 @@ type CreatePodReq struct { // 容器名称 ContainerName string `protobuf:"bytes,7,opt,name=container_name,json=containerName,proto3" json:"container_name,omitempty"` // v cpu数 - CpuPod float32 `protobuf:"fixed32,8,opt,name=cpu_pod,json=cpuPod,proto3" json:"cpu_pod,omitempty"` + CpuPod string `protobuf:"bytes,8,opt,name=cpu_pod,json=cpuPod,proto3" json:"cpu_pod,omitempty"` // 内存MB - MemoryPod float32 `protobuf:"fixed32,9,opt,name=memory_pod,json=memoryPod,proto3" json:"memory_pod,omitempty"` + MemoryPod string `protobuf:"bytes,9,opt,name=memory_pod,json=memoryPod,proto3" json:"memory_pod,omitempty"` //安全组ID 对应腾讯 SecurityGroupIds(腾讯必需) SecurityGroupId string `protobuf:"bytes,10,opt,name=security_group_id,json=securityGroupId,proto3" json:"security_group_id,omitempty"` //子网ID 对应腾讯 SubnetId(腾讯必需) @@ -292,18 +292,18 @@ func (x *CreatePodReq) GetContainerName() string { return "" } -func (x *CreatePodReq) GetCpuPod() float32 { +func (x *CreatePodReq) GetCpuPod() string { if x != nil { return x.CpuPod } - return 0 + return "" } -func (x *CreatePodReq) GetMemoryPod() float32 { +func (x *CreatePodReq) GetMemoryPod() string { if x != nil { return x.MemoryPod } - return 0 + return "" } func (x *CreatePodReq) GetSecurityGroupId() string { @@ -754,9 +754,9 @@ var file_idl_pbpod_pod_proto_rawDesc = []byte{ 0x49, 0x6d, 0x61, 0x67, 0x65, 0x12, 0x25, 0x0a, 0x0e, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x17, 0x0a, 0x07, - 0x63, 0x70, 0x75, 0x5f, 0x70, 0x6f, 0x64, 0x18, 0x08, 0x20, 0x01, 0x28, 0x02, 0x52, 0x06, 0x63, + 0x63, 0x70, 0x75, 0x5f, 0x70, 0x6f, 0x64, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x70, 0x75, 0x50, 0x6f, 0x64, 0x12, 0x1d, 0x0a, 0x0a, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x5f, - 0x70, 0x6f, 0x64, 0x18, 0x09, 0x20, 0x01, 0x28, 0x02, 0x52, 0x09, 0x6d, 0x65, 0x6d, 0x6f, 0x72, + 0x70, 0x6f, 0x64, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x50, 0x6f, 0x64, 0x12, 0x2a, 0x0a, 0x11, 0x73, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x5f, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x5f, 0x69, 0x64, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0f, 0x73, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x47, 0x72, 0x6f, 0x75, 0x70, 0x49, 0x64, @@ -782,9 +782,9 @@ var file_idl_pbpod_pod_proto_rawDesc = []byte{ 0x61, 0x67, 0x65, 0x12, 0x25, 0x0a, 0x0e, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x4e, 0x61, 0x6d, 0x65, 0x12, 0x17, 0x0a, 0x07, 0x63, 0x70, - 0x75, 0x5f, 0x70, 0x6f, 0x64, 0x18, 0x08, 0x20, 0x01, 0x28, 0x02, 0x52, 0x06, 0x63, 0x70, 0x75, + 0x75, 0x5f, 0x70, 0x6f, 0x64, 0x18, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x63, 0x70, 0x75, 0x50, 0x6f, 0x64, 0x12, 0x1d, 0x0a, 0x0a, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x5f, 0x70, 0x6f, - 0x64, 0x18, 0x09, 0x20, 0x01, 0x28, 0x02, 0x52, 0x09, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x50, + 0x64, 0x18, 0x09, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x50, 0x6f, 0x64, 0x12, 0x2a, 0x0a, 0x11, 0x73, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x5f, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x5f, 0x69, 0x64, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0f, 0x73, 0x65, 0x63, 0x75, 0x72, 0x69, 0x74, 0x79, 0x47, 0x72, 0x6f, 0x75, 0x70, 0x49, 0x64, 0x12, 0x1b, diff --git a/lan_trans/openapiv2/idl/pbpod/pod.swagger.json b/lan_trans/openapiv2/idl/pbpod/pod.swagger.json index 22339bfe..4b09e221 100644 --- a/lan_trans/openapiv2/idl/pbpod/pod.swagger.json +++ b/lan_trans/openapiv2/idl/pbpod/pod.swagger.json @@ -183,13 +183,11 @@ "title": "容器名称" }, "cpuPod": { - "type": "number", - "format": "float", + "type": "string", "title": "v cpu数" }, "memoryPod": { - "type": "number", - "format": "float", + "type": "string", "title": "内存MB" }, "securityGroupId": { @@ -358,13 +356,11 @@ "title": "容器名称" }, "cpuPod": { - "type": "number", - "format": "float", + "type": "string", "title": "vcpu数" }, "memoryPod": { - "type": "number", - "format": "float", + "type": "string", "title": "内存MB" }, "securityGroupId": {