2 ==================================================================================
4 Copyright (c) 2023 Samsung
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
10 http://www.apache.org/licenses/LICENSE-2.0
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.
18 This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 platform project (RICP).
21 ==================================================================================
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"
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"
40 ricdeployv1 "ricdeploy/api/v1"
41 "ricdeploy/internal/controller"
42 //+kubebuilder:scaffold:imports
46 scheme = runtime.NewScheme()
47 setupLog = ctrl.Log.WithName("setup")
51 utilruntime.Must(clientgoscheme.AddToScheme(scheme))
53 utilruntime.Must(ricdeployv1.AddToScheme(scheme))
54 //+kubebuilder:scaffold:scheme
58 var metricsAddr string
59 var enableLeaderElection bool
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.")
69 opts.BindFlags(flag.CommandLine)
72 ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
74 mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
76 MetricsBindAddress: metricsAddr,
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.
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,
94 setupLog.Error(err, "unable to start manager")
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")
105 //+kubebuilder:scaffold:builder
107 if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
108 setupLog.Error(err, "unable to set up health check")
111 if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
112 setupLog.Error(err, "unable to set up ready check")
116 setupLog.Info("starting manager")
117 if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
118 setupLog.Error(err, "problem running manager")