调整工程结构
This commit is contained in:
parent
fdc7c9272c
commit
e00c239862
|
@ -0,0 +1,63 @@
|
|||
// This file is auto-generated, don't edit it. Thanks.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
openapi "github.com/alibabacloud-go/darabonba-openapi/client"
|
||||
eci20180808 "github.com/alibabacloud-go/eci-20180808/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{
|
||||
AccessKeyId: accessKeyId,
|
||||
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("LTAI4GKsRHtEqUu8bzq5dt6N"), tea.String("j96G9E2ijwpiN987VC03HmcFsaJngt"))
|
||||
if _err != nil {
|
||||
return _err
|
||||
}
|
||||
|
||||
describeContainerGroupsRequest := &eci20180808.DescribeContainerGroupsRequest{
|
||||
OwnerAccount: tea.String("test"),
|
||||
RegionId: tea.String("cn-hangzhou"),
|
||||
}
|
||||
//deleteContainerGroupRequest := &eci20180808.DeleteContainerGroupRequest{
|
||||
// ResourceOwnerAccount: tea.String("test"),
|
||||
// ResourceOwnerId: tea.Int64(1),
|
||||
// OwnerAccount: tea.String("test"),
|
||||
// RegionId: tea.String("test"),
|
||||
//}
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
_, _err = client.DescribeContainerGroups(describeContainerGroupsRequest)
|
||||
//describeContainerGroupsResponse := client.DescribeContainerGroups(describeContainerGroupsRequest)
|
||||
fmt.Println(_err)
|
||||
if _err != nil {
|
||||
return _err
|
||||
}
|
||||
return _err
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := _main(tea.StringSlice(os.Args[1:]))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"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-north-4.myhuaweicloud.com>"
|
||||
// 统一身份认证服务 IAM,参考https://developer.huaweicloud.com/endpoint
|
||||
iamEndpoint = "<例如华北-北京四: https://iam.cn-north-4.myhuaweicloud.com>"
|
||||
// 地区和终端节点,参考https://developer.huaweicloud.com/endpoint
|
||||
projectName = "<例如华北-北京四: 'cn-north-4'>"
|
||||
// 获取AK/SK参考: https://support.huaweicloud.com/devg-cci/cci_kubectl_01.html#cci_kubectl_01__section17023744719
|
||||
ak = "NVZH8MA2QCWFC3KPLHWJ"
|
||||
sk = "CACGSvC4BPq0aQkgFkLfsbc7RUzBw0dD92wXZWuL"
|
||||
)
|
||||
|
||||
// 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)
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"PCM/container_api_adaptor/huawei/client"
|
||||
"PCM/container_api_adaptor/huawei/deployment"
|
||||
"PCM/container_api_adaptor/huawei/namespace"
|
||||
"PCM/container_api_adaptor/huawei/network"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 有多种认证方式,具体请参考
|
||||
//《使用cci-iam-authenticator进行认证并创建client》,https://support.huaweicloud.com/sdkreference-cci/cci_09_0004.html
|
||||
cs, err := client.NewClient()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Network为CRD对象,创建Network对象请参考
|
||||
//《使用client-go访问CCI的CRD资源Network》,https://support.huaweicloud.com/sdkreference-cci/cci_09_0004.html
|
||||
//《Namespace和Network》,https://support.huaweicloud.com/devg-cci/cci_05_0023.html#section3
|
||||
fmt.Println("start to create network")
|
||||
_, err = network.CreateNetwork()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("network created")
|
||||
|
||||
fmt.Println("start to create deployment")
|
||||
_, err = deployment.CreateDeployment(cs)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("deployment created")
|
||||
|
||||
fmt.Println("start to get deployment details")
|
||||
_, err = deployment.GetDeployment(cs)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("deployment details got")
|
||||
|
||||
fmt.Println("start to delete deployment")
|
||||
err = deployment.DeleteDeployment(cs)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("deployment deleted")
|
||||
|
||||
fmt.Println("start to delete namespace")
|
||||
err = namespace.DeleteNamespace(cs)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("namespace terminating")
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
package deployment
|
||||
|
||||
import (
|
||||
v1 "k8s.io/api/apps/v1"
|
||||
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"
|
||||
)
|
||||
|
||||
const (
|
||||
app = "test-k8s-client-deployment"
|
||||
namespace = "test-k8s-client-namespace"
|
||||
)
|
||||
|
||||
// CreateDeployment 创建Deployment
|
||||
// API参考:https://support.huaweicloud.com/api-cci/createAppsV1NamespacedDeployment.html
|
||||
func CreateDeployment(client *kubernetes.Clientset) (*v1.Deployment, error) {
|
||||
var replicas int32 = 2
|
||||
var priority int32 = 0
|
||||
|
||||
podTemplate := corev1.PodTemplateSpec{
|
||||
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": app}},
|
||||
Spec: corev1.PodSpec{
|
||||
Priority: &priority,
|
||||
ImagePullSecrets: []corev1.LocalObjectReference{{Name: "imagepull-secret"}},
|
||||
Containers: []corev1.Container{{
|
||||
Name: "container-0",
|
||||
Image: "library/nginx:stable-alpine-perl",
|
||||
Resources: corev1.ResourceRequirements{
|
||||
Requests: map[corev1.ResourceName]resource.Quantity{
|
||||
corev1.ResourceCPU: resource.MustParse("500m"),
|
||||
corev1.ResourceMemory: resource.MustParse("1024Mi"),
|
||||
},
|
||||
Limits: map[corev1.ResourceName]resource.Quantity{
|
||||
corev1.ResourceCPU: resource.MustParse("500m"),
|
||||
corev1.ResourceMemory: resource.MustParse("1024Mi"),
|
||||
},
|
||||
},
|
||||
}},
|
||||
},
|
||||
}
|
||||
|
||||
deployment := &v1.Deployment{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
APIVersion: "apps/v1",
|
||||
Kind: "Deployment",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: app,
|
||||
},
|
||||
Spec: v1.DeploymentSpec{
|
||||
Replicas: &replicas,
|
||||
Selector: &metav1.LabelSelector{
|
||||
MatchLabels: map[string]string{
|
||||
"app": app,
|
||||
},
|
||||
},
|
||||
Template: podTemplate,
|
||||
},
|
||||
}
|
||||
return client.AppsV1().Deployments(namespace).Create(nil, deployment, metav1.CreateOptions{})
|
||||
}
|
||||
|
||||
// DeleteDeployment 删除Deployment
|
||||
// API参考:https://support.huaweicloud.com/api-cci/deleteAppsV1NamespacedDeployment.html
|
||||
func DeleteDeployment(client *kubernetes.Clientset) error {
|
||||
return client.AppsV1().Deployments(namespace).Delete(nil, app, metav1.DeleteOptions{})
|
||||
}
|
||||
|
||||
// GetDeployment 查询Deployment
|
||||
// API参考:https://support.huaweicloud.com/api-cci/readAppsV1NamespacedDeployment.html
|
||||
func GetDeployment(client *kubernetes.Clientset) (*v1.Deployment, error) {
|
||||
return client.AppsV1().Deployments(namespace).Get(nil, app, metav1.GetOptions{})
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package huawei
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
|
||||
cce "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3"
|
||||
"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3/model"
|
||||
region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cce/v3/region"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ak := "NVZH8MA2QCWFC3KPLHWJ"
|
||||
sk := "CACGSvC4BPq0aQkgFkLfsbc7RUzBw0dD92wXZWuL"
|
||||
|
||||
auth := basic.NewCredentialsBuilder().
|
||||
WithAk(ak).
|
||||
WithSk(sk).
|
||||
Build()
|
||||
|
||||
client := cce.NewCceClient(
|
||||
cce.CceClientBuilder().
|
||||
WithRegion(region.ValueOf("cn-north-4")).
|
||||
WithCredential(auth).
|
||||
Build())
|
||||
|
||||
request := &model.ListClustersRequest{}
|
||||
response, err := client.ListClusters(request)
|
||||
if err == nil {
|
||||
fmt.Printf("%+v\n", response)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package namespace
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
)
|
||||
|
||||
const (
|
||||
namespace = "test-k8s-client-namespace"
|
||||
)
|
||||
|
||||
// CreateNamespace 创建命名空间
|
||||
// API参考:https://support.huaweicloud.com/api-cci/createCoreV1Namespace.html
|
||||
func CreateNamespace(cs *kubernetes.Clientset) (*corev1.Namespace, error) {
|
||||
namespace := &corev1.Namespace{
|
||||
TypeMeta: metav1.TypeMeta{},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: namespace,
|
||||
Annotations: map[string]string{
|
||||
"namespace.kubernetes.io/flavor": "general-computing",
|
||||
"network.cci.io/warm-pool-size": "10",
|
||||
},
|
||||
Labels: map[string]string{
|
||||
"rbac.authorization.cci.io/enable-k8s-rbac": "false",
|
||||
},
|
||||
},
|
||||
}
|
||||
return cs.CoreV1().Namespaces().Create(nil, namespace, metav1.CreateOptions{
|
||||
TypeMeta: metav1.TypeMeta{},
|
||||
DryRun: nil,
|
||||
FieldManager: "",
|
||||
FieldValidation: "",
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteNamespace 删除Namespace
|
||||
// API参考:https://support.huaweicloud.com/api-cci/deleteCoreV1Namespace.html
|
||||
func DeleteNamespace(cs *kubernetes.Clientset) error {
|
||||
return cs.CoreV1().Namespaces().Delete(nil, namespace, metav1.DeleteOptions{})
|
||||
}
|
||||
|
||||
// WaitNamespaceActive 查询Namespace状态,等待其状态变为"Active"
|
||||
// API参考:https://support.huaweicloud.com/api-cci/readCoreV1Namespace.html
|
||||
func WaitNamespaceActive(cs *kubernetes.Clientset) error {
|
||||
return wait.Poll(time.Second*5, time.Second*30, func() (done bool, err error) {
|
||||
ns, err := cs.CoreV1().Namespaces().Get(nil, namespace, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if ns.Status.Phase == corev1.NamespaceActive {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
})
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package network
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
"k8s.io/client-go/tools/clientcmd"
|
||||
|
||||
"examples.com/cci-examples/pkg/apis/networking.cci.io/v1beta1"
|
||||
clientset "examples.com/cci-examples/pkg/client/networking.cci.io/v1beta1"
|
||||
)
|
||||
|
||||
const (
|
||||
name = "test-k8s-client-namespace-cn-north-1-default-network"
|
||||
namespace = "test-k8s-client-namespace"
|
||||
)
|
||||
|
||||
// CreateNetwork 创建Network,并等待其状态变更为Active
|
||||
// 参考《Namespace和Network》 https://support.huaweicloud.com/devg-cci/cci_05_0023.html
|
||||
// API参考:https://support.huaweicloud.com/api-cci/createNetworkingCciIoV1beta1NamespacedNetwork.html
|
||||
func CreateNetwork() (*v1beta1.Network, error) {
|
||||
config, _ := clientcmd.BuildConfigFromFlags("", "{path to kubeconfig}")
|
||||
cs, err := clientset.NewForConfig(config)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
projectId := "<账号ID,可以在我的凭证获取>"
|
||||
domainId := "<项目ID,可以在我的凭证获取>"
|
||||
securityGroupID := "<安全组ID,可以在安全组控制台获取>"
|
||||
availableZone := "<az名称,例如cn-north-1a、cn-north-4a或cn-east-3a>"
|
||||
vpcID := "虚拟私有云的ID,可在VPC控制台获取"
|
||||
cidr := "<子网网段,例如192.168.128.0/18>"
|
||||
networkID := "<子网的网络ID,可在VPC控制台 > 子网中获取>"
|
||||
subnetID := "<子网ID,可在VPC控制台 > 子网获取>"
|
||||
|
||||
network := &v1beta1.Network{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Annotations: map[string]string{
|
||||
"network.alpha.kubernetes.io/default-security-group": securityGroupID,
|
||||
"network.alpha.kubernetes.io/domain-id": domainId,
|
||||
"network.alpha.kubernetes.io/project-id": projectId,
|
||||
},
|
||||
Name: name,
|
||||
},
|
||||
Spec: v1beta1.NetworkSpec{
|
||||
AvailableZone: availableZone,
|
||||
CIDR: cidr,
|
||||
AttachedVPC: vpcID,
|
||||
NetworkID: networkID,
|
||||
NetworkType: "underlay_neutron",
|
||||
SubnetID: subnetID,
|
||||
},
|
||||
}
|
||||
network, err = cs.NetworkingCCIV1beta1().Networks(namespace).Create(network)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 查询Network状态,等待其状态变为"Active"
|
||||
err = wait.Poll(time.Second*5, time.Second*30, func() (done bool, err error) {
|
||||
network, err = cs.NetworkingCCIV1beta1().Networks(namespace).Get(name, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if network.Status.State == v1beta1.NetworkActive {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
})
|
||||
return network, err
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package main
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
credential := common.NewCredential(
|
||||
"AKIDRefQxnhmuqTU1KRWFl58wQeCE0XoAeEZ",
|
||||
"LUMVRmGj2kFrEahhZzA5pKvHNSMASntj",
|
||||
)
|
||||
cpf := profile.NewClientProfile()
|
||||
cpf.HttpProfile.Endpoint = "tke.tencentcloudapi.com"
|
||||
client, _ := tke.NewClient(credential, "ap-beijing", cpf)
|
||||
|
||||
request := tke.NewDescribeEKSContainerInstancesRequest()
|
||||
|
||||
response, err := client.DescribeEKSContainerInstances(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())
|
||||
}
|
Loading…
Reference in New Issue