main.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright 2022.
  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. appv1 "github.com/kakao/bluegreen/api/v1"
  27. "github.com/kakao/bluegreen/controllers"
  28. "github.com/kakao/bluegreen/controllers/bluegreen"
  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(appv1.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. MetricsBindAddress: metricsAddr,
  58. Port: 9443,
  59. HealthProbeBindAddress: probeAddr,
  60. LeaderElection: enableLeaderElection,
  61. LeaderElectionID: "d627c837.demo.kakao.com",
  62. // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
  63. // when the Manager ends. This requires the binary to immediately end when the
  64. // Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
  65. // speeds up voluntary leader transitions as the new leader don't have to wait
  66. // LeaseDuration time first.
  67. //
  68. // In the default scaffold provided, the program ends immediately after
  69. // the manager stops, so would be fine to enable this option. However,
  70. // if you are doing or is intended to do any operation such as perform cleanups
  71. // after the manager stops then its usage might be unsafe.
  72. // LeaderElectionReleaseOnCancel: true,
  73. })
  74. if err != nil {
  75. setupLog.Error(err, "unable to start manager")
  76. os.Exit(1)
  77. }
  78. if err = (&controllers.BlueGreenReconciler{
  79. Client: &bluegreen.BlueGreenClient{
  80. Client: mgr.GetClient(),
  81. },
  82. Scheme: mgr.GetScheme(),
  83. }).SetupWithManager(mgr); err != nil {
  84. setupLog.Error(err, "unable to create controller", "controller", "BlueGreen")
  85. os.Exit(1)
  86. }
  87. //+kubebuilder:scaffold:builder
  88. if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
  89. setupLog.Error(err, "unable to set up health check")
  90. os.Exit(1)
  91. }
  92. if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
  93. setupLog.Error(err, "unable to set up ready check")
  94. os.Exit(1)
  95. }
  96. setupLog.Info("starting manager")
  97. if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
  98. setupLog.Error(err, "problem running manager")
  99. os.Exit(1)
  100. }
  101. }