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

✨ Migrate Command Handling to Cobra for Simplified Flag Management #1598

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
24 changes: 13 additions & 11 deletions catalogd/cmd/catalogd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"context"
"crypto/tls"
"flag"
"fmt"
Expand Down Expand Up @@ -96,24 +97,29 @@ func init() {
}

func main() {
cfg := &config{}
cmd := newRootCmd(cfg)
cmd := newRootCmd()
if err := cmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}

func newRootCmd(cfg *config) *cobra.Command {
func newRootCmd() *cobra.Command {
cfg := &config{}
cmd := &cobra.Command{
Use: "catalogd",
Short: "Catalogd is a Kubernetes operator for managing operator catalogs",
RunE: func(cmd *cobra.Command, args []string) error {
return run(cfg)
if err := validateTLSConfig(cfg); err != nil {
return err
}
cmd.SilenceUsage = true

return run(cfg, ctrl.SetupSignalHandler())
},
}

flags := cmd.PersistentFlags()
flags := cmd.Flags()
flags.StringVar(&cfg.metricsAddr, "metrics-bind-address", "", "The address for the metrics endpoint. Requires tls-cert and tls-key. (Default: ':7443')")
flags.StringVar(&cfg.probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flags.StringVar(&cfg.pprofAddr, "pprof-bind-address", "0", "The address the pprof endpoint binds to. an empty string or 0 disables pprof")
Expand Down Expand Up @@ -147,7 +153,7 @@ func newVersionCmd() *cobra.Command {
}
}

func run(cfg *config) error {
func run(cfg *config, ctx context.Context) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By convention context.Context should always be the first argument in the function signature.

ctrl.SetLogger(textlogger.NewLogger(textlogger.NewConfig()))

authFilePath := filepath.Join(os.TempDir(), fmt.Sprintf("%s-%s.json", authFilePrefix, apimachineryrand.String(8)))
Expand All @@ -163,10 +169,6 @@ func run(cfg *config) error {
}
}

if err := validateTLSConfig(cfg); err != nil {
return err
}

protocol := "http://"
if cfg.certFile != "" && cfg.keyFile != "" {
protocol = "https://"
Expand Down Expand Up @@ -243,7 +245,7 @@ func run(cfg *config) error {
return err
}

if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
if err := mgr.Start(ctx); err != nil {
return fmt.Errorf("problem running manager: %w", err)
}

Expand Down
Loading