| Giorgi Lekveishvili | 7366dbb | 2023-06-16 12:31:03 +0400 | [diff] [blame^] | 1 | /* |
| 2 | Copyright 2023. |
| 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 | headscalev1 "github.com/giolekva/pcloud/api/v1" |
| 35 | "github.com/giolekva/pcloud/controllers" |
| 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(headscalev1.AddToScheme(scheme)) |
| 48 | //+kubebuilder:scaffold:scheme |
| 49 | } |
| 50 | |
| 51 | func main() { |
| 52 | var metricsAddr string |
| 53 | var enableLeaderElection bool |
| 54 | var probeAddr string |
| 55 | var headscaleAddr string |
| 56 | flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") |
| 57 | flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") |
| 58 | flag.BoolVar(&enableLeaderElection, "leader-elect", false, |
| 59 | "Enable leader election for controller manager. "+ |
| 60 | "Enabling this will ensure there is only one active controller manager.") |
| 61 | flag.StringVar(&headscaleAddr, "headscale-api", "headscale-api", "HTTP endpoint pointing to headscale api.") |
| 62 | opts := zap.Options{ |
| 63 | Development: true, |
| 64 | } |
| 65 | opts.BindFlags(flag.CommandLine) |
| 66 | flag.Parse() |
| 67 | |
| 68 | ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) |
| 69 | |
| 70 | mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ |
| 71 | Scheme: scheme, |
| 72 | MetricsBindAddress: metricsAddr, |
| 73 | Port: 9443, |
| 74 | HealthProbeBindAddress: probeAddr, |
| 75 | LeaderElection: enableLeaderElection, |
| 76 | LeaderElectionID: "798a733c.dodo.cloud", |
| 77 | // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily |
| 78 | // when the Manager ends. This requires the binary to immediately end when the |
| 79 | // Manager is stopped, otherwise, this setting is unsafe. Setting this significantly |
| 80 | // speeds up voluntary leader transitions as the new leader don't have to wait |
| 81 | // LeaseDuration time first. |
| 82 | // |
| 83 | // In the default scaffold provided, the program ends immediately after |
| 84 | // the manager stops, so would be fine to enable this option. However, |
| 85 | // if you are doing or is intended to do any operation such as perform cleanups |
| 86 | // after the manager stops then its usage might be unsafe. |
| 87 | // LeaderElectionReleaseOnCancel: true, |
| 88 | }) |
| 89 | if err != nil { |
| 90 | setupLog.Error(err, "unable to start manager") |
| 91 | os.Exit(1) |
| 92 | } |
| 93 | |
| 94 | if err = (&controllers.HeadscaleUserReconciler{ |
| 95 | Client: mgr.GetClient(), |
| 96 | Scheme: mgr.GetScheme(), |
| 97 | Headscale: controllers.NewHeadscaleClient(headscaleAddr), |
| 98 | }).SetupWithManager(mgr); err != nil { |
| 99 | setupLog.Error(err, "unable to create controller", "controller", "HeadscaleUser") |
| 100 | os.Exit(1) |
| 101 | } |
| 102 | //+kubebuilder:scaffold:builder |
| 103 | |
| 104 | if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { |
| 105 | setupLog.Error(err, "unable to set up health check") |
| 106 | os.Exit(1) |
| 107 | } |
| 108 | if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil { |
| 109 | setupLog.Error(err, "unable to set up ready check") |
| 110 | os.Exit(1) |
| 111 | } |
| 112 | |
| 113 | setupLog.Info("starting manager") |
| 114 | if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { |
| 115 | setupLog.Error(err, "problem running manager") |
| 116 | os.Exit(1) |
| 117 | } |
| 118 | } |