istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/echo/cmd/server/main.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "os" 19 "os/signal" 20 "strconv" 21 "syscall" 22 23 "github.com/spf13/cobra" 24 // To install the xds resolvers and balancers. 25 _ "google.golang.org/grpc/xds" 26 27 "istio.io/istio/pkg/cmd" 28 "istio.io/istio/pkg/config/protocol" 29 "istio.io/istio/pkg/log" 30 "istio.io/istio/pkg/test/echo/common" 31 "istio.io/istio/pkg/test/echo/server" 32 ) 33 34 var ( 35 httpPorts []int 36 grpcPorts []int 37 tcpPorts []int 38 udpPorts []int 39 tlsPorts []int 40 hbonePorts []int 41 instanceIPPorts []int 42 localhostIPPorts []int 43 serverFirstPorts []int 44 xdsGRPCServers []int 45 metricsPort int 46 uds string 47 version string 48 cluster string 49 crt string 50 key string 51 istioVersion string 52 disableALPN bool 53 54 loggingOptions = log.DefaultOptions() 55 56 rootCmd = &cobra.Command{ 57 Use: "server", 58 Short: "Echo server application.", 59 SilenceUsage: true, 60 Long: `Echo application for testing Istio E2E`, 61 PersistentPreRunE: configureLogging, 62 Run: func(cmd *cobra.Command, args []string) { 63 ports := make(common.PortList, len(httpPorts)+len(grpcPorts)+len(tcpPorts)+len(udpPorts)+len(hbonePorts)) 64 tlsByPort := map[int]bool{} 65 for _, p := range tlsPorts { 66 tlsByPort[p] = true 67 } 68 serverFirstByPort := map[int]bool{} 69 for _, p := range serverFirstPorts { 70 serverFirstByPort[p] = true 71 } 72 xdsGRPCByPort := map[int]bool{} 73 for _, p := range xdsGRPCServers { 74 xdsGRPCByPort[p] = true 75 } 76 portIndex := 0 77 for i, p := range httpPorts { 78 ports[portIndex] = &common.Port{ 79 Name: "http-" + strconv.Itoa(i), 80 Protocol: protocol.HTTP, 81 Port: p, 82 TLS: tlsByPort[p], 83 ServerFirst: serverFirstByPort[p], 84 } 85 portIndex++ 86 } 87 for i, p := range grpcPorts { 88 ports[portIndex] = &common.Port{ 89 Name: "grpc-" + strconv.Itoa(i), 90 Protocol: protocol.GRPC, 91 Port: p, 92 TLS: tlsByPort[p], 93 ServerFirst: serverFirstByPort[p], 94 XDSServer: xdsGRPCByPort[p], 95 } 96 portIndex++ 97 } 98 for i, p := range tcpPorts { 99 ports[portIndex] = &common.Port{ 100 Name: "tcp-" + strconv.Itoa(i), 101 Protocol: protocol.TCP, 102 Port: p, 103 TLS: tlsByPort[p], 104 ServerFirst: serverFirstByPort[p], 105 } 106 portIndex++ 107 } 108 for i, p := range udpPorts { 109 ports[portIndex] = &common.Port{ 110 Name: "udp-" + strconv.Itoa(i), 111 Protocol: protocol.UDP, 112 Port: p, 113 } 114 portIndex++ 115 } 116 for i, p := range hbonePorts { 117 ports[portIndex] = &common.Port{ 118 Name: "hbone-" + strconv.Itoa(i), 119 Protocol: protocol.HBONE, 120 Port: p, 121 TLS: tlsByPort[p], 122 } 123 portIndex++ 124 } 125 instanceIPByPort := map[int]struct{}{} 126 for _, p := range instanceIPPorts { 127 instanceIPByPort[p] = struct{}{} 128 } 129 localhostIPByPort := map[int]struct{}{} 130 for _, p := range localhostIPPorts { 131 localhostIPByPort[p] = struct{}{} 132 } 133 134 s := server.New(server.Config{ 135 Ports: ports, 136 Metrics: metricsPort, 137 BindIPPortsMap: instanceIPByPort, 138 BindLocalhostPortsMap: localhostIPByPort, 139 TLSCert: crt, 140 TLSKey: key, 141 Version: version, 142 Cluster: cluster, 143 IstioVersion: istioVersion, 144 Namespace: os.Getenv("NAMESPACE"), 145 UDSServer: uds, 146 DisableALPN: disableALPN, 147 }) 148 149 if err := s.Start(); err != nil { 150 log.Error(err) 151 os.Exit(-1) 152 } 153 defer func() { 154 _ = s.Close() 155 }() 156 157 // Wait for the process to be shutdown. 158 sigs := make(chan os.Signal, 1) 159 signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) 160 <-sigs 161 }, 162 } 163 ) 164 165 func configureLogging(_ *cobra.Command, _ []string) error { 166 if err := log.Configure(loggingOptions); err != nil { 167 return err 168 } 169 return nil 170 } 171 172 func init() { 173 rootCmd.PersistentFlags().IntSliceVar(&httpPorts, "port", []int{8080}, "HTTP/1.1 ports") 174 rootCmd.PersistentFlags().IntSliceVar(&grpcPorts, "grpc", []int{7070}, "GRPC ports") 175 rootCmd.PersistentFlags().IntSliceVar(&tcpPorts, "tcp", []int{9090}, "TCP ports") 176 rootCmd.PersistentFlags().IntSliceVar(&udpPorts, "udp", []int{}, "UDP ports") 177 rootCmd.PersistentFlags().IntSliceVar(&hbonePorts, "hbone", []int{}, "HBONE ports") 178 rootCmd.PersistentFlags().IntSliceVar(&tlsPorts, "tls", []int{}, "Ports that are using TLS. These must be defined as http/grpc/tcp.") 179 rootCmd.PersistentFlags().IntSliceVar(&instanceIPPorts, "bind-ip", []int{}, "Ports that are bound to INSTANCE_IP rather than wildcard IP.") 180 rootCmd.PersistentFlags().IntSliceVar(&localhostIPPorts, "bind-localhost", []int{}, "Ports that are bound to localhost rather than wildcard IP.") 181 rootCmd.PersistentFlags().IntSliceVar(&serverFirstPorts, "server-first", []int{}, "Ports that are server first. These must be defined as tcp.") 182 rootCmd.PersistentFlags().IntSliceVar(&xdsGRPCServers, "xds-grpc-server", []int{}, "Ports that should rely on XDS configuration to serve.") 183 rootCmd.PersistentFlags().IntVar(&metricsPort, "metrics", 0, "Metrics port") 184 rootCmd.PersistentFlags().StringVar(&uds, "uds", "", "HTTP server on unix domain socket") 185 rootCmd.PersistentFlags().StringVar(&version, "version", "", "Version string") 186 rootCmd.PersistentFlags().StringVar(&cluster, "cluster", "", "Cluster where this server is deployed") 187 rootCmd.PersistentFlags().StringVar(&crt, "crt", "", "gRPC TLS server-side certificate") 188 rootCmd.PersistentFlags().StringVar(&key, "key", "", "gRPC TLS server-side key") 189 rootCmd.PersistentFlags().StringVar(&istioVersion, "istio-version", "", "Istio sidecar version") 190 rootCmd.PersistentFlags().BoolVar(&disableALPN, "disable-alpn", disableALPN, "disable ALPN negotiation") 191 192 loggingOptions.AttachCobraFlags(rootCmd) 193 194 cmd.AddFlags(rootCmd) 195 } 196 197 func main() { 198 if err := rootCmd.Execute(); err != nil { 199 os.Exit(-1) 200 } 201 }