From 27e787f2d26a91c89a5f7d9cfa275c86a0a3c543 Mon Sep 17 00:00:00 2001 From: Dan Prince Date: Thu, 29 Oct 2026 09:13:21 -0400 Subject: [PATCH] Support custom Lightspeed Stack configuration Replace spec.lcore.customization.responsesRequestMaxSize with spec.lcore.customServiceConfig, a YAML mapping recursively merged into the generated Lightspeed Stack configuration. Custom values override generated values, nested mappings preserve unspecified defaults, and lists and scalar values are replaced. Reject malformed YAML and non-mapping values during configuration reconciliation. Regenerate the CRD and deepcopy code. Document how to set customization.responses_request_max_size to 262144 through the new field while preserving the operator's system prompt defaults. Cover default preservation, nested overrides, list replacement, invalid YAML, and CRD persistence and removal in tests. Jira: OSPRH-39389 --- api/v1beta1/openstacklightspeed_types.go | 6 + ...ed.openstack.org_openstacklightspeeds.yaml | 6 + ...ightspeed_v1beta1_openstacklightspeed.yaml | 3 + docs/configuration.md | 25 ++++ internal/controller/lcore_config.go | 36 +++++- internal/controller/lcore_config_test.go | 121 ++++++++++++++++++ .../openstacklightspeed_controller_test.go | 16 +++ 7 files changed, 211 insertions(+), 2 deletions(-) diff --git a/api/v1beta1/openstacklightspeed_types.go b/api/v1beta1/openstacklightspeed_types.go index f281d051..edde60d0 100644 --- a/api/v1beta1/openstacklightspeed_types.go +++ b/api/v1beta1/openstacklightspeed_types.go @@ -137,6 +137,12 @@ type DatabaseSpec struct { // LCoreSpec defines configuration for the lightspeed-service-api container. type LCoreSpec struct { + // +kubebuilder:validation:Optional + // CustomServiceConfig is a YAML mapping recursively merged into the generated + // Lightspeed Stack configuration. Custom values override generated values; + // lists and scalar values are replaced rather than merged. + CustomServiceConfig string `json:"customServiceConfig,omitempty"` + // +kubebuilder:validation:Optional // +kubebuilder:default:={requests: {cpu: "250m", memory: "512Mi"}, limits: {cpu: "1", memory: "2Gi"}} // Resources sets compute resources for the lightspeed-service-api diff --git a/config/crd/bases/lightspeed.openstack.org_openstacklightspeeds.yaml b/config/crd/bases/lightspeed.openstack.org_openstacklightspeeds.yaml index 1ef9ed1f..97cca3c2 100644 --- a/config/crd/bases/lightspeed.openstack.org_openstacklightspeeds.yaml +++ b/config/crd/bases/lightspeed.openstack.org_openstacklightspeeds.yaml @@ -207,6 +207,12 @@ spec: description: ContainerImage overrides the lightspeed-service-api container image. When unset, the operator default is used. type: string + customServiceConfig: + description: |- + CustomServiceConfig is a YAML mapping recursively merged into the generated + Lightspeed Stack configuration. Custom values override generated values; + lists and scalar values are replaced rather than merged. + type: string logLevel: default: INFO description: 'Log level for the lightspeed-service-api container. diff --git a/config/samples/lightspeed_v1beta1_openstacklightspeed.yaml b/config/samples/lightspeed_v1beta1_openstacklightspeed.yaml index ae661b97..a02131c9 100644 --- a/config/samples/lightspeed_v1beta1_openstacklightspeed.yaml +++ b/config/samples/lightspeed_v1beta1_openstacklightspeed.yaml @@ -43,6 +43,9 @@ spec: # containerImage: quay.io/lightspeed-core/lightspeed-stack:0.7.3 # Uncomment to customize lightspeed-service-api container resources and log level: # lcore: + # customServiceConfig: | + # customization: + # responses_request_max_size: 262144 # resources: # requests: # cpu: "250m" diff --git a/docs/configuration.md b/docs/configuration.md index a9a6b1b5..21e3317d 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -55,6 +55,31 @@ model aliases map to request fields (`provider` and `model`) at query time. | `dataverseExporter.logLevel` | `INFO` | Feedback/transcript exporter sidecar. Same values as above. | | `database.logLevel` | `INFO` | PostgreSQL container. `DEBUG` also logs every SQL statement. | +## Custom Lightspeed Stack configuration + +`lcore.customServiceConfig` accepts a YAML mapping that is recursively merged into +the generated Lightspeed Stack configuration. Custom values override generated +values. Nested mappings preserve unspecified settings; lists and scalar values +are replaced. Invalid YAML or a non-mapping value causes configuration +reconciliation to fail. + +For example, increase the maximum character length of the serialized Responses +API request body: + +```yaml +spec: + lcore: + customServiceConfig: | + customization: + responses_request_max_size: 262144 +``` + +This preserves the operator's system prompt settings while adding the request +size limit. When omitted, Lightspeed Stack 0.7.3 uses its default of `131072` characters. +The configured value must be a positive integer. Use a LCore container image that +supports this setting. This request size limit is separate from +`models[].maxTokensForResponse`, which controls response tokens. + ## Data collection ```yaml diff --git a/internal/controller/lcore_config.go b/internal/controller/lcore_config.go index 6c00869d..7f8621f2 100644 --- a/internal/controller/lcore_config.go +++ b/internal/controller/lcore_config.go @@ -20,6 +20,7 @@ import ( "context" _ "embed" // Required for go:embed directives in this package "fmt" + "math" common_helper "github.com/openstack-k8s-operators/lib-common/modules/common/helper" apiv1beta1 "github.com/openstack-k8s-operators/lightspeed-operator/api/v1beta1" @@ -155,8 +156,7 @@ func buildLCoreDatabaseConfig(h *common_helper.Helper, _ *apiv1beta1.OpenStackLi } } -// buildLCoreCustomizationConfig configures system prompt customization -// Uses config field if set, otherwise falls back to default +// buildLCoreCustomizationConfig configures the system prompt. func buildLCoreCustomizationConfig() map[string]interface{} { return map[string]interface{}{ "system_prompt": getSystemPrompt(), @@ -348,6 +348,25 @@ func buildLCoreConfigYAML(ctx context.Context, h *common_helper.Helper, instance config["quota_handlers"] = quotaHandlers } + if instance.Spec.LCore != nil && instance.Spec.LCore.CustomServiceConfig != "" { + var customConfig map[string]interface{} + if err := yaml.UnmarshalStrict([]byte(instance.Spec.LCore.CustomServiceConfig), &customConfig); err != nil { + return "", fmt.Errorf("failed to parse spec.lcore.customServiceConfig as a YAML mapping: %w", err) + } + if customConfig == nil { + return "", fmt.Errorf("spec.lcore.customServiceConfig must be a YAML mapping") + } + if customization, ok := customConfig["customization"].(map[string]interface{}); ok { + if requestSize, exists := customization["responses_request_max_size"]; exists { + value, ok := requestSize.(float64) + if !ok || value < 1 || math.Trunc(value) != value { + return "", fmt.Errorf("spec.lcore.customServiceConfig customization.responses_request_max_size must be a positive integer") + } + } + } + mergeLCoreConfig(config, customConfig) + } + // Convert to YAML yamlBytes, err := yaml.Marshal(config) if err != nil { @@ -356,3 +375,16 @@ func buildLCoreConfigYAML(ctx context.Context, h *common_helper.Helper, instance return string(yamlBytes), nil } + +// mergeLCoreConfig recursively merges mappings, replacing other values with overrides. +func mergeLCoreConfig(config, overrides map[string]interface{}) { + for key, value := range overrides { + if customMap, ok := value.(map[string]interface{}); ok { + if generatedMap, ok := config[key].(map[string]interface{}); ok { + mergeLCoreConfig(generatedMap, customMap) + continue + } + } + config[key] = value + } +} diff --git a/internal/controller/lcore_config_test.go b/internal/controller/lcore_config_test.go index 9b268f53..b34d91f4 100644 --- a/internal/controller/lcore_config_test.go +++ b/internal/controller/lcore_config_test.go @@ -21,8 +21,129 @@ import ( "testing" apiv1beta1 "github.com/openstack-k8s-operators/lightspeed-operator/api/v1beta1" + "k8s.io/utils/ptr" + "sigs.k8s.io/yaml" ) +func TestBuildLCoreConfigYAML_CustomServiceConfig(t *testing.T) { + for _, tc := range []struct { + name string + lcore *apiv1beta1.LCoreSpec + want *int + }{ + {name: "lcore omitted"}, + {name: "custom config omitted", lcore: &apiv1beta1.LCoreSpec{}}, + {name: "empty mapping", lcore: &apiv1beta1.LCoreSpec{CustomServiceConfig: "{}"}}, + { + name: "minimum limit", + lcore: &apiv1beta1.LCoreSpec{ + CustomServiceConfig: "customization:\n responses_request_max_size: 1\n", + }, + want: ptr.To(1), + }, + { + name: "increased limit", + lcore: &apiv1beta1.LCoreSpec{ + CustomServiceConfig: "customization:\n responses_request_max_size: 262144\n", + }, + want: ptr.To(262144), + }, + } { + t.Run(tc.name, func(t *testing.T) { + instance := &apiv1beta1.OpenStackLightspeed{} + instance.Spec.LCore = tc.lcore + data, err := buildLCoreConfigYAML(context.Background(), newTestHelper(t), instance) + if err != nil { + t.Fatalf("build LCore configuration: %v", err) + } + var config struct { + Customization struct { + ResponsesRequestMaxSize *int `json:"responses_request_max_size"` + SystemPrompt string `json:"system_prompt"` + DisableQuerySystemPrompt bool `json:"disable_query_system_prompt"` + } `json:"customization"` + } + if err := yaml.Unmarshal([]byte(data), &config); err != nil { + t.Fatalf("parse LCore configuration: %v", err) + } + got := config.Customization.ResponsesRequestMaxSize + if !ptr.Equal(got, tc.want) { + t.Errorf("responses_request_max_size = %v, want %v", got, tc.want) + } + if config.Customization.SystemPrompt != getSystemPrompt() || !config.Customization.DisableQuerySystemPrompt { + t.Error("expected the operator system prompt and query system prompt protection") + } + }) + } +} + +func TestBuildLCoreConfigYAML_CustomServiceConfigOverrides(t *testing.T) { + instance := &apiv1beta1.OpenStackLightspeed{} + instance.Spec.LCore = &apiv1beta1.LCoreSpec{CustomServiceConfig: ` +customization: + system_prompt: Custom prompt + disable_query_system_prompt: false +service: + port: 9000 +rag: + retrieval: + tool: + sources: [custom] +extra: + enabled: true +`} + data, err := buildLCoreConfigYAML(context.Background(), newTestHelper(t), instance) + if err != nil { + t.Fatalf("build LCore configuration: %v", err) + } + var config map[string]interface{} + if err := yaml.Unmarshal([]byte(data), &config); err != nil { + t.Fatalf("parse LCore configuration: %v", err) + } + customization := config["customization"].(map[string]interface{}) + assertField(t, customization, "system_prompt", "Custom prompt") + assertField(t, customization, "disable_query_system_prompt", false) + service := config["service"].(map[string]interface{}) + assertField(t, service, "port", float64(9000)) + if len(service) < 2 { + t.Error("expected unspecified service defaults to be preserved") + } + rag := config["rag"].(map[string]interface{}) + if _, ok := rag["okp"]; !ok { + t.Error("expected rag.okp to be preserved") + } + retrieval := rag["retrieval"].(map[string]interface{}) + sources := retrieval["tool"].(map[string]interface{})["sources"].([]interface{}) + if len(sources) != 1 || sources[0] != "custom" { + t.Errorf("tool sources = %v, want [custom]", sources) + } + assertField(t, config["extra"].(map[string]interface{}), "enabled", true) +} + +func TestBuildLCoreConfigYAML_InvalidCustomServiceConfig(t *testing.T) { + for _, customConfig := range []string{ + "customization: [", + "- customization", + "scalar", + "null", + "customization: {}\ncustomization: {}", + "customization:\n responses_request_max_size: 0", + "customization:\n responses_request_max_size: -1", + "customization:\n responses_request_max_size: 1.5", + "customization:\n responses_request_max_size: '1'", + "customization:\n responses_request_max_size: null", + } { + t.Run(customConfig, func(t *testing.T) { + instance := &apiv1beta1.OpenStackLightspeed{} + instance.Spec.LCore = &apiv1beta1.LCoreSpec{CustomServiceConfig: customConfig} + data, err := buildLCoreConfigYAML(context.Background(), newTestHelper(t), instance) + if err == nil || data != "" { + t.Fatalf("expected invalid custom configuration to fail, got data %q, error %v", data, err) + } + }) + } +} + func TestBuildLCoreQuotaHandlersConfig_DisabledWhenNoLimiters(t *testing.T) { h := newTestHelper(t) diff --git a/internal/controller/openstacklightspeed_controller_test.go b/internal/controller/openstacklightspeed_controller_test.go index c37ff0d0..5f918fef 100644 --- a/internal/controller/openstacklightspeed_controller_test.go +++ b/internal/controller/openstacklightspeed_controller_test.go @@ -151,6 +151,22 @@ var _ = ginkgo.Describe("OpenStackLightspeed Controller", func() { // TODO(user): Add more specific assertions depending on your controller's reconciliation logic. // Example: If you expect a certain status condition after reconciliation, verify it here. }) + ginkgo.It("should persist and clear custom service configuration", func() { + resource := &apiv1beta1.OpenStackLightspeed{} + gomega.Expect(k8sClient.Get(ctx, typeNamespacedName, resource)).To(gomega.Succeed()) + customConfig := "customization:\n responses_request_max_size: 262144\n" + resource.Spec.LCore = &apiv1beta1.LCoreSpec{CustomServiceConfig: customConfig} + gomega.Expect(k8sClient.Update(ctx, resource)).To(gomega.Succeed()) + + stored := &apiv1beta1.OpenStackLightspeed{} + gomega.Expect(k8sClient.Get(ctx, typeNamespacedName, stored)).To(gomega.Succeed()) + gomega.Expect(stored.Spec.LCore.CustomServiceConfig).To(gomega.Equal(customConfig)) + + stored.Spec.LCore.CustomServiceConfig = "" + gomega.Expect(k8sClient.Update(ctx, stored)).To(gomega.Succeed()) + gomega.Expect(k8sClient.Get(ctx, typeNamespacedName, resource)).To(gomega.Succeed()) + gomega.Expect(resource.Spec.LCore.CustomServiceConfig).To(gomega.BeEmpty()) + }) }) })