1
:KubeVela 社区 KubeVela 目前已经支持了 AWS、Azure、GCP、阿里云、腾讯云、百度云、UCloud 等云厂商,也提供了简单快捷的命令行工具[1]引入云服务商的云资源,但是在 KubeVela 里一个。..

KubeVela 目前已经支持了 AWS、Azure、GCP、阿里云、腾讯云、百度云、UCloud 等云厂商,也提供了简单快捷的命令行工具[1]引入云服务商的云资源,但是在 KubeVela 里一个一个地支持云服务商的云资源不利于快速满足用户对于云资源的需求,本文提供了一个方案,用不到 100 行代码快速引入 AWS 前 50 最受欢迎的云资源。
同时,我们也期望用户受到本文的启发,贡献其他云服务商的云资源。

一、KubeVela快速引入AWS云资源

1.1 AWS 最受欢迎的云资源在哪里

1
Terraform 官网提供了各个云服务商的 Terraform modules,比如 AWS 的云资源 Terraform modules[2]。其中,云资源按照受欢迎的使用程度(下载量)排序,比如 AWS VPC 下载量为 1870 万次。 

通过简单分析,我们发现 AWS 前 50 Terraform modules 的数据可以通过请求 https://registry.terraform.io/v2/modules?filter%5Bprovider%5D=aws&include=latest-version&page%5Bsize%5D=50&page%5Bnumber%5D=1 获取。

1.2 开始之前

代码接受两个用户传入参数: • provider 的名称 • 该 provider 对应的 Terraform Modules 的 URL

1
对于 AWS 来说,Provider名称为 “aws”,对应的 Terraform modules 为 Terraform Modules json 格式接口[3](即在 Terraform Registry[4]中搜索 provider 为 aws 时最受欢迎的 50 种云资源)。 在执行代码之前需要确认 providerName(aws) 和 Modules 链接无误。 

1.3 执行代码

那么你就可以通过以下 100 行左右的代码(文件名 gen.go)来批量地快速引入 AWS 最受欢迎的前 50 种云资源。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/pkg/errors"
)
type TFDownload struct {
Data []DataItem `json:"data"`
Included []IncludedItem `json:"included"`
}
type IncludedItem struct {
Id string `json:"id"`
Attributes Attributes `json:"attributes"`
}
type DataItem struct {
Attributes Attributes `json:"attributes"`
Relationships Relationships `json:"relationships"`
}
type Relationships struct {
LatestVersion RelationshipLatestVersion `json:"latest-version"`
}
type RelationshipLatestVersion struct {
Data RelationshipData `json:"data"`
}
type RelationshipData struct {
Id string `json:"id"`
}
var errNoVariables = errors.New("failed to find main.tf or variables.tf in Terraform configurations")
type Attributes struct {
Name string `json:"name"`
Downloads int `json:"downloads"`
Source string `json:"source"`
Description string `json:"description"`
Verified bool `json:"verified"`
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Please provide the cloud provider name and an official Terraform modules URL")
os.Exit(1)
}
providerName := os.Args[1]
terraformModulesUrl := os.Args[2]
resp, err := http.Get(terraformModulesUrl)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
}
var modules TFDownload
if err := json.Unmarshal(body, &modules); err != nil {
fmt.Println(err.Error())
}
if _, err = os.Stat(providerName); err == nil {
if err := os.RemoveAll(providerName); err != nil {
}
fmt.Printf("Successfully deleted existed directory %s\n", providerName)
}
if _, err = os.Stat(providerName); os.IsNotExist(err) {
if err := os.Mkdir(providerName, 0755); err != nil {
if !os.IsExist(err) {
}
fmt.Printf("Successfully created directory %s\n", providerName)
}
for _, module := range modules.Data {
var description string
for _, attr := range modules.Included {
if module.Relationships.LatestVersion.Data.Id == attr.Id {
description = attr.Attributes.Description
}
if description == "" {
description = strings.ToUpper(providerName) + " " + strings.Title(module.Attributes.Name)
}
outputFile := fmt.Sprintf("%s/terraform-%s-%s.yaml", providerName, providerName, module.Attributes.Name)
if _, err := os.Stat(outputFile); !os.IsNotExist(err) {
continue
}
if providerName == "aws" && (module.Attributes.Name == "rds" || module.Attributes.Name == "s3-bucket" ||
module.Attributes.Name == "subnet" || module.Attributes.Name == "vpc") {
}
if err := generateDefinition(providerName, module.Attributes.Name, module.Attributes.Source, "", description); err != nil {
}
func generateDefinition(provider, name, gitURL, path, description string) error {
defYaml := filepath.Join(provider, fmt.Sprintf("terraform-%s-%s.yaml", provider, name))
cmd := fmt.Sprintf("vela def init %s --type component --provider %s --git %s.git --desc \"%s\" -o %s",
name, provider, gitURL, description, defYaml)
if path != "" {
cmd = fmt.Sprintf("%s --path %s", cmd, path)
}
fmt.Println(cmd)
stdout, err := exec.Command("bash", "-c", cmd).CombinedOutput()
return errors.Wrap(err, string(stdout))
}
fmt.Println(string(stdout))
return nil

`执行命令:```bash
go run gen.go aws "https://registry.terraform.io/v2/modules?filter%5Bprovider%5D=aws&include=latest-version&page%5Bsize%5D=50&page%5Bnumber%5D=1"

1.4 代码简要说明

解析云资源数据

访问用户传入的 URL,将返回的 json 数据解析为 Go 中的结构体。
资源对应的 json 格式如下:

{

1
"data": [
{
  "type": "modules",
  "id": "23",
1
"attributes": {
    "downloads": 18440513,
    "full-name": "terraform-aws-modules/vpc/aws",
    "name": "vpc",
    "namespace": "terraform-aws-modules",
    "owner-name": "",
    "provider-logo-url": "/images/providers/aws.png",
    "provider-name": "aws",
    "source": "https://github.com/terraform-aws-modules/terraform-aws-vpc",
    "verified": true
  },
1
2
3
"relationships": {
"latest-version": {
"data": {
        "id": "142143",
        "type": "module-versions"
      }
  },
1
"links": {
    "self": "/v2/modules/23"
  }
},
...

],

1
"included": [
{
  "type": "module-versions",
  "id": "36806",
    "created-at": "2020-01-03T11:35:36Z",
    "description": "Terraform module Terraform module for creating AWS IAM Roles with heredocs",
    "downloads": 260030,
    "published-at": "2020-02-06T06:26:08Z",
    "source": "",
    "tag": "v2.0.0",
    "updated-at": "2022-02-22T00:45:44Z",
    "version": "2.0.0"
  },
    "self": "/v2/module-versions/36806"
  }
},
...

],

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  
在 Modules 对应的 json 数据中,我们只关心两个键值对,即:
• data:包含 Modules 名称及属性的列表 • Included:筛选出的特定版本的 Modules 具体信息
其中,对于 data 中的每个 Module 元素,解析它的属性,Id 和 relationship 中的 latest-version 对应的 id;对于 Included 中的每个 Module 版本元素,解析它的属性和Id。
属性又解析如下五项:
• Name • Downloads • Source • Description • Verified
结构体定义在结构体 TFDownload 中,通过 http 库取 json 数据,再通过 json.Unmarshal 解析出 Terraform modules 的结构体。
##### 批量生产云资源
###### 1. 新建目录,生成资源所需文件
解析完毕后,在当前目录下新建文件夹,文件夹命名为 provider 名称。 遍历解析后的 data,对于其中每个 Module 元素,执行下述操作,为其生成相应配置文件,定义和相应文档。
###### 2. 生成定义文件
通过下述 vela 指令从模块对应的 github 仓库读取相应信息生成定义文件。

vela def init {
ModuleName} --type component --provider {
providerName} --git {
gitURL} --desc {
description} -o {
yamlFileName}


指令中需要填入的几项由解析好的 Module 结构体传入。

1
• gitURL: {Module.Attributes.Source}.git 

• description: 如果 Included 中存在元素 ID 与模块 relationship 中 latest-version 对应 ID 相同,则 description 为 Included 中对应元素属性的 description;否则 description 为 providerName 与模块名称的拼接

1
• yamlFileName:terraform-{providerName}-{Module.Attributes.Name}.yaml 

1.5 你也来试试

还有不少云服务商也提供了丰富的 Terraform modules,比如
GCP: https://registry.terraform.io/namespaces/terraform-google-modules
阿里云: https://registry.terraform.io/namespaces/terraform-alicloud-modules
你要不要也为 KubeVela 引入你正在使用的、或喜欢的云服务商的云资源?

1.6 相关链接

1
[1] 简单快捷的命令行工具 

https://kubevela.io/docs/next/platform-engineers/components/component-terraform

1
[2] AWS 的云资源Terraform modules 

https://registry.terraform.io/namespaces/terraform-aws-modules

1
[3] Terraform Modules json 格式接口 

https://registry.terraform.io/v2/modules?filter%5Bprovider%5D=aws&include=latest-version&page%5Bsize%5D=50&page%5Bnumber%5D=1

1
[4] Terraform Registry 

https://aregistry.terraform.io/

本文标题: KubeVela如何用100行代码

发布时间: 2024年01月13日 00:00

最后更新: 2025年12月30日 08:54

原始链接: https://haoxiang.eu.org/59c230c2/

版权声明: 本文著作权归作者所有,均采用CC BY-NC-SA 4.0许可协议,转载请注明出处!

× 喜欢就赞赏一下呗!
打赏二维码