Merge pull request 'sdk demo 完善' (#16) from github_branch into master
This commit is contained in:
commit
9839e52eeb
|
@ -46,23 +46,29 @@ func DeleteContainerGroup(request *DeleteContainerGroupRequest) (response *Delet
|
||||||
|
|
||||||
glog.Infof("load tenant from file finished")
|
glog.Infof("load tenant from file finished")
|
||||||
var regionId int32
|
var regionId int32
|
||||||
|
var pcmId string
|
||||||
|
podId := request.ContainerGroupId
|
||||||
|
podName := request.ContainerGroupName
|
||||||
switch request.ProviderId {
|
switch request.ProviderId {
|
||||||
case 0:
|
case 0:
|
||||||
regionId, _ = tenanter.GetAliRegionId(request.RegionId)
|
regionId, _ = tenanter.GetAliRegionId(request.RegionId)
|
||||||
|
pcmId = podId
|
||||||
case 1:
|
case 1:
|
||||||
regionId, _ = tenanter.GetTencentRegionId(request.RegionId)
|
regionId, _ = tenanter.GetTencentRegionId(request.RegionId)
|
||||||
|
pcmId = podId
|
||||||
case 2:
|
case 2:
|
||||||
regionId, _ = tenanter.GetHuaweiRegionId(request.RegionId)
|
regionId, _ = tenanter.GetHuaweiRegionId(request.RegionId)
|
||||||
|
pcmId = podName
|
||||||
case 3:
|
case 3:
|
||||||
regionId, _ = tenanter.GetK8SRegionId(request.RegionId)
|
regionId, _ = tenanter.GetK8SRegionId(request.RegionId)
|
||||||
|
pcmId = podName
|
||||||
}
|
}
|
||||||
podId := request.ContainerGroupId
|
|
||||||
podName := request.ContainerGroupName
|
|
||||||
|
|
||||||
requestPCM := &pbpod.DeletePodReq{
|
requestPCM := &pbpod.DeletePodReq{
|
||||||
RequestSource: "ali",
|
RequestSource: "ali",
|
||||||
Provider: provider,
|
Provider: provider,
|
||||||
AccountName: request.AccountName,
|
AccountName: request.AccountName,
|
||||||
|
PcmId: pcmId,
|
||||||
PodId: podId,
|
PodId: podId,
|
||||||
PodName: podName,
|
PodName: podName,
|
||||||
Namespace: request.Namespace,
|
Namespace: request.Namespace,
|
||||||
|
|
|
@ -34,7 +34,7 @@ type PodInterface interface {
|
||||||
Create(ctx context.Context, pod *v1.Pod, opts metav1.CreateOptions) (*v1.Pod, error)
|
Create(ctx context.Context, pod *v1.Pod, opts metav1.CreateOptions) (*v1.Pod, error)
|
||||||
Update(ctx context.Context, pod *v1.Pod, opts metav1.UpdateOptions) (*v1.Pod, error)
|
Update(ctx context.Context, pod *v1.Pod, opts metav1.UpdateOptions) (*v1.Pod, error)
|
||||||
UpdateStatus(ctx context.Context, pod *v1.Pod, opts metav1.UpdateOptions) (*v1.Pod, error)
|
UpdateStatus(ctx context.Context, pod *v1.Pod, opts metav1.UpdateOptions) (*v1.Pod, error)
|
||||||
Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error
|
Delete(ctx context.Context, namespace string, providerId int32, regionName string, accountName string, pcmId string, opts metav1.DeleteOptions) error
|
||||||
DeleteCollection(ctx context.Context, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error
|
DeleteCollection(ctx context.Context, opts metav1.DeleteOptions, listOpts metav1.ListOptions) error
|
||||||
Get(ctx context.Context, name string, opts metav1.GetOptions) (*v1.Pod, error)
|
Get(ctx context.Context, name string, opts metav1.GetOptions) (*v1.Pod, error)
|
||||||
List(ctx context.Context, pod *v1.Pod, opts metav1.ListOptions) (*v1.PodList, error)
|
List(ctx context.Context, pod *v1.Pod, opts metav1.ListOptions) (*v1.PodList, error)
|
||||||
|
@ -165,7 +165,7 @@ func (c *pods) Create(ctx context.Context, pod *v1.Pod, opts metav1.CreateOption
|
||||||
var cpuPod string
|
var cpuPod string
|
||||||
var memoryPod string
|
var memoryPod string
|
||||||
|
|
||||||
//华为(K8S)与其他厂商的CPU单位和格式有区别
|
//华为(K8S)与其他厂商的CPU单位和格式有区别,region根据字符串取各厂商的对应id
|
||||||
switch pod.Spec.ProviderId {
|
switch pod.Spec.ProviderId {
|
||||||
case 0:
|
case 0:
|
||||||
regionId, _ = tenanter.GetAliRegionId(pod.Spec.RegionId)
|
regionId, _ = tenanter.GetAliRegionId(pod.Spec.RegionId)
|
||||||
|
@ -186,15 +186,16 @@ func (c *pods) Create(ctx context.Context, pod *v1.Pod, opts metav1.CreateOption
|
||||||
}
|
}
|
||||||
|
|
||||||
requestPCM := &pbpod.CreatePodReq{
|
requestPCM := &pbpod.CreatePodReq{
|
||||||
RequestSource: "huawei",
|
RequestSource: "huawei",
|
||||||
Provider: provider,
|
Provider: provider,
|
||||||
AccountName: tenanters[0].AccountName(),
|
AccountName: tenanters[0].AccountName(),
|
||||||
PodName: pod.Name,
|
PodName: pod.Name,
|
||||||
RegionId: regionId,
|
RegionId: regionId,
|
||||||
ContainerImage: pod.Spec.Containers[0].Image,
|
ContainerImage: pod.Spec.Containers[0].Image,
|
||||||
ContainerName: pod.Spec.Containers[0].Name,
|
ContainerName: pod.Spec.Containers[0].Name,
|
||||||
CpuPod: cpuPod,
|
CpuPod: cpuPod,
|
||||||
MemoryPod: memoryPod,
|
MemoryPod: memoryPod,
|
||||||
|
//这里腾讯需要独立提供三个参数,目前固定使用以下三个
|
||||||
SecurityGroupId: "sg-6qlun7hd",
|
SecurityGroupId: "sg-6qlun7hd",
|
||||||
SubnetId: "subnet-mnwfg2fk",
|
SubnetId: "subnet-mnwfg2fk",
|
||||||
VpcId: "vpc-rkwt40g5",
|
VpcId: "vpc-rkwt40g5",
|
||||||
|
@ -207,44 +208,12 @@ func (c *pods) Create(ctx context.Context, pod *v1.Pod, opts metav1.CreateOption
|
||||||
TypeMeta: metav1.TypeMeta{},
|
TypeMeta: metav1.TypeMeta{},
|
||||||
ObjectMeta: metav1.ObjectMeta{},
|
ObjectMeta: metav1.ObjectMeta{},
|
||||||
Spec: v1.PodSpec{
|
Spec: v1.PodSpec{
|
||||||
ProviderId: pod.Spec.ProviderId,
|
ProviderId: pod.Spec.ProviderId,
|
||||||
RegionId: pod.Spec.RegionId,
|
RegionId: pod.Spec.RegionId,
|
||||||
AccountName: pod.Spec.AccountName,
|
AccountName: pod.Spec.AccountName,
|
||||||
Volumes: nil,
|
Volumes: nil,
|
||||||
InitContainers: nil,
|
InitContainers: nil,
|
||||||
Containers: pod.Spec.Containers,
|
Containers: pod.Spec.Containers,
|
||||||
EphemeralContainers: nil,
|
|
||||||
RestartPolicy: "",
|
|
||||||
TerminationGracePeriodSeconds: nil,
|
|
||||||
ActiveDeadlineSeconds: nil,
|
|
||||||
DNSPolicy: "",
|
|
||||||
NodeSelector: nil,
|
|
||||||
ServiceAccountName: "",
|
|
||||||
DeprecatedServiceAccount: "",
|
|
||||||
AutomountServiceAccountToken: nil,
|
|
||||||
NodeName: "",
|
|
||||||
HostNetwork: false,
|
|
||||||
HostPID: false,
|
|
||||||
HostIPC: false,
|
|
||||||
ShareProcessNamespace: nil,
|
|
||||||
SecurityContext: nil,
|
|
||||||
ImagePullSecrets: nil,
|
|
||||||
Hostname: "",
|
|
||||||
Subdomain: "",
|
|
||||||
Affinity: nil,
|
|
||||||
SchedulerName: "",
|
|
||||||
Tolerations: nil,
|
|
||||||
HostAliases: nil,
|
|
||||||
PriorityClassName: "",
|
|
||||||
Priority: nil,
|
|
||||||
DNSConfig: nil,
|
|
||||||
ReadinessGates: nil,
|
|
||||||
RuntimeClassName: nil,
|
|
||||||
EnableServiceLinks: nil,
|
|
||||||
PreemptionPolicy: nil,
|
|
||||||
Overhead: nil,
|
|
||||||
TopologySpreadConstraints: nil,
|
|
||||||
SetHostnameAsFQDN: nil,
|
|
||||||
},
|
},
|
||||||
Status: v1.PodStatus{
|
Status: v1.PodStatus{
|
||||||
Phase: "",
|
Phase: "",
|
||||||
|
@ -405,14 +374,50 @@ func (c *pods) UpdateStatus(ctx context.Context, pod *v1.Pod, opts metav1.Update
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete takes name of the pod and deletes it. Returns an error if one occurs.
|
// Delete takes name of the pod and deletes it. Returns an error if one occurs.
|
||||||
func (c *pods) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
|
func (c *pods) Delete(ctx context.Context, namespace string, providerId int32, regionName string, accountName string, pcmId string, opts metav1.DeleteOptions) error {
|
||||||
return c.client.Delete().
|
|
||||||
Namespace(c.ns).
|
provider := pbtenant.CloudProvider(providerId)
|
||||||
Resource("pods").
|
var configFile string
|
||||||
Name(name).
|
flag.StringVar(&configFile, "confHuawei", "configs/config.yaml", "config.yaml")
|
||||||
Body(&opts).
|
flag.Parse()
|
||||||
Do(ctx).
|
defer glog.Flush()
|
||||||
Error()
|
|
||||||
|
if err := tenanter.LoadCloudConfigsFromFile(configFile); err != nil {
|
||||||
|
if !errors.Is(err, tenanter.ErrLoadTenanterFileEmpty) {
|
||||||
|
glog.Fatalf("tenanter.LoadCloudConfigsFromFile error %+v", err)
|
||||||
|
}
|
||||||
|
glog.Warningf("tenanter.LoadCloudConfigsFromFile empty file path %s", configFile)
|
||||||
|
}
|
||||||
|
|
||||||
|
glog.Infof("load tenant from file finished")
|
||||||
|
var regionId int32
|
||||||
|
switch providerId {
|
||||||
|
case 0:
|
||||||
|
regionId, _ = tenanter.GetAliRegionId(regionName)
|
||||||
|
case 1:
|
||||||
|
regionId, _ = tenanter.GetTencentRegionId(regionName)
|
||||||
|
case 2:
|
||||||
|
regionId, _ = tenanter.GetHuaweiRegionId(regionName)
|
||||||
|
case 3:
|
||||||
|
regionId, _ = tenanter.GetK8SRegionId(regionName)
|
||||||
|
}
|
||||||
|
|
||||||
|
requestPCM := &pbpod.DeletePodReq{
|
||||||
|
RequestSource: "huawei",
|
||||||
|
Provider: provider,
|
||||||
|
AccountName: accountName,
|
||||||
|
PcmId: pcmId,
|
||||||
|
Namespace: namespace,
|
||||||
|
RegionId: regionId,
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := server.DeletePod(nil, requestPCM)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteCollection deletes a collection of objects.
|
// DeleteCollection deletes a collection of objects.
|
||||||
|
|
|
@ -1,56 +1,47 @@
|
||||||
//// This file is auto-generated, don't edit it. Thanks.
|
// This file is auto-generated, don't edit it. Thanks.
|
||||||
package main
|
package main
|
||||||
|
|
||||||
//
|
import (
|
||||||
//import (
|
openapi "github.com/alibabacloud-go/darabonba-openapi/client"
|
||||||
// openapi "github.com/alibabacloud-go/darabonba-openapi/client"
|
eci20180808 "github.com/alibabacloud-go/eci-20180808/v2/client"
|
||||||
// eci20180808 "github.com/alibabacloud-go/eci-20180808/v2/client"
|
"github.com/alibabacloud-go/tea/tea"
|
||||||
// "github.com/alibabacloud-go/tea/tea"
|
)
|
||||||
// "os"
|
|
||||||
//)
|
func CreateClient(accessKeyId *string, accessKeySecret *string) (_result *eci20180808.Client, _err error) {
|
||||||
//
|
config := &openapi.Config{
|
||||||
///**
|
// 您的AccessKey ID
|
||||||
// * 使用AK&SK初始化账号Client
|
AccessKeyId: accessKeyId,
|
||||||
// * @param accessKeyId
|
// 您的AccessKey Secret
|
||||||
// * @param accessKeySecret
|
AccessKeySecret: accessKeySecret,
|
||||||
// * @return Client
|
}
|
||||||
// * @throws Exception
|
// 访问的域名
|
||||||
// */
|
config.Endpoint = tea.String("eci.aliyuncs.com")
|
||||||
//func CreateClient(accessKeyId *string, accessKeySecret *string) (_result *eci20180808.Client, _err error) {
|
_result = &eci20180808.Client{}
|
||||||
// config := &openapi.Config{
|
_result, _err = eci20180808.NewClient(config)
|
||||||
// // 您的AccessKey ID
|
return _result, _err
|
||||||
// AccessKeyId: accessKeyId,
|
}
|
||||||
// // 您的AccessKey Secret
|
|
||||||
// AccessKeySecret: accessKeySecret,
|
func aliOriginMain() (_err error) {
|
||||||
// }
|
client, _err := CreateClient(tea.String(""), tea.String(""))
|
||||||
// // 访问的域名
|
if _err != nil {
|
||||||
// config.Endpoint = tea.String("eci.aliyuncs.com")
|
return _err
|
||||||
// _result = &eci20180808.Client{}
|
}
|
||||||
// _result, _err = eci20180808.NewClient(config)
|
|
||||||
// return _result, _err
|
describeContainerGroupsRequest := &eci20180808.DescribeContainerGroupsRequest{
|
||||||
//}
|
RegionId: tea.String("cn-hangzhou"),
|
||||||
//
|
}
|
||||||
//func _main(args []*string) (_err error) {
|
// 复制代码运行请自行打印 API 的返回值
|
||||||
// client, _err := CreateClient(tea.String(""), tea.String(""))
|
resp, _err := client.DescribeContainerGroups(describeContainerGroupsRequest)
|
||||||
// if _err != nil {
|
println(*resp.Body.ContainerGroups[0].ContainerGroupId)
|
||||||
// return _err
|
if _err != nil {
|
||||||
// }
|
return _err
|
||||||
//
|
}
|
||||||
// describeContainerGroupsRequest := &eci20180808.DescribeContainerGroupsRequest{
|
return _err
|
||||||
// RegionId: tea.String("cn-hangzhou"),
|
}
|
||||||
// }
|
|
||||||
// // 复制代码运行请自行打印 API 的返回值
|
func main() {
|
||||||
// resp, _err := client.DescribeContainerGroups(describeContainerGroupsRequest)
|
err := aliOriginMain()
|
||||||
// println(*resp.Body.ContainerGroups[0].ContainerGroupId)
|
if err != nil {
|
||||||
// if _err != nil {
|
panic(err)
|
||||||
// return _err
|
}
|
||||||
// }
|
}
|
||||||
// return _err
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func main() {
|
|
||||||
// err := _main(tea.StringSlice(os.Args[1:]))
|
|
||||||
// if err != nil {
|
|
||||||
// panic(err)
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
|
@ -1,186 +1,154 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
//
|
import (
|
||||||
//import (
|
"context"
|
||||||
// "context"
|
"fmt"
|
||||||
// "fmt"
|
"github.com/pkg/errors"
|
||||||
// "github.com/pkg/errors"
|
corev1 "k8s.io/api/core/v1"
|
||||||
// corev1 "k8s.io/api/core/v1"
|
"k8s.io/apimachinery/pkg/api/resource"
|
||||||
// "k8s.io/apimachinery/pkg/api/resource"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
// metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
"k8s.io/client-go/kubernetes"
|
||||||
// "k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
// huaweicci "k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/tools/clientcmd/api"
|
||||||
// "k8s.io/client-go/tools/clientcmd"
|
)
|
||||||
// "k8s.io/client-go/tools/clientcmd/api"
|
|
||||||
//)
|
const (
|
||||||
//
|
apiVersion = "client.authentication.k8s.io/v1beta1"
|
||||||
//const (
|
cciEndpoint = "https://cci.cn-east-3.myhuaweicloud.com"
|
||||||
// apiVersion = "client.authentication.k8s.io/v1beta1"
|
iamEndpoint = "https://iam.cn-east-3.myhuaweicloud.com"
|
||||||
// // 云容器实例 CCI,参考https://developer.huaweicloud.com/endpoint
|
projectName = "cn-east-3"
|
||||||
// cciEndpoint = "https://cci.cn-east-3.myhuaweicloud.com"
|
ak = "ATQTIWUT9K66VRMMXKVY"
|
||||||
// // 统一身份认证服务 IAM,参考https://developer.huaweicloud.com/endpoint
|
sk = "Wa0aixDVuhZOfDZGWvgIJQBHnyiDlGdgDn1Ai5Yy"
|
||||||
// iamEndpoint = "https://iam.cn-east-3.myhuaweicloud.com"
|
)
|
||||||
// // 地区和终端节点,参考https://developer.huaweicloud.com/endpoint
|
|
||||||
// projectName = "cn-east-3"
|
// NewClientOrigin 通过AK/SK认证创建ClientSet
|
||||||
// // 获取AK/SK参考: https://support.huaweicloud.com/devg-cci/cci_kubectl_01.html#cci_kubectl_01__section17023744719
|
func NewClientOrigin() (*kubernetes.Clientset, error) {
|
||||||
// ak = "ATQTIWUT9K66VRMMXKVY"
|
config, err := clientcmd.BuildConfigFromFlags(cciEndpoint, "")
|
||||||
// sk = "Wa0aixDVuhZOfDZGWvgIJQBHnyiDlGdgDn1Ai5Yy"
|
if err != nil {
|
||||||
//)
|
return nil, err
|
||||||
//
|
}
|
||||||
//// NewClient 通过AK/SK认证创建Clientset
|
var optionArgs []string
|
||||||
//func NewClient() (*kubernetes.Clientset, error) {
|
optionArgs = append(optionArgs, fmt.Sprintf("--iam-endpoint=%s", iamEndpoint))
|
||||||
// config, err := clientcmd.BuildConfigFromFlags(cciEndpoint, "")
|
optionArgs = append(optionArgs, fmt.Sprintf("--project-name=%s", projectName))
|
||||||
// if err != nil {
|
optionArgs = append(optionArgs, fmt.Sprintf("--token-only=false"))
|
||||||
// return nil, err
|
optionArgs = append(optionArgs, fmt.Sprintf("--ak=%s", ak))
|
||||||
// }
|
optionArgs = append(optionArgs, fmt.Sprintf("--sk=%s", sk))
|
||||||
// var optionArgs []string
|
config.ExecProvider = &api.ExecConfig{
|
||||||
// optionArgs = append(optionArgs, fmt.Sprintf("--iam-endpoint=%s", iamEndpoint))
|
Command: "cci-iam-authenticator",
|
||||||
// optionArgs = append(optionArgs, fmt.Sprintf("--project-name=%s", projectName))
|
APIVersion: apiVersion,
|
||||||
// optionArgs = append(optionArgs, fmt.Sprintf("--token-only=false"))
|
Args: append([]string{"token"}, optionArgs...),
|
||||||
// optionArgs = append(optionArgs, fmt.Sprintf("--ak=%s", ak))
|
Env: make([]api.ExecEnvVar, 0),
|
||||||
// optionArgs = append(optionArgs, fmt.Sprintf("--sk=%s", sk))
|
}
|
||||||
// config.ExecProvider = &api.ExecConfig{
|
return kubernetes.NewForConfig(config)
|
||||||
// Command: "cci-iam-authenticator",
|
}
|
||||||
// APIVersion: apiVersion,
|
|
||||||
// Args: append([]string{"token"}, optionArgs...),
|
func CreatePodOrigin(client *kubernetes.Clientset) (*corev1.Pod, error) {
|
||||||
// Env: make([]api.ExecEnvVar, 0),
|
|
||||||
// }
|
pod := corev1.Pod{
|
||||||
// return kubernetes.NewForConfig(config)
|
TypeMeta: metav1.TypeMeta{
|
||||||
//}
|
APIVersion: "core/V1",
|
||||||
//
|
Kind: "Pod",
|
||||||
//func CreatePod(client *kubernetes.Clientset) (*corev1.Pod, error) {
|
},
|
||||||
// cli, _ := NewClient()
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
//
|
Name: "hw-sdk-test",
|
||||||
// pod := corev1.Pod{
|
Namespace: "pcm",
|
||||||
// TypeMeta: metav1.TypeMeta{
|
Labels: map[string]string{"name": "test_api"},
|
||||||
// APIVersion: "core/V1",
|
},
|
||||||
// Kind: "Pod",
|
Spec: corev1.PodSpec{
|
||||||
// },
|
RestartPolicy: corev1.RestartPolicyAlways,
|
||||||
// ObjectMeta: metav1.ObjectMeta{
|
Containers: []corev1.Container{
|
||||||
// Name: "hw-sdk-test",
|
{
|
||||||
// Namespace: "pcm",
|
Name: "pcm-sdk-huawei-container",
|
||||||
// Labels: map[string]string{"name": "test_api"},
|
Image: "library/nginx:stable-alpine-perl",
|
||||||
// },
|
Resources: corev1.ResourceRequirements{
|
||||||
// Spec: corev1.PodSpec{
|
Limits: map[corev1.ResourceName]resource.Quantity{
|
||||||
// RestartPolicy: corev1.RestartPolicyAlways,
|
corev1.ResourceCPU: resource.MustParse("500m"),
|
||||||
// Containers: []corev1.Container{
|
corev1.ResourceMemory: resource.MustParse("1Gi"),
|
||||||
// {
|
},
|
||||||
// Name: "pcm-sdk-huawei-container",
|
},
|
||||||
// Image: "library/nginx:stable-alpine-perl",
|
},
|
||||||
// Resources: corev1.ResourceRequirements{
|
},
|
||||||
// Limits: map[corev1.ResourceName]resource.Quantity{
|
},
|
||||||
// corev1.ResourceCPU: resource.MustParse("500m"),
|
Status: corev1.PodStatus{},
|
||||||
// corev1.ResourceMemory: resource.MustParse("1Gi"),
|
}
|
||||||
// },
|
|
||||||
// },
|
resp, err := client.CoreV1().Pods("pcm").Create(context.TODO(), &pod, metav1.CreateOptions{})
|
||||||
// },
|
if err != nil {
|
||||||
// },
|
return nil, errors.Wrap(err, "Huaweiyun CreatePod error")
|
||||||
// },
|
}
|
||||||
// Status: corev1.PodStatus{},
|
|
||||||
// }
|
return &corev1.Pod{
|
||||||
//
|
TypeMeta: resp.TypeMeta,
|
||||||
// resp, err := cli.CoreV1().Pods("pcm").Create(context.TODO(), &pod, metav1.CreateOptions{})
|
ObjectMeta: resp.ObjectMeta,
|
||||||
// if err != nil {
|
Spec: resp.Spec,
|
||||||
// return nil, errors.Wrap(err, "Huaweiyun CreatePod error")
|
Status: resp.Status,
|
||||||
// }
|
}, nil
|
||||||
//
|
}
|
||||||
// return &corev1.Pod{
|
|
||||||
// TypeMeta: resp.TypeMeta,
|
// UpdatePodOrigin 删除Pod
|
||||||
// ObjectMeta: resp.ObjectMeta,
|
func UpdatePodOrigin(client *kubernetes.Clientset) error {
|
||||||
// Spec: resp.Spec,
|
pod := corev1.Pod{
|
||||||
// Status: resp.Status,
|
TypeMeta: metav1.TypeMeta{
|
||||||
// }, nil
|
APIVersion: "core/V1",
|
||||||
//}
|
Kind: "Pod",
|
||||||
//
|
},
|
||||||
//// UpdatePod 删除Pod
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
//func UpdatePod(client *kubernetes.Clientset) error {
|
Name: "hw-sdk-test",
|
||||||
// pod := corev1.Pod{
|
Namespace: "pcm",
|
||||||
// TypeMeta: metav1.TypeMeta{
|
Labels: map[string]string{"name": "test_api222"},
|
||||||
// APIVersion: "core/V1",
|
},
|
||||||
// Kind: "Pod",
|
Spec: corev1.PodSpec{
|
||||||
// },
|
RestartPolicy: corev1.RestartPolicyAlways,
|
||||||
// ObjectMeta: metav1.ObjectMeta{
|
Containers: []corev1.Container{
|
||||||
// Name: "hw-sdk-test",
|
{
|
||||||
// Namespace: "pcm",
|
Name: "pcm-sdk-huawei-container2",
|
||||||
// Labels: map[string]string{"name": "test_api222"},
|
Image: "library/nginx:stable-alpine-perl",
|
||||||
// },
|
Resources: corev1.ResourceRequirements{
|
||||||
// Spec: corev1.PodSpec{
|
Limits: map[corev1.ResourceName]resource.Quantity{
|
||||||
// RestartPolicy: corev1.RestartPolicyAlways,
|
corev1.ResourceCPU: resource.MustParse("500m"),
|
||||||
// Containers: []corev1.Container{
|
corev1.ResourceMemory: resource.MustParse("1Gi"),
|
||||||
// {
|
},
|
||||||
// Name: "pcm-sdk-huawei-container2",
|
},
|
||||||
// Image: "library/nginx:stable-alpine-perl",
|
},
|
||||||
// Resources: corev1.ResourceRequirements{
|
},
|
||||||
// Limits: map[corev1.ResourceName]resource.Quantity{
|
},
|
||||||
// corev1.ResourceCPU: resource.MustParse("500m"),
|
Status: corev1.PodStatus{},
|
||||||
// corev1.ResourceMemory: resource.MustParse("1Gi"),
|
}
|
||||||
// },
|
|
||||||
// },
|
client.CoreV1().Pods("pcm").Update(context.TODO(), &pod, metav1.UpdateOptions{})
|
||||||
// },
|
return nil
|
||||||
// },
|
}
|
||||||
// },
|
|
||||||
// Status: corev1.PodStatus{},
|
// DeletePodOrigin 删除Pod
|
||||||
// }
|
func DeletePodOrigin(client *kubernetes.Clientset) error {
|
||||||
//
|
return client.CoreV1().Pods("pcm").Delete(context.TODO(), "podName", metav1.DeleteOptions{})
|
||||||
// client.CoreV1().Pods("pcm").Update(context.TODO(), &pod, metav1.UpdateOptions{})
|
}
|
||||||
// return nil
|
|
||||||
//}
|
// ListPodOrigin 查询Pod
|
||||||
//
|
func ListPodOrigin(client *kubernetes.Clientset) (*corev1.PodList, error) {
|
||||||
//// DeletePod 删除Pod
|
|
||||||
//func DeletePod(client *kubernetes.Clientset) error {
|
podList, _ := client.CoreV1().Pods("pcm").List(context.TODO(), metav1.ListOptions{})
|
||||||
// return client.CoreV1().Pods("pcm").Delete(context.TODO(), "podName", metav1.DeleteOptions{})
|
println(podList.Items[0].Name)
|
||||||
//}
|
return podList, nil
|
||||||
//
|
}
|
||||||
//// ListPod 查询Pod
|
func main() {
|
||||||
//func ListPod(client *kubernetes.Clientset) (*corev1.PodList, error) {
|
|
||||||
//
|
cs, _ := NewClientOrigin()
|
||||||
// podList, _ := client.CoreV1().Pods("pcm").List(context.TODO(), metav1.ListOptions{})
|
|
||||||
// println(podList.Items[0].Name)
|
fmt.Println("start to create Pod")
|
||||||
// return podList, nil
|
CreatePodOrigin(cs)
|
||||||
//}
|
fmt.Println("Pod created")
|
||||||
//func main() {
|
|
||||||
// // 有多种认证方式,具体请参考
|
fmt.Println("start to get Pod details")
|
||||||
// //《使用cci-iam-authenticator进行认证并创建client》,https://support.huaweicloud.com/sdkreference-cci/cci_09_0004.html
|
ListPodOrigin(cs)
|
||||||
// cciConfig, err := clientcmd.BuildConfigFromFlags(cciEndpoint, "")
|
fmt.Println("Pod details got")
|
||||||
// var optionArgs []string
|
|
||||||
// optionArgs = append(optionArgs, fmt.Sprintf("--iam-endpoint=%s", iamEndpoint))
|
fmt.Println("start to update Pod")
|
||||||
// optionArgs = append(optionArgs, fmt.Sprintf("--project-name=%s", projectName))
|
UpdatePodOrigin(cs)
|
||||||
// optionArgs = append(optionArgs, fmt.Sprintf("--ak=%s", ak))
|
fmt.Println("Pod updated")
|
||||||
// optionArgs = append(optionArgs, fmt.Sprintf("--sk=%s", sk))
|
|
||||||
// cciConfig.ExecProvider = &api.ExecConfig{
|
fmt.Println("start to delete Pod")
|
||||||
// Command: "cci-iam-authenticator",
|
DeletePodOrigin(cs)
|
||||||
// APIVersion: apiVersion,
|
fmt.Println("Pod deleted")
|
||||||
// Args: append([]string{"token"}, optionArgs...),
|
|
||||||
// Env: make([]api.ExecEnvVar, 0),
|
}
|
||||||
// }
|
|
||||||
// cs, err := huaweicci.NewForConfig(cciConfig)
|
|
||||||
//
|
|
||||||
// //fmt.Println("start to create Pod")
|
|
||||||
// //_, err = CreatePod(cs)
|
|
||||||
// //if err != nil {
|
|
||||||
// // panic(err)
|
|
||||||
// //}
|
|
||||||
// //fmt.Println("Pod created")
|
|
||||||
// //
|
|
||||||
// //fmt.Println("start to get Pod details")
|
|
||||||
// //_, err = ListPod(cs)
|
|
||||||
// //if err != nil {
|
|
||||||
// // panic(err)
|
|
||||||
// //}
|
|
||||||
// //fmt.Println("Pod details got")
|
|
||||||
// //
|
|
||||||
// fmt.Println("start to update Pod")
|
|
||||||
// err = UpdatePod(cs)
|
|
||||||
// if err != nil {
|
|
||||||
// panic(err)
|
|
||||||
// }
|
|
||||||
// fmt.Println("Pod updated")
|
|
||||||
// //
|
|
||||||
// //fmt.Println("start to delete Pod")
|
|
||||||
// //err = DeletePod(cs)
|
|
||||||
// //if err != nil {
|
|
||||||
// // panic(err)
|
|
||||||
// //}
|
|
||||||
// //fmt.Println("Pod deleted")
|
|
||||||
//
|
|
||||||
//}
|
|
||||||
|
|
|
@ -12,20 +12,7 @@ import (
|
||||||
"k8s.io/client-go/tools/clientcmd/api"
|
"k8s.io/client-go/tools/clientcmd/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
// NewClient 通过AK/SK认证创建ClientSet
|
||||||
apiVersion = "client.authentication.k8s.io/v1beta1"
|
|
||||||
// 云容器实例 CCI,参考https://developer.huaweicloud.com/endpoint
|
|
||||||
cciEndpoint = "https://cci.cn-east-3.myhuaweicloud.com"
|
|
||||||
// 统一身份认证服务 IAM,参考https://developer.huaweicloud.com/endpoint
|
|
||||||
iamEndpoint = "https://iam.cn-east-3.myhuaweicloud.com"
|
|
||||||
// 地区和终端节点,参考https://developer.huaweicloud.com/endpoint
|
|
||||||
projectName = "cn-east-3"
|
|
||||||
// 获取AK/SK参考: https://support.huaweicloud.com/devg-cci/cci_kubectl_01.html#cci_kubectl_01__section17023744719
|
|
||||||
ak = "ATQTIWUT9K66VRMMXKVY"
|
|
||||||
sk = "Wa0aixDVuhZOfDZGWvgIJQBHnyiDlGdgDn1Ai5Yy"
|
|
||||||
)
|
|
||||||
|
|
||||||
// NewClient 通过AK/SK认证创建Clientset
|
|
||||||
func NewClient() (*kubernetes.Clientset, error) {
|
func NewClient() (*kubernetes.Clientset, error) {
|
||||||
config, err := clientcmd.BuildConfigFromFlags(cciEndpoint, "")
|
config, err := clientcmd.BuildConfigFromFlags(cciEndpoint, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -46,8 +33,8 @@ func NewClient() (*kubernetes.Clientset, error) {
|
||||||
return kubernetes.NewForConfig(config)
|
return kubernetes.NewForConfig(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CreatePod 创建Pod
|
||||||
func CreatePod(client *kubernetes.Clientset) (*corev1.Pod, error) {
|
func CreatePod(client *kubernetes.Clientset) (*corev1.Pod, error) {
|
||||||
cli, _ := NewClient()
|
|
||||||
|
|
||||||
pod := corev1.Pod{
|
pod := corev1.Pod{
|
||||||
TypeMeta: metav1.TypeMeta{
|
TypeMeta: metav1.TypeMeta{
|
||||||
|
@ -60,9 +47,12 @@ func CreatePod(client *kubernetes.Clientset) (*corev1.Pod, error) {
|
||||||
Labels: map[string]string{"name": "test_api"},
|
Labels: map[string]string{"name": "test_api"},
|
||||||
},
|
},
|
||||||
Spec: corev1.PodSpec{
|
Spec: corev1.PodSpec{
|
||||||
|
/****************PCM params******************/
|
||||||
|
//0: ali, 1: tencent, 2: huawei,3: k8s
|
||||||
ProviderId: 0,
|
ProviderId: 0,
|
||||||
//ali:cn-hangzhou tc:ap-guangzhou hw: cn-east-3
|
//ali:cn-hangzhou tc:ap-guangzhou hw: cn-east-3
|
||||||
RegionId: "cn-hangzhou",
|
RegionId: "cn-hangzhou",
|
||||||
|
/****************PCM params******************/
|
||||||
RestartPolicy: corev1.RestartPolicyAlways,
|
RestartPolicy: corev1.RestartPolicyAlways,
|
||||||
Containers: []corev1.Container{
|
Containers: []corev1.Container{
|
||||||
{
|
{
|
||||||
|
@ -80,7 +70,7 @@ func CreatePod(client *kubernetes.Clientset) (*corev1.Pod, error) {
|
||||||
Status: corev1.PodStatus{},
|
Status: corev1.PodStatus{},
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := cli.CoreV1().Pods("pcm").Create(context.TODO(), &pod, metav1.CreateOptions{})
|
resp, err := client.CoreV1().Pods("pcm").Create(context.TODO(), &pod, metav1.CreateOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "Huaweiyun CreatePod error")
|
return nil, errors.Wrap(err, "Huaweiyun CreatePod error")
|
||||||
}
|
}
|
||||||
|
@ -93,6 +83,23 @@ func CreatePod(client *kubernetes.Clientset) (*corev1.Pod, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListPod 查询Pod
|
||||||
|
func ListPod(client *kubernetes.Clientset) (*corev1.PodList, error) {
|
||||||
|
pod := corev1.Pod{
|
||||||
|
Spec: corev1.PodSpec{
|
||||||
|
/****************PCM params******************/
|
||||||
|
//0: ali, 1: tencent, 2: huawei,3: k8s
|
||||||
|
ProviderId: 0,
|
||||||
|
//ali:cn-hangzhou tc:ap-guangzhou hw: cn-east-3
|
||||||
|
//RegionId: "cn-hangzhou",
|
||||||
|
/****************PCM params******************/
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
podList, _ := client.CoreV1().Pods("pcm").List(context.TODO(), &pod, metav1.ListOptions{})
|
||||||
|
return podList, nil
|
||||||
|
}
|
||||||
|
|
||||||
// UpdatePod 更新Pod
|
// UpdatePod 更新Pod
|
||||||
func UpdatePod(client *kubernetes.Clientset) error {
|
func UpdatePod(client *kubernetes.Clientset) error {
|
||||||
pod := corev1.Pod{
|
pod := corev1.Pod{
|
||||||
|
@ -107,9 +114,12 @@ func UpdatePod(client *kubernetes.Clientset) error {
|
||||||
Labels: map[string]string{"name": "test_api222"},
|
Labels: map[string]string{"name": "test_api222"},
|
||||||
},
|
},
|
||||||
Spec: corev1.PodSpec{
|
Spec: corev1.PodSpec{
|
||||||
|
/****************PCM params******************/
|
||||||
|
//0: ali, 1: tencent, 2: huawei,3: k8s
|
||||||
ProviderId: 0,
|
ProviderId: 0,
|
||||||
//ali:cn-hangzhou tc:ap-guangzhou hw: cn-east-3
|
//ali:cn-hangzhou tc:ap-guangzhou hw: cn-east-3
|
||||||
RegionId: "cn-hangzhou",
|
RegionId: "cn-hangzhou",
|
||||||
|
/****************PCM params******************/
|
||||||
RestartPolicy: corev1.RestartPolicyAlways,
|
RestartPolicy: corev1.RestartPolicyAlways,
|
||||||
Containers: []corev1.Container{
|
Containers: []corev1.Container{
|
||||||
{
|
{
|
||||||
|
@ -127,71 +137,47 @@ func UpdatePod(client *kubernetes.Clientset) error {
|
||||||
Status: corev1.PodStatus{},
|
Status: corev1.PodStatus{},
|
||||||
}
|
}
|
||||||
|
|
||||||
client.CoreV1().Pods("pcm").Update(context.TODO(), &pod, metav1.UpdateOptions{})
|
_, err := client.CoreV1().Pods("pcm").Update(context.TODO(), &pod, metav1.UpdateOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeletePod 删除Pod
|
// DeletePod 删除Pod
|
||||||
func DeletePod(client *kubernetes.Clientset) error {
|
func DeletePod(client *kubernetes.Clientset) error {
|
||||||
return client.CoreV1().Pods("pcm").Delete(context.TODO(), "podName", metav1.DeleteOptions{})
|
//多出5个参数 namespace providerId regionName accountName pcmId
|
||||||
|
return client.CoreV1().Pods("pcm").Delete(context.TODO(), "pcm", 0, "cn-hangzhou",
|
||||||
|
"", "eci-bp123wba1qv9xymntd24", metav1.DeleteOptions{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListPod 查询Pod
|
|
||||||
func ListPod(client *kubernetes.Clientset) (*corev1.PodList, error) {
|
|
||||||
pod := corev1.Pod{
|
|
||||||
Spec: corev1.PodSpec{
|
|
||||||
ProviderId: 0,
|
|
||||||
//ali:cn-hangzhou tc:ap-guangzhou hw: cn-east-3
|
|
||||||
//RegionId: "ap-guangzhou",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
podList, _ := client.CoreV1().Pods("pcm").List(context.TODO(), &pod, metav1.ListOptions{})
|
|
||||||
return podList, nil
|
|
||||||
}
|
|
||||||
func main() {
|
func main() {
|
||||||
// 有多种认证方式,具体请参考
|
|
||||||
//《使用cci-iam-authenticator进行认证并创建client》,https://support.huaweicloud.com/sdkreference-cci/cci_09_0004.html
|
cs, _ := NewClient()
|
||||||
cciConfig, err := clientcmd.BuildConfigFromFlags(cciEndpoint, "")
|
|
||||||
var optionArgs []string
|
fmt.Println("start to create Pod")
|
||||||
optionArgs = append(optionArgs, fmt.Sprintf("--iam-endpoint=%s", iamEndpoint))
|
_, err := CreatePod(cs)
|
||||||
optionArgs = append(optionArgs, fmt.Sprintf("--project-name=%s", projectName))
|
if err != nil {
|
||||||
optionArgs = append(optionArgs, fmt.Sprintf("--ak=%s", ak))
|
return
|
||||||
optionArgs = append(optionArgs, fmt.Sprintf("--sk=%s", sk))
|
|
||||||
cciConfig.ExecProvider = &api.ExecConfig{
|
|
||||||
Command: "cci-iam-authenticator",
|
|
||||||
APIVersion: apiVersion,
|
|
||||||
Args: append([]string{"token"}, optionArgs...),
|
|
||||||
Env: make([]api.ExecEnvVar, 0),
|
|
||||||
}
|
}
|
||||||
cs, err := kubernetes.NewForConfig(cciConfig)
|
fmt.Println("Pod created")
|
||||||
|
fmt.Println("start to get Pod details")
|
||||||
//fmt.Println("start to create Pod")
|
_, err = ListPod(cs)
|
||||||
//_, err = CreatePod(cs)
|
if err != nil {
|
||||||
//if err != nil {
|
return
|
||||||
// panic(err)
|
}
|
||||||
//}
|
fmt.Println("Pod details got")
|
||||||
//fmt.Println("Pod created")
|
|
||||||
|
|
||||||
//fmt.Println("start to get Pod details")
|
|
||||||
//_, err = ListPod(cs)
|
|
||||||
//if err != nil {
|
|
||||||
// panic(err)
|
|
||||||
//}
|
|
||||||
//fmt.Println("Pod details got")
|
|
||||||
|
|
||||||
fmt.Println("start to update Pod")
|
fmt.Println("start to update Pod")
|
||||||
err = UpdatePod(cs)
|
err = UpdatePod(cs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return
|
||||||
}
|
}
|
||||||
fmt.Println("Pod updated")
|
fmt.Println("Pod updated")
|
||||||
//
|
fmt.Println("start to delete Pod")
|
||||||
//fmt.Println("start to delete Pod")
|
err = DeletePod(cs)
|
||||||
//err = DeletePod(cs)
|
if err != nil {
|
||||||
//if err != nil {
|
return
|
||||||
// panic(err)
|
}
|
||||||
//}
|
fmt.Println("Pod deleted")
|
||||||
//fmt.Println("Pod deleted")
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -5,6 +5,7 @@ go 1.17
|
||||||
require (
|
require (
|
||||||
github.com/alibabacloud-go/darabonba-openapi v0.1.4
|
github.com/alibabacloud-go/darabonba-openapi v0.1.4
|
||||||
github.com/alibabacloud-go/darabonba-string v1.0.0
|
github.com/alibabacloud-go/darabonba-string v1.0.0
|
||||||
|
github.com/alibabacloud-go/eci-20180808/v2 v2.0.2
|
||||||
github.com/alibabacloud-go/ecs-20140526/v2 v2.1.0
|
github.com/alibabacloud-go/ecs-20140526/v2 v2.1.0
|
||||||
github.com/alibabacloud-go/tea v1.1.15
|
github.com/alibabacloud-go/tea v1.1.15
|
||||||
github.com/alibabacloud-go/tea-utils v1.3.9
|
github.com/alibabacloud-go/tea-utils v1.3.9
|
||||||
|
|
Loading…
Reference in New Issue