From 29696c106a4c6dcb7e545b29dbee04630b4bd0d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Mon, 3 Oct 2022 18:34:56 +0200 Subject: [PATCH] refactor: split main into funcs (#4795) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- cmd/kyverno/main.go | 59 +++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/cmd/kyverno/main.go b/cmd/kyverno/main.go index 6ea273e90d..dfd363ae48 100644 --- a/cmd/kyverno/main.go +++ b/cmd/kyverno/main.go @@ -55,6 +55,7 @@ import ( "k8s.io/client-go/kubernetes" metadataclient "k8s.io/client-go/metadata" metadatainformers "k8s.io/client-go/metadata/metadatainformer" + "k8s.io/client-go/rest" ) const ( @@ -140,6 +141,32 @@ func showStartup(logger logr.Logger) { } } +func startProfiling(logger logr.Logger) { + logger = logger.WithName("profiling") + if profile { + addr := ":" + profilePort + logger.Info("Enable profiling, see details at https://github.com/kyverno/kyverno/wiki/Profiling-Kyverno-on-Kubernetes", "port", profilePort) + go func() { + if err := http.ListenAndServe(addr, nil); err != nil { + logger.Error(err, "Failed to enable profiling") + os.Exit(1) + } + }() + } +} + +func createKubeClient() (*rest.Config, *kubernetes.Clientset, error) { + clientConfig, err := config.CreateClientConfig(kubeconfig, clientRateLimitQPS, clientRateLimitBurst) + if err != nil { + return nil, nil, err + } + kubeClient, err := kubernetes.NewForConfig(clientConfig) + if err != nil { + return nil, nil, err + } + return clientConfig, kubeClient, nil +} + func main() { // parse flags if err := parseFlags(); err != nil { @@ -152,6 +179,14 @@ func main() { os.Exit(1) } logger := logging.WithName("setup") + // start profiling + startProfiling(logger) + // create client config and kube client + clientConfig, kubeClient, err := createKubeClient() + if err != nil { + logger.Error(err, "failed to create kube client") + os.Exit(1) + } // show startup message showStartup(logger) // os signal handler @@ -160,19 +195,6 @@ func main() { debug := serverIP != "" - // clients - clientConfig, err := config.CreateClientConfig(kubeconfig, clientRateLimitQPS, clientRateLimitBurst) - if err != nil { - logger.Error(err, "Failed to build kubeconfig") - os.Exit(1) - } - - kubeClient, err := kubernetes.NewForConfig(clientConfig) - if err != nil { - logger.Error(err, "Failed to create kubernetes client") - os.Exit(1) - } - // Metrics Configuration var metricsConfig *metrics.MetricsConfig metricsConfigData, err := config.NewMetricsConfigData(kubeClient) @@ -224,17 +246,6 @@ func main() { os.Exit(1) } - if profile { - addr := ":" + profilePort - logger.V(2).Info("Enable profiling, see details at https://github.com/kyverno/kyverno/wiki/Profiling-Kyverno-on-Kubernetes", "port", profilePort) - go func() { - if err := http.ListenAndServe(addr, nil); err != nil { - logger.Error(err, "Failed to enable profiling") - os.Exit(1) - } - }() - } - // informer factories kubeInformer := kubeinformers.NewSharedInformerFactory(kubeClient, resyncPeriod) kubeKyvernoInformer := kubeinformers.NewSharedInformerFactoryWithOptions(kubeClient, resyncPeriod, kubeinformers.WithNamespace(config.KyvernoNamespace()))