github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/interop/xds/server/server.go (about) 1 /* 2 * 3 * Copyright 2021 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 // Binary server is the server used for xDS interop tests. 20 package main 21 22 import ( 23 "context" 24 "flag" 25 "fmt" 26 "log" 27 "net" 28 "os" 29 30 grpc "github.com/hxx258456/ccgo/grpc" 31 "github.com/hxx258456/ccgo/grpc/admin" 32 "github.com/hxx258456/ccgo/grpc/credentials/insecure" 33 "github.com/hxx258456/ccgo/grpc/grpclog" 34 "github.com/hxx258456/ccgo/grpc/health" 35 "github.com/hxx258456/ccgo/grpc/metadata" 36 "github.com/hxx258456/ccgo/grpc/reflection" 37 "github.com/hxx258456/ccgo/grpc/xds" 38 39 xdscreds "github.com/hxx258456/ccgo/grpc/credentials/xds" 40 healthpb "github.com/hxx258456/ccgo/grpc/health/grpc_health_v1" 41 testgrpc "github.com/hxx258456/ccgo/grpc/interop/grpc_testing" 42 testpb "github.com/hxx258456/ccgo/grpc/interop/grpc_testing" 43 ) 44 45 var ( 46 port = flag.Int("port", 8080, "Listening port for test service") 47 maintenancePort = flag.Int("maintenance_port", 8081, "Listening port for maintenance services like health, reflection, channelz etc when -secure_mode is true. When -secure_mode is false, all these services will be registered on -port") 48 serverID = flag.String("server_id", "go_server", "Server ID included in response") 49 secureMode = flag.Bool("secure_mode", false, "If true, retrieve security configuration from the management server. Else, use insecure credentials.") 50 hostNameOverride = flag.String("host_name_override", "", "If set, use this as the hostname instead of the real hostname") 51 52 logger = grpclog.Component("interop") 53 ) 54 55 func getHostname() string { 56 if *hostNameOverride != "" { 57 return *hostNameOverride 58 } 59 hostname, err := os.Hostname() 60 if err != nil { 61 log.Fatalf("failed to get hostname: %v", err) 62 } 63 return hostname 64 } 65 66 // testServiceImpl provides an implementation of the TestService defined in 67 // grpc.testing package. 68 type testServiceImpl struct { 69 testgrpc.UnimplementedTestServiceServer 70 hostname string 71 serverID string 72 } 73 74 func (s *testServiceImpl) EmptyCall(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) { 75 grpc.SetHeader(ctx, metadata.Pairs("hostname", s.hostname)) 76 return &testpb.Empty{}, nil 77 } 78 79 func (s *testServiceImpl) UnaryCall(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) { 80 grpc.SetHeader(ctx, metadata.Pairs("hostname", s.hostname)) 81 return &testpb.SimpleResponse{ServerId: s.serverID, Hostname: s.hostname}, nil 82 } 83 84 // xdsUpdateHealthServiceImpl provides an implementation of the 85 // XdsUpdateHealthService defined in grpc.testing package. 86 type xdsUpdateHealthServiceImpl struct { 87 testgrpc.UnimplementedXdsUpdateHealthServiceServer 88 healthServer *health.Server 89 } 90 91 func (x *xdsUpdateHealthServiceImpl) SetServing(_ context.Context, _ *testpb.Empty) (*testpb.Empty, error) { 92 x.healthServer.SetServingStatus("", healthpb.HealthCheckResponse_SERVING) 93 return &testpb.Empty{}, nil 94 95 } 96 97 func (x *xdsUpdateHealthServiceImpl) SetNotServing(_ context.Context, _ *testpb.Empty) (*testpb.Empty, error) { 98 x.healthServer.SetServingStatus("", healthpb.HealthCheckResponse_NOT_SERVING) 99 return &testpb.Empty{}, nil 100 } 101 102 func xdsServingModeCallback(addr net.Addr, args xds.ServingModeChangeArgs) { 103 logger.Infof("Serving mode for xDS server at %s changed to %s", addr.String(), args.Mode) 104 if args.Err != nil { 105 logger.Infof("ServingModeCallback returned error: %v", args.Err) 106 } 107 } 108 109 func main() { 110 flag.Parse() 111 112 if *secureMode && *port == *maintenancePort { 113 logger.Fatal("-port and -maintenance_port must be different when -secure_mode is set") 114 } 115 116 testService := &testServiceImpl{hostname: getHostname(), serverID: *serverID} 117 healthServer := health.NewServer() 118 updateHealthService := &xdsUpdateHealthServiceImpl{healthServer: healthServer} 119 120 // If -secure_mode is not set, expose all services on -port with a regular 121 // gRPC server. 122 if !*secureMode { 123 lis, err := net.Listen("tcp4", fmt.Sprintf(":%d", *port)) 124 if err != nil { 125 logger.Fatalf("net.Listen(%s) failed: %v", fmt.Sprintf(":%d", *port), err) 126 } 127 128 server := grpc.NewServer() 129 testgrpc.RegisterTestServiceServer(server, testService) 130 healthServer.SetServingStatus("", healthpb.HealthCheckResponse_SERVING) 131 healthpb.RegisterHealthServer(server, healthServer) 132 testgrpc.RegisterXdsUpdateHealthServiceServer(server, updateHealthService) 133 reflection.Register(server) 134 cleanup, err := admin.Register(server) 135 if err != nil { 136 logger.Fatalf("Failed to register admin services: %v", err) 137 } 138 defer cleanup() 139 if err := server.Serve(lis); err != nil { 140 logger.Errorf("Serve() failed: %v", err) 141 } 142 return 143 } 144 145 // Create a listener on -port to expose the test service. 146 testLis, err := net.Listen("tcp4", fmt.Sprintf(":%d", *port)) 147 if err != nil { 148 logger.Fatalf("net.Listen(%s) failed: %v", fmt.Sprintf(":%d", *port), err) 149 } 150 151 // Create server-side xDS credentials with a plaintext fallback. 152 creds, err := xdscreds.NewServerCredentials(xdscreds.ServerOptions{FallbackCreds: insecure.NewCredentials()}) 153 if err != nil { 154 logger.Fatalf("Failed to create xDS credentials: %v", err) 155 } 156 157 // Create an xDS enabled gRPC server, register the test service 158 // implementation and start serving. 159 testServer := xds.NewGRPCServer(grpc.Creds(creds), xds.ServingModeCallback(xdsServingModeCallback)) 160 testgrpc.RegisterTestServiceServer(testServer, testService) 161 go func() { 162 if err := testServer.Serve(testLis); err != nil { 163 logger.Errorf("test server Serve() failed: %v", err) 164 } 165 }() 166 defer testServer.Stop() 167 168 // Create a listener on -maintenance_port to expose other services. 169 maintenanceLis, err := net.Listen("tcp4", fmt.Sprintf(":%d", *maintenancePort)) 170 if err != nil { 171 logger.Fatalf("net.Listen(%s) failed: %v", fmt.Sprintf(":%d", *maintenancePort), err) 172 } 173 174 // Create a regular gRPC server and register the maintenance services on 175 // it and start serving. 176 maintenanceServer := grpc.NewServer() 177 healthServer.SetServingStatus("", healthpb.HealthCheckResponse_SERVING) 178 healthpb.RegisterHealthServer(maintenanceServer, healthServer) 179 testgrpc.RegisterXdsUpdateHealthServiceServer(maintenanceServer, updateHealthService) 180 reflection.Register(maintenanceServer) 181 cleanup, err := admin.Register(maintenanceServer) 182 if err != nil { 183 logger.Fatalf("Failed to register admin services: %v", err) 184 } 185 defer cleanup() 186 if err := maintenanceServer.Serve(maintenanceLis); err != nil { 187 logger.Errorf("maintenance server Serve() failed: %v", err) 188 } 189 }