-
Notifications
You must be signed in to change notification settings - Fork 777
/
main.go
100 lines (88 loc) · 3.06 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2020 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"crypto/tls"
"crypto/x509"
"flag"
"fmt"
"io/ioutil"
pb "agones.dev/agones/pkg/allocation/go"
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
func main() {
keyFile := flag.String("key", "missing key", "the private key file for the client certificate in PEM format")
certFile := flag.String("cert", "missing cert", "the public key file for the client certificate in PEM format")
cacertFile := flag.String("cacert", "missing cacert", "the CA cert file for server signing certificate in PEM format")
externalIP := flag.String("ip", "missing external IP", "the external IP for allocator server")
port := flag.String("port", "443", "the port for allocator server")
namespace := flag.String("namespace", "default", "the game server kubernetes namespace")
multicluster := flag.Bool("multicluster", false, "set to true to enable the multi-cluster allocation")
flag.Parse()
endpoint := *externalIP + ":" + *port
cert, err := ioutil.ReadFile(*certFile)
if err != nil {
panic(err)
}
key, err := ioutil.ReadFile(*keyFile)
if err != nil {
panic(err)
}
cacert, err := ioutil.ReadFile(*cacertFile)
if err != nil {
panic(err)
}
request := &pb.AllocationRequest{
Namespace: *namespace,
MultiClusterSetting: &pb.MultiClusterSetting{
Enabled: *multicluster,
},
}
dialOpts, err := createRemoteClusterDialOption(cert, key, cacert)
if err != nil {
panic(err)
}
conn, err := grpc.Dial(endpoint, dialOpts)
if err != nil {
panic(err)
}
defer conn.Close()
grpcClient := pb.NewAllocationServiceClient(conn)
response, err := grpcClient.Allocate(context.Background(), request)
if err != nil {
panic(err)
}
fmt.Printf("response: %s\n", response.String())
}
// createRemoteClusterDialOption creates a grpc client dial option with TLS configuration.
func createRemoteClusterDialOption(clientCert, clientKey, caCert []byte) (grpc.DialOption, error) {
// Load client cert
cert, err := tls.X509KeyPair(clientCert, clientKey)
if err != nil {
return nil, err
}
tlsConfig := &tls.Config{Certificates: []tls.Certificate{cert}}
if len(caCert) != 0 {
// Load CA cert, if provided and trust the server certificate.
// This is required for self-signed certs.
tlsConfig.RootCAs = x509.NewCertPool()
if !tlsConfig.RootCAs.AppendCertsFromPEM(caCert) {
return nil, errors.New("only PEM format is accepted for server CA")
}
}
return grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)), nil
}