github.com/openshift/dpu-operator@v0.0.0-20240502153209-3af840d137c2/cmd/main.go (about)

     1  /*
     2  Copyright 2024.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    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  	"sigs.k8s.io/controller-runtime/pkg/metrics/server"
    34  	"sigs.k8s.io/controller-runtime/pkg/webhook"
    35  
    36  	configv1 "github.com/openshift/dpu-operator/api/v1"
    37  	"github.com/openshift/dpu-operator/internal/controller"
    38  	//+kubebuilder:scaffold:imports
    39  )
    40  
    41  var (
    42  	scheme   = runtime.NewScheme()
    43  	setupLog = ctrl.Log.WithName("setup")
    44  )
    45  
    46  func init() {
    47  	utilruntime.Must(clientgoscheme.AddToScheme(scheme))
    48  
    49  	utilruntime.Must(configv1.AddToScheme(scheme))
    50  	//+kubebuilder:scaffold:scheme
    51  }
    52  
    53  func main() {
    54  	var metricsAddr string
    55  	var enableLeaderElection bool
    56  	var probeAddr string
    57  	flag.StringVar(&metricsAddr, "metrics-bind-address", ":18090", "The address the metric endpoint binds to.")
    58  	flag.StringVar(&probeAddr, "health-probe-bind-address", ":18091", "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  	opts := zap.Options{
    63  		Development: true,
    64  	}
    65  	opts.BindFlags(flag.CommandLine)
    66  	flag.Parse()
    67  
    68  	ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
    69  
    70  	mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
    71  		Scheme: scheme,
    72  		Metrics: server.Options{
    73  			BindAddress: metricsAddr,
    74  		},
    75  		WebhookServer:          webhook.NewServer(webhook.Options{Port: 9443}),
    76  		HealthProbeBindAddress: probeAddr,
    77  		LeaderElection:         enableLeaderElection,
    78  		LeaderElectionID:       "1e46962d.openshift.io",
    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 = (&controller.DpuOperatorConfigReconciler{
    97  		Client: mgr.GetClient(),
    98  		Scheme: mgr.GetScheme(),
    99  	}).SetupWithManager(mgr); err != nil {
   100  		setupLog.Error(err, "unable to create controller", "controller", "DpuOperatorConfig")
   101  		os.Exit(1)
   102  	}
   103  	//+kubebuilder:scaffold:builder
   104  
   105  	if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
   106  		setupLog.Error(err, "unable to set up health check")
   107  		os.Exit(1)
   108  	}
   109  	if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
   110  		setupLog.Error(err, "unable to set up ready check")
   111  		os.Exit(1)
   112  	}
   113  
   114  	setupLog.Info("starting manager")
   115  	if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
   116  		setupLog.Error(err, "problem running manager")
   117  		os.Exit(1)
   118  	}
   119  }