-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathroot.go
More file actions
74 lines (64 loc) · 2.61 KB
/
root.go
File metadata and controls
74 lines (64 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package cmd
import (
"os"
"github.com/runconduit/conduit/controller/api/public"
pb "github.com/runconduit/conduit/controller/gen/public"
"github.com/runconduit/conduit/pkg/k8s"
"github.com/runconduit/conduit/pkg/shell"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var cfgFile string
var controlPlaneNamespace string
var apiAddr string // An empty value means "use the Kubernetes configuration"
var kubeconfigPath string
var logLevel string
var RootCmd = &cobra.Command{
Use: "conduit",
Short: "conduit manages the Conduit service mesh",
Long: `conduit manages the Conduit service mesh.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// set global log level
level, err := log.ParseLevel(logLevel)
if err != nil {
log.Fatalf("invalid log-level: %s", logLevel)
}
log.SetLevel(level)
},
}
func init() {
RootCmd.PersistentFlags().StringVarP(&controlPlaneNamespace, "conduit-namespace", "n", "conduit", "namespace in which Conduit is installed")
RootCmd.PersistentFlags().StringVar(&logLevel, "log-level", log.FatalLevel.String(), "log level, must be one of: panic, fatal, error, warn, info, debug")
}
// TODO: decide if we want to use viper
func addControlPlaneNetworkingArgs(cmd *cobra.Command) {
// Use the same argument name as `kubectl` (see the output of `kubectl options`).
cmd.PersistentFlags().StringVar(&kubeconfigPath, "kubeconfig", "", "Path to the kubeconfig file to use for CLI requests")
cmd.PersistentFlags().StringVar(&apiAddr, "api-addr", "", "Override kubeconfig and communicate directly with the control plane at host:port (mostly for testing)")
}
func newPublicAPIClient() (pb.ApiClient, error) {
if apiAddr != "" {
return public.NewInternalClient(apiAddr)
}
kubeApi, err := k8s.NewK8sAPI(shell.NewUnixShell(), kubeconfigPath)
if err != nil {
return nil, err
}
return public.NewExternalClient(controlPlaneNamespace, kubeApi)
}
// Exit with non-zero exit status without printing the command line usage and
// without printing the error message.
//
// When a `RunE` command returns an error, Cobra will print the usage message
// so the `RunE` function needs to handle any non-usage errors itself without
// returning an error. `exitSilentlyOnError` can be used as the `Run` (not
// `RunE`) function to help with this.
//
// TODO: This is used by the `version` command now; it should be used by other commands too.
func exitSilentlyOnError(f func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
if err := f(cmd, args); err != nil {
os.Exit(2) // Reserve 1 for usage errors.
}
}
}