agones.dev/agones@v1.53.0/examples/allocator-client/main.go (about) 1 // Copyright 2020 Google LLC All Rights Reserved. 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 "context" 19 "crypto/tls" 20 "crypto/x509" 21 "flag" 22 "fmt" 23 "os" 24 25 pb "agones.dev/agones/pkg/allocation/go" 26 "github.com/pkg/errors" 27 "google.golang.org/grpc" 28 "google.golang.org/grpc/credentials" 29 ) 30 31 func main() { 32 keyFile := flag.String("key", "missing key", "the private key file for the client certificate in PEM format") 33 certFile := flag.String("cert", "missing cert", "the public key file for the client certificate in PEM format") 34 cacertFile := flag.String("cacert", "missing cacert", "the CA cert file for server signing certificate in PEM format") 35 externalIP := flag.String("ip", "missing external IP", "the external IP for allocator server") 36 port := flag.String("port", "443", "the port for allocator server") 37 namespace := flag.String("namespace", "default", "the game server kubernetes namespace") 38 multicluster := flag.Bool("multicluster", false, "set to true to enable the multi-cluster allocation") 39 40 flag.Parse() 41 42 endpoint := *externalIP + ":" + *port 43 cert, err := os.ReadFile(*certFile) 44 if err != nil { 45 panic(err) 46 } 47 key, err := os.ReadFile(*keyFile) 48 if err != nil { 49 panic(err) 50 } 51 cacert, err := os.ReadFile(*cacertFile) 52 if err != nil { 53 panic(err) 54 } 55 56 request := &pb.AllocationRequest{ 57 Namespace: *namespace, 58 MultiClusterSetting: &pb.MultiClusterSetting{ 59 Enabled: *multicluster, 60 }, 61 } 62 63 dialOpts, err := createRemoteClusterDialOption(cert, key, cacert) 64 if err != nil { 65 panic(err) 66 } 67 conn, err := grpc.NewClient(endpoint, dialOpts) 68 if err != nil { 69 panic(err) 70 } 71 defer conn.Close() 72 73 grpcClient := pb.NewAllocationServiceClient(conn) 74 response, err := grpcClient.Allocate(context.Background(), request) 75 if err != nil { 76 panic(err) 77 } 78 fmt.Printf("response: %s\n", response.String()) 79 } 80 81 // createRemoteClusterDialOption creates a grpc client dial option with TLS configuration. 82 func createRemoteClusterDialOption(clientCert, clientKey, caCert []byte) (grpc.DialOption, error) { 83 // Load client cert 84 cert, err := tls.X509KeyPair(clientCert, clientKey) 85 if err != nil { 86 return nil, err 87 } 88 89 tlsConfig := &tls.Config{Certificates: []tls.Certificate{cert}} 90 if len(caCert) != 0 { 91 // Load CA cert, if provided and trust the server certificate. 92 // This is required for self-signed certs. 93 tlsConfig.RootCAs = x509.NewCertPool() 94 if !tlsConfig.RootCAs.AppendCertsFromPEM(caCert) { 95 return nil, errors.New("only PEM format is accepted for server CA") 96 } 97 } 98 99 return grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)), nil 100 }