Skip to content

Fix event driven reconciliation example#22

Merged
alexlovelltroy merged 8 commits intoOpenCHAMI:mainfrom
bmcdonald3:fix-reconciler
Nov 10, 2025
Merged

Fix event driven reconciliation example#22
alexlovelltroy merged 8 commits intoOpenCHAMI:mainfrom
bmcdonald3:fix-reconciler

Conversation

@bmcdonald3
Copy link
Member

This PR fixes a series of bugs in the code generator that prevented event-driven reconciliation from working out-of-the-box.

Previously, POST-ing a new resource (like a Rack) would succeed but would not trigger its associated reconciler. This was due to a couple of issues, including a disconnected event bus, improper resource initialization in handlers, and a data contract mismatch in the reconciler.

These changes modify the code generation templates to fix the identified issues, resulting in a fully functional reconciliation loop on a newly generated project.


List of Fixes

  • main.go.tmpl:

    • Fixes the "two event bus" bug by creating a single event bus instance.
    • This one bus is now correctly shared with both the global handlers (via events.SetGlobalEventBus) and the reconcile.Controller.
    • Explicitly calls events.SetEventConfig to ensure the event system is globally enabled before any handlers or controllers are initialized.
  • handlers.go.tmpl:

    • The Create function for all resources now correctly initializes metadata by setting CreatedAt/UpdatedAt timestamps.
    • For reconcilable resources, it now sets Status.Phase = "Pending" on creation. This is the change that gives the reconciler an initial state to act upon.
  • reconciler_generated.go.tmpl:

    • The generated Reconcile function now correctly unmarshals the json.RawMessage it receives from the event bus into a typed struct (e.g., *rack.Rack). This fixes a interface conversion panic.
    • The emission of the .reconciled event at the end of the function is commented out to prevent the infinite reconciliation loop I observed.
  • examples/04-rack-reconciliation/pkg/reconcilers/rack_reconciler.go:

    • Fixed a double-pointer bug where &rackResource (a **rack.Rack) was being passed to the client instead of rackResource (a *rack.Rack).

Testing

1. Server Logs

Before (Buggy):
The POST request is logged, but no reconciliation is triggered. The server is silent.

2025/11/10 11:08:23 "POST http://localhost:8080/racktemplates HTTP/1.1" ...
2025/11/10 11:08:34 "POST http://localhost:8080/racks HTTP/1.1" ...
(No further logs)

After (Fixed):
The POST request immediately triggers the reconciliation controller, which provisions all child resources.

2025/11/10 11:25:50 "POST http://localhost:8080/racks HTTP/1.1" ...
[DEBUG] Processing reconciliation for Rack/rack-ae572c8b (reason: Event: rack-inventory.resource.rack.created)
[DEBUG] Reconciling Rack Rack/rack-ae572c8b
[INFO] Using RackTemplate standard-rack: 2 chassis, 4 blades per chassis, 2 nodes per blade
[DEBUG] Created Chassis chas-c181ca03
[DEBUG] Created Blade blade-a70bd769
[DEBUG] Created BMC bmc-a4f26e2c
...
[INFO] Rack rack-01 provisioned: 2 chassis, 8 blades, 16 nodes, 8 BMCs
[DEBUG] Reconciliation successful for Rack/rack-ae572c8b

2. curl Response for POST /racks

Before (Buggy):
The POST response shows an empty phase in the status, failing to indicate that it's pending reconciliation.

$ curl -X POST http://localhost:8080/racks ...
{
  "apiVersion": "v1",
  "kind": "Rack",
  ...
  "status": {
-   "phase": "",
    "totalChassis": 0,
    ...
  }
}

After (Fixed):
The POST response now correctly shows "phase": "Pending", as set by the fixed handler template.

$ curl -X POST http://localhost:8080/racks ...
{
  "apiVersion": "v1",
  "kind": "Rack",
  ...
  "status": {
+   "phase": "Pending",
    "totalChassis": 0,
    ...
  }
}

And finally:

$ curl http://localhost:8080/racks/rack-ae572c8b | jq
{
  "apiVersion": "v1",
  "kind": "Rack",
  "schemaVersion": "v1",
  "metadata": {
    "name": "rack-01",
    "uid": "rack-ae572c8b",
    "createdAt": "2025-11-10T11:25:50.910672-08:00",
    "updatedAt": "2025-11-10T11:25:50-08:00"
  },
  "spec": {
    "templateUID": "rktmpl-ec5e5cd1",
    "location": ""
  },
  "status": {
    "phase": "Ready",
    "chassisUIDs": [
      "chas-c181ca03",
      "chas-9703abb6"
    ],
    "totalChassis": 2,
    "totalBlades": 8,
    "totalNodes": 16,
    "totalBMCs": 8,
    "conditions": [
      {
        "type": "Ready",
        "status": "True",
        "reason": "ReconcileSuccess",
        "message": "Reconciliation successful",
        "lastTransitionTime": "2025-11-10T11:25:50-08:00"
      }
    ]
  }
}

Signed-off-by: Ben McDonald <ben.mcdonald@hpe.com>
Signed-off-by: Ben McDonald <ben.mcdonald@hpe.com>
Signed-off-by: Ben McDonald <ben.mcdonald@hpe.com>
Signed-off-by: Ben McDonald <ben.mcdonald@hpe.com>
Signed-off-by: Ben McDonald <ben.mcdonald@hpe.com>
Signed-off-by: Ben McDonald <ben.mcdonald@hpe.com>
Signed-off-by: Ben McDonald <ben.mcdonald@hpe.com>
Signed-off-by: Ben McDonald <ben.mcdonald@hpe.com>
@alexlovelltroy alexlovelltroy merged commit ab4249a into OpenCHAMI:main Nov 10, 2025
3 checks passed
@bmcdonald3 bmcdonald3 deleted the fix-reconciler branch November 10, 2025 20:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants