利用 Crossplane 的 XRD 特性,我们可以通过 Composition 来定义自定义的组合资源,将需要的资源组合在一起部署和管理;通过在 XRD 中定义和配置对应的 API,提供该组合资源对外暴露的参数。
本文介绍如何创建一个包含 CVM、VPC 和对应子网的组合资源
XCvm
。为了构建XCvm
资源,需要创建并定义xrd.yaml
、composition.yaml
、xcvm.yaml
,然后执行这三个配置文件。定义 XRD
首先我们需要创建自定义 API,即为后面创建的复合资源,定义该复合资源对应的参数和类型。
我们在
xrd.yaml
定义了vpcName
, subnetName
, availabilityZone
作为必选参数,用户使用该复合资源时,必须提供这三个参数,用于创建 VPC 和对应子网。其余如 imageId
,availabilityZone
,instanceType
,instanceChargeType
为可选参数。# xrd.yamlapiVersion: apiextensions.crossplane.io/v1kind: CompositeResourceDefinitionmetadata:name: xcvms.crd.tencentcloud.crossplane.iospec:group: crd.tencentcloud.crossplane.ionames:kind: XCvmplural: xcvmsversions:- name: v1alpha1served: truereferenceable: trueschema:openAPIV3Schema:type: objectproperties:spec:type: objectproperties: # 参数定义vpcName:type: stringsubnetName:type: stringimageId:type: stringdefault: "img-9qrfy1xt" # 默认值availabilityZone:type: stringinstanceType:type: stringinstanceChargeType:type: stringrequired: # 必选参数- vpcName- subnetName- availabilityZonestatus:description: A Status represents the observed stateproperties:share:description: Freeform field containing status informationtype: objectx-kubernetes-preserve-unknown-fields: truetype: object
定义 Composition
接着,在
composition.yaml
中定义复合资源,包括一个 VPC,一个子网,和在该 VPC 下的 CVM 实例。用户可在资源创建后,通过
status.share
获取 CVM 的实例 Id。# composition.yamlapiVersion: apiextensions.crossplane.io/v1kind: Compositionmetadata:name: composition-cvmspec:compositeTypeRef:apiVersion: crd.tencentcloud.crossplane.io/v1alpha1kind: XCvmresources:- name: vpcbase:apiVersion: vpc.tencentcloud.crossplane.io/v1alpha1kind: VPCspec:forProvider:cidrBlock: "10.1.0.0/16"patches:- fromFieldPath: spec.vpcNametoFieldPath: spec.forProvider.name- type: ToCompositeFieldPathfromFieldPath: status.atProvider.idtoFieldPath: status.share.vpcId- name: subnetbase:apiVersion: vpc.tencentcloud.crossplane.io/v1alpha1kind: Subnetspec:forProvider:cidrBlock: "10.1.2.0/24"patches:- fromFieldPath: status.share.vpcId # 使用上面创建的vpctoFieldPath: spec.forProvider.vpcId- fromFieldPath: spec.subnetNametoFieldPath: spec.forProvider.name- fromFieldPath: spec.availabilityZonetoFieldPath: spec.forProvider.availabilityZone- type: ToCompositeFieldPathfromFieldPath: status.atProvider.idtoFieldPath: status.share.subnetId- name: cvmbase:apiVersion: cvm.tencentcloud.crossplane.io/v1alpha1kind: Instancespec:forProvider:instanceName: test-crossplane-cvm# imageId: "img-9qrfy1xt"patches:- fromFieldPath: status.share.vpcId # 使用上面创建的vpctoFieldPath: spec.forProvider.vpcId- fromFieldPath: status.share.subnetId # 使用上面创建的subnettoFieldPath: spec.forProvider.subnetId- fromFieldPath: spec.availabilityZonetoFieldPath: spec.forProvider.availabilityZone- fromFieldPath: spec.imageIdtoFieldPath: spec.forProvider.imageId- type: ToCompositeFieldPathfromFieldPath: status.atProvider.idtoFieldPath: status.share.cvmId # cvm 实例Id
创建 XCvm 资源
在
xcvm.yaml
中为复合资源设置子网名称和可用区。#xcvm
.yamlapiVersion: crd.tencentcloud.crossplane.io/v1alpha1kind: XCvmmetadata:name: xcvm-example # 复合资源前缀spec:vpcName: xvpcName # 指定VPC和子网名称subnetName: xsubnetNameavailabilityZone: ap-guangzhou-3 # 指定可用区# imageId: img-xxxx # 使用指定镜像 Id
使用
kubectl apply
命令依次执行上述配置文件,完成资源的创建。kubectl
apply -f xrd.yamlkubectl
apply -f composition.yamlkubectl
apply -fxcvm
.yaml
执行结果如下:
创建了前缀为
xcvm-example-
的 VPC、子网和 CVM 实例。❯kubectl
get managedNAME READY SYNCED EXTERNAL-NAME AGEinstance.cvm.tencentcloud.crossplane.io/xcvm-example-75vc2 True True ins-7d5rb9us 3m4sNAME READY SYNCED EXTERNAL-NAME AGEsubnet.vpc.tencentcloud.crossplane.io/xcvm-example-zlshp True True subnet-2hmfd8go 5mNAME READY SYNCED EXTERNAL-NAME AGEvpc.vpc.tencentcloud.crossplane.io/xcvm-example-rd5gq True True vpc-3ofo55dx 5m
![](https://qcloudimg.tencent-cloud.cn/image/document/2fe4b9d1d27f4d534566698239d23982.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/2fe4b9d1d27f4d534566698239d23982.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/4b20cbcdef24c6949484a47caeda3314.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/4b20cbcdef24c6949484a47caeda3314.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/03a92f090411c76cae2487ef1c30dee7.png)
![](https://qcloudimg.tencent-cloud.cn/image/document/03a92f090411c76cae2487ef1c30dee7.png)