diff --git a/operator/crd/crd.go b/operator/crd/crd.go new file mode 100644 index 00000000000..bf1c1ff86f5 --- /dev/null +++ b/operator/crd/crd.go @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright Authors of Tetragon + +package crd + +import ( + "fmt" + + "github.com/cilium/cilium/pkg/logging" + "github.com/cilium/cilium/pkg/logging/logfields" + "github.com/cilium/tetragon/operator/option" + "github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/client" + "github.com/cilium/tetragon/pkg/k8s/version" + version2 "github.com/cilium/tetragon/pkg/version" + "github.com/sirupsen/logrus" + "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" +) + +var log = logging.DefaultLogger.WithField(logfields.LogSubsys, "crd") + +func RegisterCRDs() { + restConfig, err := getConfig() + if err != nil { + log.WithError(err).Fatal("Unable to check k8s configuration") + } + + k8sClient, err := kubernetes.NewForConfig(restConfig) + if err != nil { + log.WithError(err).Fatal("Unable to create k8s client") + } + + k8sAPIExtClient, err := clientset.NewForConfig(restConfig) + if err != nil { + log.WithError(err).Fatal("Unable to create k8s API ext. client") + } + + err = version.UpdateK8sServerVersion(k8sClient) + if err != nil { + log.WithError(err).Fatal("Unable to check k8s version") + } + + log.WithFields(logrus.Fields{ + "config": fmt.Sprintf("%+v", option.Config), + "version": version2.Version, + }).Info("Starting Tetragon Operator") + capabilities := version.Capabilities() + if !capabilities.MinimalVersionMet { + log.Fatalf("Minimal kubernetes version not met: %s < %s", + version.Version(), version.MinimalVersionConstraint) + } + + // Register the CRDs after validating that we are running on a supported + // version of K8s. + if !option.Config.SkipCRDCreation { + if err := client.RegisterCRDs(k8sAPIExtClient); err != nil { + log.WithError(err).Fatal("Unable to register CRDs") + } + } else { + log.Info("Skipping creation of CRDs") + } + + log.Info("Initialization complete") +} + +func getConfig() (*rest.Config, error) { + if option.Config.KubeCfgPath != "" { + return clientcmd.BuildConfigFromFlags("", option.Config.KubeCfgPath) + } + return rest.InClusterConfig() +} diff --git a/operator/main.go b/operator/main.go index ce5384dcdaf..88d88f57942 100644 --- a/operator/main.go +++ b/operator/main.go @@ -19,20 +19,13 @@ import ( "os" "path/filepath" + "github.com/cilium/tetragon/operator/crd" operatorOption "github.com/cilium/tetragon/operator/option" - "github.com/cilium/tetragon/pkg/k8s/apis/cilium.io/client" - k8sversion "github.com/cilium/tetragon/pkg/k8s/version" - "github.com/cilium/tetragon/pkg/version" - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/tools/clientcmd" "github.com/cilium/cilium/pkg/logging" "github.com/cilium/cilium/pkg/logging/logfields" - "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/viper" - apiextclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset" - "k8s.io/client-go/rest" ) var ( @@ -44,70 +37,18 @@ var ( Use: binaryName, Short: "Run " + binaryName, Run: func(cmd *cobra.Command, args []string) { + // Populate option.Config with options from CLI. + configPopulate() cmdRefDir := viper.GetString(operatorOption.CMDRef) if cmdRefDir != "" { genMarkdown(cmd, cmdRefDir) os.Exit(0) } - operatorExecute() + crd.RegisterCRDs() }, } ) -func getConfig() (*rest.Config, error) { - if operatorOption.Config.KubeCfgPath != "" { - return clientcmd.BuildConfigFromFlags("", operatorOption.Config.KubeCfgPath) - } - return rest.InClusterConfig() -} - -func operatorExecute() { - // Prepopulate option.Config with options from CLI. - configPopulate() - - restConfig, err := getConfig() - if err != nil { - log.WithError(err).Fatal("Unable to check k8s configuration") - } - - k8sClient, err := kubernetes.NewForConfig(restConfig) - if err != nil { - log.WithError(err).Fatal("Unable to create k8s client") - } - - k8sAPIExtClient, err := apiextclientset.NewForConfig(restConfig) - if err != nil { - log.WithError(err).Fatal("Unable to create k8s API ext. client") - } - - err = k8sversion.UpdateK8sServerVersion(k8sClient) - if err != nil { - log.WithError(err).Fatal("Unable to check k8s version") - } - - log.WithFields(logrus.Fields{ - "config": fmt.Sprintf("%+v", operatorOption.Config), - "version": version.Version, - }).Info("Starting Tetragon Operator") - capabilities := k8sversion.Capabilities() - if !capabilities.MinimalVersionMet { - log.Fatalf("Minimal kubernetes version not met: %s < %s", - k8sversion.Version(), k8sversion.MinimalVersionConstraint) - } - - // Register the CRDs after validating that we are running on a supported - // version of K8s. - if !operatorOption.Config.SkipCRDCreation { - if err := client.RegisterCRDs(k8sAPIExtClient); err != nil { - log.WithError(err).Fatal("Unable to register CRDs") - } - } else { - log.Info("Skipping creation of CRDs") - } - - log.Info("Initialization complete") -} - func main() { if err := rootCmd.Execute(); err != nil { fmt.Println(err)