invoke starts a goroutine for reconciling the resource and tracks the invocation in c.invocations. It must be called while c.mu is held.
(r *runtimev1.Resource)
| 1279 | // invoke starts a goroutine for reconciling the resource and tracks the invocation in c.invocations. |
| 1280 | // It must be called while c.mu is held. |
| 1281 | func (c *Controller) invoke(r *runtimev1.Resource) error { |
| 1282 | // Set status to running |
| 1283 | n := r.Meta.Name |
| 1284 | err := c.catalog.updateStatus(n, runtimev1.ReconcileStatus_RECONCILE_STATUS_RUNNING, time.Time{}) |
| 1285 | if err != nil { |
| 1286 | return fmt.Errorf("error updating dag node %q: %w", nameStr(n), err) |
| 1287 | } |
| 1288 | |
| 1289 | // Track invocation |
| 1290 | ctx, cancel := context.WithCancel(context.Background()) |
| 1291 | inv := &invocation{ |
| 1292 | name: n, |
| 1293 | isHidden: r.Meta.Hidden, |
| 1294 | isDelete: r.Meta.DeletedOn != nil, |
| 1295 | isRename: r.Meta.RenamedFrom != nil, |
| 1296 | startedOn: time.Now(), |
| 1297 | cancelFn: cancel, |
| 1298 | } |
| 1299 | c.invocations[nameStr(n)] = inv |
| 1300 | |
| 1301 | // Log invocation |
| 1302 | logArgs := []zap.Field{zap.String("name", n.Name), zap.String("type", PrettifyResourceKind(n.Kind))} |
| 1303 | if inv.isDelete { |
| 1304 | logArgs = append(logArgs, zap.Bool("deleted", inv.isDelete)) |
| 1305 | } |
| 1306 | if inv.isRename { |
| 1307 | logArgs = append(logArgs, zap.String("renamed_from", r.Meta.RenamedFrom.Name)) |
| 1308 | } |
| 1309 | c.Logger.Info("Reconciling resource", logArgs...) |
| 1310 | |
| 1311 | // Start reconcile in background |
| 1312 | ctx = contextWithInvocation(ctx, inv) |
| 1313 | go func() { |
| 1314 | defer func() { |
| 1315 | // Catch panics and set as error |
| 1316 | if r := recover(); r != nil { |
| 1317 | stack := make([]byte, 64<<10) |
| 1318 | stack = stack[:runtime.Stack(stack, false)] |
| 1319 | c.Logger.Error("panic in reconciler", zap.String("name", n.Name), zap.String("type", n.Kind), zap.Any("error", r), zap.String("stack", string(stack))) |
| 1320 | |
| 1321 | inv.result = ReconcileResult{Err: fmt.Errorf("panic: %v", r)} |
| 1322 | if inv.holdsLock { |
| 1323 | c.Unlock(ctx) |
| 1324 | } |
| 1325 | } |
| 1326 | // Ensure ctx cancel is called (just for cleanup) |
| 1327 | cancel() |
| 1328 | // Send invocation to event loop for post-processing |
| 1329 | c.completed <- inv |
| 1330 | }() |
| 1331 | |
| 1332 | // Start tracing span |
| 1333 | reconciler := c.reconciler(n.Kind) |
| 1334 | tracerAttrs := []attribute.KeyValue{ |
| 1335 | attribute.String("instance_id", c.InstanceID), |
| 1336 | attribute.String("name", n.Name), |
| 1337 | attribute.String("type", PrettifyResourceKind(n.Kind)), |
| 1338 | } |
no test coverage detected