@@ -20,9 +20,10 @@ import (
2020 "strconv"
2121 "syscall"
2222
23- flag "github.com/spf13/pflag "
23+ "github.com/spf13/cobra "
2424
2525 "istio.io/istio/pilot/pkg/model"
26+ "istio.io/istio/pkg/cmd"
2627 "istio.io/istio/pkg/log"
2728 "istio.io/istio/pkg/test/application"
2829 "istio.io/istio/pkg/test/application/echo"
@@ -35,67 +36,77 @@ var (
3536 version string
3637 crt string
3738 key string
38- )
3939
40- var (
41- logOptionsFromCommandline = log .DefaultOptions ()
42- )
40+ loggingOptions = log .DefaultOptions ()
4341
44- func init () {
45- logOptionsFromCommandline .AttachFlags (
46- func (p * []string , name string , value []string , usage string ) {
47- // TODO(ozben): Implement string array method for capturing the complete set of log settings.
42+ rootCmd = & cobra.Command {
43+ Use : "server" ,
44+ Short : "Echo server application." ,
45+ SilenceUsage : true ,
46+ Long : `Echo application for testing Istio E2E` ,
47+ PersistentPreRunE : configureLogging ,
48+ Run : func (cmd * cobra.Command , args []string ) {
49+ ports := make (model.PortList , len (httpPorts )+ len (grpcPorts ))
50+ portIndex := 0
51+ for i , p := range httpPorts {
52+ ports [portIndex ] = & model.Port {
53+ Name : "http-" + strconv .Itoa (i ),
54+ Protocol : model .ProtocolHTTP ,
55+ Port : p ,
56+ }
57+ portIndex ++
58+ }
59+ for i , p := range grpcPorts {
60+ ports [portIndex ] = & model.Port {
61+ Name : "grpc-" + strconv .Itoa (i ),
62+ Protocol : model .ProtocolGRPC ,
63+ Port : p ,
64+ }
65+ portIndex ++
66+ }
67+
68+ f := & echo.Factory {
69+ Ports : ports ,
70+ TLSCert : crt ,
71+ TLSCKey : key ,
72+ Version : version ,
73+ UDSServer : uds ,
74+ }
75+ if _ , err := f .NewApplication (application.Dialer {}); err != nil {
76+ log .Errora (err )
77+ os .Exit (- 1 )
78+ }
79+
80+ // Wait for the process to be shutdown.
81+ sigs := make (chan os.Signal , 1 )
82+ signal .Notify (sigs , syscall .SIGINT , syscall .SIGTERM )
83+ <- sigs
4884 },
49- flag .StringVar ,
50- flag .IntVar ,
51- flag .BoolVar )
85+ }
86+ )
5287
53- flag .IntSliceVar (& httpPorts , "port" , []int {8080 }, "HTTP/1.1 ports" )
54- flag .IntSliceVar (& grpcPorts , "grpc" , []int {7070 }, "GRPC ports" )
55- flag .StringVar (& uds , "uds" , "" , "HTTP server on unix domain socket" )
56- flag .StringVar (& version , "version" , "" , "Version string" )
57- flag .StringVar (& crt , "crt" , "" , "gRPC TLS server-side certificate" )
58- flag .StringVar (& key , "key" , "" , "gRPC TLS server-side key" )
88+ func configureLogging (_ * cobra.Command , _ []string ) error {
89+ if err := log .Configure (loggingOptions ); err != nil {
90+ return err
91+ }
92+ return nil
5993}
6094
61- func main () {
62- flag .Parse ()
95+ func init () {
96+ rootCmd .PersistentFlags ().IntSliceVar (& httpPorts , "port" , []int {8080 }, "HTTP/1.1 ports" )
97+ rootCmd .PersistentFlags ().IntSliceVar (& grpcPorts , "grpc" , []int {7070 }, "GRPC ports" )
98+ rootCmd .PersistentFlags ().StringVar (& uds , "uds" , "" , "HTTP server on unix domain socket" )
99+ rootCmd .PersistentFlags ().StringVar (& version , "version" , "" , "Version string" )
100+ rootCmd .PersistentFlags ().StringVar (& crt , "crt" , "" , "gRPC TLS server-side certificate" )
101+ rootCmd .PersistentFlags ().StringVar (& key , "key" , "" , "gRPC TLS server-side key" )
63102
64- _ = log . Configure ( logOptionsFromCommandline )
103+ loggingOptions . AttachCobraFlags ( rootCmd )
65104
66- ports := make (model.PortList , len (httpPorts )+ len (grpcPorts ))
67- portIndex := 0
68- for i , p := range httpPorts {
69- ports [portIndex ] = & model.Port {
70- Name : "http-" + strconv .Itoa (i ),
71- Protocol : model .ProtocolHTTP ,
72- Port : p ,
73- }
74- portIndex ++
75- }
76- for i , p := range grpcPorts {
77- ports [portIndex ] = & model.Port {
78- Name : "grpc-" + strconv .Itoa (i ),
79- Protocol : model .ProtocolGRPC ,
80- Port : p ,
81- }
82- portIndex ++
83- }
105+ cmd .AddFlags (rootCmd )
106+ }
84107
85- f := & echo.Factory {
86- Ports : ports ,
87- TLSCert : crt ,
88- TLSCKey : key ,
89- Version : version ,
90- UDSServer : uds ,
91- }
92- if _ , err := f .NewApplication (application.Dialer {}); err != nil {
93- log .Errora (err )
108+ func main () {
109+ if err := rootCmd .Execute (); err != nil {
94110 os .Exit (- 1 )
95111 }
96-
97- // Wait for the process to be shutdown.
98- sigs := make (chan os.Signal , 1 )
99- signal .Notify (sigs , syscall .SIGINT , syscall .SIGTERM )
100- <- sigs
101112}
0 commit comments