有奖:语音产品征文挑战赛等你来> HOT
本文介绍如何创建一个包含安全组和对应规则的组合资源XSecurityGroup。为了构建XSecurityGroup资源,需要创建并定义xrd.yamlcomposition.yamlxsecuritygroup.yaml,然后执行这三个配置文件。

定义 XRD

首先需要创建自定义 API,即为后面创建的复合资源,定义该复合资源对应的参数和类型。
xrd.yaml 定义安全组的入站和出站配置。其中 sgIngresssgEgress 为必选参数。
# xrd.yaml
apiVersion: apiextensions.crossplane.io/v1
kind: CompositeResourceDefinition
metadata:
name: xsecuritygroups.crd.tencentcloud.crossplane.io
spec:
group: crd.tencentcloud.crossplane.io
names:
kind: XSecurityGroup
plural: xsecuritygroups
versions:
- name: v1alpha1
served: true
referenceable: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
sgIngress: # 定义入站规则
type: array
items:
type: string
sgEgress: # 定义出站规则
type: array
items:
type: string
required:
- sgIngress
- sgEgress
status:
description: A Status represents the observed state
properties:
share:
description: Freeform field containing status information
type: object
x-kubernetes-preserve-unknown-fields: true
type: object

定义 Composition

接着,在 composition.yaml 中定义复合资源,包括一个安全组和安全组规则。
用户可在资源创建后,通过 status.share.sgId 获取安全组 Id。
# composition.yaml
apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: composition-sg
spec:
compositeTypeRef:
apiVersion: crd.tencentcloud.crossplane.io/v1alpha1
kind: XSecurityGroup
resources:
# 定义安全组
- name: sg
base:
apiVersion: vpc.tencentcloud.crossplane.io/v1alpha1
kind: SecurityGroup
spec:
forProvider:
name: test-crossplane-sg # 安全组名称
description: Security Group for the XSecurityGroup.
patches:
- type: ToCompositeFieldPath
fromFieldPath: status.atProvider.id
toFieldPath: status.share.sgId

# 定义安全组规则
- name: sgrules
base:
apiVersion: vpc.tencentcloud.crossplane.io/v1alpha1
kind: SecurityGroupLiteRule
spec:
patches:
- fromFieldPath: status.share.sgId
toFieldPath: spec.forProvider.securityGroupId
- fromFieldPath: spec.sgIngress
toFieldPath: spec.forProvider.ingress
- fromFieldPath: spec.sgEgress
toFieldPath: spec.forProvider.egress
- type: ToCompositeFieldPath
fromFieldPath: status.atProvider.id
toFieldPath: status.share.sgruleId

创建 XSecurityGroup 资源

现在,在 xsecuritygroup.yaml 中为复合资源设置具体的安全组规则。
# xsecuritygroup.yaml
apiVersion: crd.tencentcloud.crossplane.io/v1alpha1
kind: XSecurityGroup
metadata:
name: xsg-example
spec:
# 入站规则
sgIngress:
- "DROP#0.0.0.0/0#ALL#ALL" # 根据需求设置规则
# 出站规则
sgEgress:
- "DROP#0.0.0.0/0#ALL#ALL" # 根据需求设置规则
使用 kubectl apply 命令依次执行上述配置文件,完成资源的创建。
kubectl apply -f xrd.yaml
kubectl apply -f composition.yaml
kubectl apply -f xsecuritygroup.yaml
执行结果如下:
创建了前缀为 xsg-example- 的安全组实例。
kubectl get managed
NAME READY SYNCED EXTERNAL-NAME AGE
securitygroupliterule.vpc.tencentcloud.crossplane.io/xsg-example-bks7q True True sg-3o46faav 5h48m

NAME READY SYNCED EXTERNAL-NAME AGE
securitygroup.vpc.tencentcloud.crossplane.io/xsg-example-j6fxz True True sg-3o46faav 5h48m
此时,您可以在控制台查看创建的安全组资源