Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor files under operator directory #1459

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions operator/flags.go → operator/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,43 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium

package main
package cmd

import (
"os"
"path/filepath"
"strings"

"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/tetragon/operator/crd"
operatorOption "github.com/cilium/tetragon/operator/option"
"github.com/cilium/tetragon/pkg/cmdref"
"github.com/cilium/tetragon/pkg/option"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func init() {
initializeFlags()
}
// New create a new root command.
func New() *cobra.Command {
binaryName := filepath.Base(os.Args[0])
log := logging.DefaultLogger.WithField(logfields.LogSubsys, binaryName)

rootCmd := &cobra.Command{
Use: binaryName,
Short: "Run " + binaryName,
Run: func(cmd *cobra.Command, args []string) {
// Populate option.Config with options from CLI.
operatorOption.ConfigPopulate()
cmdRefDir := viper.GetString(operatorOption.CMDRef)
if cmdRefDir != "" {
cmdref.GenMarkdown(cmd, cmdRefDir)
os.Exit(0)
}
crd.RegisterCRDs()
},
}

func initializeFlags() {
cobra.OnInitialize(func() {
replacer := strings.NewReplacer("-", "_", ".", "_")
viper.SetEnvKeyReplacer(replacer)
Expand Down Expand Up @@ -48,12 +68,6 @@ func initializeFlags() {
flags.Bool(operatorOption.SkipPodInfoCRD, false, "When true, PodInfo Custom Resource Definition (CRD) will not be created")

viper.BindPFlags(flags)
}

// configPopulate sets all options with the values from viper.
func configPopulate() {
operatorOption.Config.SkipCRDCreation = viper.GetBool(operatorOption.SkipCRDCreation)
operatorOption.Config.KubeCfgPath = viper.GetString(operatorOption.KubeCfgPath)
operatorOption.Config.ConfigDir = viper.GetString(operatorOption.ConfigDir)
operatorOption.Config.SkipPodInfoCRD = viper.GetBool(operatorOption.SkipPodInfoCRD)
return rootCmd
}
32 changes: 2 additions & 30 deletions operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,12 @@ package main
import (
"fmt"
"os"
"path/filepath"

"github.com/cilium/tetragon/operator/crd"
operatorOption "github.com/cilium/tetragon/operator/option"

"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
binaryName = filepath.Base(os.Args[0])

log = logging.DefaultLogger.WithField(logfields.LogSubsys, binaryName)

rootCmd = &cobra.Command{
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)
}
crd.RegisterCRDs()
},
}
"github.com/cilium/tetragon/operator/cmd"
)

func main() {
if err := rootCmd.Execute(); err != nil {
if err := cmd.New().Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
Expand Down
10 changes: 10 additions & 0 deletions operator/option/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package option

import "github.com/spf13/viper"

const (
TetragonOpEnvPrefix = "TETRAGON_OPERATOR"

Expand Down Expand Up @@ -42,3 +44,11 @@ type OperatorConfig struct {

// Config represents the operator configuration.
var Config = &OperatorConfig{}

// ConfigPopulate sets all options with the values from viper.
func ConfigPopulate() {
Config.SkipCRDCreation = viper.GetBool(SkipCRDCreation)
Config.KubeCfgPath = viper.GetString(KubeCfgPath)
Config.ConfigDir = viper.GetString(ConfigDir)
Config.SkipPodInfoCRD = viper.GetBool(SkipPodInfoCRD)
}
8 changes: 6 additions & 2 deletions operator/cmdref.go → pkg/cmdref/cmdref.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium

package main
package cmdref

import (
"fmt"
"strings"

"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)

var log = logging.DefaultLogger.WithField(logfields.LogSubsys, "cmdref")

func linkHandler(s string) string {
// The generated files have a 'See also' section but the URL's are
// hardcoded to use Markdown but we only want / have them in HTML
Expand All @@ -25,7 +29,7 @@ func filePrepend(_ string) string {
return fmt.Sprintf("%s\n\n", "<!-- This file was autogenerated via tetragon-operator --cmdref, do not edit manually-->")
}

func genMarkdown(cmd *cobra.Command, cmdRefDir string) {
func GenMarkdown(cmd *cobra.Command, cmdRefDir string) {
// Remove the line 'Auto generated by spf13/cobra on ...'
cmd.DisableAutoGenTag = true
if err := doc.GenMarkdownTreeCustom(cmd, cmdRefDir, filePrepend, linkHandler); err != nil {
Expand Down