From 9779b216eac7f750cd9901bf739871883cf75ea3 Mon Sep 17 00:00:00 2001 From: Siye Wang Date: Tue, 5 Nov 2024 14:41:27 +0800 Subject: [PATCH] PANDARIA: Add aliyun ecs client --- clients/aliyun/client.go | 31 ++++++++++++++++++++++++ clients/aliyun/config.go | 50 +++++++++++++++++++++++++++++++++++++++ clients/rancher/client.go | 6 +++++ go.mod | 2 ++ go.sum | 16 +++++++++++++ 5 files changed, 105 insertions(+) create mode 100644 clients/aliyun/client.go create mode 100644 clients/aliyun/config.go diff --git a/clients/aliyun/client.go b/clients/aliyun/client.go new file mode 100644 index 00000000..76ac7383 --- /dev/null +++ b/clients/aliyun/client.go @@ -0,0 +1,31 @@ +package aliyun + +import ( + "github.com/aliyun/alibaba-cloud-sdk-go/sdk" + "github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials" + ecs "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs" + "github.com/rancher/shepherd/pkg/config" +) + +// ECSClient is a struct that wraps the needed ALIYUNECSConfig object, and ecs.ECs which makes the actual calls to aliyun. +type ECSClient struct { + Client *ecs.Client + ClientConfig *ALIYUNECSConfigs +} + +// NewECSClient is a constructor that creates an *ECSClient which a wrapper for a "github.com/aliyun/alibaba-cloud-sdk-go/services/ecs" session and +// the aliyun ecs config. +func NewECSClient() (*ECSClient, error) { + ecsConfig := sdk.NewConfig() + aliyunecsClientConfig := new(ALIYUNECSConfigs) + config.LoadConfig(ConfigurationFileKey, aliyunecsClientConfig) + credential := credentials.NewAccessKeyCredential(aliyunecsClientConfig.ALIYUNECSAccessKeyID, aliyunecsClientConfig.ALIYUNECSSecretAccessKey) + client, err := ecs.NewClientWithOptions(aliyunecsClientConfig.Region, ecsConfig, credential) + if err != nil { + return nil, err + } + return &ECSClient{ + Client: client, + ClientConfig: aliyunecsClientConfig, + }, nil +} diff --git a/clients/aliyun/config.go b/clients/aliyun/config.go new file mode 100644 index 00000000..43b22624 --- /dev/null +++ b/clients/aliyun/config.go @@ -0,0 +1,50 @@ +package aliyun + +// The json/yaml config key for the ALIYUNECSConfig +const ConfigurationFileKey = "aliyunECSConfigs" + +// ALIYUNECSConfigs is the AWS authentication configuration for accessing and launching ec2 instances +type ALIYUNECSConfigs struct { + ALIYUNECSConfig []ALIYUNECSConfig `json:"aliyunECSConfig" yaml:"aliyunECSConfig"` + ALIYUNECSAccessKeyID string `json:"aliyunAccessKeyID" yaml:"aliyunAccessKeyID"` + ALIYUNECSSecretAccessKey string `json:"aliyunSecretAccessKey" yaml:"aliyunSecretAccessKey"` + Region string `json:"region" yaml:"region"` +} + +// ALIYUNECSConfig is the instance-specific configuration needed to launch ec2 instances in AWS +type ALIYUNECSConfig struct { + ImageID string `json:"imageId" yaml:"imageId"` + SSHPassword string `json:"sshPassword" yaml:"sshPassword"` + SSHKeyPairName string `json:"sshKeypair" yaml:"sshKeypair"` + SSHPrivateKeyPath string `json:"sshPrivateKeyPath" yaml:"sshPrivateKeyPath"` + InstanceType string `json:"instanceType" yaml:"instanceType"` + SecurityGroupId string `json:"securityGroupId" yaml:"securityGroupId"` + SecurityGroupName string `json:"securityGroup" yaml:"securityGroup"` + VpcId string `json:"vpcId" yaml:"vpcId"` + VSwitchId string `json:"vswitchId" yaml:"vswitchId"` + Zone string `json:"zone" yaml:"zone"` + PrivateIPOnly bool `json:"privateAddressOnly" yaml:"privateAddressOnly"` + InternetMaxBandwidthOut string `json:"internetMaxBandwidth" yaml:"internetMaxBandwidth"` + InternetChargeType string `json:"internetChargeType" yaml:"internetChargeType"` + RouteCIDR string `json:"routeCidr" yaml:"routeCidr"` + SLBID string `json:"slbId" yaml:"slbId"` + DiskSize string `json:"diskSize" yaml:"diskSize"` + DiskFS string `json:"diskFs" yaml:"diskFs"` + DiskCategory string `json:"diskCategory" yaml:"diskCategory"` + Description string `json:"description" yaml:"description"` + APIEndpoint string `json:"apiEndpoint" yaml:"apiEndpoint"` + SystemDiskCategory string `json:"systemDiskCategory" yaml:"systemDiskCategory"` + SystemDiskSize string `json:"SystemDiskSize" yaml:"SystemDiskSize"` + ResourceGroupId string `json:"resourceGroupId" yaml:"resourceGroupId"` + InstanceChargeType string `json:"instanceChargeType" yaml:"instanceChargeType"` + Period string `json:"period" yaml:"period"` + PeriodUnit string `json:"periodUnit" yaml:"periodUnit"` + SpotStrategy string `json:"spotStrategy" yaml:"spotStrategy"` + SpotPriceLimit string `json:"spotPriceLimit" yaml:"spotPriceLimit"` + SpotDuration string `json:"spotDuration" yaml:"spotDuration"` + AliyunUser string `json:"aliyunUser" yaml:"aliyunUser"` + DockerVersion string `json:"dockerVersion" yaml:"dockerVersion"` + System string `json:"system" yaml:"system"` + OpenPorts []string `json:"openPort" yaml:"openPort"` + Roles []string `json:"roles" yaml:"roles"` +} diff --git a/clients/rancher/client.go b/clients/rancher/client.go index de0d684e..0d6f73ea 100644 --- a/clients/rancher/client.go +++ b/clients/rancher/client.go @@ -11,6 +11,7 @@ import ( "github.com/pkg/errors" "github.com/rancher/norman/httperror" + "github.com/rancher/shepherd/clients/aliyun" "github.com/rancher/shepherd/clients/cvm" frameworkDynamic "github.com/rancher/shepherd/clients/dynamic" "github.com/rancher/shepherd/clients/ec2" @@ -313,6 +314,11 @@ func (c *Client) GetHuaweiClient() (*huawei.Client, error) { return huawei.NewClient() } +// GetAliYunClient is a helper function that instantiates a aliyun ecs client to communicate with the ecs instances on aliyun. +func (c *Client) GetAliYunClient() (*aliyun.ECSClient, error) { + return aliyun.NewECSClient() +} + // GetManagementWatchInterface is a functions used to get a watch.Interface from a resource created by the Management Client. // As is the Management resources do not have a watch.Interface, so therefore, the dynamic Client is used to get the watch.Interface. // The `schemaType` is a string that is found in different Management clients packages. Ex) management.ProjectType diff --git a/go.mod b/go.mod index eb17514c..189ac6ad 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ replace ( require ( github.com/Masterminds/semver/v3 v3.2.1 + github.com/aliyun/alibaba-cloud-sdk-go v1.62.591 github.com/aws/aws-sdk-go v1.50.38 github.com/cnrancher/cce-operator v0.4.6 github.com/creasty/defaults v1.5.2 @@ -140,6 +141,7 @@ require ( github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.1.0-rc2.0.20221005185240-3a7f492d3f1b // indirect github.com/opencontainers/runc v1.1.12 // indirect + github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b // indirect github.com/peterbourgon/diskv v2.0.1+incompatible // indirect github.com/prometheus/client_golang v1.18.0 // indirect github.com/prometheus/client_model v0.5.0 // indirect diff --git a/go.sum b/go.sum index 284cb27b..5bd24c03 100644 --- a/go.sum +++ b/go.sum @@ -765,6 +765,7 @@ github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg6 github.com/Azure/go-autorest v10.8.1+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/HdrHistogram/hdrhistogram-go v1.1.2/go.mod h1:yDgFjdqOqDEKOvasDdhWNXYg9BVp4O+o5f6V/ehm6Oo= github.com/JohnCGriffin/overflow v0.0.0-20211019200055-46fa312c352c/go.mod h1:X0CRv0ky0k6m906ixxpzmDRLvX58TFUKS2eePweuyxk= github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ= github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE= @@ -818,6 +819,8 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE= github.com/alexflint/go-filemutex v0.0.0-20171022225611-72bdc8eae2ae/go.mod h1:CgnQgUtFrFz9mxFNtED3jI5tLDjKlOM+oUF/sTk6ps0= +github.com/aliyun/alibaba-cloud-sdk-go v1.62.591 h1:r30izVxfKxlAct6sq3m+Ehr+SwScQv7qnAoHGCpCJjo= +github.com/aliyun/alibaba-cloud-sdk-go v1.62.591/go.mod h1:CJJYa1ZMxjlN/NbXEwmejEnBkhi0DV+Yb3B2lxf+74o= github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230305170008-8188dc5388df/go.mod h1:pSwJ0fSY5KhvocuWSx4fz3BA8OrA1bQn+K1Eli3BRwM= @@ -1204,6 +1207,7 @@ github.com/gogo/protobuf v1.3.0/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXP github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/goji/httpauth v0.0.0-20160601135302-2da839ab0f4d/go.mod h1:nnjvkQ9ptGaCkuDUx6wNykzzlUixGxvkme+H/lnzb+A= github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= @@ -1417,6 +1421,7 @@ github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOl github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.0.0-20160803190731-bd40a432e4c7/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= +github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= @@ -1427,6 +1432,7 @@ github.com/jonboulle/clockwork v0.2.2/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUB github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= +github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= @@ -1651,6 +1657,8 @@ github.com/opencontainers/selinux v1.6.0/go.mod h1:VVGKuOLlE7v4PJyT6h7mNWvq1rzqi github.com/opencontainers/selinux v1.8.0/go.mod h1:RScLhm78qiWa2gbVCcGkC7tCGdgk3ogry1nUQF8Evvo= github.com/opencontainers/selinux v1.8.2/go.mod h1:MUIHuUEvKB1wtJjQdOyYRgOnLD2xAPP8dBsCoU0KuF8= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= +github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b h1:FfH+VrHHk6Lxt9HdVS0PXzSXFyS2NbZKXv33FYPol0A= +github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b/go.mod h1:AC62GU6hc0BrNm+9RK9VSiwa/EUe1bkIeFORAMcHvJU= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc= @@ -1866,6 +1874,10 @@ github.com/tjfoc/gmsm v1.4.1/go.mod h1:j4INPkHWMrhJb38G+J6W4Tw0AbuN8Thu3PbdVYhVc github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tmc/grpc-websocket-proxy v0.0.0-20201229170055-e5319fda7802/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75/go.mod h1:KO6IkyS8Y3j8OdNO85qEYBsRPuteD+YciPomcXdrMnk= +github.com/uber/jaeger-client-go v2.30.0+incompatible h1:D6wyKGCecFaSRUpo8lCVbaOOb6ThwMmTEbhRwtKR97o= +github.com/uber/jaeger-client-go v2.30.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMWRy2pZqQJSXxYSwNYOkTr/Z6d3Kk= +github.com/uber/jaeger-lib v2.4.1+incompatible h1:td4jdvLcExb4cBISKIpHuGoVXh+dVKhn2Um6rjCsSsg= +github.com/uber/jaeger-lib v2.4.1+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= @@ -1969,6 +1981,8 @@ go.starlark.net v0.0.0-20230525235612-a134d8f9ddca h1:VdD38733bfYv5tUZwEIskMM93V go.starlark.net v0.0.0-20230525235612-a134d8f9ddca/go.mod h1:jxU+3+j+71eXOW14274+SmmuW82qJzl6iZSeqEtTGds= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= +go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo= go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= @@ -2867,6 +2881,7 @@ gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2/go.mod h1:Xk6kEKp8OKb+X14hQBKW gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k= @@ -2890,6 +2905,7 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=