From 4fd5cb812a8a69db8d6a7100b877a43a492fd1dc Mon Sep 17 00:00:00 2001 From: Patrick Hemmer Date: Sat, 7 Apr 2018 14:47:15 -0400 Subject: [PATCH] update command line options documentation (#372) * update command line options documentation * add test for command line help doc --- Documentation/README.md | 64 +++++++++++++++++++++++------------------ Makefile | 2 +- kube-router.go | 24 ++++++++++------ kube-router_test.go | 49 +++++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 38 deletions(-) create mode 100644 kube-router_test.go diff --git a/Documentation/README.md b/Documentation/README.md index e52f184d..e187edf5 100644 --- a/Documentation/README.md +++ b/Documentation/README.md @@ -99,34 +99,42 @@ Also you can choose to run kube-router as agent running on each cluster node. Al ### command line options ``` -Usage of ./kube-router: - --advertise-cluster-ip Add Cluster IP of the service to the RIB so that it gets advertises to the BGP peers. - --advertise-external-ip Add External IP of service to the RIB so that it gets advertised to the BGP peers. - --cleanup-config Cleanup iptables rules, ipvs, ipset configuration and exit. - --cluster-asn uint ASN number under which cluster nodes will run iBGP. - --cluster-cidr string CIDR range of pods in the cluster. It is used to identify traffic originating from and destinated to pods. - --config-sync-period duration The delay between apiserver configuration synchronizations (e.g. '5s', '1m'). Must be greater than 0. (default 1m0s) - --enable-overlay When enable-overlay set to true, IP-in-IP tunneling is used for pod-to-pod networking across nodes in different subnets. When set to false no tunneling is used and routing infrastrcture is expected to route traffic for pod-to-pod networking across nodes in different subnets (default true) - --enable-pod-egress SNAT traffic from Pods to destinations outside the cluster. (default true) - --hairpin-mode Add iptable rules for every Service Endpoint to support hairpin traffic. - -h, --help Print usage information. - --hostname-override string Overrides the NodeName of the node. Set this if kube-router is unable to determine your NodeName automatically. - --iptables-sync-period duration The delay between iptables rule synchronizations (e.g. '5s', '1m'). Must be greater than 0. (default 1m0s) - --ipvs-sync-period duration The delay between ipvs config synchronizations (e.g. '5s', '1m', '2h22m'). Must be greater than 0. (default 1m0s) - --kubeconfig string Path to kubeconfig file with authorization information (the master location is set by the master flag). - --masquerade-all SNAT all traffic to cluster IP/node port. - --master string The address of the Kubernetes API server (overrides any value in kubeconfig). - --metrics-port int Prometheus metrics port to use ( default 8080 ) - --metrics-path string Path to serve Prometheus metrics on ( default /metrics ) - --nodeport-bindon-all-ip For service of NodePort type create IPVS service that listens on all IP's of the node. - --nodes-full-mesh Each node in the cluster will setup BGP peering with rest of the nodes. (default true) - --peer-router-asns uintSlice ASN numbers of the BGP peer to which cluster nodes will advertise cluster ip and node's pod cidr. (default []) - --peer-router-ips ipSlice The ip address of the external router to which all nodes will peer and advertise the cluster ip and pod cidr's. (default []) - --peer-router-passwords stringSlice Password for authenticating against the BGP peer defined with "--peer-router-ips". - --routes-sync-period duration The delay between route updates and advertisements (e.g. '5s', '1m', '2h22m'). Must be greater than 0. (default 1m0s) - --run-firewall Enables Network Policy -- sets up iptables to provide ingress firewall for pods. (default true) - --run-router Enables Pod Networking -- Advertises and learns the routes to Pods via iBGP. (default true) - --run-service-proxy Enables Service Proxy -- sets up IPVS for Kubernetes Services. (default true)``` +Usage of kube-router: + --advertise-cluster-ip Add Cluster IP of the service to the RIB so that it gets advertises to the BGP peers. + --advertise-external-ip Add External IP of service to the RIB so that it gets advertised to the BGP peers. + --advertise-loadbalancer-ip Add LoadbBalancer IP of service status as set by the LB provider to the RIB so that it gets advertised to the BGP peers. + --bgp-graceful-restart Enables the BGP Graceful Restart capability so that routes are preserved on unexpected restarts + --cleanup-config Cleanup iptables rules, ipvs, ipset configuration and exit. + --cluster-asn uint ASN number under which cluster nodes will run iBGP. + --cluster-cidr string CIDR range of pods in the cluster. It is used to identify traffic originating from and destinated to pods. + --config-sync-period duration The delay between apiserver configuration synchronizations (e.g. '5s', '1m'). Must be greater than 0. (default 1m0s) + --enable-ibgp Enables peering with nodes with the same ASN, if disabled will only peer with external BGP peers (default true) + --enable-overlay When enable-overlay set to true, IP-in-IP tunneling is used for pod-to-pod networking across nodes in different subnets. When set to false no tunneling is used and routing infrastrcture is expected to route traffic for pod-to-pod networking across nodes in different subnets (default true) + --enable-pod-egress SNAT traffic from Pods to destinations outside the cluster. (default true) + --enable-pprof Enables pprof for debugging performance and memory leak issues. + --hairpin-mode Add iptable rules for every Service Endpoint to support hairpin traffic. + --health-port uint16 Health check port, 0 = Disabled (default 20244) + -h, --help Print usage information. + --hostname-override string Overrides the NodeName of the node. Set this if kube-router is unable to determine your NodeName automatically. + --iptables-sync-period duration The delay between iptables rule synchronizations (e.g. '5s', '1m'). Must be greater than 0. (default 1m0s) + --ipvs-sync-period duration The delay between ipvs config synchronizations (e.g. '5s', '1m', '2h22m'). Must be greater than 0. (default 1m0s) + --kubeconfig string Path to kubeconfig file with authorization information (the master location is set by the master flag). + --masquerade-all SNAT all traffic to cluster IP/node port. + --master string The address of the Kubernetes API server (overrides any value in kubeconfig). + --metrics-path string Prometheus metrics path (default "/metrics") + --metrics-port uint16 Prometheus metrics port, (Default 0, Disabled) + --nodeport-bindon-all-ip For service of NodePort type create IPVS service that listens on all IP's of the node. + --nodes-full-mesh Each node in the cluster will setup BGP peering with rest of the nodes. (default true) + --peer-router-asns uints ASN numbers of the BGP peer to which cluster nodes will advertise cluster ip and node's pod cidr. (default []) + --peer-router-ips ipSlice The ip address of the external router to which all nodes will peer and advertise the cluster ip and pod cidr's. (default []) + --peer-router-multihop-ttl uint8 Enable eBGP multihop supports -- sets multihop-ttl. (Relevant only if ttl >= 2) + --peer-router-passwords strings Password for authenticating against the BGP peer defined with "--peer-router-ips". + --routes-sync-period duration The delay between route updates and advertisements (e.g. '5s', '1m', '2h22m'). Must be greater than 0. (default 1m0s) + --run-firewall Enables Network Policy -- sets up iptables to provide ingress firewall for pods. (default true) + --run-router Enables Pod Networking -- Advertises and learns the routes to Pods via iBGP. (default true) + --run-service-proxy Enables Service Proxy -- sets up IPVS for Kubernetes Services. (default true) + -v, --v string log level for V logs (default "0") + -V, --version Print version information. ``` ### requirements diff --git a/Makefile b/Makefile index fb051319..e6f93a72 100644 --- a/Makefile +++ b/Makefile @@ -39,7 +39,7 @@ kube-router: @echo Finished kube-router binary build. test: gofmt ## Runs code quality pipelines (gofmt, tests, coverage, lint, etc) - go test github.com/cloudnativelabs/kube-router/app/... github.com/cloudnativelabs/kube-router/utils/ + go test github.com/cloudnativelabs/kube-router github.com/cloudnativelabs/kube-router/app/... github.com/cloudnativelabs/kube-router/utils/ vagrant-up: export docker=$(DOCKER) vagrant-up: export DEV_IMG=$(REGISTRY_DEV):$(IMG_TAG) diff --git a/kube-router.go b/kube-router.go index cc0f33e2..5c25501f 100644 --- a/kube-router.go +++ b/kube-router.go @@ -14,7 +14,14 @@ import ( ) func main() { + if err := Main(); err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err) + os.Exit(1) + } + os.Exit(0) +} +func Main() error { config := options.NewKubeRouterConfig() config.AddFlags(pflag.CommandLine) pflag.Parse() @@ -28,28 +35,26 @@ func main() { if config.HelpRequested { pflag.Usage() - os.Exit(0) + return nil } if config.Version { app.PrintVersion(false) - os.Exit(0) + return nil } if os.Geteuid() != 0 { - fmt.Fprintf(os.Stderr, "kube-router needs to be run with privileges to execute iptables, ipset and configure ipvs\n") - os.Exit(1) + return fmt.Errorf("kube-router needs to be run with privileges to execute iptables, ipset and configure ipvs") } if config.CleanupConfig { app.CleanupConfigAndExit() - os.Exit(0) + return nil } kubeRouter, err := app.NewKubeRouterDefault(config) if err != nil { - fmt.Fprintf(os.Stderr, "Failed to parse kube-router config: %v\n", err) - os.Exit(1) + return fmt.Errorf("Failed to parse kube-router config: %v", err) } if config.EnablePprof { @@ -60,7 +65,8 @@ func main() { err = kubeRouter.Run() if err != nil { - fmt.Fprintf(os.Stderr, "Failed to run kube-router: %v\n", err) - os.Exit(1) + return fmt.Errorf("Failed to run kube-router: %v", err) } + + return nil } diff --git a/kube-router_test.go b/kube-router_test.go new file mode 100644 index 00000000..6693b47f --- /dev/null +++ b/kube-router_test.go @@ -0,0 +1,49 @@ +package main + +import ( + "bytes" + "io" + "os" + "sync" + "testing" +) + +func TestMainHelp(t *testing.T) { + origStderr := os.Stderr + stderrR, stderrW, _ := os.Pipe() + os.Stderr = stderrW + defer func() { os.Stderr = origStderr }() + + stderrBuf := bytes.NewBuffer(nil) + wg := &sync.WaitGroup{} + wg.Add(1) + go func() { + io.Copy(stderrBuf, stderrR) + wg.Done() + }() + + origArgs := os.Args + os.Args = []string{"kube-router", "--help"} + defer func() { os.Args = origArgs }() + + if err := Main(); err != nil { + t.Fatalf("kube-router exited with error: %s\n", err) + } + stderrW.Close() + wg.Wait() + + docF, err := os.Open("Documentation/README.md") + if err != nil { + t.Fatalf("could not open Documentation/README.md: %s\n", err) + } + docBuf := bytes.NewBuffer(nil) + docBuf.ReadFrom(docF) + docF.Close() + + exp := append([]byte("```\n"), stderrBuf.Bytes()...) + exp = append(exp, []byte("```\n")...) + + if !bytes.Contains(docBuf.Bytes(), exp) { + t.Errorf("Documentation/README.md 'command line options' section does not match `kube-router --help`.\nExpected:\n%s", exp) + } +}