Skip to content

Commit

Permalink
options, app: - Handle more config errors
Browse files Browse the repository at this point in the history
              - Add --help/-h for usage information
  • Loading branch information
bzub committed Jul 6, 2017
1 parent cb661f8 commit 7dedc3f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
2 changes: 2 additions & 0 deletions app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

type KubeRouterConfig struct {
HelpRequested bool
Kubeconfig string
Master string
ConfigSyncPeriod time.Duration
Expand Down Expand Up @@ -41,6 +42,7 @@ func NewKubeRouterConfig() *KubeRouterConfig {
}

func (s *KubeRouterConfig) AddFlags(fs *pflag.FlagSet) {
fs.BoolVarP(&s.HelpRequested, "help", "h", false, "Print usage information.")
fs.BoolVar(&s.RunServiceProxy, "run-service-proxy", s.RunServiceProxy, "If false, kube-router wont setup IPVS for services proxy. True by default.")
fs.BoolVar(&s.RunFirewall, "run-firewall", s.RunFirewall, "If false, kube-router wont setup iptables to provide ingress firewall for pods. True by default.")
fs.BoolVar(&s.RunRouter, "run-router", s.RunRouter, "If true each node advertise routes the rest of the nodes and learn the routes for the pods. True by default.")
Expand Down
14 changes: 7 additions & 7 deletions app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ func NewKubeRouterDefault(config *options.KubeRouterConfig) (*KubeRouter, error)

if len(config.Master) == 0 && len(config.Kubeconfig) == 0 {
if _, err := os.Stat("/var/lib/kube-router/kubeconfig"); os.IsNotExist(err) {
panic("Either one of --master or --kubeconfig must be specified. Or valid kubeconfig file must exist as /var/lib/kube-router/kubeconfig")
return nil, errors.New("Either one of --master or --kubeconfig must be specified. Or valid kubeconfig file must exist as /var/lib/kube-router/kubeconfig")
}
config.Kubeconfig = "/var/lib/kube-router/kubeconfig"
}

clientconfig, err := clientcmd.BuildConfigFromFlags(config.Master, config.Kubeconfig)
if err != nil {
panic(err.Error())
return nil, errors.New("Failed to build configuration from CLI: " + err.Error())
}

clientset, err := kubernetes.NewForConfig(clientconfig)
if err != nil {
panic(err.Error())
return nil, errors.New("Failed to create Kubernetes client: " + err.Error())
}

return &KubeRouter{Client: clientset, Config: config}, nil
Expand Down Expand Up @@ -108,7 +108,7 @@ func (kr *KubeRouter) Run() error {

err = kr.startApiWatchers()
if err != nil {
panic("Failed to start API watchers: " + err.Error())
return errors.New("Failed to start API watchers: " + err.Error())
}

if !(kr.Config.RunFirewall || kr.Config.RunServiceProxy || kr.Config.RunRouter) {
Expand All @@ -119,7 +119,7 @@ func (kr *KubeRouter) Run() error {
if kr.Config.RunFirewall {
npc, err := controllers.NewNetworkPolicyController(kr.Client, kr.Config)
if err != nil {
panic("Failed to create network policy controller: " + err.Error())
return errors.New("Failed to create network policy controller: " + err.Error())
}
npcStopCh = make(chan struct{})
wg.Add(1)
Expand All @@ -129,7 +129,7 @@ func (kr *KubeRouter) Run() error {
if kr.Config.RunRouter {
nrc, err := controllers.NewNetworkRoutingController(kr.Client, kr.Config)
if err != nil {
panic("Failed to create network routing controller: " + err.Error())
return errors.New("Failed to create network routing controller: " + err.Error())
}
nrcStopCh = make(chan struct{})
wg.Add(1)
Expand All @@ -139,7 +139,7 @@ func (kr *KubeRouter) Run() error {
if kr.Config.RunServiceProxy {
nsc, err := controllers.NewNetworkServicesController(kr.Client, kr.Config)
if err != nil {
panic("Failed to create network services controller: " + err.Error())
return errors.New("Failed to create network services controller: " + err.Error())
}
nscStopCh = make(chan struct{})
wg.Add(1)
Expand Down
10 changes: 8 additions & 2 deletions kube-router.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ func main() {

flag.Set("logtostderr", "true")

if config.HelpRequested {
pflag.Usage()
os.Exit(0)
}

if os.Geteuid() != 0 {
fmt.Fprintf(os.Stderr, "kube-router need to be run by user with previlages to execute iptables, ipset and configure ipvs\n")
fmt.Fprintf(os.Stderr, "kube-router needs to be run with privileges to execute iptables, ipset and configure ipvs\n")
os.Exit(1)
}

Expand All @@ -34,7 +39,8 @@ func main() {
os.Exit(1)
}

if err = kubeRouter.Run(); err != nil {
err = kubeRouter.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to run kube-router: %v\n", err)
os.Exit(1)
}
Expand Down

0 comments on commit 7dedc3f

Please sign in to comment.