メインコンテンツまでスキップ

CartsStack RGD の定義

前のページで紹介した CartsStack RGD、つまりブループリントを記述しましょう。その2つの主要なセクションは schema(ユーザーが提供する入力)と resources(kro がそれらから作成するグラフ)です:

~/environment/eks-workshop/modules/fastpaths/eks-capabilities/kro/rgd/cartsstack-rgd.yaml
apiVersion: kro.run/v1alpha1
kind: ResourceGraphDefinition
metadata:
name: cartsstack
spec:
schema:
apiVersion: v1alpha1
kind: CartsStack
group: kro.run
spec:
tableName: string | required=true
namespace: string | required=true
image: string | default="public.ecr.aws/aws-containers/retail-store-sample-cart:1.2.1"
replicas: integer | default=1
status:
tableArn: ${table.status.ackResourceMetadata.arn}
resources:
- id: ns
template:
apiVersion: v1
kind: Namespace
metadata:
name: ${schema.spec.namespace}
- id: table
template:
apiVersion: dynamodb.services.k8s.aws/v1alpha1
kind: Table
metadata:
name: items
namespace: ${schema.spec.namespace}
spec:
tableName: ${schema.spec.tableName}
billingMode: PAY_PER_REQUEST
attributeDefinitions:
- attributeName: id
attributeType: "S"
- attributeName: customerId
attributeType: "S"
keySchema:
- attributeName: id
keyType: HASH
globalSecondaryIndexes:
- indexName: idx_global_customerId
keySchema:
- attributeName: customerId
keyType: HASH
- attributeName: id
keyType: RANGE
projection:
projectionType: "ALL"
- id: sa
template:
apiVersion: v1
kind: ServiceAccount
metadata:
name: carts
namespace: ${schema.spec.namespace}
- id: config
template:
apiVersion: v1
kind: ConfigMap
metadata:
name: carts
namespace: ${schema.spec.namespace}
data:
RETAIL_CART_PERSISTENCE_PROVIDER: dynamodb
RETAIL_CART_PERSISTENCE_DYNAMODB_TABLE_NAME: ${schema.spec.tableName}
- id: deployment
template:
apiVersion: apps/v1
kind: Deployment
metadata:
name: carts
namespace: ${schema.spec.namespace}
labels:
app.kubernetes.io/created-by: eks-workshop
app.kubernetes.io/type: app
spec:
replicas: ${schema.spec.replicas}
selector:
matchLabels:
app.kubernetes.io/name: carts
app.kubernetes.io/instance: carts
app.kubernetes.io/component: service
template:
metadata:
labels:
app.kubernetes.io/name: carts
app.kubernetes.io/instance: carts
app.kubernetes.io/component: service
app.kubernetes.io/created-by: eks-workshop
spec:
serviceAccountName: ${sa.metadata.name}
securityContext:
fsGroup: 1000
containers:
- name: carts
image: ${schema.spec.image}
imagePullPolicy: IfNotPresent
env:
- name: JAVA_OPTS
value: -XX:MaxRAMPercentage=75.0 -Djava.security.egd=file:/dev/urandom
envFrom:
- configMapRef:
name: ${config.metadata.name}
ports:
- name: http
containerPort: 8080
protocol: TCP
readinessProbe:
httpGet:
path: /actuator/health/readiness
port: 8080
initialDelaySeconds: 15
periodSeconds: 3
livenessProbe:
httpGet:
path: /actuator/health/liveness
port: 8080
initialDelaySeconds: 45
periodSeconds: 3
resources:
limits:
memory: 1Gi
requests:
cpu: 250m
memory: 1Gi
securityContext:
capabilities:
drop:
- ALL
readOnlyRootFilesystem: true
runAsNonRoot: true
runAsUser: 1000
volumeMounts:
- mountPath: /tmp
name: tmp-volume
volumes:
- name: tmp-volume
emptyDir:
medium: Memory
- id: service
template:
apiVersion: v1
kind: Service
metadata:
name: carts
namespace: ${schema.spec.namespace}
labels:
app.kubernetes.io/created-by: eks-workshop
spec:
type: ClusterIP
ports:
- port: 80
targetPort: http
protocol: TCP
name: http
selector:
app.kubernetes.io/name: carts
app.kubernetes.io/instance: carts
app.kubernetes.io/component: service
A

spec.schema は、生の OpenAPI ではなく、kro の SimpleSchema 構文(string | required=true の形式)を使用して、ユーザー向け CR の形状を宣言します。image や replicas のようなオプションフィールドはデフォルト値を取得するため、最小限のインスタンスは tableName と namespace のみを設定します。

B

spec.resources は kro が作成するグラフです。各エントリには id(他のリソースからその出力を参照するために使用)と template(実際のマニフェスト)があります。ユーザーの入力を取り込む ${schema.spec.X} 参照と、リソース間の ${table.status...arn} / ${sa.metadata.name} 参照に注目してください。これが kro がそれらを作成する順序を推論する方法です。

RGD を適用します:

~$kubectl apply -k ~/environment/eks-workshop/modules/fastpaths/eks-capabilities/kro/rgd
resourcegraphdefinition.kro.run/cartsstack created

kro は RGD を同期的に検証します: 参照するリソースの実際の Kubernetes スキーマに対してすべての ${...} 式を型チェックし、循環依存を検出します。何か問題がある場合、適用は説明的なエラーですぐに失敗します。

RGD が Active に達するまで待ちます。この時点で、kro はクラスタ内に CartsStack CRD を動的に生成して登録しています:

~$kubectl wait rgd cartsstack --for=jsonpath='{.status.state}'=Active --timeout=60s
resourcegraphdefinition.kro.run/cartsstack condition met

新しい CartsStack kind が現在、Kubernetes API のファーストクラスであることを確認します:

~$kubectl api-resources --api-group=kro.run | grep -E 'NAME|cartsstacks'
NAME                       SHORTNAMES   APIVERSION         NAMESPACED   KIND
cartsstacks                             kro.run/v1alpha1   true         CartsStack
備考

cartsstacks は、それを定義する RGD がクラスタースコープであっても namespaced です。RGD はクラスター全体に存在します。インスタンスは常に特定の Namespace 内に存在します。

スキーマが存在します。次にインスタンスを適用します。