blob: 8987127e078dda7dd05933db879dfbd280654a27 [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"
Giorgi Lekveishvilie58fc592023-12-07 13:24:07 +040033 "sigs.k8s.io/controller-runtime/pkg/metrics/server"
Giorgi Lekveishvilie8b2f012023-11-30 19:05:03 +040034
35 dodocloudv1 "github.com/giolekva/pcloud/core/ns-controller/api/v1"
36 "github.com/giolekva/pcloud/core/ns-controller/controllers"
37 //+kubebuilder:scaffold:imports
38
39 "github.com/go-git/go-billy/v5/osfs"
40)
41
42var (
43 scheme = runtime.NewScheme()
44 setupLog = ctrl.Log.WithName("setup")
45)
46
47func init() {
48 utilruntime.Must(clientgoscheme.AddToScheme(scheme))
49
50 utilruntime.Must(dodocloudv1.AddToScheme(scheme))
51 //+kubebuilder:scaffold:scheme
52}
53
54func main() {
55 var metricsAddr string
56 var enableLeaderElection bool
57 var probeAddr string
58 var configDir string
Giorgi Lekveishvilie58fc592023-12-07 13:24:07 +040059 var apiPort int
Giorgi Lekveishvilie8b2f012023-11-30 19:05:03 +040060 flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
61 flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
62 flag.BoolVar(&enableLeaderElection, "leader-elect", false,
63 "Enable leader election for controller manager. "+
64 "Enabling this will ensure there is only one active controller manager.")
65 flag.StringVar(&configDir, "config-dir", "/etc/pcloud/dns-zone-configs", "Path to the DNS configurations directory")
Giorgi Lekveishvilie58fc592023-12-07 13:24:07 +040066 flag.IntVar(&apiPort, "api-port", 8082, "Port to listen for API requests")
Giorgi Lekveishvilie8b2f012023-11-30 19:05:03 +040067 opts := zap.Options{
68 Development: true,
69 }
70 opts.BindFlags(flag.CommandLine)
71 flag.Parse()
72
73 ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
74
75 mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Giorgi Lekveishvilie58fc592023-12-07 13:24:07 +040076 Scheme: scheme,
77 Metrics: server.Options{
78 BindAddress: metricsAddr,
79 },
80 // Port: 9443,
Giorgi Lekveishvilie8b2f012023-11-30 19:05:03 +040081 HealthProbeBindAddress: probeAddr,
82 LeaderElection: enableLeaderElection,
83 LeaderElectionID: "c1db6143.dodo.cloud",
84 // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
85 // when the Manager ends. This requires the binary to immediately end when the
86 // Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
87 // speeds up voluntary leader transitions as the new leader don't have to wait
88 // LeaseDuration time first.
89 //
90 // In the default scaffold provided, the program ends immediately after
91 // the manager stops, so would be fine to enable this option. However,
92 // if you are doing or is intended to do any operation such as perform cleanups
93 // after the manager stops then its usage might be unsafe.
94 // LeaderElectionReleaseOnCancel: true,
95 })
96 if err != nil {
97 setupLog.Error(err, "unable to start manager")
98 os.Exit(1)
99 }
100
101 if err != nil {
102 setupLog.Error(err, "unable to chroot into configs directory")
103 os.Exit(1)
104 }
105 store, err := controllers.NewFSZoneStoreFactory(osfs.New(configDir))
106 if err != nil {
107 setupLog.Error(err, "unable to create zone store")
108 os.Exit(1)
109 }
Giorgi Lekveishvilie58fc592023-12-07 13:24:07 +0400110 s := NewServer(apiPort, store)
111 go s.Start()
Giorgi Lekveishvilie8b2f012023-11-30 19:05:03 +0400112 if err = (&controllers.DNSZoneReconciler{
113 Client: mgr.GetClient(),
114 Scheme: mgr.GetScheme(),
115 Store: store,
116 }).SetupWithManager(mgr); err != nil {
117 setupLog.Error(err, "unable to create controller", "controller", "DNSZone")
118 os.Exit(1)
119 }
120 //+kubebuilder:scaffold:builder
121
122 if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
123 setupLog.Error(err, "unable to set up health check")
124 os.Exit(1)
125 }
126 if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
127 setupLog.Error(err, "unable to set up ready check")
128 os.Exit(1)
129 }
130
131 setupLog.Info("starting manager")
132 if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
133 setupLog.Error(err, "problem running manager")
134 os.Exit(1)
135 }
136}