github.com/ferryproxy/api@v0.4.2/main.go (about) 1 /* 2 Copyright 2022 FerryProxy Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "flag" 21 "os" 22 23 // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) 24 // to ensure that exec-entrypoint and run can make use of them. 25 _ "k8s.io/client-go/plugin/pkg/client/auth" 26 27 "k8s.io/apimachinery/pkg/runtime" 28 utilruntime "k8s.io/apimachinery/pkg/util/runtime" 29 clientgoscheme "k8s.io/client-go/kubernetes/scheme" 30 ctrl "sigs.k8s.io/controller-runtime" 31 "sigs.k8s.io/controller-runtime/pkg/healthz" 32 "sigs.k8s.io/controller-runtime/pkg/log/zap" 33 34 trafficv1alpha2 "github.com/ferryproxy/api/apis/traffic/v1alpha2" 35 trafficcontrollers "github.com/ferryproxy/api/controllers/traffic" 36 //+kubebuilder:scaffold:imports 37 ) 38 39 var ( 40 scheme = runtime.NewScheme() 41 setupLog = ctrl.Log.WithName("setup") 42 ) 43 44 func init() { 45 utilruntime.Must(clientgoscheme.AddToScheme(scheme)) 46 47 utilruntime.Must(trafficv1alpha2.AddToScheme(scheme)) 48 //+kubebuilder:scaffold:scheme 49 } 50 51 func main() { 52 var metricsAddr string 53 var enableLeaderElection bool 54 var probeAddr string 55 flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") 56 flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") 57 flag.BoolVar(&enableLeaderElection, "leader-elect", false, 58 "Enable leader election for controller manager. "+ 59 "Enabling this will ensure there is only one active controller manager.") 60 opts := zap.Options{ 61 Development: true, 62 } 63 opts.BindFlags(flag.CommandLine) 64 flag.Parse() 65 66 ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) 67 68 mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ 69 Scheme: scheme, 70 MetricsBindAddress: metricsAddr, 71 Port: 9443, 72 HealthProbeBindAddress: probeAddr, 73 LeaderElection: enableLeaderElection, 74 LeaderElectionID: "9c89bd98.ferryproxy.io", 75 // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily 76 // when the Manager ends. This requires the binary to immediately end when the 77 // Manager is stopped, otherwise, this setting is unsafe. Setting this significantly 78 // speeds up voluntary leader transitions as the new leader don't have to wait 79 // LeaseDuration time first. 80 // 81 // In the default scaffold provided, the program ends immediately after 82 // the manager stops, so would be fine to enable this option. However, 83 // if you are doing or is intended to do any operation such as perform cleanups 84 // after the manager stops then its usage might be unsafe. 85 // LeaderElectionReleaseOnCancel: true, 86 }) 87 if err != nil { 88 setupLog.Error(err, "unable to start manager") 89 os.Exit(1) 90 } 91 92 if err = (&trafficcontrollers.HubReconciler{ 93 Client: mgr.GetClient(), 94 Scheme: mgr.GetScheme(), 95 }).SetupWithManager(mgr); err != nil { 96 setupLog.Error(err, "unable to create controller", "controller", "Hub") 97 os.Exit(1) 98 } 99 if err = (&trafficcontrollers.RouteReconciler{ 100 Client: mgr.GetClient(), 101 Scheme: mgr.GetScheme(), 102 }).SetupWithManager(mgr); err != nil { 103 setupLog.Error(err, "unable to create controller", "controller", "Route") 104 os.Exit(1) 105 } 106 if err = (&trafficcontrollers.RoutePolicyReconciler{ 107 Client: mgr.GetClient(), 108 Scheme: mgr.GetScheme(), 109 }).SetupWithManager(mgr); err != nil { 110 setupLog.Error(err, "unable to create controller", "controller", "RoutePolicy") 111 os.Exit(1) 112 } 113 //+kubebuilder:scaffold:builder 114 115 if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { 116 setupLog.Error(err, "unable to set up health check") 117 os.Exit(1) 118 } 119 if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil { 120 setupLog.Error(err, "unable to set up ready check") 121 os.Exit(1) 122 } 123 124 setupLog.Info("starting manager") 125 if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { 126 setupLog.Error(err, "problem running manager") 127 os.Exit(1) 128 } 129 }