main.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 main
  14. import (
  15. "flag"
  16. "os"
  17. // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
  18. // to ensure that exec-entrypoint and run can make use of them.
  19. _ "k8s.io/client-go/plugin/pkg/client/auth"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. utilruntime "k8s.io/apimachinery/pkg/util/runtime"
  22. clientgoscheme "k8s.io/client-go/kubernetes/scheme"
  23. ctrl "sigs.k8s.io/controller-runtime"
  24. "sigs.k8s.io/controller-runtime/pkg/healthz"
  25. "sigs.k8s.io/controller-runtime/pkg/log/zap"
  26. metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
  27. databasev1 "github.com/iwanhae/nodb/api/v1"
  28. "github.com/iwanhae/nodb/internal/controller"
  29. //+kubebuilder:scaffold:imports
  30. )
  31. var (
  32. scheme = runtime.NewScheme()
  33. setupLog = ctrl.Log.WithName("setup")
  34. )
  35. func init() {
  36. utilruntime.Must(clientgoscheme.AddToScheme(scheme))
  37. utilruntime.Must(databasev1.AddToScheme(scheme))
  38. //+kubebuilder:scaffold:scheme
  39. }
  40. func main() {
  41. var metricsAddr string
  42. var enableLeaderElection bool
  43. var probeAddr string
  44. flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
  45. flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
  46. flag.BoolVar(&enableLeaderElection, "leader-elect", false,
  47. "Enable leader election for controller manager. "+
  48. "Enabling this will ensure there is only one active controller manager.")
  49. opts := zap.Options{
  50. Development: true,
  51. }
  52. opts.BindFlags(flag.CommandLine)
  53. flag.Parse()
  54. ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
  55. mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
  56. Scheme: scheme,
  57. Metrics: metricsserver.Options{BindAddress: metricsAddr},
  58. HealthProbeBindAddress: probeAddr,
  59. LeaderElection: enableLeaderElection,
  60. LeaderElectionID: "58a3e48b.iwanhae.kr",
  61. // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
  62. // when the Manager ends. This requires the binary to immediately end when the
  63. // Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
  64. // speeds up voluntary leader transitions as the new leader don't have to wait
  65. // LeaseDuration time first.
  66. //
  67. // In the default scaffold provided, the program ends immediately after
  68. // the manager stops, so would be fine to enable this option. However,
  69. // if you are doing or is intended to do any operation such as perform cleanups
  70. // after the manager stops then its usage might be unsafe.
  71. // LeaderElectionReleaseOnCancel: true,
  72. })
  73. if err != nil {
  74. setupLog.Error(err, "unable to start manager")
  75. os.Exit(1)
  76. }
  77. if err = (&controller.PostgreSQLReconciler{
  78. Client: mgr.GetClient(),
  79. Scheme: mgr.GetScheme(),
  80. }).SetupWithManager(mgr); err != nil {
  81. setupLog.Error(err, "unable to create controller", "controller", "PostgreSQL")
  82. os.Exit(1)
  83. }
  84. //+kubebuilder:scaffold:builder
  85. if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
  86. setupLog.Error(err, "unable to set up health check")
  87. os.Exit(1)
  88. }
  89. if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
  90. setupLog.Error(err, "unable to set up ready check")
  91. os.Exit(1)
  92. }
  93. setupLog.Info("starting manager")
  94. if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
  95. setupLog.Error(err, "problem running manager")
  96. os.Exit(1)
  97. }
  98. }