main.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. //+kubebuilder:scaffold:imports
  28. )
  29. var (
  30. scheme = runtime.NewScheme()
  31. setupLog = ctrl.Log.WithName("setup")
  32. )
  33. func init() {
  34. utilruntime.Must(clientgoscheme.AddToScheme(scheme))
  35. //+kubebuilder:scaffold:scheme
  36. }
  37. func main() {
  38. var metricsAddr string
  39. var enableLeaderElection bool
  40. var probeAddr string
  41. flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
  42. flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
  43. flag.BoolVar(&enableLeaderElection, "leader-elect", false,
  44. "Enable leader election for controller manager. "+
  45. "Enabling this will ensure there is only one active controller manager.")
  46. opts := zap.Options{
  47. Development: true,
  48. }
  49. opts.BindFlags(flag.CommandLine)
  50. flag.Parse()
  51. ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
  52. mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
  53. Scheme: scheme,
  54. Metrics: metricsserver.Options{BindAddress: metricsAddr},
  55. HealthProbeBindAddress: probeAddr,
  56. LeaderElection: enableLeaderElection,
  57. LeaderElectionID: "58a3e48b.iwanhae.kr",
  58. // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
  59. // when the Manager ends. This requires the binary to immediately end when the
  60. // Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
  61. // speeds up voluntary leader transitions as the new leader don't have to wait
  62. // LeaseDuration time first.
  63. //
  64. // In the default scaffold provided, the program ends immediately after
  65. // the manager stops, so would be fine to enable this option. However,
  66. // if you are doing or is intended to do any operation such as perform cleanups
  67. // after the manager stops then its usage might be unsafe.
  68. // LeaderElectionReleaseOnCancel: true,
  69. })
  70. if err != nil {
  71. setupLog.Error(err, "unable to start manager")
  72. os.Exit(1)
  73. }
  74. //+kubebuilder:scaffold:builder
  75. if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
  76. setupLog.Error(err, "unable to set up health check")
  77. os.Exit(1)
  78. }
  79. if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
  80. setupLog.Error(err, "unable to set up ready check")
  81. os.Exit(1)
  82. }
  83. setupLog.Info("starting manager")
  84. if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
  85. setupLog.Error(err, "problem running manager")
  86. os.Exit(1)
  87. }
  88. }