blob: 36529a88479e8001f7644ad750d15a3454b2eac0 [file] [log] [blame]
Giorgi Lekveishvilie8b2f012023-11-30 19:05:03 +04001/*
2Copyright 2023.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package main
18
19import (
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 dodocloudv1 "github.com/giolekva/pcloud/core/ns-controller/api/v1"
35 "github.com/giolekva/pcloud/core/ns-controller/controllers"
36 //+kubebuilder:scaffold:imports
37
38 "github.com/go-git/go-billy/v5/osfs"
39)
40
41var (
42 scheme = runtime.NewScheme()
43 setupLog = ctrl.Log.WithName("setup")
44)
45
46func init() {
47 utilruntime.Must(clientgoscheme.AddToScheme(scheme))
48
49 utilruntime.Must(dodocloudv1.AddToScheme(scheme))
50 //+kubebuilder:scaffold:scheme
51}
52
53func main() {
54 var metricsAddr string
55 var enableLeaderElection bool
56 var probeAddr string
57 var configDir string
58 flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
59 flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
60 flag.BoolVar(&enableLeaderElection, "leader-elect", false,
61 "Enable leader election for controller manager. "+
62 "Enabling this will ensure there is only one active controller manager.")
63 flag.StringVar(&configDir, "config-dir", "/etc/pcloud/dns-zone-configs", "Path to the DNS configurations directory")
64 opts := zap.Options{
65 Development: true,
66 }
67 opts.BindFlags(flag.CommandLine)
68 flag.Parse()
69
70 ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
71
72 mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
73 Scheme: scheme,
74 MetricsBindAddress: metricsAddr,
75 Port: 9443,
76 HealthProbeBindAddress: probeAddr,
77 LeaderElection: enableLeaderElection,
78 LeaderElectionID: "c1db6143.dodo.cloud",
79 // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
80 // when the Manager ends. This requires the binary to immediately end when the
81 // Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
82 // speeds up voluntary leader transitions as the new leader don't have to wait
83 // LeaseDuration time first.
84 //
85 // In the default scaffold provided, the program ends immediately after
86 // the manager stops, so would be fine to enable this option. However,
87 // if you are doing or is intended to do any operation such as perform cleanups
88 // after the manager stops then its usage might be unsafe.
89 // LeaderElectionReleaseOnCancel: true,
90 })
91 if err != nil {
92 setupLog.Error(err, "unable to start manager")
93 os.Exit(1)
94 }
95
96 if err != nil {
97 setupLog.Error(err, "unable to chroot into configs directory")
98 os.Exit(1)
99 }
100 store, err := controllers.NewFSZoneStoreFactory(osfs.New(configDir))
101 if err != nil {
102 setupLog.Error(err, "unable to create zone store")
103 os.Exit(1)
104 }
105 if err = (&controllers.DNSZoneReconciler{
106 Client: mgr.GetClient(),
107 Scheme: mgr.GetScheme(),
108 Store: store,
109 }).SetupWithManager(mgr); err != nil {
110 setupLog.Error(err, "unable to create controller", "controller", "DNSZone")
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}