Simple guides introducing Fabrica with hands-on examples.
Understanding Fabrica’s resource structure, UID generation, labels, annotations, and lifecycle.
Fabrica follows the Kubernetes resource pattern with a flattened envelope structure. All resources explicitly define standard fields:
import "github.com/openchami/fabrica/pkg/fabrica"
type Device struct {
APIVersion string `json:"apiVersion"` // "v1" or "example.fabrica.dev/v1"
Kind string `json:"kind"` // "Device", "User", "Product"
Metadata fabrica.Metadata `json:"metadata"` // Name, UID, labels, annotations, timestamps
Spec DeviceSpec `json:"spec"` // Desired state (you define)
Status DeviceStatus `json:"status"` // Observed state (you define)
}
// The Metadata struct provides:
type Metadata struct {
Name string `json:"name"`
UID string `json:"uid"`
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
Migration Note: If you’re familiar with older Fabrica versions that used
resource.Resourceembedding, the current pattern uses explicit fields withfabrica.Metadatafor better clarity and flexibility.
The version of the API that defines this resource.
APIVersion: "v1" // Stable version
APIVersion: "v2beta1" // Beta version
APIVersion: "v3alpha1" // Alpha version
Usage:
The type of resource.
Kind: "Device" // IoT device
Kind: "User" // User account
Kind: "Product" // Product catalog item
Convention: Use PascalCase singular nouns.
Standard metadata for all resources:
type Metadata struct {
UID string // Unique identifier
Name string // Human-readable name
Labels map[string]string // Queryable key-value pairs
Annotations map[string]string // Non-queryable metadata
CreatedAt time.Time // Creation timestamp
UpdatedAt time.Time // Last update timestamp
}
Your custom specification - what you want:
type DeviceSpec struct {
Name string `json:"name"`
Location string `json:"location"`
Model string `json:"model"`
Config map[string]string `json:"config,omitempty"`
}
Principles:
Your custom status - what actually is:
type DeviceStatus struct {
Active bool `json:"active"`
LastSeen string `json:"lastSeen,omitempty"`
IPAddress string `json:"ipAddress,omitempty"`
Health string `json:"health,omitempty"`
Conditions []Condition `json:"conditions,omitempty"`
}
Principles:
Fabrica generates prefixed UIDs to make resource identifiers easy to recognize.
<prefix>_<26 lowercase base32 characters encoding a UUIDv7>
Examples:
dev_01h455vb4pex5vsknk084sn02q (Device)
usr_01h455vb4pex5vsknk084sn02q (User)
prd_01h455vb4pex5vsknk084sn02q (Product)
func init() {
resource.RegisterResourcePrefix("Device", "dev")
resource.RegisterResourcePrefix("User", "usr")
resource.RegisterResourcePrefix("Product", "prd")
}
New prefixes must contain at most 63 lowercase ASCII letters or internal underscores, begin and end with a letter, and be nonempty. Existing legacy alphanumeric registrations remain supported for reads and kind lookup, including numeric prefixes. New UID generation rejects numeric prefixes.
Automatic (recommended):
// Framework generates UID automatically on create
device := &Device{
Spec: DeviceSpec{Name: "Sensor 1"},
}
// UID will be generated: dev_01h455vb4pex5vsknk084sn02q
Manual:
uid, err := resource.GenerateUIDForResource("Device")
// Returns: "dev_01h455vb4pex5vsknk084sn02q"
// Deprecated helper: explicitly generate a legacy UID
uid, err := resource.GenerateUIDWithLength("dev", 12)
// Returns: "dev-1a2b3c4d5e6f"
Parsing and validation accept both legacy prefix-hex UIDs and TypeID 0.3.0
strings. New identifiers use TypeID/UUIDv7. ParseUID keeps its current
signature; its second return value is the original hex or base32 suffix.
// Parse UID
prefix, random, err := resource.ParseUID("dev-deadbeef")
// prefix = "dev", random = "deadbeef" (legacy ID)
// Validate UID
valid := resource.IsValidUID("dev_01h455vb4pex5vsknk084sn02q") // true
// Get resource type from UID
kind, err := resource.GetResourceTypeFromUID("dev_01h455vb4pex5vsknk084sn02q")
// Returns: "Device"
prefix, suffix, err := resource.ParseUID("dev_01h455vb4pex5vsknk084sn02q")
// prefix = "dev", suffix = "01h455vb4pex5vsknk084sn02q"
valid = resource.IsValidUID("dev_01h455vb4pex5vsknk084sn02q") // true
kind, err = resource.GetResourceTypeFromUID("dev_01h455vb4pex5vsknk084sn02q")
// Returns: "Device" if "dev" is registered
Parsing does not require registration, modify identifiers, or emit deprecation logs. TypeID parsing accepts spec-valid UUID payloads of any version and bare suffixes without a prefix; resource-type lookup still requires a registered prefix. Legacy acceptance rules, including uppercase hex and empty parts, are preserved. The text of parsing errors may differ from earlier releases.
Use uid.Codec.Parse for the format and decoded
UUID. See the prefix audit before
preparing a service for TypeID generation.
Rebuilding with the upgraded Fabrica runtime changes newly assigned UIDs immediately. Existing generated handlers already call the runtime helper; there is no legacy-default toggle. Regenerate with the matching Fabrica binary to refresh OpenAPI descriptions and examples, then tidy, build, and test the service. Existing identifiers, URLs, references, and stored file names are not converted.
New UIDs use an underscore separator and a 26-character suffix. Allow up to 90 characters for a resource UID (63-character prefix plus separator and suffix). Update custom regular expressions, client-side splitting, and any restricted database columns. Keep accepting legacy identifiers for reads.
Audit custom prefix registrations before upgrading. Numeric or overlong legacy prefixes still register for lookup, but creation under those prefixes returns an error. Generated services with incompatible route-derived prefixes fail generation before output is written. Do not automatically rename an existing kind’s prefix: it can break lookup of its old identities. Define and test an explicit compatibility mapping before changing such a registration. See the prefix compatibility guide. For release checks, mixed-store verification, and rollback guidance, follow the UID migration guide.
Human-readable identifier:
device.Metadata.Name = "temperature-sensor-01"
Best Practices:
System-generated unique identifier:
device.Metadata.UID = "dev_01h455vb4pex5vsknk084sn02q"
Best Practices:
/devices/dev_01h455vb4pex5vsknk084sn02q)Automatically managed:
device.Metadata.CreatedAt = time.Now() // On create
device.Metadata.UpdatedAt = time.Now() // On every update
Utilities:
// How old is the resource?
age := device.Age()
// When was it last updated?
lastUpdate := device.LastUpdated()
// Mark as updated
device.Touch()
Queryable key-value pairs for selection and grouping:
device.SetLabel("environment", "production")
device.SetLabel("location", "datacenter-01")
device.SetLabel("team", "platform")
Use labels for:
?label=environment=production)Example queries:
// Get label
env, exists := device.GetLabel("environment")
// Check if label exists
if device.HasLabel("critical") {
// Handle critical device
}
// Match multiple labels
selector := map[string]string{
"environment": "production",
"location": "datacenter-01",
}
if device.MatchesLabels(selector) {
// Device matches criteria
}
// Get all labels
labels := device.GetLabels()
for key, value := range labels {
fmt.Printf("%s=%s\n", key, value)
}
Non-queryable metadata for additional context:
device.SetAnnotation("description", "Primary temperature sensor for cold storage")
device.SetAnnotation("contact.email", "ops@example.com")
device.SetAnnotation("purchased.date", "2024-01-15")
device.SetAnnotation("warranty.expires", "2027-01-15")
Use annotations for:
Example usage:
// Get annotation
desc, exists := device.GetAnnotation("description")
// Get all annotations
annotations := device.GetAnnotations()
// Remove annotation
device.RemoveAnnotation("old-field")
| Use Case | Use Labels | Use Annotations |
|---|---|---|
| Filtering/querying | ✅ | ❌ |
| Grouping resources | ✅ | ❌ |
| Human documentation | ❌ | ✅ |
| External references | ❌ | ✅ |
| Configuration data | ❌ | ✅ |
// Create resource
device := &Device{
APIVersion: "infra.example.io/v1",
Kind: "Device",
Metadata: Metadata{},
Spec: DeviceSpec{
Name: "Temperature Sensor",
Location: "Warehouse A",
},
}
// Metadata is initialized
device.Metadata.Initialize("temp-sensor-01", uid)
device.SetLabel("location", "warehouse-a")
// Save to storage
storage.Save(ctx, device)
What happens:
dev_01h455vb4pex5vsknk084sn02qCreatedAt, UpdatedAt// Get by UID
device, err := storage.Load(ctx, "dev_01h455vb4pex5vsknk084sn02q")
// List all
devices, err := storage.LoadAll(ctx)
// Filter by labels (application level)
productionDevices := []Device{}
for _, d := range devices {
if d.Metadata.Labels["environment"] == "production" {
productionDevices = append(productionDevices, d)
}
}
// Load resource
device, err := storage.Load(ctx, "dev_01h455vb4pex5vsknk084sn02q")
// Update spec (desired state)
device.Spec.Location = "Warehouse B"
// Update label
device.SetLabel("location", "warehouse-b")
// Update status (observed state)
device.Status.IPAddress = "192.168.1.100"
device.Status.LastSeen = time.Now().Format(time.RFC3339)
// Mark as updated
device.Touch()
// Save
storage.Save(ctx, device)
What happens:
UpdatedAt timestamp refreshed// Delete by UID
err := storage.Delete(ctx, "dev_01h455vb4pex5vsknk084sn02q")
What happens:
DO:
✅ Use flattened envelope structure
✅ Use json tags
✅ Separate Spec and Status
✅ Include APIVersion and Kind fields
✅ Add validation methods
type Device struct {
APIVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Metadata Metadata `json:"metadata"`
Spec DeviceSpec `json:"spec"`
Status DeviceStatus `json:"status,omitempty"`
}
func (d *Device) Validate() error {
if d.Spec.Name == "" {
return fmt.Errorf("name required")
}
return nil
}
DON’T:
❌ Mix Spec and Status fields
❌ Forget json tags
❌ Use UUID for UID
❌ Store computed values in Spec
type BadDevice struct {
Name string // Should be in Spec
Online bool // Should be in Status
}
DO:
✅ Use for queryable attributes
✅ Use lowercase with hyphens
✅ Keep values simple
✅ Use consistent naming
device.SetLabel("environment", "production")
device.SetLabel("team", "platform")
device.SetLabel("location", "us-west-2")
DON’T:
❌ Store large values
❌ Use for documentation
❌ Include sensitive data
❌ Use inconsistent formats
device.SetLabel("Description", "A very long description...")
device.SetLabel("api_key", "secret123")
DO:
✅ Use for documentation
✅ Store external references
✅ Include context
✅ Use structured keys
device.SetAnnotation("description", "Primary sensor")
device.SetAnnotation("contact.email", "ops@example.com")
device.SetAnnotation("external.id", "EXT-12345")
device.SetAnnotation("docs.url", "https://docs.example.com/devices/temp")
DO:
✅ Include conditions
✅ Add health indicators
✅ Record timestamps
✅ Show actual state
type DeviceStatus struct {
Online bool `json:"online"`
LastSeen string `json:"lastSeen,omitempty"`
Health string `json:"health,omitempty"`
Conditions []Condition `json:"conditions,omitempty"`
}
Condition Pattern:
Conditions follow Kubernetes conventions and automatically publish events when changed:
import "github.com/openchami/fabrica/pkg/resource"
type Condition struct {
Type string `json:"type"` // "Ready", "Healthy", "Available"
Status string `json:"status"` // "True", "False", "Unknown"
LastTransitionTime time.Time `json:"lastTransitionTime"` // Auto-set when status changes
Reason string `json:"reason,omitempty"` // Machine-readable reason
Message string `json:"message,omitempty"` // Human-readable description
}
// Manual condition setting
device.Status.Conditions = []Condition{
{
Type: "Ready",
Status: "True",
Reason: "DeviceOnline",
Message: "Device is ready to accept commands",
LastTransitionTime: time.Now(),
},
}
// Recommended: Use helper functions (publishes events automatically)
ctx := context.Background()
changed := resource.SetResourceCondition(ctx, device,
"Ready", "True", "DeviceOnline", "Device is operational")
if changed {
// CloudEvent published: "io.fabrica.condition.ready"
log.Println("Ready condition changed")
}
// Common condition patterns
resource.SetResourceCondition(ctx, device, "Healthy", "True", "HealthCheckPassed", "All health checks passing")
resource.SetResourceCondition(ctx, device, "Available", "False", "Maintenance", "Device under maintenance")
resource.SetResourceCondition(ctx, device, "Connected", "Unknown", "NetworkTimeout", "Network connectivity uncertain")
Condition Event Integration:
{prefix}.condition.{type} (e.g., io.fabrica.condition.ready)Fabrica resources provide:
Next Steps:
Questions? GitHub Issues