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