Skip to content

Commit

Permalink
operator: Move the CRD registration logic to a separate package
Browse files Browse the repository at this point in the history
We'll introduce a proper operator deployment in #794. Once the operator
deployment is available, we can move the CRD registration logic there
instead of calling it from an init container in the Tetragon daemonset.
This commit moves the CRD registration logic to a separate package so
that it can be called from outside the main package.

Signed-off-by: Michi Mutsuzaki <michi@isovalent.com>
  • Loading branch information
michi-covalent committed Aug 11, 2023
1 parent ea1fdd6 commit 4d4dedf
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 63 deletions.
73 changes: 73 additions & 0 deletions operator/crd/crd.go
Original file line number Diff line number Diff line change
@@ -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()
}
67 changes: 4 additions & 63 deletions operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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)
Expand Down

0 comments on commit 4d4dedf

Please sign in to comment.