postgresql.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. "encoding/hex"
  18. "fmt"
  19. "github.com/LocoStack/loco-operator/api/v1alpha1"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "sigs.k8s.io/controller-runtime/pkg/client"
  22. )
  23. const (
  24. POSTGRES_DEFAULT_STORAGE_SIZE = "1Gi"
  25. POSTGRES_PASSWORD_SECRET_KEY = "password"
  26. )
  27. type PostgreSQLReconciler struct {
  28. *DefaultComponentReconciler
  29. client client.Client
  30. scheme *runtime.Scheme
  31. stack *v1alpha1.Stack
  32. component *v1alpha1.Component
  33. }
  34. func NewPostgreSQLReconciler(client client.Client, scheme *runtime.Scheme, stack *v1alpha1.Stack, component *v1alpha1.Component) *PostgreSQLReconciler {
  35. return &PostgreSQLReconciler{
  36. DefaultComponentReconciler: NewDefaultComponentReconciler(client, scheme, stack, component),
  37. client: client,
  38. scheme: scheme,
  39. stack: stack,
  40. component: component,
  41. }
  42. }
  43. func (r *PostgreSQLReconciler) ReconcileComponent(ctx context.Context, tmpl *v1alpha1.Template, variables map[string]string) ([]client.Object, error) {
  44. if err := r.ReconcileStorage(ctx, "postgresql", POSTGRES_DEFAULT_STORAGE_SIZE); err != nil {
  45. return nil, fmt.Errorf("reconciling postgresql storage: %w", err)
  46. }
  47. if err := r.ReconcileKey(ctx, "postgresql", POSTGRES_PASSWORD_SECRET_KEY, genrateKey); err != nil {
  48. return nil, fmt.Errorf("reconciling postgresql key: %w", err)
  49. }
  50. if _, err := r.DefaultComponentReconciler.ReconcileComponent(ctx, tmpl, variables); err != nil {
  51. return nil, err
  52. }
  53. return nil, nil
  54. }
  55. func genrateKey() (string, error) {
  56. b := make([]byte, 32)
  57. if _, err := rand.Read(b); err != nil {
  58. return "", err
  59. }
  60. return hex.EncodeToString(b), nil
  61. }