Kaynağa Gözat

feat(runtime): 接入 lambdagent 运行时的模板与调度

为 LocoStack 增加 lambdagent runtime 支持:注册镜像模板、补齐 Agent 身份
变量、分派到 loco-agents reconciler,并为非 root 容器提供动态 PVC 与健康
探针,使 PaaS 能发现并调度该运行时的 Agent。

- 新增 LambdagentTemplate 并在 templates.go 注册 template/runtime
- Agent 组件注入 agentName/agentUid,展开稳定的 AGENT_SERVICE_* 身份
- component controller 将 lambdagent 分派到 LocoAgentsReconciler
- 新增 ReconcileDynamicStorage,按默认 StorageClass 创建工作区 PVC
- 为 lambdagent 增加 /health readiness/liveness 探针
- 补充 discovery.k8s.io/endpointslices 的 RBAC 与 kubebuilder 标记
KongLingTT 1 ay önce
ebeveyn
işleme
a29567b792

+ 8 - 0
config/rbac/role.yaml

@@ -30,6 +30,14 @@ rules:
   - patch
   - update
   - watch
+- apiGroups:
+  - discovery.k8s.io
+  resources:
+  - endpointslices
+  verbs:
+  - get
+  - list
+  - watch
 - apiGroups:
   - batch
   resources:

+ 2 - 0
internal/controller/agent_controller.go

@@ -149,6 +149,8 @@ func (r *AgentReconciler) reconcileComponent(ctx context.Context, agent *v1alpha
 		comp.Spec.Template = &tmpl
 		comp.Spec.Variables = map[string]string{
 			"systemPrompt": agent.Spec.SystemPrompt,
+			"agentName":    agent.Name,
+			"agentUid":     string(agent.UID),
 		}
 		comp.Spec.Dependencies = dependencies
 		if agent.Spec.StackRef != nil {

+ 2 - 1
internal/controller/component_controller.go

@@ -54,6 +54,7 @@ type ComponentReconciler struct {
 // +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch
 // +kubebuilder:rbac:groups="",resources=services,verbs=get;list;watch;create;update;patch
 // +kubebuilder:rbac:groups="apps",resources=deployments,verbs=get;list;watch;create;update;patch
+// +kubebuilder:rbac:groups="discovery.k8s.io",resources=endpointslices,verbs=get;list;watch
 
 func (r *ComponentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
 	log := logf.FromContext(ctx)
@@ -180,7 +181,7 @@ func (r *ComponentReconciler) getComponentReconciler(comp *v1alpha1.Component, s
 		return reconciler.NewLlamaCPPReconciler(r.Client, r.Scheme, stack, comp)
 	case "loco-context":
 		return reconciler.NewLocoContextReconciler(r.Client, r.Scheme, stack, comp)
-	case "loco-agents":
+	case "loco-agents", "lambdagent":
 		return reconciler.NewLocoAgentsReconciler(r.Client, r.Scheme, stack, comp)
 	case "opencode":
 		return reconciler.NewLocoOpenCodeAgentReconciler(r.Client, r.Scheme, stack, comp)

+ 25 - 1
internal/reconciler/common.go

@@ -25,6 +25,7 @@ import (
 	"github.com/LocoStack/loco-operator/api/v1alpha1"
 	corev1 "k8s.io/api/core/v1"
 	"k8s.io/apimachinery/pkg/runtime"
+	"k8s.io/apimachinery/pkg/util/intstr"
 	"sigs.k8s.io/controller-runtime/pkg/client"
 )
 
@@ -150,7 +151,7 @@ func buildContainerSpec(variables map[string]string, tmpl *v1alpha1.Template) co
 			Protocol:      corev1.ProtocolTCP,
 		})
 	}
-	return corev1.Container{
+	container := corev1.Container{
 		Name:            "main",
 		Image:           tmpl.Spec.Runtime.Image,
 		ImagePullPolicy: corev1.PullIfNotPresent,
@@ -162,6 +163,29 @@ func buildContainerSpec(variables map[string]string, tmpl *v1alpha1.Template) co
 		Ports:           ports,
 		Resources:       tmpl.Spec.Resources,
 	}
+	if tmpl.Spec.Runtime.Name == "lambdagent" && tmpl.Spec.Runtime.Port != 0 {
+		container.ReadinessProbe = &corev1.Probe{
+			ProbeHandler: corev1.ProbeHandler{HTTPGet: &corev1.HTTPGetAction{
+				Path: "/health",
+				Port: intstr.FromInt32(tmpl.Spec.Runtime.Port),
+			}},
+			InitialDelaySeconds: 5,
+			PeriodSeconds:       10,
+			TimeoutSeconds:      3,
+			FailureThreshold:    3,
+		}
+		container.LivenessProbe = &corev1.Probe{
+			ProbeHandler: corev1.ProbeHandler{HTTPGet: &corev1.HTTPGetAction{
+				Path: "/health",
+				Port: intstr.FromInt32(tmpl.Spec.Runtime.Port),
+			}},
+			InitialDelaySeconds: 15,
+			PeriodSeconds:       20,
+			TimeoutSeconds:      3,
+			FailureThreshold:    3,
+		}
+	}
+	return container
 }
 
 func buildPodSpec(variables map[string]string, tmpl *v1alpha1.Template) (corev1.PodSpec, error) {

+ 30 - 2
internal/reconciler/component.go

@@ -58,8 +58,9 @@ func NewDefaultComponentReconciler(client client.Client, scheme *runtime.Scheme,
 func (r *DefaultComponentReconciler) ReconcileComponent(ctx context.Context, tmpl *v1alpha1.Template, variables map[string]string) ([]client.Object, error) {
 	r.component.Labels = r.ResourceLabels()
 	tmplVars := map[string]string{
-		"metadata.name":     r.ResourceName(""),
-		"spec.runtime.port": fmt.Sprintf("%d", tmpl.Spec.Runtime.Port),
+		"metadata.name":      r.ResourceName(""),
+		"metadata.namespace": r.component.GetNamespace(),
+		"spec.runtime.port":  fmt.Sprintf("%d", tmpl.Spec.Runtime.Port),
 	}
 	maps.Copy(tmplVars, variables)
 	maps.Copy(tmplVars, r.component.Spec.Variables)
@@ -158,6 +159,33 @@ func (r *DefaultComponentReconciler) ReconcileStorage(ctx context.Context, name,
 	return nil
 }
 
+// ReconcileDynamicStorage creates a PVC bound by the cluster's default
+// StorageClass. Runtimes such as lambdagent need a writable volume owned by a
+// non-root UID, which the hostPath fallback above cannot provide.
+func (r *DefaultComponentReconciler) ReconcileDynamicStorage(ctx context.Context, name, size string) error {
+	pvc := &corev1.PersistentVolumeClaim{ObjectMeta: metav1.ObjectMeta{Name: r.ResourceName(name), Namespace: r.component.GetNamespace()}}
+	storage := resource.MustParse(size)
+	_, err := controllerutil.CreateOrUpdate(ctx, r.client, pvc, func() error {
+		if pvc.Labels == nil {
+			pvc.Labels = map[string]string{}
+		}
+		maps.Copy(pvc.Labels, r.ResourceLabels())
+		// StorageClass and volumeName are immutable after binding; only apply
+		// the provisioning spec when the PVC is first created.
+		if pvc.ResourceVersion == "" {
+			pvc.Spec.AccessModes = []corev1.PersistentVolumeAccessMode{corev1.ReadWriteOnce}
+			pvc.Spec.VolumeName = ""
+			pvc.Spec.StorageClassName = nil
+			if pvc.Spec.Resources.Requests == nil {
+				pvc.Spec.Resources.Requests = corev1.ResourceList{}
+			}
+			pvc.Spec.Resources.Requests[corev1.ResourceStorage] = storage
+		}
+		return controllerutil.SetControllerReference(r.component, pvc, r.scheme)
+	})
+	return err
+}
+
 func (r *DefaultComponentReconciler) reconcilePersistentVolume(ctx context.Context, name, size string) (string, error) {
 	pvName := fmt.Sprintf("%s-%s", r.component.GetNamespace(), r.ResourceName(name))
 	pv := &corev1.PersistentVolume{ObjectMeta: metav1.ObjectMeta{Name: pvName}}

+ 7 - 0
internal/reconciler/loco_agents.go

@@ -53,6 +53,13 @@ func NewLocoAgentsReconciler(client client.Client, scheme *runtime.Scheme, stack
 }
 
 func (r *LocoAgentsReconciler) ReconcileComponent(ctx context.Context, tmpl *v1alpha1.Template, variables map[string]string) ([]client.Object, error) {
+	// lambdagent persists workspaces/artifacts on a per-Agent PVC owned by LocoStack.
+	if tmpl.Spec.Runtime.Name == "lambdagent" {
+		if err := r.ReconcileDynamicStorage(ctx, "data", "5Gi"); err != nil {
+			return nil, fmt.Errorf("Failed to reconcile lambdagent workspace: %w", err)
+		}
+	}
+
 	deps, comps, err := r.getDependencies(ctx)
 	if err != nil {
 		return nil, fmt.Errorf("Failed to get dependencies for %s: %w", r.component.Name, err)

+ 110 - 0
pkg/templates/locostack/agents.go

@@ -78,3 +78,113 @@ var LocoAgentsTemplate = v1alpha1.Template{
 		},
 	},
 }
+
+// LambdagentTemplate implements the same LocoStack agent contract as
+// LocoAgentsTemplate, but backed by the lambdagent agent runtime image instead of
+// the TypeScript reference implementation. It exists as a separate template so it
+// can be selected independently in the UI; the component reconciler treats it
+// exactly like "loco-agents" (see ComponentReconciler.getComponentReconciler).
+var LambdagentTemplate = v1alpha1.Template{
+	Name: "lambdagent",
+	Spec: v1alpha1.TemplateSpec{
+		Runtime: v1alpha1.RuntimeSpec{
+			Name:  "lambdagent",
+			Image: "lambdagent-locostack:1.4.2",
+			Env: []corev1.EnvVar{
+				{
+					Name:  "PORT",
+					Value: "$(spec.runtime.port)",
+				},
+				{
+					Name:  "AGENT_SERVICE_AGENT_ID",
+					Value: "locostack:$(metadata.namespace):$(agentName)",
+				},
+				{
+					Name:  "AGENT_SERVICE_AGENT_UID",
+					Value: "$(agentUid)",
+				},
+				{
+					Name: "AGENT_SERVICE_SERVICE_INSTANCE_ID",
+					ValueFrom: &corev1.EnvVarSource{
+						FieldRef: &corev1.ObjectFieldSelector{
+							FieldPath: "metadata.name",
+						},
+					},
+				},
+				{
+					Name:  "AGENT_SERVICE_AGENT_CONFIG",
+					Value: "/etc/locostack/agent.yaml",
+				},
+				{
+					Name:  "AGENT_SERVICE_MCP_CONFIG",
+					Value: "/etc/mcp/mcp.json",
+				},
+				{
+					Name:  "AGENT_SERVICE_DATA_DIR",
+					Value: "/data/agent-service",
+				},
+				{
+					Name:  "AGENT_SERVICE_PATH_POLICY",
+					Value: "managed",
+				},
+				{
+					Name:  "AGENT_SERVICE_ALLOW_WORKSPACE_OVERRIDE",
+					Value: "false",
+				},
+			},
+			VolumeMounts: []corev1.VolumeMount{
+				{
+					Name:      "config",
+					MountPath: "/etc/locostack",
+					ReadOnly:  true,
+				},
+				{
+					Name:      "mcp-config",
+					MountPath: "/etc/mcp",
+					ReadOnly:  true,
+				},
+				{
+					Name:      "workspace",
+					MountPath: "/data/agent-service",
+				},
+			},
+			Port: 8080,
+		},
+		SecurityContext: &corev1.PodSecurityContext{
+			RunAsNonRoot: ptr.To(true),
+			RunAsUser:    ptr.To[int64](10001),
+			RunAsGroup:   ptr.To[int64](10001),
+			FSGroup:      ptr.To[int64](10001),
+			SeccompProfile: &corev1.SeccompProfile{
+				Type: corev1.SeccompProfileTypeRuntimeDefault,
+			},
+			SupplementalGroups: []int64{},
+		},
+		Volumes: []corev1.Volume{
+			{
+				Name: "config",
+				VolumeSource: corev1.VolumeSource{
+					Secret: &corev1.SecretVolumeSource{
+						SecretName: "$(metadata.name)-config",
+					},
+				},
+			},
+			{
+				Name: "mcp-config",
+				VolumeSource: corev1.VolumeSource{
+					Secret: &corev1.SecretVolumeSource{
+						SecretName: "$(metadata.name)-mcp-config",
+					},
+				},
+			},
+			{
+				Name: "workspace",
+				VolumeSource: corev1.VolumeSource{
+					PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
+						ClaimName: "$(metadata.name)-data",
+					},
+				},
+			},
+		},
+	},
+}

+ 2 - 0
pkg/templates/templates.go

@@ -277,4 +277,6 @@ func init() {
 	Manager.RegisterRuntime(locostack.LocoAgentsTemplate.Spec.Runtime)
 	Manager.RegisterTemplate(opencode.OpenCodeAgentTemplate)
 	Manager.RegisterRuntime(opencode.OpenCodeAgentTemplate.Spec.Runtime)
+	Manager.RegisterTemplate(locostack.LambdagentTemplate)
+	Manager.RegisterRuntime(locostack.LambdagentTemplate.Spec.Runtime)
 }