You need to enable JavaScript to run this app.
导航
Go SDK
最近更新时间:2023.10.17 16:55:32首次发布时间:2022.08.02 22:04:40

本文主要描述容器服务 Go 语言 SDK 的安装和配置,及使用 Go SDK 调用云端 API 的示例。

使用说明

  • 仅支持 Go 1.5 及以上版本(推荐使用 Go 1.12+ 版本)。
  • SDK 根目录(Service 目录)下的文件不要修改,由代码生成器自动生成。
  • 建议使用 go mod 方式进行依赖管理。

安装配置

  1. 安装火山引擎 SDK。
    go get github.com/volcengine/volcengine-go-sdk/volcengine
    go get github.com/volcengine/volcengine-go-sdk/volcengine/session
    
  2. 安装容器服务 SDK。
    go get github.com/volcengine/volcengine-go-sdk/service/vke
    

SDK 示例

创建集群(CreateCluster)

下方示例中参数的详细说明及配置方法,请参见 CreateCluster

// Example Code generated by Beijing Volcanoengine Technology.
package main

import (
        "github.com/volcengine/volcengine-go-sdk/service/vke"
        "github.com/volcengine/volcengine-go-sdk/volcengine"
        "github.com/volcengine/volcengine-go-sdk/volcengine/credentials"
        "github.com/volcengine/volcengine-go-sdk/volcengine/session"
)

func main() {
        // 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。
        ak, sk, region := "Your AK", "Your SK", "Region"
        config := volcengine.NewConfig().
                WithRegion(region).
                WithCredentials(credentials.NewStaticCredentials(ak, sk, ""))
        sess, err := session.NewSession(config)
        if err != nil {
                panic(err)
        }
        svc := vke.New(sess)
        reqPublicAccessNetworkConfig := &vke.PublicAccessNetworkConfigForCreateClusterInput{
                Bandwidth:   volcengine.Int32(10),
                BillingType: volcengine.Int32(3),
        }
        reqApiServerPublicAccessConfig := &vke.ApiServerPublicAccessConfigForCreateClusterInput{
                PublicAccessNetworkConfig: reqPublicAccessNetworkConfig,
        }
        reqClusterConfig := &vke.ClusterConfigForCreateClusterInput{
                ApiServerPublicAccessConfig:        reqApiServerPublicAccessConfig,
                ApiServerPublicAccessEnabled:       volcengine.Bool(true),
                ResourcePublicAccessDefaultEnabled: volcengine.Bool(false),
                SubnetIds:                          volcengine.StringSlice([]string{"subnet-id"}),
        }
        reqFlannelConfig := &vke.FlannelConfigForCreateClusterInput{
                MaxPodsPerNode: volcengine.Int32(64),
                PodCidrs:       volcengine.StringSlice([]string{"172.17.**.**/19"}),
        }
        reqPodsConfig := &vke.PodsConfigForCreateClusterInput{
                FlannelConfig:  reqFlannelConfig,
                PodNetworkMode: volcengine.String("Flannel"),
        }
        reqServicesConfig := &vke.ServicesConfigForCreateClusterInput{
                ServiceCidrsv4: volcengine.StringSlice([]string{"172.25.**.**/17"}),
        }
        reqTags := &vke.TagForCreateClusterInput{
                Key:   volcengine.String("tag-key"),
                Value: volcengine.String("tag-value"),
        }
        createClusterInput := &vke.CreateClusterInput{
                ClientToken:             volcengine.String("client-token"),
                ClusterConfig:           reqClusterConfig,
                DeleteProtectionEnabled: volcengine.Bool(false),
                Description:             volcengine.String("cluster-description"),
                KubernetesVersion:       volcengine.String("1.24"),
                Name:                    volcengine.String("cluster-name"),
                PodsConfig:              reqPodsConfig,
                ServicesConfig:          reqServicesConfig,
                Tags:                    []*vke.TagForCreateClusterInput{reqTags},
        }

        // 复制代码运行示例,请自行打印 API 返回值。
        _, err = svc.CreateCluster(createClusterInput)
        if err != nil {
                // 复制代码运行示例,请自行打印 API 错误信息。
                panic(err)
        }
}

更新集群配置(UpdateClusterConfig)

下方示例中参数的详细说明及配置方法,请参见 UpdateClusterConfig

// Example Code generated by Beijing Volcanoengine Technology.
package main

import (
        "github.com/volcengine/volcengine-go-sdk/service/vke"
        "github.com/volcengine/volcengine-go-sdk/volcengine"
        "github.com/volcengine/volcengine-go-sdk/volcengine/credentials"
        "github.com/volcengine/volcengine-go-sdk/volcengine/session"
)

func main() {
        // 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。
        ak, sk, region := "Your AK", "Your SK", "Region"
        config := volcengine.NewConfig().
                WithRegion(region).
                WithCredentials(credentials.NewStaticCredentials(ak, sk, ""))
        sess, err := session.NewSession(config)
        if err != nil {
                panic(err)
        }
        svc := vke.New(sess)
        reqPublicAccessNetworkConfig := &vke.PublicAccessNetworkConfigForUpdateClusterConfigInput{
                Bandwidth:   volcengine.Int32(100),
                BillingType: volcengine.Int32(3),
        }
        reqApiServerPublicAccessConfig := &vke.ApiServerPublicAccessConfigForUpdateClusterConfigInput{
                PublicAccessNetworkConfig: reqPublicAccessNetworkConfig,
        }
        reqClusterConfig := &vke.ClusterConfigForUpdateClusterConfigInput{
                ApiServerPublicAccessConfig:  reqApiServerPublicAccessConfig,
                ApiServerPublicAccessEnabled: volcengine.Bool(true),
        }
        updateClusterConfigInput := &vke.UpdateClusterConfigInput{
                ClientToken:   volcengine.String("client-token"),
                ClusterConfig: reqClusterConfig,
                Description:   volcengine.String("cluster-description"),
                Id:            volcengine.String("cluster-id"),
                Name:          volcengine.String("cluster-name"),
        }

        // 复制代码运行示例,请自行打印 API 返回值。
        _, err = svc.UpdateClusterConfig(updateClusterConfigInput)
        if err != nil {
                // 复制代码运行示例,请自行打印 API 错误信息。
                panic(err)
        }
}

获取集群列表及详情(ListClusters)

下方示例中参数的详细说明及配置方法,请参见 ListClusters

// Example Code generated by Beijing Volcanoengine Technology.
package main

import (
        "github.com/volcengine/volcengine-go-sdk/service/vke"
        "github.com/volcengine/volcengine-go-sdk/volcengine"
        "github.com/volcengine/volcengine-go-sdk/volcengine/credentials"
        "github.com/volcengine/volcengine-go-sdk/volcengine/session"
)

func main() {
        // 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。
        ak, sk, region := "Your AK", "Your SK", "Region"
        config := volcengine.NewConfig().
                WithRegion(region).
                WithCredentials(credentials.NewStaticCredentials(ak, sk, ""))
        sess, err := session.NewSession(config)
        if err != nil {
                panic(err)
        }
        svc := vke.New(sess)
        reqFilter := &vke.FilterForListClustersInput{
                Ids: volcengine.StringSlice([]string{"cluster-id"}),
        }
        listClustersInput := &vke.ListClustersInput{
                Filter:     reqFilter,
                PageNumber: volcengine.Int32(1),
                PageSize:   volcengine.Int32(10),
        }

        // 复制代码运行示例,请自行打印 API 返回值。
        _, err = svc.ListClusters(listClustersInput)
        if err != nil {
                // 复制代码运行示例,请自行打印 API 错误信息。
                panic(err)
        }
}

创建节点池(CreateNodePool)

下方示例中参数的详细说明及配置方法,请参见 CreateNodePool

// Example Code generated by Beijing Volcanoengine Technology.
package main

import (
        "github.com/volcengine/volcengine-go-sdk/service/vke"
        "github.com/volcengine/volcengine-go-sdk/volcengine"
        "github.com/volcengine/volcengine-go-sdk/volcengine/credentials"
        "github.com/volcengine/volcengine-go-sdk/volcengine/session"
)

func main() {
        // 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。
        ak, sk, region := "Your AK", "Your SK", "Region"
        config := volcengine.NewConfig().
                WithRegion(region).
                WithCredentials(credentials.NewStaticCredentials(ak, sk, ""))
        sess, err := session.NewSession(config)
        if err != nil {
                panic(err)
        }
        svc := vke.New(sess)
        reqAutoScaling := &vke.AutoScalingForCreateNodePoolInput{
                DesiredReplicas: volcengine.Int32(1),
                Enabled:         volcengine.Bool(true),
                MaxReplicas:     volcengine.Int32(100),
                MinReplicas:     volcengine.Int32(0),
                Priority:        volcengine.Int32(10),
        }
        reqLabels := &vke.LabelForCreateNodePoolInput{
                Key:   volcengine.String("label-key"),
                Value: volcengine.String("label-value"),
        }
        reqTaints := &vke.TaintForCreateNodePoolInput{
                Effect: volcengine.String("NoSchedule"),
                Key:    volcengine.String("taint-key"),
                Value:  volcengine.String("taint-value"),
        }
        reqKubernetesConfig := &vke.KubernetesConfigForCreateNodePoolInput{
                Cordon: volcengine.Bool(false),
                Labels: []*vke.LabelForCreateNodePoolInput{reqLabels},
                Taints: []*vke.TaintForCreateNodePoolInput{reqTaints},
        }
        reqDataVolumes := &vke.DataVolumeForCreateNodePoolInput{
                Size: volcengine.Int32(20),
                Type: volcengine.String("ESSD_PL0"),
        }
        reqLogin := &vke.LoginForCreateNodePoolInput{
                Password: volcengine.String("***"),
        }
        reqSecurity := &vke.SecurityForCreateNodePoolInput{
                Login:              reqLogin,
                SecurityStrategies: volcengine.StringSlice([]string{"Hids"}),
        }
        reqSystemVolume := &vke.SystemVolumeForCreateNodePoolInput{
                Size: volcengine.Int32(40),
                Type: volcengine.String("ESSD_PL0"),
        }
        reqTags := &vke.TagForCreateNodePoolInput{
                Key:   volcengine.String("tag-key"),
                Value: volcengine.String("tag-value"),
        }
        reqNodeConfig := &vke.NodeConfigForCreateNodePoolInput{
                DataVolumes:        []*vke.DataVolumeForCreateNodePoolInput{reqDataVolumes},
                ImageId:            volcengine.String("image-id"),
                InstanceChargeType: volcengine.String("PostPaid"),
                InstanceTypeIds:    volcengine.StringSlice([]string{"instance-type"}),
                NamePrefix:         volcengine.String("prefix"),
                Security:           reqSecurity,
                SubnetIds:          volcengine.StringSlice([]string{"subnet-id"}),
                SystemVolume:       reqSystemVolume,
                Tags:               []*vke.TagForCreateNodePoolInput{reqTags},
        }
        reqTags1 := &vke.TagForCreateNodePoolInput{
                Key:   volcengine.String("tag-key"),
                Value: volcengine.String("tag-value"),
        }
        createNodePoolInput := &vke.CreateNodePoolInput{
                AutoScaling:      reqAutoScaling,
                ClientToken:      volcengine.String("client-token"),
                ClusterId:        volcengine.String("cluster-id"),
                KubernetesConfig: reqKubernetesConfig,
                Name:             volcengine.String("nodepool-name"),
                NodeConfig:       reqNodeConfig,
                Tags:             []*vke.TagForCreateNodePoolInput{reqTags1},
        }

        // 复制代码运行示例,请自行打印 API 返回值。
        _, err = svc.CreateNodePool(createNodePoolInput)
        if err != nil {
                // 复制代码运行示例,请自行打印 API 错误信息。
                panic(err)
        }
}

更新节点池配置(UpdateNodePoolConfig)

下方示例中参数的详细说明及配置方法,请参见 UpdateNodePoolConfig

// Example Code generated by Beijing Volcanoengine Technology.
package main

import (
        "github.com/volcengine/volcengine-go-sdk/service/vke"
        "github.com/volcengine/volcengine-go-sdk/volcengine"
        "github.com/volcengine/volcengine-go-sdk/volcengine/credentials"
        "github.com/volcengine/volcengine-go-sdk/volcengine/session"
)

func main() {
        // 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。
        ak, sk, region := "Your AK", "Your SK", "Region"
        config := volcengine.NewConfig().
                WithRegion(region).
                WithCredentials(credentials.NewStaticCredentials(ak, sk, ""))
        sess, err := session.NewSession(config)
        if err != nil {
                panic(err)
        }
        svc := vke.New(sess)
        reqAutoScaling := &vke.AutoScalingForUpdateNodePoolConfigInput{
                DesiredReplicas: volcengine.Int32(10),
        }
        updateNodePoolConfigInput := &vke.UpdateNodePoolConfigInput{
                AutoScaling: reqAutoScaling,
                ClientToken: volcengine.String("client-token"),
                ClusterId:   volcengine.String("cluster-id"),
                Id:          volcengine.String("nodepool-id"),
                Name:        volcengine.String("nodepool-name"),
        }

        // 复制代码运行示例,请自行打印 API 返回值。
        _, err = svc.UpdateNodePoolConfig(updateNodePoolConfigInput)
        if err != nil {
                // 复制代码运行示例,请自行打印 API 错误信息。
                panic(err)
        }
}

查询节点池列表及详情(ListNodePools)

下方示例中参数的详细说明及配置方法,请参见 ListNodePools

// Example Code generated by Beijing Volcanoengine Technology.
package main

import (
        "github.com/volcengine/volcengine-go-sdk/service/vke"
        "github.com/volcengine/volcengine-go-sdk/volcengine"
        "github.com/volcengine/volcengine-go-sdk/volcengine/credentials"
        "github.com/volcengine/volcengine-go-sdk/volcengine/session"
)

func main() {
        // 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。
        ak, sk, region := "Your AK", "Your SK", "Region"
        config := volcengine.NewConfig().
                WithRegion(region).
                WithCredentials(credentials.NewStaticCredentials(ak, sk, ""))
        sess, err := session.NewSession(config)
        if err != nil {
                panic(err)
        }
        svc := vke.New(sess)
        reqFilter := &vke.FilterForListNodePoolsInput{
                ClusterIds: volcengine.StringSlice([]string{"cluster-id"}),
                Ids:        volcengine.StringSlice([]string{"nodepool-id"}),
        }
        listNodePoolsInput := &vke.ListNodePoolsInput{
                Filter:     reqFilter,
                PageNumber: volcengine.Int32(1),
                PageSize:   volcengine.Int32(10),
        }

        // 复制代码运行示例,请自行打印 API 返回值。
        _, err = svc.ListNodePools(listNodePoolsInput)
        if err != nil {
                // 复制代码运行示例,请自行打印 API 错误信息。
                panic(err)
        }
}

安装组件(CreateAddon)

下方示例中参数的详细说明及配置方法,请参见 CreateAddon

// Example Code generated by Beijing Volcanoengine Technology.
package main

import (
        "github.com/volcengine/volcengine-go-sdk/service/vke"
        "github.com/volcengine/volcengine-go-sdk/volcengine"
        "github.com/volcengine/volcengine-go-sdk/volcengine/credentials"
        "github.com/volcengine/volcengine-go-sdk/volcengine/session"
)

func main() {
        // 您火山引擎账号的 Access Key ID、Secret Access Key 和业务所在地域(Region)。
        ak, sk, region := "Your AK", "Your SK", "Region"
        config := volcengine.NewConfig().
                WithRegion(region).
                WithCredentials(credentials.NewStaticCredentials(ak, sk, ""))
        sess, err := session.NewSession(config)
        if err != nil {
                panic(err)
        }
        svc := vke.New(sess)
        createAddonInput := &vke.CreateAddonInput{
                ClientToken:    volcengine.String("client-token"),
                ClusterId:      volcengine.String("cluster-id"),
                DeployMode:     volcengine.String("Managed"),
                DeployNodeType: volcengine.String("Node"),
                Name:           volcengine.String("cluster-autoscaler"),
        }

        // 复制代码运行示例,请自行打印 API 返回值。
        _, err = svc.CreateAddon(createAddonInput)
        if err != nil {
                // 复制代码运行示例,请自行打印 API 错误信息。
                panic(err)
        }
}