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

การเขียนผู้ให้บริการ: บทแนะนำทีละขั้นตอน

หน้านี้พาไปทำงานเขียนเกสต์ของผู้ให้บริการทั้งงาน ตั้งแต่ไดเรกทอรีว่างจนถึงบิลด์ที่เขียนไฟล์ที่ผู้ให้บริการเรนเดอร์ เป็นคู่มือประกอบเชิงปฏิบัติของ คู่มือนักพัฒนาปลั๊กอิน ซึ่งบันทึกเอนทิตีและวงจรชีวิต และของ สัญญาของปลั๊กอิน ซึ่งบันทึกซ็อกเก็ต

ผู้ให้บริการที่สร้างที่นี่เล็กพอจะอ่านจบในครั้งเดียวและทำงานจริง: เรนเดอร์ไฟล์สภาพแวดล้อม เป้าหมายนี้เป็นผู้ให้บริการแรกที่ดี เพราะการแปลของเป้าหมายนี้เฉพาะรูปแบบจริงๆ — ไฟล์สภาพแวดล้อมมีไวยากรณ์คีย์ของตนเอง กฎการใส่เครื่องหมายอัญประกาศของตนเอง และไม่มีบล็อกเมทาดาทาให้ใส่ที่มาของบิลด์ — เกสต์จึงต้องตัดสินใจทุกอย่างที่ผู้ให้บริการขนาดใหญ่ตัดสินใจ โดยไม่มีโค้ดก้อนใหญ่มาซ่อนการตัดสินใจเหล่านั้น

เกี่ยวกับบล็อกในหน้านี้ บล็อกที่แสดงเอาต์พุตทุกบล็อกคัดลอกมาจากการรันจริงของขั้นตอนด้านล่าง ตามลำดับที่ปรากฏ โดยเป็นไปตามธรรมเนียมเอาต์พุต: บล็อกหนึ่งครบถ้วน หรือเป็นข้อความตัดตอนที่ระบุว่าบรรทัดใดถูกละไว้ นี่เป็นบทแนะนำที่คุณประกอบเอง ไม่ใช่พื้นที่ทำงานที่ clone ได้ บล็อกซอร์สจึงเป็นไฟล์ที่ต้องสร้าง ไม่ใช่ไฟล์ที่ต้องเช็กเอาต์

สิ่งที่คุณต้องมี

  • ชุดเครื่องมือ Go ซึ่งคอมไพล์เป็น WebAssembly โดยไม่ต้องมีเครื่องมือเพิ่ม
  • สำเนาที่เช็กเอาต์ของชุดพัฒนาปลั๊กอิน ซึ่ง คู่มือนักพัฒนาปลั๊กอิน แนะนำไว้ และของแพ็กเกจ ABI ที่ชุดเครื่องมือนั้นสร้างบน: go.mod ของเกสต์ด้านล่างชี้ไปยังทั้งสอง
  • ไบนารี aggrega

รูปทรงของผลงานที่เสร็จแล้ว

ผู้ให้บริการคืออาร์ติแฟกต์สองรายการ: เกสต์ .wasm และเอนทิตี Plugin ที่พาเกสต์เข้าสู่แค็ตตาล็อก ไม่มีอย่างอื่นอีก — ไม่มีการเปลี่ยนโฮสต์ ไม่มีการลงทะเบียน ไม่มีการบิลด์คอมไพเลอร์ การเพิ่มผู้ให้บริการคือการวางสองไฟล์นั้นในที่ที่บิลด์มองเห็น

พื้นที่ทำงานที่บทแนะนำนี้จบลงด้วย:

dotenv-walkthrough/
guest/ the Go module that compiles to the .wasm
go.mod
go.sum
main.go
catalog/ the Component and Recipe you author
component/example/compute/checkout/1.0.0/component.yaml
recipe/dotenv/checkout/1.0.0/recipe.yaml
dev-catalog/ the generated Plugin entity, with the .wasm beside it
plugin/provider/dotenv/0.1.0/plugin.yaml
plugin/provider/dotenv/0.1.0/provider--dotenv.wasm
dotenv.wasm the compiled guest
overlay.yaml the identity you supply to the entity generator
aggregate.yaml
manifest.yaml
release.yaml
.aggrega-config.yaml

1. การรายงานตนเองของเกสต์

ทุกปลั๊กอินมีเอ็กซ์พอร์ต describe() โฮสต์เรียกเอ็กซ์พอร์ตนี้หนึ่งครั้งเมื่อโหลดเกสต์ และอ่านคำตอบเป็นคำอธิบายของเกสต์เองว่าตนคืออะไร สำหรับผู้ให้บริการ คำตอบนั้นมีสี่ส่วนที่สำคัญ

สร้าง guest/main.go:

// A provider guest that renders environment-file artifacts.
package main
import (
"bytes"
"fmt"
"sort"
"strings"
pdk "github.com/extism/go-pdk"
"github.com/aggrega-ai/aggrega-pdk/go/pkg/aggrega"
)
func descriptor() aggrega.PluginDescriptor {
return aggrega.PluginDescriptor{
AbiVersion: aggrega.APIVersion,
PluginType: "provider",
Description: "Render artifact bodies into environment files, quoting and escaping each value as the format requires.",
Capabilities: []string{"dotenv"},
Exports: []aggrega.PluginExport{
{Name: "render_node"},
},
Metadata: &aggrega.PluginDescriptorMeta{License: "Apache-2.0"},
}
}
//go:wasmexport describe
func describe() int32 {
out, err := aggrega.MarshalDescriptor(descriptor())
if err != nil {
pdk.SetError(err)
return 1
}
pdk.Output(out)
return 0
}

PluginType บอกว่าเกสต์นี้ให้บริการซ็อกเก็ตใด Capabilities คือรายการที่ Recipe หาค่าเทียบด้วย: Recipe ที่ spec.capability เป็น dotenv จะถูกกำหนดเส้นทางมายังเกสต์นี้ Exports ระบุชื่อจุดเข้าที่โฮสต์อาจกระจายไปถึง render_node คือจุดที่ผู้ให้บริการทุกรายต้องมี

ตัวอธิบายนี้ไม่สมบูรณ์โดยเจตนา อย่างที่ฉบับร่างแรกมักเป็น บิลด์ครั้งแรกด้านล่างคือจุดที่เรื่องนี้ปรากฏ

2. การเรนเดอร์โหนด

render_node ถูกเรียกหนึ่งครั้งต่อส่วนประกอบที่บิลด์เรนเดอร์ผ่านผู้ให้บริการนี้ รับตัวตนของโหนด บริบทการเรนเดอร์ และอินพุตที่ประเมินไว้ล่วงหน้าตามที่ Recipe ที่ผูกไว้ประกาศ — ในที่นี้คือชุดอาร์ติแฟกต์ที่โฮสต์อ่านและประเมินเป็นเทมเพลตไว้แล้ว

งานทั้งหมดของเกสต์คือตัดสินใจว่าจะส่งไบต์อะไรออกมา เพิ่มลงใน guest/main.go:

// needsQuoting reports whether a value cannot be written bare in an
// environment file. A bare value ends at the first space, so anything
// carrying whitespace has to be quoted; the quote characters, the comment
// introducer and the expansion introducer would each be read as syntax.
func needsQuoting(v string) bool {
if v == "" {
return true
}
return strings.ContainsAny(v, " \t\n\r\"'#$\\")
}
// quote wraps a value in double quotes and escapes what the format reads as
// syntax inside them. Order matters: the backslash is escaped first, or the
// escapes introduced afterwards would be escaped a second time.
func quote(v string) string {
v = strings.ReplaceAll(v, `\`, `\\`)
v = strings.ReplaceAll(v, `"`, `\"`)
v = strings.ReplaceAll(v, "\n", `\n`)
v = strings.ReplaceAll(v, "\r", `\r`)
v = strings.ReplaceAll(v, "\t", `\t`)
return `"` + v + `"`
}
// normaliseKey maps an authored key into the environment-variable grammar:
// upper case, with the separators an author reaches for folded to
// underscores. It reports an error rather than correcting a key that still
// does not fit, because a silently-renamed variable is one the application
// never reads.
func normaliseKey(k string) (string, error) {
n := strings.ToUpper(k)
n = strings.NewReplacer("-", "_", ".", "_", "/", "_").Replace(n)
if n == "" {
return "", fmt.Errorf("empty key")
}
for i, r := range n {
switch {
case r >= 'A' && r <= 'Z', r == '_':
case r >= '0' && r <= '9' && i > 0:
default:
return "", fmt.Errorf("key %q maps to %q, which is not a valid environment-variable name", k, n)
}
}
return n, nil
}

การตัดสินใจสามข้อในส่วนย่อยนั้นเป็นของผู้ให้บริการเอง และไม่มีชั้นอื่นตัดสินใจแทนได้ อักขระใดบังคับให้ใส่เครื่องหมายอัญประกาศเป็นข้อเท็จจริงเกี่ยวกับไฟล์สภาพแวดล้อม ไวยากรณ์ชื่อตัวแปรก็เช่นกัน และการเลือก ปฏิเสธ คีย์ที่ไม่เข้ากับรูปแบบแทนการเปลี่ยนชื่อก็เช่นกัน — ตัวแปรที่ถูกเปลี่ยนชื่อคือตัวแปรที่แอปพลิเคชันจะมองหาแล้วไม่พบ ซึ่งแย่กว่าบิลด์ที่หยุด

ตอนนี้ถึงสองส่วนที่เหลือ: ที่ใดเป็นที่ของที่มา และตัวเอ็กซ์พอร์ตเอง

// provenanceComment renders the build-provenance key set the host computed as
// comment lines. An environment file has no metadata block, so a comment
// header is where provenance can go; the keys are emitted in sorted order so
// the bytes are identical across runs of identical input.
func provenanceComment(annotations map[string]string) []byte {
if len(annotations) == 0 {
return nil
}
keys := make([]string, 0, len(annotations))
for k := range annotations {
keys = append(keys, k)
}
sort.Strings(keys)
var out bytes.Buffer
for _, k := range keys {
fmt.Fprintf(&out, "# %s=%s\n", k, annotations[k])
}
out.WriteByte('\n')
return out.Bytes()
}
// renderBody translates one artifact body into environment-file lines. A
// blank line and a comment pass through untouched; every other line is
// KEY=value, and a line carrying no separator is a loud failure rather than a
// line the guest guesses at.
func renderBody(name string, body []byte) ([]byte, error) {
var out bytes.Buffer
for i, raw := range strings.Split(string(body), "\n") {
line := strings.TrimSpace(raw)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
k, v, found := strings.Cut(line, "=")
if !found {
return nil, fmt.Errorf("%s line %d: %q declares no value; an entry is KEY=value", name, i+1, line)
}
key, err := normaliseKey(strings.TrimSpace(k))
if err != nil {
return nil, fmt.Errorf("%s line %d: %w", name, i+1, err)
}
val := strings.TrimSpace(v)
if needsQuoting(val) {
val = quote(val)
}
fmt.Fprintf(&out, "%s=%s\n", key, val)
}
return out.Bytes(), nil
}
//go:wasmexport render_node
func render_node() int32 {
var in aggrega.RenderNodeInput
if err := in.DecodeCBOR(pdk.Input()); err != nil {
pdk.SetError(fmt.Errorf("dotenv: decode RenderNodeInput: %w", err))
return 1
}
if in.ArtifactsProjection == nil || len(in.ArtifactsProjection.Entries) == 0 {
pdk.SetError(fmt.Errorf("dotenv: node %q supplied no artifacts to render", in.Node.ID))
return 1
}
var files []aggrega.RenderedFile
for _, e := range in.ArtifactsProjection.Entries {
body, err := renderBody(e.Name, e.Content)
if err != nil {
pdk.SetError(fmt.Errorf("dotenv: %w", err))
return 1
}
files = append(files, aggrega.RenderedFile{
Path: e.Name,
Content: append(provenanceComment(e.Annotations), body...),
Format: "text",
})
}
outputBytes, err := aggrega.RenderNodeOutput{Files: files}.EncodeCBOR()
if err != nil {
pdk.SetError(fmt.Errorf("dotenv: encode RenderNodeOutput: %w", err))
return 1
}
pdk.Output(outputBytes)
return 0
}
func main() {}

เพย์โหลดข้ามเข้ามาในการเข้ารหัสแบบไบนารี และชุดเครื่องมืออ่านและเขียนให้ โฮสต์ส่งอินพุตของ render_node เป็น CBOR แบบแคนอนิคัลและอ่านคำตอบในการเข้ารหัสเดียวกัน เกสต์จึงถอดรหัสด้วย DecodeCBOR ของชนิดอินพุต และเข้ารหัสด้วย EncodeCBOR ของชนิดเอาต์พุต — ไม่มีไลบรารี CBOR และไม่มีสิ่งใดที่ต้องแยกวิเคราะห์ด้วยมือ Content ของไฟล์เป็นไบต์สไลซ์ที่เกสต์เติมด้วยไบต์ที่ต้องเขียนอย่างตรงตัว ตัวเข้ารหัสส่งไบต์เหล่านั้นเป็นไบต์ดิบ และโฮสต์เขียนตามที่เป็นทุกประการ

โฮสต์ส่งเนื้อหาที่ประเมินแล้วให้เกสต์ Content ของแต่ละรายการอาร์ติแฟกต์คือเนื้อหาที่เขียนไว้ ซึ่งนิพจน์เทมเพลตถูกหาค่าเทียบกับบริบทการเรนเดอร์แล้ว — เกสต์ไม่ประเมินเทมเพลตเลยและไม่อ่านไฟล์เลย นี่คือสิ่งที่รักษาเส้นแบ่ง AOT ของคอมไพเลอร์ไว้: เกสต์เป็นฟังก์ชันบริสุทธิ์จากเพย์โหลดไปเป็นไบต์

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

guest/go.mod อ้างอิงสำเนาที่เช็กเอาต์ของชุดเครื่องมือของคุณ:

module example.com/dotenv-provider
go 1.26.0
require (
github.com/aggrega-ai/aggrega-pdk/go v0.0.0
github.com/extism/go-pdk v1.1.3
)
require github.com/aggrega-ai/aggrega-abi/go v0.0.0 // indirect
replace github.com/aggrega-ai/aggrega-pdk/go => /path/to/aggrega-pdk/go
replace github.com/aggrega-ai/aggrega-abi/go => /path/to/aggrega-abi/go

3. บิลด์เกสต์

เกสต์คือบิลด์ Go ธรรมดาที่เลือกเป้าหมาย WebAssembly:

Terminal window
cd guest
go mod tidy
GOWORK=off GOOS=wasip1 GOARCH=wasm go build -trimpath -buildvcs=false \
-ldflags "-X github.com/aggrega-ai/aggrega-pdk/go/pkg/aggrega.Version=0.1.0 \
-X github.com/aggrega-ai/aggrega-pdk/go/pkg/aggrega.Commit=walkthru" \
-buildmode=c-shared -o ../dotenv.wasm ./

go mod tidy เขียน go.sum ซึ่งเป็นเช็กซัมของการพึ่งพา ที่บิลด์ปฏิเสธรันหากไม่มี -trimpath และ -buildvcs=false กันตราประทับของเส้นทางบิลด์และการควบคุมเวอร์ชันออกจากไบนารี ซอร์สเดียวกันที่คอมมิตเดียวกันจึงให้ไบต์เดียวกัน ค่า -ldflags สองค่าประทับเวอร์ชันของเกสต์เองลงไป ซึ่งขั้นตอนถัดไปอ่าน

ยืนยันเอ็กซ์พอร์ตก่อนไปต่อ:

Terminal window
aggrega inspect --plugin dotenv.wasm
Plugin .wasm: dotenv.wasm
Exports:
render_node
hostFunctions (derived from binary, not yet recorded by aggrega plugin sync): (none)

เกสต์นี้ไม่นำเข้าฟังก์ชันของโฮสต์ ซึ่งเป็นค่าเริ่มต้นแบบสิทธิ์น้อยที่สุด: ไม่ต้องใช้ระบบไฟล์หรือเครือข่าย เพราะทุกสิ่งที่เรนเดอร์มาถึงในเพย์โหลด

4. สร้างเอนทิตี Plugin ขึ้น

ฟิลด์สัญญาของเอนทิตีไม่ได้เขียนด้วยมือ — อ่านออกมาจากเกสต์ เอนทิตีจึงสัญญาสิ่งที่ไบนารีไม่ทำไม่ได้ คุณระบุเพียงตัวตนในส่วนซ้อนทับขนาดเล็ก

overlay.yaml:

apiVersion: aggrega.ai/v1beta1
kind: Plugin
metadata:
name: dotenv
Terminal window
aggrega plugin gen-catalog --plugin dotenv.wasm \
--overlay overlay.yaml --output-dir dev-catalog/plugin
generated dev-catalog/plugin/provider/dotenv/0.1.0/plugin.yaml

เอนทิตีที่สร้างขึ้น:

# Generated from the plugin by `aggrega plugin gen-catalog`. You may narrow its capability lists; edit nothing else here.
apiVersion: aggrega.ai/v1beta1
kind: Plugin
metadata:
name: dotenv
version: 0.1.0
pluginType: provider
description: Render artifact bodies into environment files, quoting and escaping each value as the format requires.
spec:
abiVersion: v1beta1
exports:
- name: render_node
provider:
capabilities:
- dotenv

สังเกตสิ่งที่ ไม่มี อยู่ที่นั่น: ตัวชี้ใดๆ ไปยัง .wasm ตำแหน่งของปลั๊กอินได้จากตัวตนของปลั๊กอิน — พิกัด pluginType/name บวกรีจิสทรีที่กำหนดค่าไว้ — จึงไม่มีฟิลด์ตำแหน่งให้เขียนและไม่มีให้ผิด ไดเจสต์ของเนื้อหาถูกบันทึกเมื่อหาค่า Release ในไฟล์ล็อกของ Release นั้น ไม่ใช่ในเอนทิตี

5. วางเกสต์ในที่ที่บิลด์จะมองหา

ขณะที่คุณกำลังพัฒนาเกสต์ ยังไม่มีสิ่งใดเผยแพร่ให้ดึง บิลด์จึงอ่านเกสต์จากไดเรกทอรีแค็ตตาล็อก: .wasm วาง ข้าง เอนทิตีของตน ตั้งชื่อตามซ็อกเก็ตและปลั๊กอิน

dev-catalog/plugin/provider/dotenv/0.1.0/plugin.yaml
dev-catalog/plugin/provider/dotenv/0.1.0/provider--dotenv.wasm
Terminal window
cp dotenv.wasm dev-catalog/plugin/provider/dotenv/0.1.0/provider--dotenv.wasm

ชื่อไฟล์มีทั้งซ็อกเก็ตและชื่อ เพราะชื่อปลั๊กอินเปล่าๆ ไม่ซ้ำกันเฉพาะภายในซ็อกเก็ตของตน — ปลั๊กอินสองตัวที่ชื่อ dotenv ที่ซ็อกเก็ตต่างกันต้องไปถึงไฟล์สองไฟล์

คัดลอกไฟล์ที่บิลด์แล้วไปไว้ที่นั่นแทนการลิงก์ไปหา ลิงก์ในที่นั้นถูกตามไปเฉพาะที่ใดที่หนึ่งภายในพื้นที่ทำงาน — หรือสำหรับไดเรกทอรีแค็ตตาล็อกที่ระบุไว้นอกพื้นที่ทำงาน ที่ใดที่หนึ่งภายในไดเรกทอรีนั้น ลิงก์ที่นำไปไกลกว่านั้นทำให้บิลด์และการ pull หยุดโดยระบุลิงก์ ตามที่ เส้นแบ่งของพื้นที่ทำงาน กำหนดไว้

เอนทิตีปลั๊กอินของผู้ให้บริการในไดเรกทอรีแค็ตตาล็อกไม่เคยถูกปักหมุด และทั้งการ pull และบิลด์ไม่ดึง .wasm ของปลั๊กอินนั้น ไฟล์จึงต้องอยู่ที่นั่นเมื่อหาค่า Release การ pull ตรวจสอบก่อนเขียนไฟล์ล็อก: เมื่อไม่พบ .wasm ทั้งข้างเอนทิตีและที่อื่นที่บิลด์อ่านปลั๊กอิน การ pull จะหยุดและระบุไฟล์กับไดเรกทอรีที่คาดไว้ ตามที่ A plugin’s .wasm is not on this machine (ไม่พบโมดูลของปลั๊กอินในเครื่องนี้) อธิบายไว้

ชี้พื้นที่ทำงานไปยังไดเรกทอรีแค็ตตาล็อกทั้งสองใน .aggrega-config.yaml:

workspace:
catalog_dirs:
- catalog
- dev-catalog
auto_pull: true

auto_pull ทำให้ aggrega build หาค่า Release ภายในกระบวนการเดียวกัน บิลด์ครั้งแรกจึงไม่ต้องมีขั้นตอนหาค่าแยกต่างหาก

6. เอกสารที่มุ่งเป้าไปที่ผู้ให้บริการ

ไฟล์สี่ไฟล์ Component ประกาศสิ่งที่ผู้เขียนตั้งได้ Recipe ผูกความสามารถและเขียนอาร์ติแฟกต์ Aggregate ระบุเจตนา Release กำหนดตำแหน่งที่จะวาง

catalog/component/example/compute/checkout/1.0.0/component.yaml:

apiVersion: aggrega.ai/v1beta1
kind: Component
metadata:
name: example/compute/checkout
version: 1.0.0
description: An application whose configuration is delivered as an environment file.
labels:
component.aggrega.ai/type: server
component.aggrega.ai/category: compute
spec:
schema:
values:
service_name:
type: string
default: checkout
description: The name the application reports itself under.
log_level:
type: string
default: info
description: The application's log verbosity.
greeting:
type: string
default: hello
description: A value carrying characters the environment-file format must quote.
infra:
endpoint:
type: string
default: http://localhost:8080
description: The endpoint the operator points the application at.

catalog/recipe/dotenv/checkout/1.0.0/recipe.yaml:

apiVersion: aggrega.ai/v1beta1
kind: Recipe
metadata:
name: checkout
version: 1.0.0
description: Renders the application's configuration as an environment file.
spec:
capability: dotenv
implements: example/compute/checkout:1.0.0
artifacts:
app.env: |
service-name = {{ .values.service_name }}
log.level = {{ .values.log_level }}
greeting = {{ .values.greeting }}
endpoint = {{ .infra.endpoint }}

spec.capability: dotenv คือการผูกทั้งหมด ซึ่งระบุความสามารถ ไม่ใช่ปลั๊กอิน: บิลด์หาค่าให้เป็นปลั๊กอินผู้ให้บริการใดก็ตามที่ประกาศความสามารถนั้น เมื่อมีปลั๊กอินเพียงหนึ่งเดียวที่ประกาศ — เช่นที่นี่ — ไม่ต้องมีสิ่งอื่น

คีย์ที่เขียนไม่ได้อยู่ในรูปแบบตัวแปรสภาพแวดล้อมโดยเจตนา service-name และ log.level เป็นการสะกดที่ผู้เขียนจะเขียนตามธรรมชาติ และการทำให้เป็นรูปแบบปกติเป็นหน้าที่ของผู้ให้บริการ

aggregate.yaml:

apiVersion: aggrega.ai/v1beta1
kind: Aggregate
metadata:
name: aggregate
version: "1.0.0"
description: One application whose configuration is an environment file.
spec:
components:
app:
type: example/compute/checkout
values:
service_name: checkout
log_level: debug
greeting: hello world

manifest.yaml:

apiVersion: aggrega.ai/v1beta1
kind: Manifest
metadata:
name: manifest
version: "1.0.0"
description: Binds the component type to the environment-file recipe.
spec:
mappings:
components:
type:
example/compute/checkout:1.0.0: checkout:1.0.0

release.yaml:

apiVersion: aggrega.ai/v1beta1
kind: Release
metadata:
name: walkthrough
environment: production
version: "1.0.0"
spec:
aggregate: aggregate
manifest: manifest
boundaries:
infra:
directory: infra
components:
app:
infra:
endpoint: "https://checkout.example.net"

7. บิลด์ครั้งแรก และสิ่งที่บิลด์ปฏิเสธ

Terminal window
aggrega build --release release.yaml --auto-pull
warning: plugin "provider/dotenv" resolved from local with no recorded digest to check it against, so it runs unverified; it is read from beside its Plugin entity in a catalog directory on every build, and a pull records its digest only for a plugin attached at a transform role; otherwise publish the plugin and reference the published entity to have its digest recorded
[… omitted: the notice saying the Release could not be rendered to discover its external inputs, carrying the error below beneath its first line]
Pull complete: release.lock (entities=5, source_artifacts=0, external_inputs=0)
[… omitted: a line saying again that external inputs were not fully discovered]
[CATALOG_ERROR E0265] instance app
provider does not support the node's input kind (node "app"). provider "dotenv" does not support artifacts-only recipes (this provider supports: none); declare one of the supported input kinds, or target a provider that supports artifacts-only

อ่านสองบรรทัดแยกกัน เพราะสองบรรทัดบอกสิ่งที่ต่างกัน

คำเตือนนี้เป็นสิ่งที่คาดไว้ขณะพัฒนา และจะไม่หายไป คำเตือนบอกว่าเกสต์รันโดยไม่มีไดเจสต์ของเนื้อหาที่บันทึกไว้ให้ตรวจเทียบ ซึ่งเป็นความจริงของเกสต์ที่คุณเพิ่งบิลด์เมื่อหนึ่งนาทีก่อน: ไม่มีสิ่งใดเผยแพร่เกสต์ จึงไม่มีไดเจสต์บันทึกไว้ คำเตือนยังบอกด้วยว่าเหตุใดการหาค่า Release ซ้ำจึงไม่เปลี่ยนเรื่องนี้ — เกสต์ที่อ่านจากข้างเอนทิตีของตนในไดเรกทอรีแค็ตตาล็อกถูกอ่านใหม่ทุกบิลด์ และการ pull บันทึกไดเจสต์เฉพาะปลั๊กอินกำกับดูแลที่แนบที่บทบาท transform ซึ่งเกสต์นี้ไม่ใช่ — และสิ่งที่จะเปลี่ยนได้ คือการเผยแพร่ปลั๊กอินและอ้างอิงเอนทิตีที่เผยแพร่แล้ว อ่านคำเตือนว่าเป็นบิลด์ที่ปฏิเสธอย่างถูกต้องที่จะอ้างการตรวจสอบซึ่งไม่ได้ทำ และคาดว่าจะเห็นตราบเท่าที่เกสต์ยังอยู่ภายในเครื่อง

ข้อผิดพลาดคือข้อค้นพบที่แท้จริง และวงเล็บคือการวินิจฉัย ผู้ให้บริการถูกพบ โหลด และถามว่าเรนเดอร์อะไร และตอบว่า none — this provider supports: none ตัวอธิบายที่เขียนตอนต้นระบุซ็อกเก็ต ความสามารถ และเอ็กซ์พอร์ต แต่ไม่เคยบอกว่าเกสต์รองรับ Recipe ประเภทอินพุตใดในสามประเภท โฮสต์ไม่เดา: ด่านตรวจประเภทอินพุตอ่านชุดที่เกสต์ประกาศและปฏิเสธโหนดที่เกสต์ไม่เคยอ้าง

นี่คือรูปทรงของความล้มเหลวของผู้ให้บริการรายแรกส่วนใหญ่ เกสต์ไม่มีปัญหา การรายงานตนเองต่างหากที่ไม่ครบ describe() ไม่ใช่พิธีการที่โฮสต์ยอมให้ผ่าน — แต่เป็นพื้นฐานทั้งหมดที่โฮสต์ซึ่งไม่ขึ้นกับรูปแบบใช้ตัดสินใจว่าจะส่งอะไรไปที่ใด

8. ประกาศสัญญาพฤติกรรมการเรนเดอร์

เพิ่มสัญญาลงในตัวอธิบายใน guest/main.go:

Capabilities: []string{"dotenv"},
Contract: &aggrega.ProviderContract{
InputKinds: aggrega.InputKinds{
Sourced: false,
Synthesized: false,
ArtifactsStandalone: true,
},
BoundaryAssembly: "none",
},
Exports: []aggrega.PluginExport{

ArtifactsStandalone: true คือข้ออ้างที่ด่านตรวจมองหา ค่า false สองค่าก็เป็นข้ออ้างเช่นกัน และการระบุไว้คือจุดประสงค์: Recipe ที่ vendor ทรีซอร์ส หรือที่ประกาศเวิร์กโหลด จะถูกปฏิเสธก่อนการเรนเดอร์ใดรัน พร้อมข้อความที่ระบุชื่อผู้ให้บริการนี้ — แทนที่จะไปถึง render_node แล้วล้มเหลวที่ใดที่หนึ่งภายใน

BoundaryAssembly: "none" หมายความว่าเอาต์พุตของแต่ละโหนดถูกเขียนที่ที่เอาต์พุตนั้นไปอยู่ โดยไม่มีอะไรให้รวม ผู้ให้บริการที่รวมหลายส่วนประกอบเป็นเอกสารเดียวประกาศ merge หรือ nest แทน และมีเอ็กซ์พอร์ต assemble_boundary เพื่อทำเช่นนั้น นั่นเป็นสิ่งถัดไปที่เหมาะจะเพิ่มให้ผู้ให้บริการนี้ และบันทึกไว้ใน สัญญาของปลั๊กอิน

บิลด์เกสต์ใหม่ สร้างเอนทิตีขึ้นใหม่ แล้ววาง .wasm ใหม่กลับข้างเอนทิตี:

Terminal window
cd guest
GOWORK=off GOOS=wasip1 GOARCH=wasm go build -trimpath -buildvcs=false \
-ldflags "-X github.com/aggrega-ai/aggrega-pdk/go/pkg/aggrega.Version=0.1.0 \
-X github.com/aggrega-ai/aggrega-pdk/go/pkg/aggrega.Commit=walkthru" \
-buildmode=c-shared -o ../dotenv.wasm ./
cd ..
aggrega plugin gen-catalog --plugin dotenv.wasm \
--overlay overlay.yaml --output-dir dev-catalog/plugin
cp dotenv.wasm dev-catalog/plugin/provider/dotenv/0.1.0/provider--dotenv.wasm

เอนทิตีที่สร้างใหม่ตอนนี้มีสัญญา ซึ่งอ่านจากไบนารี:

[… omitted: the header comments and the metadata block, unchanged from the first generation]
spec:
abiVersion: v1beta1
exports:
- name: render_node
provider:
capabilities:
- dotenv
contract:
inputKinds:
artifactsStandalone: true
boundaryAssembly: none

การสร้างเอนทิตีใหม่หลังการบิลด์เกสต์ใหม่ทุกครั้งเป็นส่วนหนึ่งของวงจร เอนทิตีระบุซ้ำสิ่งที่ไบนารีรายงาน และทั้งสองถูกตรวจเทียบกันเมื่อโหลดเกสต์: เอ็กซ์พอร์ตหรือค่าสัญญาที่เอนทิตีอ้างแต่ไบนารีไม่รายงานคือความไม่ตรงกันที่บิลด์หยุด โดยระบุข้ออ้างนั้น ข้ออ้างที่เอนทิตีละไว้ไม่ถูกตรวจ เอนทิตีที่ถูกทิ้งไว้หลังการบิลด์ใหม่ที่เพิ่มสิ่งให้ไบนารี — อย่างที่ขั้นตอนนี้ทำ — จึงยังโหลดได้ แต่ไม่ได้บรรยายไบนารีอีก

9. บิลด์ที่เรนเดอร์

Terminal window
aggrega build --release release.yaml --auto-pull
warning: plugin "provider/dotenv" resolved from local with no recorded digest to check it against, so it runs unverified; it is read from beside its Plugin entity in a catalog directory on every build, and a pull records its digest only for a plugin attached at a transform role; otherwise publish the plugin and reference the published entity to have its digest recorded
Pull complete: release.lock (entities=5, source_artifacts=0, external_inputs=0)
[… omitted: a line reporting total build time, which differs on every run]
Build completed: 1 file written to <workspace>/dist

บรรทัดปิดระบุทรีเอาต์พุตเต็ม โดย <workspace> แทนรูทของพื้นที่ทำงาน ทรีที่เรนเดอร์อยู่ข้างใต้ทรีนั้น:

dist/infra/dotenv/app/app.env

และไฟล์ที่ผู้ให้บริการเขียน แบบครบถ้วน:

1.0.0
# aggrega.ai/boundary-path=infra
SERVICE_NAME=checkout
LOG_LEVEL=debug
GREETING="hello world"
ENDPOINT=https://checkout.example.net

ทุกการตัดสินใจของเกสต์มองเห็นได้ในเจ็ดบรรทัดนั้น

  • service-name และ log.level เข้ามาตามที่เขียนและออกไปเป็น SERVICE_NAME และ LOG_LEVEL ผู้เขียนไม่ต้องเขียนไวยากรณ์ของเป้าหมายเลย
  • GREETING ถูกใส่เครื่องหมายอัญประกาศและอีกสามตัวไม่ถูกใส่ hello world มีช่องว่าง หากไม่ใส่เครื่องหมายอัญประกาศจะจบที่ hello ส่วนปลายทางไม่มีอักขระใดที่รูปแบบอ่านเป็นไวยากรณ์ การใส่เครื่องหมายอัญประกาศจึงเพิ่มแต่สิ่งรบกวน
  • คีย์ที่มาสองคีย์เป็นความคิดเห็น เพราะไฟล์สภาพแวดล้อมไม่มีที่อื่นให้ใส่ ผู้ให้บริการที่เอาต์พุตมีบล็อกเมทาดาทาจะใส่คีย์เดียวกันไว้ที่นั่นแทน โฮสต์คำนวณคีย์ เกสต์เลือกตำแหน่ง
  • ค่ามาจากผู้เขียนสองคนที่ต่างกัน service_name, log_level และ greeting เป็นเจตนาของนักพัฒนา จาก Aggregate ส่วน endpoint เป็นของผู้ดำเนินการ จาก Release เกสต์ไม่เห็นความต่างทั้งสองเลย — ได้รับเนื้อหาที่ประเมินแล้วหนึ่งชุด

10. สองสิ่งที่ควรตรวจก่อนไว้ใจ

ว่าเอาต์พุตกำหนดแน่นอน เก็บเอาต์พุต บิลด์ใหม่ แล้วเปรียบเทียบ:

Terminal window
cp -r dist ../capture && rm -rf dist
aggrega build --release release.yaml
diff -r ../capture dist

diff ที่ไม่พิมพ์อะไรคือผลที่ต้องการ ความกำหนดแน่นอนเป็นสัญญาของเกสต์ ไม่ใช่ของโฮสต์ และเกสต์ที่วนซ้ำ Go map ที่ใดที่หนึ่งในเส้นทางเอาต์พุตจะไม่ผ่านการตรวจนี้ ทั้งที่ดูถูกต้องสมบูรณ์ในบิลด์ครั้งเดียว

ว่าการปฏิเสธของเกสต์ดังพอ ทำให้บรรทัดหนึ่งของอาร์ติแฟกต์ใน Recipe เสีย เพื่อให้ไม่มีค่าที่ประกาศ:

artifacts:
app.env: |
service-name = {{ .values.service_name }}
this line declares no value
[… omitted: the unverified-plugin warning, and the pull's lines saying the Release could not be rendered to discover its external inputs]
[CATALOG_ERROR] instance app
rendering node "app"
caused by: wasm: plugin "provider/dotenv:0.1.0" call to render_node: dotenv: app.env line 2: "this line declares no value" declares no value; an entry is KEY=value

ข้อความของเกสต์เองมาถึงผู้เขียนครบถ้วน พร้อมไฟล์และบรรทัดที่ข้อความมา ควรตรวจเรื่องนี้โดยเจตนา เพราะความล้มเหลวทางเลือกอื่น — เกสต์ที่ข้ามบรรทัดที่แยกวิเคราะห์ไม่ได้อย่างเงียบๆ — ทำให้ได้บิลด์ที่สำเร็จและการปรับใช้ที่ขาดตัวแปร โดยไม่มีสิ่งใดในเอาต์พุตบอก

11. สิ่งที่ควรเพิ่มต่อไป

ผู้ให้บริการที่สร้างที่นี่ครบถ้วนแต่น้อยที่สุด เรียงตามความถี่ที่แต่ละอย่างเป็นที่ต้องการอย่างคร่าวๆ:

  • assemble_boundary เพื่อรวมเอาต์พุตของหลายส่วนประกอบเป็นเอกสารเดียว ประกาศผ่าน BoundaryAssembly

  • validate_node_input เพื่อตรวจสอบอินพุตของโหนดเชิงโครงสร้างก่อนการเรนเดอร์ใดรัน อินพุตที่ผิดรูปจึงถูกรายงานที่เส้นแบ่งของการเขียน แทนที่จะกลางการเรนเดอร์

  • ข้อเท็จจริงของการเรนเดอร์ เพื่อรายงานบางอย่างเกี่ยวกับสิ่งที่เกสต์เขียน ซึ่งมีเพียงความรู้ของผู้ให้บริการนี้เรื่องรูปแบบไฟล์สภาพแวดล้อมที่มองเห็นได้ — ในที่นี้คือชื่อตัวแปรที่แต่ละไฟล์กำหนด ข้อเท็จจริงคือชื่อกับรายการ ซึ่งทั้งสองเป็นคำศัพท์ของผู้ให้บริการนี้เอง ส่งกลับไว้ข้างไฟล์ — และค่าเป็นอาร์เรย์ JSON เสมอ แม้มีรายการเดียว มิฉะนั้นบิลด์หยุด:

    // names lists the variables the files define, in the order they were written.
    value, err := json.Marshal(names)
    if err != nil {
    pdk.SetError(fmt.Errorf("dotenv: marshal render fact: %w", err))
    return 1
    }
    out := aggrega.RenderNodeOutput{
    Files: files,
    RenderFacts: []aggrega.RenderFact{{Name: "variable_names", Value: value}},
    }

    คอมไพเลอร์เก็บข้อเท็จจริงตามลำดับที่เกสต์ระบุ ตรวจเพียงว่าแต่ละค่าเป็นรายการ ส่งรายการให้เทมเพลตของ Feature ที่อ่านรายการเหล่านั้น และเขียนไฟล์เดียวกันไม่ว่าจะรายงานข้อเท็จจริงหรือไม่ บันทึกชื่อของแต่ละข้อเท็จจริงและสิ่งที่รายการของข้อเท็จจริงนั้นเก็บเป็นส่วนหนึ่งของสัญญาของผู้ให้บริการ

  • รายการที่ละออก เมื่อผู้ให้บริการเรนเดอร์ซอร์ส อินพุตของการเรนเดอร์ซอร์สอาจมี Omits ซึ่งเป็นรายการที่ระบุหน่วยที่เรนเดอร์ซึ่งต้องละไว้ ข้าง SourceListing ผู้ให้บริการที่ปฏิบัติตามจะละทุกหน่วยที่ตรงกันออกจากสิ่งที่ส่งกลับ รายงานจำนวนหนึ่งค่าต่อรายการใน Omitted และประกาศ Omits: true ในสัญญาของตน รายการเหล่านี้ไม่เคยมาพร้อมการเรนเดอร์แบบอาร์ติแฟกต์อย่างเดียวเช่นของผู้ให้บริการนี้ ผู้ให้บริการนี้จึงพบรายการเหล่านั้นหลังเรนเดอร์อินพุตแบบอิงซอร์สเท่านั้น การละหน่วยที่เรนเดอร์ออก ในสัญญาของปลั๊กอินมีรูปทรงทั้งหมด

  • การเผยแพร่ ซึ่งเปลี่ยนการจัดวาง dev-catalog ภายในเครื่องเป็นอาร์ติแฟกต์ที่ปักหมุดด้วยไดเจสต์ ซึ่ง Release หาค่าและยืนยันได้ คู่มือนักพัฒนาปลั๊กอิน ครอบคลุมวงจรชีวิตนั้น ภายใต้ขั้นตอนการเผยแพร่ ขั้นตอน pull และขั้นตอนบิลด์

เพย์โหลดและรูปทรงของผลลัพธ์ของแต่ละซ็อกเก็ตอยู่ใน สัญญาของปลั๊กอิน