postgresql_pod_controller.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // will update Status anyway
  44. defer func() {
  45. err = r.Status().Update(ctx, &obj)
  46. if err != nil {
  47. logger.Error(err, "failed to update status")
  48. }
  49. }()
  50. // Find Pod
  51. pod := corev1.Pod{}
  52. if err := r.Get(ctx, req.NamespacedName, &pod); 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 pod.Labels[templates.LabelKeyType] != templates.LabelValuePostgreSQL {
  62. // This pod is not for PostgreSQL
  63. return ctrl.Result{}, nil
  64. }
  65. logger.Info("reconcile", "namespace", pod.Namespace, "name", pod.Name)
  66. cond := podutil.GetPodReadyCondition(pod.Status)
  67. if cond == nil {
  68. obj.Status.Status = databasev1.Status_Initializing
  69. } else if cond.Status == corev1.ConditionTrue {
  70. obj.Status.Status = databasev1.Status_Ready
  71. } else {
  72. obj.Status.Status = databasev1.Status_NotReady
  73. }
  74. return ctrl.Result{}, nil
  75. }
  76. // SetupWithManager sets up the controller with the Manager.
  77. func (r *PostgreSQLPodReconciler) SetupWithManager(mgr ctrl.Manager) error {
  78. return ctrl.NewControllerManagedBy(mgr).
  79. For(&corev1.Pod{}).
  80. Complete(r)
  81. }