Browse Source

feat(template): add loco-agents template

Thomas Zhang 4 months ago
parent
commit
0d92c19774

+ 2 - 0
internal/controller/component_controller.go

@@ -173,6 +173,8 @@ 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":
+		return reconciler.NewLocoAgentsReconciler(r.Client, r.Scheme, stack, comp)
 	default:
 		return reconciler.NewDefaultComponentReconciler(r.Client, r.Scheme, stack, comp)
 	}

+ 142 - 0
internal/reconciler/loco_agents.go

@@ -0,0 +1,142 @@
+/*
+Copyright 2026 LocoStack.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package reconciler
+
+import (
+	"context"
+	"crypto/sha256"
+	"encoding/hex"
+	"fmt"
+	"maps"
+
+	"github.com/LocoStack/loco-operator/api/v1alpha1"
+	"go.yaml.in/yaml/v2"
+	corev1 "k8s.io/api/core/v1"
+	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
+	"k8s.io/apimachinery/pkg/runtime"
+	"sigs.k8s.io/controller-runtime/pkg/client"
+	"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
+)
+
+type LocoAgentsReconciler struct {
+	*DefaultComponentReconciler
+	*AgentReconciler
+	client    client.Client
+	scheme    *runtime.Scheme
+	stack     *v1alpha1.Stack
+	component *v1alpha1.Component
+}
+
+func NewLocoAgentsReconciler(client client.Client, scheme *runtime.Scheme, stack *v1alpha1.Stack, component *v1alpha1.Component) *LocoAgentsReconciler {
+	return &LocoAgentsReconciler{
+		DefaultComponentReconciler: NewDefaultComponentReconciler(client, scheme, stack, component),
+		AgentReconciler:            NewAgentReconciler(client, scheme, stack, component),
+		client:                     client,
+		scheme:                     scheme,
+		stack:                      stack,
+		component:                  component,
+	}
+}
+
+func (r *LocoAgentsReconciler) ReconcileComponent(ctx context.Context, tmpl *v1alpha1.Template, variables map[string]string) ([]client.Object, error) {
+	deps, comps, err := r.getDependencies(ctx)
+	if err != nil {
+		return nil, fmt.Errorf("Failed to get dependencies for %s: %w", r.component.Name, err)
+	}
+
+	agentConfigHash, err := r.reconcileAgentConfig(ctx, r.component.Spec.Variables["systemPrompt"], comps)
+	if err != nil {
+		return nil, fmt.Errorf("Failed to reconcile config for %s: %w", r.component.Name, err)
+	}
+
+	mcpConfigHash, err := r.reconcileMcpConfig(ctx, comps)
+	if err != nil {
+		return nil, fmt.Errorf("Failed to reconcile MCP config for %s: %w", r.component.Name, err)
+	}
+
+	h := sha256.Sum256([]byte(agentConfigHash + mcpConfigHash))
+	configHash := hex.EncodeToString(h[:])
+	if tmpl.Metadata.Annotations == nil {
+		tmpl.Metadata.Annotations = make(map[string]string)
+	}
+	maps.Copy(tmpl.Metadata.Annotations, map[string]string{
+		"locostack.com/configHash": configHash,
+	})
+
+	if _, err := r.DefaultComponentReconciler.ReconcileComponent(ctx, tmpl, variables); err != nil {
+		return nil, err
+	}
+	return deps, nil
+}
+
+type modelConfig struct {
+	Endpoint string `yaml:"endpoint"`
+	APIKey   string `yaml:"apiKey,omitempty"`
+	Name     string `yaml:"name,omitempty"`
+}
+
+type endpoint struct {
+	Name     string `json:"name"`
+	Endpoint string `json:"endpoint"`
+}
+
+type agentConfig struct {
+	SystemPrompt   string      `yaml:"systemPrompt"`
+	Model          modelConfig `yaml:"model"`
+	KnowledgeBases []endpoint  `yaml:"knowledgeBases,omitempty"`
+	MCPConfigPath  string      `yaml:"mcpConfigPath,omitempty"`
+}
+
+func (r *LocoAgentsReconciler) reconcileAgentConfig(ctx context.Context, systemPrompt string, comps *AgentComponents) (string, error) {
+	config := agentConfig{
+		SystemPrompt: systemPrompt,
+		Model: modelConfig{
+			Endpoint: comps.Model.URL,
+			APIKey:   comps.GatewayKey,
+			Name:     comps.Model.Name,
+		},
+		MCPConfigPath: "/etc/mcp/mcp.json",
+	}
+	for _, kb := range comps.KnowledgeBases {
+		config.KnowledgeBases = append(config.KnowledgeBases, endpoint{
+			Name:     kb.Name,
+			Endpoint: kb.URL,
+		})
+	}
+	yamlBytes, err := yaml.Marshal(config)
+	h := sha256.Sum256(yamlBytes)
+	configHash := hex.EncodeToString(h[:])
+	if err != nil {
+		return "", fmt.Errorf("marshaling agent config: %w", err)
+	}
+	secret := &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: r.ResourceName("config"), Namespace: r.component.GetNamespace()}}
+	_, err = controllerutil.CreateOrUpdate(ctx, r.client, secret, func() error {
+		if secret.Labels == nil {
+			secret.Labels = map[string]string{}
+		}
+		maps.Copy(secret.Labels, r.ResourceLabels())
+		if secret.Annotations == nil {
+			secret.Annotations = make(map[string]string)
+		}
+		secret.Annotations["hash"] = configHash
+		secret.Data = map[string][]byte{
+			"agent.yaml": yamlBytes,
+		}
+		return controllerutil.SetControllerReference(r.component, secret, r.scheme)
+	})
+	return configHash, err
+}

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

@@ -0,0 +1,80 @@
+/*
+Copyright 2026 LocoStack.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package locostack
+
+import (
+	"github.com/LocoStack/loco-operator/api/v1alpha1"
+	corev1 "k8s.io/api/core/v1"
+	"k8s.io/utils/ptr"
+)
+
+var LocoAgentsTemplate = v1alpha1.Template{
+	Name: "loco-agents",
+	Spec: v1alpha1.TemplateSpec{
+		Runtime: v1alpha1.RuntimeSpec{
+			Name:  "loco-simple-agent",
+			Image: "locostack.com/loco-simple-agent:latest",
+			Env: []corev1.EnvVar{
+				{
+					Name:  "PORT",
+					Value: "$(spec.runtime.port)",
+				},
+			},
+			VolumeMounts: []corev1.VolumeMount{
+				{
+					Name:      "config",
+					MountPath: "/etc/locostack",
+					ReadOnly:  true,
+				},
+				{
+					Name:      "mcp-config",
+					MountPath: "/etc/mcp",
+					ReadOnly:  true,
+				},
+			},
+			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",
+					},
+				},
+			},
+		},
+	},
+}

+ 2 - 0
pkg/templates/templates.go

@@ -272,4 +272,6 @@ func init() {
 	Manager.RegisterRuntime(locostack.LocoContextTemplate.Spec.Runtime)
 	Manager.RegisterTemplate(locostack.LocoSimpleRAGIngestionTemplate)
 	Manager.RegisterTemplate(locostack.LocoSimpleRAGEgestionTemplate)
+	Manager.RegisterTemplate(locostack.LocoAgentsTemplate)
+	Manager.RegisterRuntime(locostack.LocoAgentsTemplate.Spec.Runtime)
 }