ข้ามไปยังเนื้อหา
AggregaAggregaAggregaAggrega
เลือกภาษา: ไทย
หน้านี้ถูกแปลด้วยเครื่อง อ่านต้นฉบับภาษาอังกฤษ

งานที่รันตามคำขอ: Aggregate เดียว สี่เป้าหมาย

งานที่มี core/manual-trigger ปรับใช้โดยไม่เริ่มรันบน Kubernetes, Docker Compose, Terraform และ CloudFormation เคียงกับงานที่ไม่แนบอะไรเลย พร้อมคำสั่งที่เริ่มการรันบนแต่ละเป้าหมาย

งานบางอย่างรันจนเสร็จเมื่อมีคนขอเท่านั้น เช่น การแก้ข้อมูลครั้งเดียว หรือการสำรองตามคำขอ core/manual-trigger คือ Feature สำหรับกรณีนั้น เมื่อแนบกับ core/compute/task ที่ไม่มีตารางเวลา จะปรับใช้งานโดยไม่เริ่มรัน และแต่ละการรันคือการรันที่มีคนเริ่มด้วยมือ ด้วยคำสั่งของเป้าหมายนั้นเอง

พื้นที่ทำงานนี้บิลด์ Aggregate เดียวสำหรับสี่เป้าหมาย — Kubernetes ผ่าน Helm, Docker Compose, Terraform และ CloudFormation — และแสดงบนแต่ละเป้าหมายว่าการแนบเปลี่ยนอะไรในเอาต์พุตที่เรนเดอร์ และคำสั่งที่เริ่มการรัน งานที่สองไม่มีการแนบ จึงเห็นความต่างได้ในการบิลด์เดียวกัน Releases อีกสองรายการคาดว่าจะหยุด และหน้านี้อ้างอิงสิ่งที่แต่ละรายการพิมพ์

สิ่งที่ตัวอย่างนี้บิลด์

อินสแตนซ์Component ของแค็ตตาล็อกการแนบสิ่งที่เริ่มการรัน
data-fixcore/compute/taskcore/manual-triggerมีคนเริ่มด้วยมือบนทุกเป้าหมาย
reportcore/compute/taskไม่มีตารางเวลาของงาน หรือบน Docker Compose คือการเริ่มสแตก

การแนบอยู่ใน Aggregate ข้างงานที่มันเปลี่ยน: การรันตามคำขอเป็นส่วนหนึ่งของสิ่งที่ data-fix เป็น นักพัฒนาจึงเขียนไว้ และ Feature ไม่รับค่าใดและไม่รับ infra Release ไม่เขียนสิ่งใดให้การแนบนี้

ตารางเวลาของ report เป็นพารามิเตอร์ report_schedule ที่ Aggregate ประกาศด้วยค่าเริ่มต้นว่าง และอ่านเข้าไปในค่า schedule ของงาน:

aggregate.yaml
spec:
parameters:
report_schedule:
type: string
default: ""
[… omitted: the parameter's description]
components:
[… omitted: data-fix with its attachment, and the comment above each task]
report:
type: core/compute/task
values:
schedule: "{{ .parameters.report_schedule }}"

เป็นพารามิเตอร์เพราะทั้งสี่เป้าหมายไม่ตรงกันเรื่องงานที่ไม่มีการแนบ Kubernetes รันงานตามตารางเวลาเป็น CronJob หรือรันครั้งเดียวตอนปรับใช้หากไม่มีตารางเวลา Docker Compose ไม่มีตัวจัดตารางเวลา จึงรันงานหนึ่งครั้งทุกครั้งที่สแตกเริ่ม และปฏิเสธตารางเวลา บน AWS ไม่มีสิ่งใดรันงาน Amazon ECS เมื่อ Terraform หรือ CloudFormation นำไปใช้ งานจึงต้องมีตารางเวลาหรือการแนบ และถูกปฏิเสธเมื่อไม่มีทั้งสองอย่าง ดังนั้น Releases ของ Kubernetes, Terraform และ CloudFormation ระบุค่าให้ report_schedule และ Release ของ Docker Compose ปล่อยให้ว่าง

data-fix บิลด์แบบเดียวกันบนทั้งสี่เป้าหมาย โดยไม่มีตารางเวลาและไม่มีพารามิเตอร์

Releases หกรายการ

ReleaseManifestreport_scheduleการบิลด์
release-helm.yamlmanifest-helm.yaml30 2 * * 1-5เสร็จสมบูรณ์
release-compose.yamlmanifest-compose.yamlว่างเสร็จสมบูรณ์
release-terraform.yamlmanifest-terraform.yaml30 2 * * 1-5เสร็จสมบูรณ์
release-cloudformation.yamlmanifest-cloudformation.yaml30 2 * * 1-5เสร็จสมบูรณ์
release-compose-scheduled.yamlmanifest-compose.yaml30 2 * * 1-5หยุด
release-terraform-unscheduled.yamlmanifest-terraform.yamlว่างหยุด

Manifest แต่ละรายการแมป core/compute/task ไปยัง Recipe ของเป้าหมายนั้น และผูก core/manual-trigger กับ FeatureRecipe ของเป้าหมายนั้น ได้แก่ helm/task กับ helm/manual-trigger, compose/task กับ compose/manual-trigger, terraform/task-ecs กับ terraform/manual-trigger และ cloudformation/task-ecs กับ cloudformation/manual-trigger Manifest ที่ไม่ผูก FeatureRecipe ให้การแนบจะหยุดการบิลด์โดยระบุการแนบนั้น

Release แต่ละรายการระบุอิมเมจที่แต่ละงานรันไว้ใต้ infra.image เพราะ core/compute/task ไม่แพ็กเกจซอฟต์แวร์ใดเลย Releases ของ AWS ยังระบุไว้ใต้ infra.overrides ถึงคลัสเตอร์ ซับเน็ต กลุ่มความปลอดภัย และบทบาทการรันที่แต่ละงานใช้ และในที่ที่ report มีตารางเวลา ระบุบทบาทที่ตารางเวลาใช้เริ่มการรันแต่ละครั้ง ค่าเฉพาะบัญชีทุกค่าที่นั่นเป็นตัวยึดตำแหน่ง

ร่างตัวอย่าง

คุณบิลด์ตัวอย่างนี้ได้ด้วยคำสั่งด้านล่าง หรือ ให้ผู้ช่วย AI บิลด์ให้คุณ

เขียนตัวอย่างลงในโฟลเดอร์ใหม่ที่ตั้งชื่อตามตัวอย่าง:

Terminal window
aggrega draft --example task-run-on-demand

จากนั้นจะพิมพ์คำสั่งที่ต้องรันถัดไป คำสั่งแรกเปลี่ยนเข้าไปในโฟลเดอร์ใหม่:

Terminal window
cd task-run-on-demand

บิลด์

พื้นที่ทำงานอ่านแค็ตตาล็อกไลบรารีมาตรฐานจากรีจิสทรีของ hub ที่ .aggrega-config.yaml ระบุชื่อ Recipe ทุกรายการที่นี่เรนเดอร์จากแค็ตตาล็อกเพียงอย่างเดียว ดังนั้นทุกบิลด์รันแบบออฟไลน์ได้เมื่อหาค่าไฟล์ล็อกแล้ว

Terminal window
aggrega build --release release-helm.yaml --auto-pull # one Release

ในสำเนาที่เช็กเอาต์ของที่เก็บเอกสารประกอบ ซึ่งมีตัวช่วยบิลด์ที่ใช้ร่วมกันซึ่งสำเนาที่เขียนโดย aggrega draft --example ไม่มี ./check.sh บิลด์ Releases ทั้งหกรายการและตรวจสอบผลลัพธ์ของแต่ละรายการ สคริปต์พิมพ์หนึ่งบรรทัดต่อ Release และออกด้วยรหัสการออก 0 เฉพาะเมื่อทุกรายการจบตามที่หน้านี้อธิบาย:

RELEASE EXIT EXPECTED VERDICT
----------------------------------------------------------------------------------------------------
release-helm.yaml 0 complete OK: completed, the files the README shows
release-compose.yaml 0 complete OK: completed, the files the README shows
release-terraform.yaml 0 complete OK: completed, the files the README shows
release-cloudformation.yaml 0 complete OK: completed, the files the README shows
release-compose-scheduled.yaml 1 halt OK: halted with the expected refusal
release-terraform-unscheduled.yaml 1 halt OK: halted with the expected refusal
check: every build ended as expected.

กับผู้ช่วย AI

ผู้ช่วย AI ที่ ตั้งค่าด้วยเครื่องมือของ Aggrega ทำสิ่งเดียวกันได้ โดยวางพรอมต์นี้ลงไป

ใช้เครื่องมือของ Aggrega เท่านั้น และอย่าสร้างโฟลเดอร์เอง หากโฟลเดอร์ Workspace ยังไม่มีโฟลเดอร์ `task-run-on-demand` ให้เรียกเครื่องมือ `draft` หนึ่งครั้งโดยตั้ง `example` เป็น `task-run-on-demand` และไม่ระบุ `dir` เครื่องมือจะเขียนตัวอย่างลงในโฟลเดอร์ `task-run-on-demand` ใหม่ จากนั้นเรียกเครื่องมือ `build` โดยตั้ง `workspace_root` เป็น `task-run-on-demand` ตั้ง `release` เป็น `release-helm.yaml` ตั้ง `auto_pull` เป็น true และตั้ง `write` เป็น true แล้วบอกฉันว่าบิลด์เขียนไฟล์ใดบ้าง ไฟล์เหล่านั้นอยู่ใต้ `task-run-on-demand/dist/`

Kubernetes

release-helm.yaml เขียนสองไฟล์ คือ CronJob หนึ่งรายการต่อหนึ่งงาน และไม่มีอื่นใด data-fix เป็น CronJob ที่ถูกระงับไว้ บนตารางเวลาที่นักพัฒนาไม่เคยเขียน:

dist/helm/data-fix-cronjob.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: data-fix
[… omitted: the labels and annotations the build writes on every resource]
spec:
schedule: 0 0 30 2 *
suspend: true
jobTemplate:
spec:
template:
[… omitted: the pod template's metadata, the same labels again]
spec:
containers:
- name: main
image: registry.example.com/orders-data-fix:2.0.1
[… omitted: the container's CPU and memory requests and limits]
restartPolicy: Never

CronJob ต้องมีตารางเวลา และ 0 0 30 2 * ระบุวันที่ 30 กุมภาพันธ์ ซึ่งไม่มีปีใดมีวันนั้น: เมื่อถูกระงับ CronJob ไม่เริ่มการรันใดเลย และเมื่อกลับมาทำงานก็ยังไม่มีเวลาให้เริ่ม หากไม่มีการแนบ งานที่ไม่มีตารางเวลาจะเป็น Job ซึ่งรันทันทีที่ปรับใช้

report เป็น CronJob บนตารางเวลาที่ Release ระบุค่า และไม่ถูกระงับ:

dist/helm/report-cronjob.yaml
[… omitted: apiVersion, kind and metadata, as in data-fix-cronjob.yaml under report's name]
spec:
schedule: 30 2 * * 1-5
jobTemplate:
[… omitted: the job template, as in data-fix-cronjob.yaml with report's image]

การรัน data-fix หนึ่งครั้งคือ Job ที่สร้างจาก CronJob ของมัน ภายใต้ชื่อที่คุณเลือก:

Terminal window
kubectl create job data-fix-run-1 --from=cronjob/data-fix

คำสั่งเดียวกัน เมื่อระบุ --from=cronjob/report จะเริ่มการรันเพิ่มของ report ระหว่างการรันตามตารางเวลา

Docker Compose

release-compose.yaml เขียนไฟล์ Compose หนึ่งไฟล์ แต่ละงานเป็นบริการที่ Compose ไม่เริ่มใหม่เมื่อสิ้นสุดการทำงาน data-fix ยังมีโปรไฟล์ manual ด้วย และ report ไม่มี:

dist/compose/docker-compose.yaml
services:
data-fix:
image: registry.example.com/orders-data-fix:2.0.1
[… omitted: the labels the build writes on every service, and the service's network]
restart: "no"
[… omitted: the CPU and memory limits and reservations]
profiles:
- manual
report:
image: registry.example.com/orders-report:1.4.2
[… omitted: the labels and the network, as on data-fix]
restart: "no"
[… omitted: the CPU and memory limits and reservations, as on data-fix]
[… omitted: the network the services join]

Compose เริ่มบริการที่มีโปรไฟล์เฉพาะเมื่อเปิดใช้โปรไฟล์นั้นหรือระบุชื่อบริการในบรรทัดคำสั่ง ดังนั้น docker compose up จะรัน report หนึ่งครั้งและปล่อย data-fix ไว้ในสถานะหยุด การรัน data-fix เริ่มได้ด้วยการระบุชื่อบริการ และ --rm ลบคอนเทนเนอร์เมื่อการรันสิ้นสุด:

Terminal window
docker compose -f dist/compose/docker-compose.yaml run --rm data-fix

Terraform

release-terraform.yaml เขียนการกำหนดค่ารูทที่มีบล็อกโมดูลหนึ่งบล็อกต่อหนึ่งงาน และโมดูลของแต่ละงานอยู่ข้างกัน ทั้งสองบล็อกต่างกันที่ตารางเวลา:

dist/terraform/aggrega.modules.tf
# Root configuration for this boundary. Each component rendered here is one
# module block below, and the directory that block points at holds that
# component's own files. One configuration means one dependency graph, which is
# what lets a reference or a depends_on reach from one component to another.
module "data-fix" {
source = "./modules/data-fix"
assign_public_ip = false
cluster = "arn:aws:ecs:eu-west-1:123456789012:cluster/example-cluster"
cpu = "256"
environment = {}
execution_role_arn = "arn:aws:iam::123456789012:role/example-ecs-task-execution"
image = "registry.example.com/orders-data-fix:2.0.1"
memory = "512"
name = "data-fix"
security_groups = ["sg-0fedcba9876543210"]
subnet_ids = ["subnet-0123456789abcdef0"]
}
module "report" {
source = "./modules/report"
assign_public_ip = false
cluster = "arn:aws:ecs:eu-west-1:123456789012:cluster/example-cluster"
cpu = "256"
environment = {}
execution_role_arn = "arn:aws:iam::123456789012:role/example-ecs-task-execution"
image = "registry.example.com/orders-report:1.4.2"
memory = "512"
name = "report"
schedule_expression = "cron(30 2 ? * 2,3,4,5,6 *)"
schedule_expression_timezone = "UTC"
scheduler_role_arn = "arn:aws:iam::123456789012:role/example-scheduler"
security_groups = ["sg-0fedcba9876543210"]
subnet_ids = ["subnet-0123456789abcdef0"]
}

โมดูลประกาศตารางเวลาของ EventBridge Scheduler เฉพาะเมื่อได้รับนิพจน์ ดังนั้นนิยามงานของ data-fix จึงอยู่โดดๆ:

dist/terraform/modules/data-fix/main.tf
[… omitted: the provider requirement, the Region lookup, the task definition and the comments on each]
resource "aws_scheduler_schedule" "this" {
count = var.schedule_expression == null ? 0 : 1
[… omitted: the rest of the schedule]

Recipe เขียนตารางเวลาของ report ในรูปแบบของ EventBridge Scheduler เอง: 30 2 * * 1-5 กลายเป็น cron(30 2 ? * 2,3,4,5,6 *) โดยวันในสัปดาห์นับจากวันอาทิตย์เป็น 1 ใน UTC เพราะ Release ไม่ระบุเขตเวลา

CloudFormation

release-cloudformation.yaml เขียนเทมเพลตสแตกหนึ่งไฟล์ ซึ่งมีนิยามงานของแต่ละงานและตารางเวลาสำหรับ report เท่านั้น:

dist/cloudformation/cloudformation-stack.json
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"DataFixTaskDefinition": {
[… omitted: the provenance metadata the build writes on every resource]
"Properties": {
"ContainerDefinitions": [
{
"Essential": true,
"Image": "registry.example.com/orders-data-fix:2.0.1",
"Name": "main"
}
],
"Cpu": "256",
"ExecutionRoleArn": "arn:aws:iam::123456789012:role/example-ecs-task-execution",
"Family": "data-fix",
"Memory": "512",
"NetworkMode": "awsvpc",
"RequiresCompatibilities": [
"FARGATE"
]
},
"Type": "AWS::ECS::TaskDefinition"
},
"ReportSchedule": {
[… omitted: the provenance metadata]
"Properties": {
"FlexibleTimeWindow": {
"Mode": "OFF"
},
"ScheduleExpression": "cron(30 2 ? * 2,3,4,5,6 *)",
"ScheduleExpressionTimezone": "UTC",
"Target": {
"Arn": "arn:aws:ecs:eu-west-1:123456789012:cluster/example-cluster",
"EcsParameters": {
[… omitted: the launch type, the network configuration and the task count each run starts with]
"TaskDefinitionArn": {
"Ref": "ReportTaskDefinition"
}
},
"RoleArn": "arn:aws:iam::123456789012:role/example-scheduler"
}
},
"Type": "AWS::Scheduler::Schedule"
},
"ReportTaskDefinition": {
[… omitted: the same shape as DataFixTaskDefinition, with report's image and family]
}
}
}

ไม่มีสิ่งใดในสแตกอ้างถึง DataFixTaskDefinition จึงไม่มีสิ่งใดเริ่มรัน

การเริ่มการรันบน Amazon ECS

บนทั้งสองเป้าหมายของ AWS การรัน data-fix เริ่มด้วยการดำเนินการ RunTask ของ Amazon ECS โดยระบุ family ของนิยามงานตามที่เรนเดอร์ และการกำหนดค่าคลัสเตอร์และเครือข่ายที่ Release ระบุไว้ใต้ infra.overrides:

Terminal window
aws ecs run-task \
--cluster arn:aws:ecs:eu-west-1:123456789012:cluster/example-cluster \
--task-definition data-fix \
--launch-type FARGATE \
--network-configuration 'awsvpcConfiguration={subnets=[subnet-0123456789abcdef0],securityGroups=[sg-0fedcba9876543210],assignPublicIp=DISABLED}'

เมื่อระบุด้วย family เพียงอย่างเดียว นิยามงานจะหาค่าเป็น revision ที่ใช้งานอยู่ล่าสุด

สิ่งที่การบิลด์ไม่ทำ

บิลด์เขียนไฟล์แล้วหยุด ไม่เริ่มการรันบนเป้าหมายใดเลย: ไม่ติดต่อคลัสเตอร์ ไม่เริ่มสแตก Compose และไม่เรียก AWS API ใดเลย และการแนบเปลี่ยนเฉพาะสิ่งที่ไฟล์ระบุ บิลด์ไม่เขียนสคริปต์ด้วย: ทุกคำสั่งในหน้านี้เป็นของเป้าหมายเอง รันโดยผู้ที่ดำเนินการเป้าหมายนั้น และไม่มีคำสั่งใดอยู่ใน dist/ การทำให้การแนบเป็นจริงไม่เพิ่มไฟล์ของตนเองบนเป้าหมายใดเลย การเปลี่ยนแปลงที่แต่ละรายการทำอยู่ในเอาต์พุตของงานเอง ดังที่แสดงข้างต้น

Releases สองรายการที่หยุด

release-compose-scheduled.yaml ระบุค่าให้ report_schedule บน Docker Compose และแนบ core/manual-trigger กับ report ด้วย บน Kubernetes และ AWS งานที่มีตารางเวลารันตามตารางนั้นไม่ว่าจะมีการแนบหรือไม่ แต่บน Docker Compose ที่ไม่มีตัวจัดตารางเวลา งานที่มีตารางเวลาไม่มีรูปแบบการทำให้เป็นรูปธรรม ไม่ว่าจะแนบหรือไม่ aggrega build --release release-compose-scheduled.yaml --auto-pull จะหยุด:

[… omitted: what resolving the lockfile prints, the same halt among it]
[CATALOG_ERROR E0033]
Recipe/FeatureRecipe assertions
caused by: Recipe "task" instance "report" assertion "schedule_refused": [… omitted: the template position and the assertion's own source]: error calling fail: assertion failed: the component "report" is scheduled "30 2 * * 1-5", and Docker Compose has no scheduler: nothing in a compose file starts a service at a time, so the run would happen at none of the times written. Map core/compute/task to helm/task in the Manifest, which runs it on its schedule as a Kubernetes CronJob; or remove the schedule from the component in the Aggregate to run it once at each docker compose up, or attach core/manual-trigger to it there to start each run with docker compose run --rm

release-terraform-unscheduled.yaml ปล่อย report_schedule ว่างบน Terraform ดังนั้น report จึงไม่มีทั้งตารางเวลาและการแนบ และไม่มีสิ่งใดบน AWS ที่จะเริ่มรันงานนี้เลย นี่คือกรณีที่ core/manual-trigger มีไว้สำหรับ และ aggrega build --release release-terraform-unscheduled.yaml --auto-pull หยุดโดยระบุการแนบนั้น:

[… omitted: what resolving the lockfile prints, the same halt among it]
[CATALOG_ERROR E0033]
Recipe/FeatureRecipe assertions
caused by: Recipe "task-ecs" instance "report" assertion "schedule_translates": [… omitted: the template position and the assertion's own source]: error calling fail: assertion failed: the component "report" has no schedule and carries no core/manual-trigger attachment, so it runs once, and nothing on AWS starts a run that happens once: neither Terraform nor CloudFormation runs an Amazon ECS task when it applies, so the run would never happen. Give the component a schedule in the Aggregate; attach core/manual-trigger to it there to deploy its task definition alone, for the operator to start each run with aws ecs run-task; or map core/compute/task to helm/task in the Manifest, which runs it once on Kubernetes

ที่นี่ตารางเวลาไปถึงงานผ่านพารามิเตอร์ที่ Release ระบุค่า การให้ตารางเวลาแก่ส่วนประกอบจึงหมายถึงการระบุค่าให้ report_schedule ดังที่ release-terraform.yaml ทำ