postgresql_svc_controller.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. "github.com/iwanhae/nodb/internal/templates"
  17. "k8s.io/apimachinery/pkg/runtime"
  18. ctrl "sigs.k8s.io/controller-runtime"
  19. "sigs.k8s.io/controller-runtime/pkg/client"
  20. "sigs.k8s.io/controller-runtime/pkg/log"
  21. databasev1 "github.com/iwanhae/nodb/api/v1"
  22. corev1 "k8s.io/api/core/v1"
  23. )
  24. // PostgreSQLReconciler reconciles a PostgreSQL object
  25. type PostgreSQLServiceReconciler struct {
  26. client.Client
  27. Scheme *runtime.Scheme
  28. }
  29. //+kubebuilder:rbac:groups="",resources=services,verbs=watch;get;list
  30. //+kubebuilder:rbac:groups=database.iwanhae.kr,resources=postgresqls/status,verbs=get;update;patch
  31. func (r *PostgreSQLServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, err error) {
  32. logger := log.FromContext(ctx)
  33. // Find PostgreSQL
  34. obj := databasev1.PostgreSQL{}
  35. if err := r.Get(ctx, req.NamespacedName, &obj); err != nil {
  36. if client.IgnoreNotFound(err) == nil {
  37. return ctrl.Result{}, nil
  38. }
  39. logger.Error(err, "resource not found")
  40. return ctrl.Result{}, err
  41. }
  42. original := obj.DeepCopy()
  43. // will update Status anyway
  44. defer func() {
  45. err = r.Status().Patch(ctx, &obj, client.MergeFrom(original))
  46. if err != nil {
  47. logger.Error(err, "failed to update status")
  48. }
  49. }()
  50. // Find Svc
  51. svc := corev1.Service{}
  52. if err := r.Get(ctx, req.NamespacedName, &svc); err != nil {
  53. if client.IgnoreNotFound(err) == nil {
  54. // Have PostgreSQL, but no pod
  55. obj.Status.Status = databasev1.Status_Error
  56. return ctrl.Result{}, nil
  57. }
  58. logger.Error(err, "resource not found")
  59. return ctrl.Result{}, err
  60. }
  61. if svc.Labels[templates.LabelKeyType] != templates.LabelValuePostgreSQL {
  62. // This svc is not for PostgreSQL
  63. return ctrl.Result{}, nil
  64. }
  65. logger.Info("reconcile", "namespace", svc.Namespace, "name", svc.Name)
  66. obj.Status.ListenOn.Port = svc.Spec.Ports[0].NodePort
  67. return ctrl.Result{}, nil
  68. }
  69. // SetupWithManager sets up the controller with the Manager.
  70. func (r *PostgreSQLServiceReconciler) SetupWithManager(mgr ctrl.Manager) error {
  71. return ctrl.NewControllerManagedBy(mgr).
  72. For(&corev1.Service{}).
  73. Complete(r)
  74. }