Skip to content

Commit

Permalink
Seperate App common flags to a package
Browse files Browse the repository at this point in the history
Signed-off-by: Kiefer Chang <kiefer.chang@suse.com>
  • Loading branch information
bk201 authored and guangbochen committed Jun 11, 2021
1 parent 2f77a63 commit 7614afd
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 76 deletions.
82 changes: 8 additions & 74 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,21 @@ package main
import (
"context"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os"

"github.com/ehazlett/simplelog"
"github.com/rancher/wrangler/pkg/signals"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"

"github.com/harvester/harvester/pkg/cmd"
"github.com/harvester/harvester/pkg/config"
"github.com/harvester/harvester/pkg/server"
"github.com/harvester/harvester/pkg/version"
)

var (
profileAddress = "localhost:6060"
KubeConfig string
)

func main() {
var options config.Options

app := cli.NewApp()
app.Name = "harvester"
app.Version = version.FriendlyVersion()
app.Usage = ""
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "kubeconfig",
EnvVar: "KUBECONFIG",
Usage: "Kube config for accessing k8s cluster",
Destination: &KubeConfig,
},
cli.BoolFlag{
Name: "debug",
EnvVar: "HARVESTER_DEBUG",
Usage: "Enable debug logs",
Destination: &options.Debug,
},
cli.BoolFlag{
Name: "trace",
EnvVar: "HARVESTER_TRACE",
Usage: "Enable trace logs",
Destination: &options.Trace,
},
flags := []cli.Flag{
cli.IntFlag{
Name: "threadiness",
EnvVar: "THREADINESS",
Expand Down Expand Up @@ -100,12 +69,6 @@ func main() {
Usage: "Enable HCI mode. Additional controllers are registered in HCI mode",
Destination: &options.HCIMode,
},
cli.StringFlag{
Name: "profile-listen-address",
Value: "0.0.0.0:6060",
Usage: "Address to listen on for profiling",
Destination: &profileAddress,
},
cli.BoolFlag{
Name: "rancher-embedded",
EnvVar: "RANCHER_EMBEDDED",
Expand All @@ -119,47 +82,18 @@ func main() {
Destination: &options.RancherURL,
},
}
app.Action = func(c *cli.Context) error {
// enable profiler
if profileAddress != "" {
go func() {
log.Println(http.ListenAndServe(profileAddress, nil))
}()
}
initLogs(c, options)
return run(c, options)
}

if err := app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
}

func initLogs(c *cli.Context, options config.Options) {
switch c.String("log-format") {
case "simple":
logrus.SetFormatter(&simplelog.StandardFormatter{})
case "text":
logrus.SetFormatter(&logrus.TextFormatter{})
case "json":
logrus.SetFormatter(&logrus.JSONFormatter{})
}
logrus.SetOutput(os.Stdout)
if options.Debug {
logrus.SetLevel(logrus.DebugLevel)
logrus.Debugf("Loglevel set to [%v]", logrus.DebugLevel)
}
if options.Trace {
logrus.SetLevel(logrus.TraceLevel)
logrus.Tracef("Loglevel set to [%v]", logrus.TraceLevel)
}
app := cmd.NewApp("Harvester API Server", "", flags, func(commonOptions *config.CommonOptions) error {
return run(commonOptions, options)
})
app.Run()
}

func run(c *cli.Context, options config.Options) error {
func run(commonOptions *config.CommonOptions, options config.Options) error {
logrus.Info("Starting controller")
ctx := signals.SetupSignalHandler(context.Background())

kubeConfig, err := server.GetConfig(KubeConfig)
kubeConfig, err := server.GetConfig(commonOptions.KubeConfig)
if err != nil {
return fmt.Errorf("failed to find kubeconfig: %v", err)
}
Expand Down
112 changes: 112 additions & 0 deletions pkg/cmd/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package cmd

import (
"log"
"net/http"
"os"

"github.com/ehazlett/simplelog"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"

"github.com/harvester/harvester/pkg/config"
"github.com/harvester/harvester/pkg/version"
)

type App struct {
app *cli.App
Options *config.CommonOptions
}

type Action func(*config.CommonOptions) error

func NewApp(name string, usage string, flags []cli.Flag, action Action) *App {
cliApp := cli.NewApp()

cliApp.Name = name
cliApp.Version = version.FriendlyVersion()
cliApp.Usage = usage

// common flags
options := config.CommonOptions{}
cliApp.Flags = []cli.Flag{
cli.StringFlag{
Name: "kubeconfig",
EnvVar: "KUBECONFIG",
Usage: "Kube config for accessing k8s cluster",
Destination: &options.KubeConfig,
},
cli.StringFlag{
Name: "profile-listen-address",
Value: "0.0.0.0:6060",
Usage: "Address to listen on for profiling",
Destination: &options.ProfilerAddress,
},
cli.BoolFlag{
Name: "debug",
EnvVar: "HARVESTER_DEBUG",
Usage: "Enable debug logs",
Destination: &options.Debug,
},
cli.BoolFlag{
Name: "trace",
EnvVar: "HARVESTER_TRACE",
Usage: "Enable trace logs",
Destination: &options.Trace,
},
cli.StringFlag{
Name: "log-format",
EnvVar: "HARVESTER_LOG_FORMAT",
Usage: "Log format",
Value: "text",
Destination: &options.LogFormat,
},
}

cliApp.Flags = append(cliApp.Flags, flags...)
cliApp.Action = func(c *cli.Context) error {
initProfiling(&options)
initLogs(&options)
return action(&options)
}

return &App{
app: cliApp,
Options: &options,
}
}

func initProfiling(options *config.CommonOptions) {
// enable profiler
if options.ProfilerAddress != "" {
go func() {
log.Println(http.ListenAndServe(options.ProfilerAddress, nil))
}()
}
}

func initLogs(options *config.CommonOptions) {
switch options.LogFormat {
case "simple":
logrus.SetFormatter(&simplelog.StandardFormatter{})
case "json":
logrus.SetFormatter(&logrus.JSONFormatter{})
default:
logrus.SetFormatter(&logrus.TextFormatter{})
}
logrus.SetOutput(os.Stdout)
if options.Debug {
logrus.SetLevel(logrus.DebugLevel)
logrus.Debugf("Loglevel set to [%v]", logrus.DebugLevel)
}
if options.Trace {
logrus.SetLevel(logrus.TraceLevel)
logrus.Tracef("Loglevel set to [%v]", logrus.TraceLevel)
}
}

func (a *App) Run() {
if err := a.app.Run(os.Args); err != nil {
logrus.Fatal(err)
}
}
10 changes: 10 additions & 0 deletions pkg/config/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package config

type CommonOptions struct {
Debug bool
Trace bool
LogFormat string

ProfilerAddress string
KubeConfig string
}
2 changes: 0 additions & 2 deletions pkg/config/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ type Options struct {
Threadiness int
HTTPListenPort int
HTTPSListenPort int
Debug bool
Trace bool

SkipAuthentication bool
RancherEmbedded bool
Expand Down

0 comments on commit 7614afd

Please sign in to comment.