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")
|
||||
var regionId int32
|
||||
var pcmId string
|
||||
podId := request.ContainerGroupId
|
||||
podName := request.ContainerGroupName
|
||||
switch request.ProviderId {
|
||||
case 0:
|
||||
regionId, _ = tenanter.GetAliRegionId(request.RegionId)
|
||||
pcmId = podId
|
||||
case 1:
|
||||
regionId, _ = tenanter.GetTencentRegionId(request.RegionId)
|
||||
pcmId = podId
|
||||
case 2:
|
||||
regionId, _ = tenanter.GetHuaweiRegionId(request.RegionId)
|
||||
pcmId = podName
|
||||
case 3:
|
||||
regionId, _ = tenanter.GetK8SRegionId(request.RegionId)
|
||||
pcmId = podName
|
||||
}
|
||||
podId := request.ContainerGroupId
|
||||
podName := request.ContainerGroupName
|
||||
|
||||
requestPCM := &pbpod.DeletePodReq{
|
||||
RequestSource: "ali",
|
||||
Provider: provider,
|
||||
AccountName: request.AccountName,
|
||||
PcmId: pcmId,
|
||||
PodId: podId,
|
||||
PodName: podName,
|
||||
Namespace: request.Namespace,
|
||||
|
|
|
@ -34,7 +34,7 @@ type PodInterface interface {
|
|||
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)
|
||||
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
|
||||
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)
|
||||
|
@ -165,7 +165,7 @@ func (c *pods) Create(ctx context.Context, pod *v1.Pod, opts metav1.CreateOption
|
|||
var cpuPod string
|
||||
var memoryPod string
|
||||
|
||||
//华为(K8S)与其他厂商的CPU单位和格式有区别
|
||||
//华为(K8S)与其他厂商的CPU单位和格式有区别,region根据字符串取各厂商的对应id
|
||||
switch pod.Spec.ProviderId {
|
||||
case 0:
|
||||
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{
|
||||
RequestSource: "huawei",
|
||||
Provider: provider,
|
||||
AccountName: tenanters[0].AccountName(),
|
||||
PodName: pod.Name,
|
||||
RegionId: regionId,
|
||||
ContainerImage: pod.Spec.Containers[0].Image,
|
||||
ContainerName: pod.Spec.Containers[0].Name,
|
||||
CpuPod: cpuPod,
|
||||
MemoryPod: memoryPod,
|
||||
RequestSource: "huawei",
|
||||
Provider: provider,
|
||||
AccountName: tenanters[0].AccountName(),
|
||||
PodName: pod.Name,
|
||||
RegionId: regionId,
|
||||
ContainerImage: pod.Spec.Containers[0].Image,
|
||||
ContainerName: pod.Spec.Containers[0].Name,
|
||||
CpuPod: cpuPod,
|
||||
MemoryPod: memoryPod,
|
||||
//这里腾讯需要独立提供三个参数,目前固定使用以下三个
|
||||
SecurityGroupId: "sg-6qlun7hd",
|
||||
SubnetId: "subnet-mnwfg2fk",
|
||||
VpcId: "vpc-rkwt40g5",
|
||||
|
@ -207,44 +208,12 @@ func (c *pods) Create(ctx context.Context, pod *v1.Pod, opts metav1.CreateOption
|
|||
TypeMeta: metav1.TypeMeta{},
|
||||
ObjectMeta: metav1.ObjectMeta{},
|
||||
Spec: v1.PodSpec{
|
||||
ProviderId: pod.Spec.ProviderId,
|
||||
RegionId: pod.Spec.RegionId,
|
||||
AccountName: pod.Spec.AccountName,
|
||||
Volumes: nil,
|
||||
InitContainers: nil,
|
||||
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,
|
||||
ProviderId: pod.Spec.ProviderId,
|
||||
RegionId: pod.Spec.RegionId,
|
||||
AccountName: pod.Spec.AccountName,
|
||||
Volumes: nil,
|
||||
InitContainers: nil,
|
||||
Containers: pod.Spec.Containers,
|
||||
},
|
||||
Status: v1.PodStatus{
|
||||
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.
|
||||
func (c *pods) Delete(ctx context.Context, name string, opts metav1.DeleteOptions) error {
|
||||
return c.client.Delete().
|
||||
Namespace(c.ns).
|
||||
Resource("pods").
|
||||
Name(name).
|
||||
Body(&opts).
|
||||
Do(ctx).
|
||||
Error()
|
||||
func (c *pods) Delete(ctx context.Context, namespace string, providerId int32, regionName string, accountName string, pcmId string, opts metav1.DeleteOptions) error {
|
||||
|
||||
provider := pbtenant.CloudProvider(providerId)
|
||||
var configFile string
|
||||
flag.StringVar(&configFile, "confHuawei", "configs/config.yaml", "config.yaml")
|
||||
flag.Parse()
|
||||
defer glog.Flush()
|
||||
|
||||
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.
|
||||
|
|
|
@ -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
|
||||
|
||||
//
|
||||
//import (
|
||||
// openapi "github.com/alibabacloud-go/darabonba-openapi/client"
|
||||
// eci20180808 "github.com/alibabacloud-go/eci-20180808/v2/client"
|
||||
// "github.com/alibabacloud-go/tea/tea"
|
||||
// "os"
|
||||
//)
|
||||
//
|
||||
///**
|
||||
// * 使用AK&SK初始化账号Client
|
||||
// * @param accessKeyId
|
||||
// * @param accessKeySecret
|
||||
// * @return Client
|
||||
// * @throws Exception
|
||||
// */
|
||||
//func CreateClient(accessKeyId *string, accessKeySecret *string) (_result *eci20180808.Client, _err error) {
|
||||
// config := &openapi.Config{
|
||||
// // 您的AccessKey ID
|
||||
// AccessKeyId: accessKeyId,
|
||||
// // 您的AccessKey Secret
|
||||
// AccessKeySecret: accessKeySecret,
|
||||
// }
|
||||
// // 访问的域名
|
||||
// config.Endpoint = tea.String("eci.aliyuncs.com")
|
||||
// _result = &eci20180808.Client{}
|
||||
// _result, _err = eci20180808.NewClient(config)
|
||||
// return _result, _err
|
||||
//}
|
||||
//
|
||||
//func _main(args []*string) (_err error) {
|
||||
// client, _err := CreateClient(tea.String(""), tea.String(""))
|
||||
// if _err != nil {
|
||||
// return _err
|
||||
// }
|
||||
//
|
||||
// describeContainerGroupsRequest := &eci20180808.DescribeContainerGroupsRequest{
|
||||
// RegionId: tea.String("cn-hangzhou"),
|
||||
// }
|
||||
// // 复制代码运行请自行打印 API 的返回值
|
||||
// resp, _err := client.DescribeContainerGroups(describeContainerGroupsRequest)
|
||||
// println(*resp.Body.ContainerGroups[0].ContainerGroupId)
|
||||
// if _err != nil {
|
||||
// return _err
|
||||
// }
|
||||
// return _err
|
||||
//}
|
||||
//
|
||||
//func main() {
|
||||
// err := _main(tea.StringSlice(os.Args[1:]))
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
//}
|
||||
import (
|
||||
openapi "github.com/alibabacloud-go/darabonba-openapi/client"
|
||||
eci20180808 "github.com/alibabacloud-go/eci-20180808/v2/client"
|
||||
"github.com/alibabacloud-go/tea/tea"
|
||||
)
|
||||
|
||||
func CreateClient(accessKeyId *string, accessKeySecret *string) (_result *eci20180808.Client, _err error) {
|
||||
config := &openapi.Config{
|
||||
// 您的AccessKey ID
|
||||
AccessKeyId: accessKeyId,
|
||||
// 您的AccessKey Secret
|
||||
AccessKeySecret: accessKeySecret,
|
||||
}
|
||||
// 访问的域名
|
||||
config.Endpoint = tea.String("eci.aliyuncs.com")
|
||||
_result = &eci20180808.Client{}
|
||||
_result, _err = eci20180808.NewClient(config)
|
||||
return _result, _err
|
||||
}
|
||||
|
||||
func aliOriginMain() (_err error) {
|
||||
client, _err := CreateClient(tea.String(""), tea.String(""))
|
||||
if _err != nil {
|
||||
return _err
|
||||
}
|
||||
|
||||
describeContainerGroupsRequest := &eci20180808.DescribeContainerGroupsRequest{
|
||||
RegionId: tea.String("cn-hangzhou"),
|
||||
}
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
resp, _err := client.DescribeContainerGroups(describeContainerGroupsRequest)
|
||||
println(*resp.Body.ContainerGroups[0].ContainerGroupId)
|
||||
if _err != nil {
|
||||
return _err
|
||||
}
|
||||
return _err
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := aliOriginMain()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,186 +1,154 @@
|
|||
package main
|
||||
|
||||
//
|
||||
//import (
|
||||
// "context"
|
||||
// "fmt"
|
||||
// "github.com/pkg/errors"
|
||||
// corev1 "k8s.io/api/core/v1"
|
||||
// "k8s.io/apimachinery/pkg/api/resource"
|
||||
// metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
// "k8s.io/client-go/kubernetes"
|
||||
// huaweicci "k8s.io/client-go/kubernetes"
|
||||
// "k8s.io/client-go/tools/clientcmd"
|
||||
// "k8s.io/client-go/tools/clientcmd/api"
|
||||
//)
|
||||
//
|
||||
//const (
|
||||
// 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) {
|
||||
// config, err := clientcmd.BuildConfigFromFlags(cciEndpoint, "")
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// var optionArgs []string
|
||||
// optionArgs = append(optionArgs, fmt.Sprintf("--iam-endpoint=%s", iamEndpoint))
|
||||
// optionArgs = append(optionArgs, fmt.Sprintf("--project-name=%s", projectName))
|
||||
// optionArgs = append(optionArgs, fmt.Sprintf("--token-only=false"))
|
||||
// optionArgs = append(optionArgs, fmt.Sprintf("--ak=%s", ak))
|
||||
// optionArgs = append(optionArgs, fmt.Sprintf("--sk=%s", sk))
|
||||
// config.ExecProvider = &api.ExecConfig{
|
||||
// Command: "cci-iam-authenticator",
|
||||
// APIVersion: apiVersion,
|
||||
// Args: append([]string{"token"}, optionArgs...),
|
||||
// Env: make([]api.ExecEnvVar, 0),
|
||||
// }
|
||||
// return kubernetes.NewForConfig(config)
|
||||
//}
|
||||
//
|
||||
//func CreatePod(client *kubernetes.Clientset) (*corev1.Pod, error) {
|
||||
// cli, _ := NewClient()
|
||||
//
|
||||
// pod := corev1.Pod{
|
||||
// TypeMeta: metav1.TypeMeta{
|
||||
// APIVersion: "core/V1",
|
||||
// Kind: "Pod",
|
||||
// },
|
||||
// ObjectMeta: metav1.ObjectMeta{
|
||||
// Name: "hw-sdk-test",
|
||||
// Namespace: "pcm",
|
||||
// Labels: map[string]string{"name": "test_api"},
|
||||
// },
|
||||
// Spec: corev1.PodSpec{
|
||||
// RestartPolicy: corev1.RestartPolicyAlways,
|
||||
// Containers: []corev1.Container{
|
||||
// {
|
||||
// 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"),
|
||||
// corev1.ResourceMemory: resource.MustParse("1Gi"),
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// Status: corev1.PodStatus{},
|
||||
// }
|
||||
//
|
||||
// resp, err := cli.CoreV1().Pods("pcm").Create(context.TODO(), &pod, metav1.CreateOptions{})
|
||||
// if err != nil {
|
||||
// return nil, errors.Wrap(err, "Huaweiyun CreatePod error")
|
||||
// }
|
||||
//
|
||||
// return &corev1.Pod{
|
||||
// TypeMeta: resp.TypeMeta,
|
||||
// ObjectMeta: resp.ObjectMeta,
|
||||
// Spec: resp.Spec,
|
||||
// Status: resp.Status,
|
||||
// }, nil
|
||||
//}
|
||||
//
|
||||
//// UpdatePod 删除Pod
|
||||
//func UpdatePod(client *kubernetes.Clientset) error {
|
||||
// pod := corev1.Pod{
|
||||
// TypeMeta: metav1.TypeMeta{
|
||||
// APIVersion: "core/V1",
|
||||
// Kind: "Pod",
|
||||
// },
|
||||
// ObjectMeta: metav1.ObjectMeta{
|
||||
// Name: "hw-sdk-test",
|
||||
// Namespace: "pcm",
|
||||
// Labels: map[string]string{"name": "test_api222"},
|
||||
// },
|
||||
// Spec: corev1.PodSpec{
|
||||
// RestartPolicy: corev1.RestartPolicyAlways,
|
||||
// Containers: []corev1.Container{
|
||||
// {
|
||||
// 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"),
|
||||
// corev1.ResourceMemory: resource.MustParse("1Gi"),
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// Status: corev1.PodStatus{},
|
||||
// }
|
||||
//
|
||||
// client.CoreV1().Pods("pcm").Update(context.TODO(), &pod, metav1.UpdateOptions{})
|
||||
// return nil
|
||||
//}
|
||||
//
|
||||
//// DeletePod 删除Pod
|
||||
//func DeletePod(client *kubernetes.Clientset) error {
|
||||
// return client.CoreV1().Pods("pcm").Delete(context.TODO(), "podName", metav1.DeleteOptions{})
|
||||
//}
|
||||
//
|
||||
//// ListPod 查询Pod
|
||||
//func ListPod(client *kubernetes.Clientset) (*corev1.PodList, error) {
|
||||
//
|
||||
// podList, _ := client.CoreV1().Pods("pcm").List(context.TODO(), metav1.ListOptions{})
|
||||
// println(podList.Items[0].Name)
|
||||
// return podList, nil
|
||||
//}
|
||||
//func main() {
|
||||
// // 有多种认证方式,具体请参考
|
||||
// //《使用cci-iam-authenticator进行认证并创建client》,https://support.huaweicloud.com/sdkreference-cci/cci_09_0004.html
|
||||
// cciConfig, err := clientcmd.BuildConfigFromFlags(cciEndpoint, "")
|
||||
// var optionArgs []string
|
||||
// optionArgs = append(optionArgs, fmt.Sprintf("--iam-endpoint=%s", iamEndpoint))
|
||||
// optionArgs = append(optionArgs, fmt.Sprintf("--project-name=%s", projectName))
|
||||
// optionArgs = append(optionArgs, fmt.Sprintf("--ak=%s", ak))
|
||||
// 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 := 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")
|
||||
//
|
||||
//}
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
"k8s.io/client-go/tools/clientcmd/api"
|
||||
)
|
||||
|
||||
const (
|
||||
apiVersion = "client.authentication.k8s.io/v1beta1"
|
||||
cciEndpoint = "https://cci.cn-east-3.myhuaweicloud.com"
|
||||
iamEndpoint = "https://iam.cn-east-3.myhuaweicloud.com"
|
||||
projectName = "cn-east-3"
|
||||
ak = "ATQTIWUT9K66VRMMXKVY"
|
||||
sk = "Wa0aixDVuhZOfDZGWvgIJQBHnyiDlGdgDn1Ai5Yy"
|
||||
)
|
||||
|
||||
// NewClientOrigin 通过AK/SK认证创建ClientSet
|
||||
func NewClientOrigin() (*kubernetes.Clientset, error) {
|
||||
config, err := clientcmd.BuildConfigFromFlags(cciEndpoint, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var optionArgs []string
|
||||
optionArgs = append(optionArgs, fmt.Sprintf("--iam-endpoint=%s", iamEndpoint))
|
||||
optionArgs = append(optionArgs, fmt.Sprintf("--project-name=%s", projectName))
|
||||
optionArgs = append(optionArgs, fmt.Sprintf("--token-only=false"))
|
||||
optionArgs = append(optionArgs, fmt.Sprintf("--ak=%s", ak))
|
||||
optionArgs = append(optionArgs, fmt.Sprintf("--sk=%s", sk))
|
||||
config.ExecProvider = &api.ExecConfig{
|
||||
Command: "cci-iam-authenticator",
|
||||
APIVersion: apiVersion,
|
||||
Args: append([]string{"token"}, optionArgs...),
|
||||
Env: make([]api.ExecEnvVar, 0),
|
||||
}
|
||||
return kubernetes.NewForConfig(config)
|
||||
}
|
||||
|
||||
func CreatePodOrigin(client *kubernetes.Clientset) (*corev1.Pod, error) {
|
||||
|
||||
pod := corev1.Pod{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: "core/V1",
|
||||
Kind: "Pod",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "hw-sdk-test",
|
||||
Namespace: "pcm",
|
||||
Labels: map[string]string{"name": "test_api"},
|
||||
},
|
||||
Spec: corev1.PodSpec{
|
||||
RestartPolicy: corev1.RestartPolicyAlways,
|
||||
Containers: []corev1.Container{
|
||||
{
|
||||
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"),
|
||||
corev1.ResourceMemory: resource.MustParse("1Gi"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Status: corev1.PodStatus{},
|
||||
}
|
||||
|
||||
resp, err := client.CoreV1().Pods("pcm").Create(context.TODO(), &pod, metav1.CreateOptions{})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "Huaweiyun CreatePod error")
|
||||
}
|
||||
|
||||
return &corev1.Pod{
|
||||
TypeMeta: resp.TypeMeta,
|
||||
ObjectMeta: resp.ObjectMeta,
|
||||
Spec: resp.Spec,
|
||||
Status: resp.Status,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UpdatePodOrigin 删除Pod
|
||||
func UpdatePodOrigin(client *kubernetes.Clientset) error {
|
||||
pod := corev1.Pod{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: "core/V1",
|
||||
Kind: "Pod",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "hw-sdk-test",
|
||||
Namespace: "pcm",
|
||||
Labels: map[string]string{"name": "test_api222"},
|
||||
},
|
||||
Spec: corev1.PodSpec{
|
||||
RestartPolicy: corev1.RestartPolicyAlways,
|
||||
Containers: []corev1.Container{
|
||||
{
|
||||
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"),
|
||||
corev1.ResourceMemory: resource.MustParse("1Gi"),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Status: corev1.PodStatus{},
|
||||
}
|
||||
|
||||
client.CoreV1().Pods("pcm").Update(context.TODO(), &pod, metav1.UpdateOptions{})
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeletePodOrigin 删除Pod
|
||||
func DeletePodOrigin(client *kubernetes.Clientset) error {
|
||||
return client.CoreV1().Pods("pcm").Delete(context.TODO(), "podName", metav1.DeleteOptions{})
|
||||
}
|
||||
|
||||
// ListPodOrigin 查询Pod
|
||||
func ListPodOrigin(client *kubernetes.Clientset) (*corev1.PodList, error) {
|
||||
|
||||
podList, _ := client.CoreV1().Pods("pcm").List(context.TODO(), metav1.ListOptions{})
|
||||
println(podList.Items[0].Name)
|
||||
return podList, nil
|
||||
}
|
||||
func main() {
|
||||
|
||||
cs, _ := NewClientOrigin()
|
||||
|
||||
fmt.Println("start to create Pod")
|
||||
CreatePodOrigin(cs)
|
||||
fmt.Println("Pod created")
|
||||
|
||||
fmt.Println("start to get Pod details")
|
||||
ListPodOrigin(cs)
|
||||
fmt.Println("Pod details got")
|
||||
|
||||
fmt.Println("start to update Pod")
|
||||
UpdatePodOrigin(cs)
|
||||
fmt.Println("Pod updated")
|
||||
|
||||
fmt.Println("start to delete Pod")
|
||||
DeletePodOrigin(cs)
|
||||
fmt.Println("Pod deleted")
|
||||
|
||||
}
|
||||
|
|
|
@ -12,20 +12,7 @@ import (
|
|||
"k8s.io/client-go/tools/clientcmd/api"
|
||||
)
|
||||
|
||||
const (
|
||||
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
|
||||
// NewClient 通过AK/SK认证创建ClientSet
|
||||
func NewClient() (*kubernetes.Clientset, error) {
|
||||
config, err := clientcmd.BuildConfigFromFlags(cciEndpoint, "")
|
||||
if err != nil {
|
||||
|
@ -46,8 +33,8 @@ func NewClient() (*kubernetes.Clientset, error) {
|
|||
return kubernetes.NewForConfig(config)
|
||||
}
|
||||
|
||||
// CreatePod 创建Pod
|
||||
func CreatePod(client *kubernetes.Clientset) (*corev1.Pod, error) {
|
||||
cli, _ := NewClient()
|
||||
|
||||
pod := corev1.Pod{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
|
@ -60,9 +47,12 @@ func CreatePod(client *kubernetes.Clientset) (*corev1.Pod, error) {
|
|||
Labels: map[string]string{"name": "test_api"},
|
||||
},
|
||||
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",
|
||||
RegionId: "cn-hangzhou",
|
||||
/****************PCM params******************/
|
||||
RestartPolicy: corev1.RestartPolicyAlways,
|
||||
Containers: []corev1.Container{
|
||||
{
|
||||
|
@ -80,7 +70,7 @@ func CreatePod(client *kubernetes.Clientset) (*corev1.Pod, error) {
|
|||
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 {
|
||||
return nil, errors.Wrap(err, "Huaweiyun CreatePod error")
|
||||
}
|
||||
|
@ -93,6 +83,23 @@ func CreatePod(client *kubernetes.Clientset) (*corev1.Pod, error) {
|
|||
}, 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
|
||||
func UpdatePod(client *kubernetes.Clientset) error {
|
||||
pod := corev1.Pod{
|
||||
|
@ -107,9 +114,12 @@ func UpdatePod(client *kubernetes.Clientset) error {
|
|||
Labels: map[string]string{"name": "test_api222"},
|
||||
},
|
||||
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",
|
||||
RegionId: "cn-hangzhou",
|
||||
/****************PCM params******************/
|
||||
RestartPolicy: corev1.RestartPolicyAlways,
|
||||
Containers: []corev1.Container{
|
||||
{
|
||||
|
@ -127,71 +137,47 @@ func UpdatePod(client *kubernetes.Clientset) error {
|
|||
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
|
||||
}
|
||||
|
||||
// DeletePod 删除Pod
|
||||
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() {
|
||||
// 有多种认证方式,具体请参考
|
||||
//《使用cci-iam-authenticator进行认证并创建client》,https://support.huaweicloud.com/sdkreference-cci/cci_09_0004.html
|
||||
cciConfig, err := clientcmd.BuildConfigFromFlags(cciEndpoint, "")
|
||||
var optionArgs []string
|
||||
optionArgs = append(optionArgs, fmt.Sprintf("--iam-endpoint=%s", iamEndpoint))
|
||||
optionArgs = append(optionArgs, fmt.Sprintf("--project-name=%s", projectName))
|
||||
optionArgs = append(optionArgs, fmt.Sprintf("--ak=%s", ak))
|
||||
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, _ := NewClient()
|
||||
|
||||
fmt.Println("start to create Pod")
|
||||
_, err := CreatePod(cs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
cs, err := kubernetes.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("Pod created")
|
||||
fmt.Println("start to get Pod details")
|
||||
_, err = ListPod(cs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fmt.Println("Pod details got")
|
||||
fmt.Println("start to update Pod")
|
||||
err = UpdatePod(cs)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return
|
||||
}
|
||||
fmt.Println("Pod updated")
|
||||
//
|
||||
//fmt.Println("start to delete Pod")
|
||||
//err = DeletePod(cs)
|
||||
//if err != nil {
|
||||
// panic(err)
|
||||
//}
|
||||
//fmt.Println("Pod deleted")
|
||||
fmt.Println("start to delete Pod")
|
||||
err = DeletePod(cs)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
fmt.Println("Pod deleted")
|
||||
|
||||
}
|
||||
|
|
1
go.mod
1
go.mod
|
@ -5,6 +5,7 @@ go 1.17
|
|||
require (
|
||||
github.com/alibabacloud-go/darabonba-openapi v0.1.4
|
||||
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/tea v1.1.15
|
||||
github.com/alibabacloud-go/tea-utils v1.3.9
|
||||
|
|
Loading…
Reference in New Issue