blob: 6e14be530d74de96d662610d5ec27ef97a93a866 [file] [log] [blame]
Giorgi Lekveishvilie7746c62023-07-20 10:07:06 +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 "golang.org/x/crypto/ssh"
22 "os"
23
24 // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
25 // to ensure that exec-entrypoint and run can make use of them.
26 _ "k8s.io/client-go/plugin/pkg/client/auth"
27
28 "k8s.io/apimachinery/pkg/runtime"
29 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
30 clientgoscheme "k8s.io/client-go/kubernetes/scheme"
31 ctrl "sigs.k8s.io/controller-runtime"
32 "sigs.k8s.io/controller-runtime/pkg/healthz"
33 "sigs.k8s.io/controller-runtime/pkg/log/zap"
34
35 transportv1 "github.com/giolekva/pcloud/api/v1"
36 "github.com/giolekva/pcloud/controllers"
37 //+kubebuilder:scaffold:imports
38)
39
40var (
41 scheme = runtime.NewScheme()
42 setupLog = ctrl.Log.WithName("setup")
43)
44
45func init() {
46 utilruntime.Must(clientgoscheme.AddToScheme(scheme))
47
48 utilruntime.Must(transportv1.AddToScheme(scheme))
49 //+kubebuilder:scaffold:scheme
50}
51
52func main() {
53 var metricsAddr string
54 var enableLeaderElection bool
55 var probeAddr string
56 var sshKey string
57 flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
58 flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
59 flag.BoolVar(&enableLeaderElection, "leader-elect", false,
60 "Enable leader election for controller manager. "+
61 "Enabling this will ensure there is only one active controller manager.")
62 flag.StringVar(&sshKey, "ssh-key", "", "Path to private SSH key file.")
63 opts := zap.Options{
64 Development: true,
65 }
66 opts.BindFlags(flag.CommandLine)
67 flag.Parse()
68
69 ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
70
71 privKey, err := os.ReadFile(sshKey)
72 if err != nil {
73 panic(err)
74 }
75 signer, err := ssh.ParsePrivateKey(privKey)
76
77 mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
78 Scheme: scheme,
79 MetricsBindAddress: metricsAddr,
80 Port: 9443,
81 HealthProbeBindAddress: probeAddr,
82 LeaderElection: enableLeaderElection,
83 LeaderElectionID: "798a733c.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 = (&controllers.ServiceTransportReconciler{
102 Client: mgr.GetClient(),
103 Scheme: mgr.GetScheme(),
104 Signer: signer,
105 }).SetupWithManager(mgr); err != nil {
106 setupLog.Error(err, "unable to create controller", "controller", "ServiceTransport")
107 os.Exit(1)
108 }
109 //+kubebuilder:scaffold:builder
110
111 if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
112 setupLog.Error(err, "unable to set up health check")
113 os.Exit(1)
114 }
115 if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
116 setupLog.Error(err, "unable to set up ready check")
117 os.Exit(1)
118 }
119
120 setupLog.Info("starting manager")
121 if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
122 setupLog.Error(err, "problem running manager")
123 os.Exit(1)
124 }
125}