본문으로 건너뛰기

Composition 생성하기

CompositeResourceDefinition (XRD)은 Composite Resource (XR)의 타입과 스키마를 정의합니다. 이것은 Crossplane에 원하는 XR과 그 필드에 대해 알려줍니다. XRD는 CustomResourceDefinition (CRD)과 유사하지만 더 의견이 반영된 구조를 가지고 있습니다. XRD를 생성하는 것은 주로 OpenAPI "구조적 스키마"를 지정하는 것을 포함합니다.

애플리케이션 팀 멤버들이 각자의 네임스페이스에 DynamoDB 테이블을 생성할 수 있도록 하는 정의를 제공하는 것으로 시작하겠습니다. 이 예시에서 사용자는 이름, 키 속성, 그리고 인덱스 이름 필드만 지정하면 됩니다.

전체 XRD 매니페스트 확장하기
~/environment/eks-workshop/modules/automation/controlplanes/crossplane/compositions/composition/definition.yaml
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

apiVersion: apiextensions.crossplane.io/v1
kind: CompositeResourceDefinition
metadata:
name: xdynamodbtables.awsblueprints.io
spec:
group: awsblueprints.io
names:
kind: XDynamoDBTable
plural: xdynamodbtables
claimNames:
kind: DynamoDBTable
plural: dynamodbtables
connectionSecretKeys:
- tableName
versions:
- name: v1alpha1
served: true
referenceable: true
schema:
openAPIV3Schema:
type: object
description: Table is the Schema for the tables API
properties:
spec:
type: object
properties:
resourceConfig:
properties:
deletionPolicy:
description: Defaults to Delete
enum:
- Delete
- Orphan
type: string
name:
type: string
providerConfigName:
type: string
default: aws-provider-config
region:
type: string
default: ""
tags:
additionalProperties:
type: string
description: Key-value map of resource tags.
type: object
required:
- region
type: object
dynamoConfig:
properties:
attribute: #required for hashKey and/or rangeKey
items:
properties:
name: #name of the hashKey and/or rangeKey
type: string
type:
enum:
- B #binary
- N #number
- S #string
type: string
required:
- name
- type
type: object
type: array
hashKey:
type: string
rangeKey:
type: string
billingMode:
type: string
default: PAY_PER_REQUEST
readCapacity:
type: number
writeCapacity:
type: number
globalSecondaryIndex:
items:
properties:
hashKey:
type: string
name:
type: string
rangeKey:
type: string
readCapacity:
type: number
writeCapacity:
type: number
projectionType:
type: string
default: ALL
nonKeyAttributes: #required for gsi
items:
type: string
type: array
type: object
required:
- name
type: array
localSecondaryIndex:
items:
properties:
name:
type: string
rangeKey:
type: string
projectionType:
type: string
nonKeyAttributes: #required for lsi
items:
type: string
type: array
type: object
required:
- name
- rangeKey
- projectionType
- nonKeyAttributes
type: array
required:
- attribute
type: object
required:
- dynamoConfig
status:
type: object
description: TableStatus defines the observed state of Table
properties:
tableArn:
description: Indicates this table's ARN
type: string
tableName:
description: Indicates this table's Name
type: string
required:
- spec

XRD 매니페스트에서 DynamoDB 관련 구성을 살펴볼 수 있습니다.

다음은 DynamoDB 테이블 이름 지정이 필요한 섹션입니다:

                resourceConfig:
properties:
deletionPolicy:
description: Defaults to Delete
enum:
- Delete
- Orphan
type: string
name:
type: string

이 섹션은 DynamoDB 테이블 키 속성 지정을 제공합니다:

                dynamoConfig:
properties:
attribute: #required for hashKey and/or rangeKey
items:
properties:
name: #name of the hashKey and/or rangeKey
type: string
type:
enum:
- B #binary
- N #number
- S #string
type: string
required:
- name
- type
type: object
type: array
hashKey:
type: string
rangeKey:
type: string

이것은 Global Secondary Index 지정에 대한 섹션입니다:

                    globalSecondaryIndex:
items:
properties:
hashKey:
type: string
name:
type: string
rangeKey:
type: string
readCapacity:
type: number
writeCapacity:
type: number
projectionType:
type: string
default: ALL
nonKeyAttributes: #required for gsi
items:
type: string
type: array
type: object
required:
- name
type: array

이것은 Local Secondary Index 지정에 대한 섹션입니다:

                    localSecondaryIndex:
items:
properties:
name:
type: string
rangeKey:
type: string
projectionType:
type: string
nonKeyAttributes: #required for lsi
items:
type: string
type: array
type: object
required:
- name
- rangeKey
- projectionType
- nonKeyAttributes
type: array

Composition은 Composite Resource가 생성될 때 Crossplane이 수행해야 할 작업에 대해 알려줍니다. 각 Composition은 XR과 하나 이상의 Managed Resource 집합 간의 링크를 설정합니다. XR이 생성, 업데이트 또는 삭제되면 관련 Managed Resource들도 그에 따라 생성, 업데이트 또는 삭제됩니다.

관리 리소스 Table을 프로비저닝하는 Composition을 보려면 확장하세요:
~/environment/eks-workshop/modules/automation/controlplanes/crossplane/compositions/composition/table.yaml
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

apiVersion: apiextensions.crossplane.io/v1
kind: Composition
metadata:
name: table.dynamodb.awsblueprints.io
labels:
awsblueprints.io/provider: aws
awsblueprints.io/environment: dev
spec:
writeConnectionSecretsToNamespace: crossplane-system
compositeTypeRef:
apiVersion: awsblueprints.io/v1alpha1
kind: XDynamoDBTable
patchSets:
- name: common-fields
patches:
- type: FromCompositeFieldPath
fromFieldPath: spec.resourceConfig.providerConfigName
toFieldPath: spec.providerConfigRef.name
- type: FromCompositeFieldPath
fromFieldPath: spec.name
toFieldPath: metadata.annotations[crossplane.io/external-name]
- type: FromCompositeFieldPath
fromFieldPath: metadata.name
toFieldPath: metadata.annotations[crossplane.io/external-name]
transforms:
- type: string
string:
type: Regexp
regexp:
match: ^(.*?)-crossplane
resources:
- name: table
connectionDetails:
- type: FromFieldPath
name: tableName
fromFieldPath: status.atProvider.id
base:
apiVersion: dynamodb.aws.upbound.io/v1beta1
kind: Table
spec:
forProvider:
writeConnectionSecretToRef:
name: cartsdynamo
namespace: crossplane-system
region: ""
providerConfigRef:
name: aws-provider-config
patches:
- type: PatchSet
patchSetName: common-fields
- type: FromCompositeFieldPath
fromFieldPath: spec.dynamoConfig.attribute
toFieldPath: spec.forProvider.attribute
policy:
mergeOptions:
appendSlice: true
keepMapValues: true
- type: FromCompositeFieldPath
fromFieldPath: spec.resourceConfig.tags
toFieldPath: spec.forProvider.tags
policy:
mergeOptions:
keepMapValues: true
- type: FromCompositeFieldPath
fromFieldPath: spec.dynamoConfig.attribute[0].name
toFieldPath: spec.forProvider.hashKey
- type: FromCompositeFieldPath
fromFieldPath: spec.dynamoConfig.billingMode
toFieldPath: spec.forProvider.billingMode
- type: FromCompositeFieldPath
fromFieldPath: spec.dynamoConfig.rangeKey
toFieldPath: spec.forProvider.rangeKey
- type: FromCompositeFieldPath
fromFieldPath: spec.dynamoConfig.readCapacity
toFieldPath: spec.forProvider.readCapacity
- type: FromCompositeFieldPath
fromFieldPath: spec.dynamoConfig.writeCapacity
toFieldPath: spec.forProvider.writeCapacity
- type: FromCompositeFieldPath
fromFieldPath: spec.dynamoConfig.globalSecondaryIndex[0].name
toFieldPath: spec.forProvider.globalSecondaryIndex[0].name
- type: FromCompositeFieldPath
fromFieldPath: spec.dynamoConfig.attribute[1].name
toFieldPath: spec.forProvider.globalSecondaryIndex[0].hashKey
- type: FromCompositeFieldPath
fromFieldPath: spec.dynamoConfig.globalSecondaryIndex[0].projectionType
toFieldPath: spec.forProvider.globalSecondaryIndex[0].projectionType
policy:
mergeOptions:
keepMapValues: true
- type: FromCompositeFieldPath
fromFieldPath: spec.dynamoConfig.localSecondaryIndex
toFieldPath: spec.forProvider.localSecondaryIndex
policy:
mergeOptions:
keepMapValues: true
- type: ToCompositeFieldPath
fromFieldPath: status.atProvider.id
toFieldPath: status.tableName
- type: ToCompositeFieldPath
fromFieldPath: status.atProvider.arn
toFieldPath: status.tableArn

이것을 여러 부분으로 나누어 살펴보면 더 잘 이해할 수 있습니다.

이 섹션은 XR의 spec.name 필드를 Managed Resource의 external-name 어노테이션에 매핑하며, Crossplane은 이를 사용하여 AWS의 실제 DynamoDB 테이블 이름을 설정합니다.

        - type: FromCompositeFieldPath
fromFieldPath: spec.name
toFieldPath: metadata.annotations[crossplane.io/external-name]

이것은 XR에서 관리되는 DynamoDB 리소스로 모든 속성 정의를 전송하여, Crossplane이 적절한 데이터 타입으로 테이블 스키마를 생성할 수 있게 합니다.

        - type: FromCompositeFieldPath
fromFieldPath: spec.dynamoConfig.attribute
toFieldPath: spec.forProvider.attribute
policy:
mergeOptions:
appendSlice: true
keepMapValues: true

이것은 XR의 첫 번째 속성을 DynamoDB 테이블의 기본 키 구조의 파티션 키(해시 키)로 매핑합니다.

        - type: FromCompositeFieldPath
fromFieldPath: spec.dynamoConfig.attribute[0].name
toFieldPath: spec.forProvider.hashKey

이것은 XR 사양에서 관리 리소스로 GSI 이름을 전송하여, Crossplane이 DynamoDB 테이블에 명명된 Global Secondary Index를 생성할 수 있게 합니다.

        - type: FromCompositeFieldPath
fromFieldPath: spec.dynamoConfig.globalSecondaryIndex[0].name
toFieldPath: spec.forProvider.globalSecondaryIndex[0].name

이것은 XR에서 관리 리소스로 LSI 구성을 매핑하여, Crossplane이 지정된 이름과 속성으로 Local Secondary Index를 프로비저닝할 수 있게 합니다.

        - type: FromCompositeFieldPath
fromFieldPath: spec.dynamoConfig.localSecondaryIndex
toFieldPath: spec.forProvider.localSecondaryIndex

이제 이 구성을 EKS 클러스터에 적용해 보겠습니다:

~$kubectl apply -k ~/environment/eks-workshop/modules/automation/controlplanes/crossplane/compositions/composition
compositeresourcedefinition.apiextensions.crossplane.io/xdynamodbtables.awsblueprints.io created
composition.apiextensions.crossplane.io/table.dynamodb.awsblueprints.io created

이러한 리소스가 준비되면, DynamoDB 테이블 생성을 위한 Crossplane Composition을 성공적으로 설정한 것입니다. 이 추상화는 애플리케이션 개발자들이 기본 AWS 관련 세부 사항을 이해할 필요 없이 표준화된 DynamoDB 테이블을 프로비저닝할 수 있게 해줍니다.