{"modulePath":"github.com/bborbe/agent-task-executor","version":"v0.71.2","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package spawner\n\n## Variables\n\n```go\nvar ErrTaskContentTooLarge = stderrors.New(\"task_content_too_large\")\n```\nErrTaskContentTooLarge marks a task whose rendered TASK\\_CONTENT env entry exceeds MAX\\_ARG\\_STRLEN. The condition is deterministic — no redelivery can shrink the body — so the consumer must classify it as a permanent spawn error, dead-letter the event and skip it once. Retrying instead would return the error from the consume loop, restart the executor with the Kafka offset unmarked, and redeliver the identical message forever: one oversized task would become an executor-wide crash loop rather than a single failed task.\n\n## Types\n\n```go\ntype JobSpawner interface {\n\t// SpawnJob creates a K8s Job for the task and returns the job name.\n\t// Returns the job name even when the job already exists (idempotent).\n\t// sessionID is stamped onto the Job as CLAUDE_SESSION_ID so the agent CLI\n\t// runs under the same id the caller recorded in the task frontmatter.\n\tSpawnJob(\n\t\tctx context.Context,\n\t\ttask lib.Task,\n\t\tconfig pkg.AgentConfiguration,\n\t\tsessionID string,\n\t) (string, error)\n\t// IsJobActive returns true if an active (not completed/failed) K8s Job exists\n\t// for the given task identifier in namespace. Uses the\n\t// agent.benjamin-borbe.de/task-id label set by SpawnJob.\n\tIsJobActive(\n\t\tctx context.Context,\n\t\tnamespace k8s.Namespace,\n\t\ttaskIdentifier lib.TaskIdentifier,\n\t) (bool, error)\n\t// CountActiveJobs returns the number of non-terminal K8s Jobs currently\n\t// running for the given assignee in namespace. Uses the\n\t// agent.benjamin-borbe.de/assignee label set by SpawnJob. Jobs spawned before\n\t// that label existed are not counted, so the cap under-counts during the\n\t// rollout and never over-counts.\n\tCountActiveJobs(ctx context.Context, namespace k8s.Namespace, assignee string) (int, error)\n\t// ListActiveJobs returns the set of task identifiers that currently have an\n\t// active (not completed/failed) Job for the assignee in namespace, from ONE\n\t// list call. IsJobActive and CountActiveJobs are both answered from it, so a\n\t// reconcile pass that evaluates N tasks costs one call per assignee instead\n\t// of 2N.\n\t// Added 2026-10-19: the per-task path issued two list calls each, and a pass\n\t// over ~70 eligible tasks consumed its whole budget in client-go's default\n\t// 5 QPS rate limiter before reaching the spawn.\n\tListActiveJobs(\n\t\tctx context.Context,\n\t\tnamespace k8s.Namespace,\n\t\tassignee string,\n\t) (map[lib.TaskIdentifier]bool, error)\n\t// JobExists reports whether a Job with this name still exists in namespace.\n\t// It answers the question IsJobActive cannot: a Job that finished and was\n\t// garbage-collected is gone from here, while one another cluster created was\n\t// never here at all. The spawn guard uses it to tell a live foreign Job from\n\t// a stale entry in this executor's own store.\n\tJobExists(ctx context.Context, namespace k8s.Namespace, jobName string) (bool, error)\n}\n```\nJobSpawner creates a K8s Job for a task.\n\n```go\nfunc NewJobSpawner(\n\tkubeClient kubernetes.Interface,\n\tnamespace k8s.Namespace,\n\tkafkaBrokers string,\n\tbranch string,\n\ttopicPrefix string,\n\tcurrentDateTimeGetter libtime.CurrentDateTimeGetter,\n\tjobTTLSecondsAfterFinished int32,\n\tjobKafkaClientCertSecret string,\n\tjobKafkaCaCertSecret string,\n\tjobFsGroup int64,\n) JobSpawner\n```\nNewJobSpawner creates a new JobSpawner backed by the K8s batch/v1 API. jobTTLSecondsAfterFinished controls how long completed Job pods survive before Kubernetes' TTL controller garbage-collects them. jobKafkaClientCertSecret and jobKafkaCaCertSecret are the names of K8s secrets to mount into spawned Jobs for Kafka mTLS; empty strings mean no cert mounting. jobFsGroup is the pod-level fsGroup stamped onto spawned Jobs when cert mounting is active (see applyKafkaCertVolumes); 0 disables it.\n\n```go\ntype PodQuotaLimiter interface {\n\t// PodQuota returns the pod limit of the ResourceQuota whose scopeSelector\n\t// matches PriorityClass In [priorityClassName] in namespace.\n\t//\n\t// bounded distinguishes \"no quota scopes this class\" from \"the quota admits\n\t// zero pods\". Without it a quota carrying hard.pods: 0 would be\n\t// indistinguishable from an absent one, admission would fall back to the\n\t// declared cap, and the executor would claim work the quota can never start —\n\t// the exact defect this reader exists to remove, in the most restrictive\n\t// configuration.\n\tPodQuota(\n\t\tctx context.Context,\n\t\tnamespace libk8s.Namespace,\n\t\tpriorityClassName string,\n\t) (limit int, bounded bool, err error)\n}\n```\nPodQuotaLimiter reports the pod capacity the namespace ResourceQuota enforces for one agent priority class.\n\n```go\nfunc NewPodQuotaLimiter(\n\tkubeClient kubernetes.Interface,\n\tcurrentDateTime libtime.CurrentDateTimeGetter,\n) PodQuotaLimiter\n```\nNewPodQuotaLimiter creates a PodQuotaLimiter backed by the K8s core/v1 API.\n\n```go\ntype ServiceReconcileLoop interface {\n\t// ReconcileOnce runs one full pass over the known Configs.\n\tReconcileOnce(ctx context.Context) error\n\t// Run reconciles once immediately, then on every tick until ctx is cancelled.\n\tRun(ctx context.Context) error\n}\n```\nServiceReconcileLoop ensures one StatefulSet exists per Config whose spec.type is service, and removes the StatefulSet when a Config stops being one.\n\nIt is deliberately a periodic pass rather than an informer hook: the Config informer already exists to populate the resolver's store, and a loop keeps the two concerns separate while giving the same convergence. A Config deleted entirely is collected by the ownerRef only when its copy sits in the executor's own namespace; a cross-namespace copy carries none, so the same pass that reconciles also reaps it (see ReapOrphanedCopies), and the \"stopped being a service\" transition needs the explicit undeploy.\n\n```go\nfunc NewServiceReconcileLoop(\n\tprovider k8s.Provider[agentv1.Config],\n\tresolver pkg.ConfigResolver,\n\treconciler ServiceReconciler,\n\tinterval time.Duration,\n) ServiceReconcileLoop\n```\nNewServiceReconcileLoop returns a loop that reconciles service Configs read from provider, resolving each one through resolver.\n\n```go\ntype ServiceReconciler interface {\n\t// ReconcileService creates or updates the StatefulSet for a service Config.\n\t// It is idempotent: an existing StatefulSet has only its mutable fields\n\t// merged, so an image change rolls the pod and an unchanged Config is a no-op.\n\tReconcileService(\n\t\tctx context.Context,\n\t\tconfig agentv1.Config,\n\t\tresolved pkg.AgentConfiguration,\n\t) error\n\t// UndeployService removes the StatefulSet a Config owns, in the Config's\n\t// resolved namespace — but only if the executor actually owns it. It also\n\t// sweeps every remaining namespace for copies this Config left behind when\n\t// its serviceNamespace changed.\n\t//\n\t// The ownership check is not defensive decoration. The loop calls this for\n\t// every Config that is not a service, and a StatefulSet's name here is just\n\t// the Config's name, which any other chart in the namespace may also use\n\t// (recurring-task-creator is itself a StatefulSet). Deleting by name alone\n\t// would therefore let a job Config tear down an unrelated workload.\n\t//\n\t// Idempotent and honest: a missing StatefulSet is not an error (the ownerRef\n\t// may already have collected it), and a StatefulSet this executor does not\n\t// own is left untouched and reported as such, never as a removal.\n\tUndeployService(ctx context.Context, config agentv1.Config) error\n\t// ReapOrphanedCopies deletes cross-namespace StatefulSets whose Config no longer\n\t// exists, so deleting a CR does not leave a workload running forever with the\n\t// Config's Secret and PVC. Called once per reconcile pass, not per Config.\n\tReapOrphanedCopies(ctx context.Context, configs []agentv1.Config)\n}\n```\nServiceReconciler reconciles one long-running StatefulSet per Config whose spec.type is service.\n\nIt is the identity-shape counterpart to JobSpawner. Where that creates a short-lived Job per task and phase, this creates a workload that outlives any single task and is addressed directly. Both read the same Config CR, and the discriminator between them is ConfigSpec.Type.\n\n```go\nfunc NewServiceReconciler(\n\tdeployer k8s.StatefulSetDeployer,\n\tkubeClient kubernetes.Interface,\n\tnamespace k8s.Namespace,\n\tstorageClass string,\n) ServiceReconciler\n```\nNewServiceReconciler returns a ServiceReconciler that deploys StatefulSets through the given deployer.\n\n","path":"github.com/bborbe/agent-task-executor/pkg/spawner","name":"spawner","synopsis":"","isRedistributable":true}
