postgresql_controller.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. Copyright 2023.
  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 controller
  14. import (
  15. "context"
  16. apierrors "k8s.io/apimachinery/pkg/api/errors"
  17. "k8s.io/apimachinery/pkg/api/resource"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. ctrl "sigs.k8s.io/controller-runtime"
  20. "sigs.k8s.io/controller-runtime/pkg/client"
  21. "sigs.k8s.io/controller-runtime/pkg/log"
  22. databasev1 "github.com/iwanhae/nodb/api/v1"
  23. "github.com/iwanhae/nodb/internal/templates"
  24. "github.com/pkg/errors"
  25. corev1 "k8s.io/api/core/v1"
  26. )
  27. // PostgreSQLReconciler reconciles a PostgreSQL object
  28. type PostgreSQLReconciler struct {
  29. client.Client
  30. Scheme *runtime.Scheme
  31. }
  32. //+kubebuilder:rbac:groups=database.iwanhae.kr,resources=postgresqls,verbs=get;list;watch;create;update;patch;delete
  33. //+kubebuilder:rbac:groups=database.iwanhae.kr,resources=postgresqls/status,verbs=get;update;patch
  34. //+kubebuilder:rbac:groups=database.iwanhae.kr,resources=postgresqls/finalizers,verbs=update
  35. //+kubebuilder:rbac:groups="",resources=pods,verbs=get;create
  36. // Reconcile is part of the main kubernetes reconciliation loop which aims to
  37. // move the current state of the cluster closer to the desired state.
  38. // TODO(user): Modify the Reconcile function to compare the state specified by
  39. // the PostgreSQL object against the actual cluster state, and then
  40. // perform operations to make the cluster state reflect the state specified by
  41. // the user.
  42. //
  43. // For more details, check Reconcile and its Result here:
  44. // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.16.0/pkg/reconcile
  45. func (r *PostgreSQLReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
  46. logger := log.FromContext(ctx)
  47. obj := databasev1.PostgreSQL{}
  48. if err := r.Get(ctx, req.NamespacedName, &obj); err != nil {
  49. if client.IgnoreNotFound(err) == nil {
  50. return ctrl.Result{}, nil
  51. }
  52. logger.Error(err, "resource not found")
  53. return ctrl.Result{}, err
  54. }
  55. logger.Info("reconcile", "namespace", obj.Namespace, "name", obj.Name)
  56. // Pending: Creating Pod if not exists
  57. // do not handle spec update
  58. pod := corev1.Pod{}
  59. if err := r.Client.Get(ctx, req.NamespacedName, &pod); err == nil {
  60. // if found return
  61. logger.Info("ignore already existing pod")
  62. return ctrl.Result{}, nil
  63. } else if !apierrors.IsNotFound(err) {
  64. // weird error
  65. return ctrl.Result{}, err
  66. }
  67. pod = templates.PostgreSQLPod(templates.PostgreSQLOpts{
  68. Name: obj.Name,
  69. Namespace: obj.Namespace,
  70. Tag: obj.Spec.Version,
  71. User: obj.Spec.User,
  72. Password: obj.Spec.Password,
  73. Database: obj.Spec.Database,
  74. Memory: resource.MustParse("1Gi"),
  75. Owner: &obj,
  76. })
  77. logger.Info("create pod", "namespace", pod.Namespace, "name", pod.Name)
  78. if err := r.Client.Create(ctx, &pod); err != nil {
  79. return ctrl.Result{}, errors.Wrap(err, "failed to create pod")
  80. }
  81. obj.Status.Status = databasev1.Status_Initializing
  82. logger.Info("update status")
  83. if err := r.Status().Update(ctx, &obj); err != nil {
  84. return ctrl.Result{}, errors.Wrap(err, "failed to patch status")
  85. }
  86. return ctrl.Result{}, nil
  87. }
  88. // SetupWithManager sets up the controller with the Manager.
  89. func (r *PostgreSQLReconciler) SetupWithManager(mgr ctrl.Manager) error {
  90. return ctrl.NewControllerManagedBy(mgr).
  91. For(&databasev1.PostgreSQL{}).
  92. Complete(r)
  93. }