litellm.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright 2026 LocoStack.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package reconciler
  14. import (
  15. "context"
  16. "crypto/rand"
  17. "crypto/sha256"
  18. "encoding/hex"
  19. "fmt"
  20. "maps"
  21. "github.com/LocoStack/loco-operator/api/v1alpha1"
  22. "github.com/LocoStack/loco-operator/pkg/templates/litellm"
  23. "go.yaml.in/yaml/v2"
  24. corev1 "k8s.io/api/core/v1"
  25. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  26. "k8s.io/apimachinery/pkg/runtime"
  27. "sigs.k8s.io/controller-runtime/pkg/client"
  28. "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
  29. )
  30. type LiteLLMReconciler struct {
  31. *DefaultComponentReconciler
  32. client client.Client
  33. scheme *runtime.Scheme
  34. stack *v1alpha1.Stack
  35. component *v1alpha1.Component
  36. }
  37. func NewLiteLLMReconciler(client client.Client, scheme *runtime.Scheme, stack *v1alpha1.Stack, component *v1alpha1.Component) *LiteLLMReconciler {
  38. return &LiteLLMReconciler{
  39. DefaultComponentReconciler: NewDefaultComponentReconciler(client, scheme, stack, component),
  40. client: client,
  41. scheme: scheme,
  42. stack: stack,
  43. component: component,
  44. }
  45. }
  46. func (r *LiteLLMReconciler) ReconcileComponent(ctx context.Context, tmpl *v1alpha1.Template, variables map[string]string) ([]client.Object, error) {
  47. if err := r.ReconcileKey(ctx, "litellm", litellm.LITELLM_AUTH_SECRET_KEY, generateKey); err != nil {
  48. return nil, fmt.Errorf("Failed to reconcile LiteLLM master key: %w", err)
  49. }
  50. configHash, err := r.reconcileConfigMap(ctx)
  51. if err != nil {
  52. return nil, fmt.Errorf("Failed to reconcile LiteLLM config: %w", err)
  53. }
  54. if tmpl.Metadata.Annotations == nil {
  55. tmpl.Metadata.Annotations = make(map[string]string)
  56. }
  57. maps.Copy(tmpl.Metadata.Annotations, map[string]string{
  58. "locostack.com/configHash": configHash,
  59. })
  60. if _, err := r.DefaultComponentReconciler.ReconcileComponent(ctx, tmpl, variables); err != nil {
  61. return nil, err
  62. }
  63. return nil, nil
  64. }
  65. func (r *LiteLLMReconciler) reconcileConfigMap(ctx context.Context) (string, error) {
  66. var o11yComp *v1alpha1.Component
  67. if r.stack.Spec.Observability != nil && r.stack.Spec.Observability.Enabled {
  68. o11yComp = &v1alpha1.Component{}
  69. if err := r.client.Get(ctx, client.ObjectKey{Name: fmt.Sprintf("observability-%s", r.stack.Name), Namespace: r.stack.Namespace}, o11yComp); err != nil {
  70. return "", fmt.Errorf("Failed to get Observability component: %w", err)
  71. }
  72. }
  73. configBuilder := litellm.LiteLLMConfigBuilder{
  74. MasterKeyEnvName: litellm.LITELLM_MASTER_KEY_ENV_NAME,
  75. Stack: r.stack,
  76. ObservabilityComponent: o11yComp,
  77. }
  78. config, err := configBuilder.BuildLiteLLMConfig()
  79. if err != nil {
  80. return "", fmt.Errorf("Failed to build LiteLLM config: %w", err)
  81. }
  82. configData, err := yaml.Marshal(config)
  83. if err != nil {
  84. return "", fmt.Errorf("Failed to marshal LiteLLM config: %w", err)
  85. }
  86. configDataStr := string(configData)
  87. h := sha256.Sum256([]byte(configDataStr))
  88. configHash := hex.EncodeToString(h[:])
  89. cm := &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: r.ResourceName("config"), Namespace: r.component.GetNamespace()}}
  90. if _, err := controllerutil.CreateOrUpdate(ctx, r.client, cm, func() error {
  91. if cm.Labels == nil {
  92. cm.Labels = map[string]string{}
  93. }
  94. maps.Copy(cm.Labels, r.ResourceLabels())
  95. if cm.Annotations == nil {
  96. cm.Annotations = make(map[string]string)
  97. }
  98. cm.Annotations["hash"] = configHash
  99. cm.Data = map[string]string{litellm.LITELLM_CONFIG_KEY: configDataStr}
  100. return controllerutil.SetControllerReference(r.component, cm, r.scheme)
  101. }); err != nil {
  102. return "", err
  103. }
  104. return configHash, nil
  105. }
  106. func generateKey() (string, error) {
  107. b := make([]byte, 24)
  108. if _, err := rand.Read(b); err != nil {
  109. return "", err
  110. }
  111. return "sk-" + hex.EncodeToString(b), nil
  112. }