postgresql_pod_controller.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. podutil "k8s.io/kubernetes/pkg/api/v1/pod"
  24. )
  25. // PostgreSQLReconciler reconciles a PostgreSQL object
  26. type PostgreSQLPodReconciler struct {
  27. client.Client
  28. Scheme *runtime.Scheme
  29. }
  30. //+kubebuilder:rbac:groups="",resources=pods,verbs=watch;get
  31. //+kubebuilder:rbac:groups=database.iwanhae.kr,resources=postgresqls/status,verbs=get;update;patch
  32. func (r *PostgreSQLPodReconciler) Reconcile(ctx context.Context, req ctrl.Request) (result ctrl.Result, err error) {
  33. logger := log.FromContext(ctx)
  34. // Find PostgreSQL
  35. obj := databasev1.PostgreSQL{}
  36. if err := r.Get(ctx, req.NamespacedName, &obj); err != nil {
  37. if client.IgnoreNotFound(err) == nil {
  38. return ctrl.Result{}, nil
  39. }
  40. logger.Error(err, "resource not found")
  41. return ctrl.Result{}, err
  42. }
  43. original := obj.DeepCopy()
  44. // will update Status anyway
  45. defer func() {
  46. err = r.Status().Patch(ctx, &obj, client.MergeFrom(original))
  47. if err != nil {
  48. logger.Error(err, "failed to update status")
  49. }
  50. }()
  51. // Find Pod
  52. pod := corev1.Pod{}
  53. if err := r.Get(ctx, req.NamespacedName, &pod); err != nil {
  54. if client.IgnoreNotFound(err) == nil {
  55. // Have PostgreSQL, but no pod
  56. obj.Status.Status = databasev1.Status_Error
  57. return ctrl.Result{}, nil
  58. }
  59. logger.Error(err, "resource not found")
  60. return ctrl.Result{}, err
  61. }
  62. if pod.Labels[templates.LabelKeyType] != templates.LabelValuePostgreSQL {
  63. // This pod is not for PostgreSQL
  64. return ctrl.Result{}, nil
  65. }
  66. logger.Info("reconcile", "namespace", pod.Namespace, "name", pod.Name)
  67. cond := podutil.GetPodReadyCondition(pod.Status)
  68. if cond == nil {
  69. obj.Status.Conditions.Pod = databasev1.Status_Initializing
  70. } else if cond.Status == corev1.ConditionTrue {
  71. obj.Status.Conditions.Pod = databasev1.Status_Ready
  72. } else {
  73. obj.Status.Conditions.Pod = databasev1.Status_NotReady
  74. }
  75. if pod.Spec.NodeName != "" {
  76. obj.Status.ListenOn.Node = pod.Spec.NodeName
  77. obj.Status.ListenOn.Host = pod.Status.HostIP
  78. }
  79. return ctrl.Result{}, nil
  80. }
  81. // SetupWithManager sets up the controller with the Manager.
  82. func (r *PostgreSQLPodReconciler) SetupWithManager(mgr ctrl.Manager) error {
  83. return ctrl.NewControllerManagedBy(mgr).
  84. For(&corev1.Pod{}).
  85. Complete(r)
  86. }