|
| 1 | +/* |
| 2 | +Copyright 2026. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +*/ |
| 10 | + |
| 11 | +package controller |
| 12 | + |
| 13 | +import ( |
| 14 | + "context" |
| 15 | + "fmt" |
| 16 | + |
| 17 | + "github.com/go-logr/logr" |
| 18 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 19 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 20 | + "k8s.io/client-go/tools/events" |
| 21 | + ctrl "sigs.k8s.io/controller-runtime" |
| 22 | + "sigs.k8s.io/controller-runtime/pkg/builder" |
| 23 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 24 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 26 | + |
| 27 | + "github.com/kagent-dev/kagent/go/api/v1alpha2" |
| 28 | + "github.com/kagent-dev/kagent/go/core/pkg/sandboxbackend" |
| 29 | +) |
| 30 | + |
| 31 | +// OpenShellAgentHarnessController reconciles AgentHarness resources that use the |
| 32 | +// OpenShell runtime. |
| 33 | +type OpenShellAgentHarnessController struct { |
| 34 | + Client client.Client |
| 35 | + Recorder events.EventRecorder |
| 36 | + OpenClawBackend sandboxbackend.AsyncBackend |
| 37 | + HermesBackend sandboxbackend.AsyncBackend |
| 38 | +} |
| 39 | + |
| 40 | +func (r *OpenShellAgentHarnessController) backendFor(ah *v1alpha2.AgentHarness) sandboxbackend.AsyncBackend { |
| 41 | + switch ah.Spec.Backend { |
| 42 | + case v1alpha2.AgentHarnessBackendOpenClaw, v1alpha2.AgentHarnessBackendNemoClaw: |
| 43 | + return r.OpenClawBackend |
| 44 | + case v1alpha2.AgentHarnessBackendHermes: |
| 45 | + return r.HermesBackend |
| 46 | + default: |
| 47 | + return nil |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func (r *OpenShellAgentHarnessController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { |
| 52 | + log := ctrl.LoggerFrom(ctx).WithValues("agentHarness", req.NamespacedName) |
| 53 | + |
| 54 | + var ah v1alpha2.AgentHarness |
| 55 | + if err := r.Client.Get(ctx, req.NamespacedName, &ah); err != nil { |
| 56 | + if apierrors.IsNotFound(err) { |
| 57 | + return ctrl.Result{}, nil |
| 58 | + } |
| 59 | + return ctrl.Result{}, fmt.Errorf("get AgentHarness: %w", err) |
| 60 | + } |
| 61 | + if effectiveAgentHarnessRuntime(&ah) != v1alpha2.AgentHarnessRuntimeOpenshell { |
| 62 | + return ctrl.Result{}, nil |
| 63 | + } |
| 64 | + |
| 65 | + if !ah.DeletionTimestamp.IsZero() { |
| 66 | + return r.reconcileDelete(ctx, &ah) |
| 67 | + } |
| 68 | + |
| 69 | + if controllerutil.AddFinalizer(&ah, agentHarnessFinalizer) { |
| 70 | + if err := r.Client.Update(ctx, &ah); err != nil { |
| 71 | + return ctrl.Result{}, fmt.Errorf("add finalizer: %w", err) |
| 72 | + } |
| 73 | + return ctrl.Result{Requeue: true}, nil |
| 74 | + } |
| 75 | + |
| 76 | + backend := r.backendFor(&ah) |
| 77 | + if backend == nil { |
| 78 | + return reconcileBackendUnavailable(ctx, r.Client, &ah, v1alpha2.AgentHarnessRuntimeOpenshell) |
| 79 | + } |
| 80 | + |
| 81 | + return r.reconcileBackend(ctx, req, &ah, backend, log) |
| 82 | +} |
| 83 | + |
| 84 | +func (r *OpenShellAgentHarnessController) reconcileBackend(ctx context.Context, req ctrl.Request, ah *v1alpha2.AgentHarness, backend sandboxbackend.AsyncBackend, log logr.Logger) (ctrl.Result, error) { |
| 85 | + res, err := backend.EnsureAgentHarness(ctx, ah) |
| 86 | + if err != nil { |
| 87 | + log.Error(err, "EnsureAgentHarness failed") |
| 88 | + setAgentHarnessCondition(ah, v1alpha2.AgentHarnessConditionTypeAccepted, metav1.ConditionFalse, |
| 89 | + "EnsureFailed", err.Error()) |
| 90 | + setAgentHarnessCondition(ah, v1alpha2.AgentHarnessConditionTypeReady, metav1.ConditionFalse, |
| 91 | + "EnsureFailed", err.Error()) |
| 92 | + if perr := patchAgentHarnessStatus(ctx, r.Client, ah); perr != nil { |
| 93 | + return ctrl.Result{}, perr |
| 94 | + } |
| 95 | + return ctrl.Result{}, err |
| 96 | + } |
| 97 | + |
| 98 | + ah.Status.BackendRef = &v1alpha2.AgentHarnessStatusRef{ |
| 99 | + Backend: ah.Spec.Backend, |
| 100 | + ID: res.Handle.ID, |
| 101 | + } |
| 102 | + if res.Endpoint != "" { |
| 103 | + ah.Status.Connection = &v1alpha2.AgentHarnessConnection{Endpoint: res.Endpoint} |
| 104 | + } |
| 105 | + setAgentHarnessCondition(ah, v1alpha2.AgentHarnessConditionTypeAccepted, metav1.ConditionTrue, |
| 106 | + "AgentHarnessAccepted", "backend accepted sandbox request") |
| 107 | + |
| 108 | + st, reason, msg := backend.GetStatus(ctx, res.Handle) |
| 109 | + pending := postReadyBootstrapPending(ah) |
| 110 | + if st == metav1.ConditionTrue && pending { |
| 111 | + setAgentHarnessCondition(ah, v1alpha2.AgentHarnessConditionTypeActorReady, st, reason, msg) |
| 112 | + setAgentHarnessCondition(ah, v1alpha2.AgentHarnessConditionTypeBootstrapReady, metav1.ConditionFalse, |
| 113 | + "BootstrapPending", |
| 114 | + "waiting for post-ready bootstrap (OnAgentHarnessReady) to finish") |
| 115 | + setAgentHarnessCondition(ah, v1alpha2.AgentHarnessConditionTypeReady, metav1.ConditionFalse, |
| 116 | + "BootstrapPending", |
| 117 | + "gateway sandbox is ready; waiting for post-ready bootstrap (OnAgentHarnessReady) to finish") |
| 118 | + } else { |
| 119 | + setAgentHarnessCondition(ah, v1alpha2.AgentHarnessConditionTypeActorReady, st, reason, msg) |
| 120 | + if pending { |
| 121 | + setAgentHarnessCondition(ah, v1alpha2.AgentHarnessConditionTypeBootstrapReady, metav1.ConditionFalse, |
| 122 | + "ActorNotReady", "waiting for actor before post-ready bootstrap") |
| 123 | + } |
| 124 | + setAgentHarnessCondition(ah, v1alpha2.AgentHarnessConditionTypeReady, st, reason, msg) |
| 125 | + } |
| 126 | + ah.Status.ObservedGeneration = ah.Generation |
| 127 | + |
| 128 | + if err := patchAgentHarnessStatus(ctx, r.Client, ah); err != nil { |
| 129 | + return ctrl.Result{}, err |
| 130 | + } |
| 131 | + |
| 132 | + if st != metav1.ConditionTrue { |
| 133 | + return ctrl.Result{RequeueAfter: agentHarnessNotReadyRequeue}, nil |
| 134 | + } |
| 135 | + if pending { |
| 136 | + if err := maybePostReadyBootstrap(ctx, client.ObjectKeyFromObject(ah), ah, res.Handle, backend); err != nil { |
| 137 | + log.Error(err, "post-ready sandbox bootstrap failed") |
| 138 | + return ctrl.Result{}, err |
| 139 | + } |
| 140 | + var latest v1alpha2.AgentHarness |
| 141 | + if err := r.Client.Get(ctx, req.NamespacedName, &latest); err != nil { |
| 142 | + return ctrl.Result{}, fmt.Errorf("get AgentHarness after bootstrap: %w", err) |
| 143 | + } |
| 144 | + st2, reason2, msg2 := backend.GetStatus(ctx, res.Handle) |
| 145 | + setAgentHarnessCondition(&latest, v1alpha2.AgentHarnessConditionTypeActorReady, st2, reason2, msg2) |
| 146 | + setAgentHarnessCondition(&latest, v1alpha2.AgentHarnessConditionTypeBootstrapReady, metav1.ConditionTrue, |
| 147 | + "BootstrapComplete", "post-ready bootstrap completed") |
| 148 | + setAgentHarnessCondition(&latest, v1alpha2.AgentHarnessConditionTypeReady, st2, reason2, msg2) |
| 149 | + latest.Status.ObservedGeneration = latest.Generation |
| 150 | + if err := r.Client.Status().Update(ctx, &latest); err != nil { |
| 151 | + return ctrl.Result{}, fmt.Errorf("update AgentHarness status after bootstrap: %w", err) |
| 152 | + } |
| 153 | + } |
| 154 | + return ctrl.Result{}, nil |
| 155 | +} |
| 156 | + |
| 157 | +func (r *OpenShellAgentHarnessController) reconcileDelete(ctx context.Context, ah *v1alpha2.AgentHarness) (ctrl.Result, error) { |
| 158 | + if !controllerutil.ContainsFinalizer(ah, agentHarnessFinalizer) { |
| 159 | + return ctrl.Result{}, nil |
| 160 | + } |
| 161 | + |
| 162 | + if ah.Status.BackendRef != nil { |
| 163 | + actorID := ah.Status.BackendRef.ID |
| 164 | + if actorID != "" { |
| 165 | + backend := r.backendFor(ah) |
| 166 | + actorDone := true |
| 167 | + var err error |
| 168 | + if backend != nil { |
| 169 | + actorDone, err = backend.DeleteAgentHarness(ctx, sandboxbackend.Handle{ID: actorID}) |
| 170 | + } |
| 171 | + if err != nil { |
| 172 | + if r.Recorder != nil { |
| 173 | + r.Recorder.Eventf(ah, nil, "Warning", "AgentHarnessDeleteFailed", "DeleteAgentHarness", "%s", err.Error()) |
| 174 | + } |
| 175 | + return ctrl.Result{RequeueAfter: agentHarnessNotReadyRequeue}, err |
| 176 | + } |
| 177 | + if !actorDone { |
| 178 | + setAgentHarnessCondition(ah, v1alpha2.AgentHarnessConditionTypeActorReady, |
| 179 | + metav1.ConditionFalse, "ActorDeleting", fmt.Sprintf("waiting for backend actor %q deletion", actorID)) |
| 180 | + if err := patchAgentHarnessStatus(ctx, r.Client, ah); err != nil { |
| 181 | + return ctrl.Result{}, err |
| 182 | + } |
| 183 | + return ctrl.Result{RequeueAfter: agentHarnessNotReadyRequeue}, nil |
| 184 | + } |
| 185 | + } |
| 186 | + ah.Status.BackendRef = nil |
| 187 | + if err := patchAgentHarnessStatus(ctx, r.Client, ah); err != nil { |
| 188 | + return ctrl.Result{}, err |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + controllerutil.RemoveFinalizer(ah, agentHarnessFinalizer) |
| 193 | + if err := r.Client.Update(ctx, ah); err != nil { |
| 194 | + return ctrl.Result{}, fmt.Errorf("remove finalizer: %w", err) |
| 195 | + } |
| 196 | + return ctrl.Result{}, nil |
| 197 | +} |
| 198 | + |
| 199 | +// SetupWithManager registers the OpenShell AgentHarness controller with the manager. |
| 200 | +func (r *OpenShellAgentHarnessController) SetupWithManager(mgr ctrl.Manager) error { |
| 201 | + b := ctrl.NewControllerManagedBy(mgr). |
| 202 | + WithOptions(controller.Options{NeedLeaderElection: new(true)}). |
| 203 | + For(&v1alpha2.AgentHarness{}, builder.WithPredicates(agentHarnessRuntimePredicate(v1alpha2.AgentHarnessRuntimeOpenshell))) |
| 204 | + return b.Named("agentharness-openshell").Complete(r) |
| 205 | +} |
0 commit comments