/
helper_func.go
376 lines (327 loc) · 13.3 KB
/
helper_func.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright 2023 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 allochelper is a package for helper function that is used by e2e tests
package allochelper
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"net"
"testing"
"time"
pb "agones.dev/agones/pkg/allocation/go"
agonesv1 "agones.dev/agones/pkg/apis/agones/v1"
multiclusterv1 "agones.dev/agones/pkg/apis/multicluster/v1"
e2e "agones.dev/agones/test/e2e/framework"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/wait"
)
const (
agonesSystemNamespace = "agones-system"
allocatorServiceName = "agones-allocator"
allocatorTLSName = "allocator-tls"
tlsCrtTag = "tls.crt"
tlsKeyTag = "tls.key"
allocatorReqURLFmt = "%s:%d"
allocatorClientSecretName = "allocator-client.default"
allocatorClientSecretNamespace = "default"
replicasCount = 5
)
// CopyDefaultAllocatorClientSecret copys the allocator client secret
func CopyDefaultAllocatorClientSecret(ctx context.Context, t *testing.T, toNamespace string, framework *e2e.Framework) {
kubeCore := framework.KubeClient.CoreV1()
clientSecret, err := kubeCore.Secrets(allocatorClientSecretNamespace).Get(ctx, allocatorClientSecretName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Could not retrieve default allocator client secret %s/%s: %v", allocatorClientSecretNamespace, allocatorClientSecretName, err)
}
clientSecret.ObjectMeta.Namespace = toNamespace
clientSecret.ResourceVersion = ""
_, err = kubeCore.Secrets(toNamespace).Create(ctx, clientSecret, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Could not copy default allocator client %s/%s secret to namespace %s: %v", allocatorClientSecretNamespace, allocatorClientSecretName, toNamespace, err)
}
}
// CreateAllocationPolicy create a allocation policy
func CreateAllocationPolicy(ctx context.Context, t *testing.T, framework *e2e.Framework, p *multiclusterv1.GameServerAllocationPolicy) {
t.Helper()
mc := framework.AgonesClient.MulticlusterV1()
policy, err := mc.GameServerAllocationPolicies(p.Namespace).Create(ctx, p, metav1.CreateOptions{})
if err != nil {
t.Fatalf("creating allocation policy failed: %s", err)
}
t.Logf("created allocation policy %v", policy)
}
// GetAllocatorEndpoint gets the allocator LB endpoint
func GetAllocatorEndpoint(ctx context.Context, t *testing.T, framework *e2e.Framework) (string, int32) {
kubeCore := framework.KubeClient.CoreV1()
svc, err := kubeCore.Services(agonesSystemNamespace).Get(ctx, allocatorServiceName, metav1.GetOptions{})
if !assert.Nil(t, err) {
t.FailNow()
}
if !assert.NotNil(t, svc.Status.LoadBalancer) {
t.FailNow()
}
if !assert.Equal(t, 1, len(svc.Status.LoadBalancer.Ingress)) {
t.FailNow()
}
if !assert.NotNil(t, 0, svc.Status.LoadBalancer.Ingress[0].IP) {
t.FailNow()
}
port := svc.Spec.Ports[0]
return svc.Status.LoadBalancer.Ingress[0].IP, port.Port
}
// CreateRemoteClusterDialOption creates a grpc client dial option with proper certs to make a remote call.
//
//nolint:unparam
func CreateRemoteClusterDialOption(ctx context.Context, namespace, clientSecretName string, tlsCA []byte, framework *e2e.Framework) (grpc.DialOption, error) {
tlsConfig, err := GetTLSConfig(ctx, namespace, clientSecretName, tlsCA, framework)
if err != nil {
return nil, err
}
return grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)), nil
}
// GetTLSConfig gets the namesapce client secret
func GetTLSConfig(ctx context.Context, namespace, clientSecretName string, tlsCA []byte, framework *e2e.Framework) (*tls.Config, error) {
kubeCore := framework.KubeClient.CoreV1()
clientSecret, err := kubeCore.Secrets(namespace).Get(ctx, clientSecretName, metav1.GetOptions{})
if err != nil {
return nil, errors.Errorf("getting client secret %s/%s failed: %s", namespace, clientSecretName, err)
}
// Create http client using cert
clientCert := clientSecret.Data[tlsCrtTag]
clientKey := clientSecret.Data[tlsKeyTag]
if clientCert == nil || clientKey == nil {
return nil, errors.New("missing certificate")
}
// Load client cert
cert, err := tls.X509KeyPair(clientCert, clientKey)
if err != nil {
return nil, err
}
rootCA := x509.NewCertPool()
if !rootCA.AppendCertsFromPEM(tlsCA) {
return nil, errors.New("could not append PEM format CA cert")
}
return &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: rootCA,
}, nil
}
// CreateFleet creates a game server fleet
func CreateFleet(ctx context.Context, namespace string, framework *e2e.Framework) (*agonesv1.Fleet, error) {
return CreateFleetWithOpts(ctx, namespace, framework, func(*agonesv1.Fleet) {})
}
// CreateFleetWithOpts creates a game server fleet with the designated options
func CreateFleetWithOpts(ctx context.Context, namespace string, framework *e2e.Framework, opts func(fleet *agonesv1.Fleet)) (*agonesv1.Fleet, error) {
fleets := framework.AgonesClient.AgonesV1().Fleets(namespace)
fleet := defaultFleet(namespace, framework)
opts(fleet)
return fleets.Create(ctx, fleet, metav1.CreateOptions{})
}
// RefreshAllocatorTLSCerts refreshes the allocator TLS cert with a newly generated cert
func RefreshAllocatorTLSCerts(ctx context.Context, t *testing.T, host string, framework *e2e.Framework) []byte {
t.Helper()
pub, priv := generateTLSCertPair(t, host)
// verify key pair
if _, err := tls.X509KeyPair(pub, priv); err != nil {
t.Fatalf("generated key pair failed create cert: %s", err)
}
kubeCore := framework.KubeClient.CoreV1()
require.Eventually(t, func() bool {
s, err := kubeCore.Secrets(agonesSystemNamespace).Get(ctx, allocatorTLSName, metav1.GetOptions{})
if err != nil {
t.Logf("failed getting secret %s/%s failed: %s", agonesSystemNamespace, allocatorTLSName, err)
return false
}
s.Data[tlsCrtTag] = pub
s.Data[tlsKeyTag] = priv
if _, err := kubeCore.Secrets(agonesSystemNamespace).Update(ctx, s, metav1.UpdateOptions{}); err != nil {
t.Logf("failed updating secrets failed: %s", err)
return false
}
return true
}, time.Minute, time.Second, "Could not update Secret")
t.Logf("Allocator TLS is refreshed with public CA: %s for endpoint %s", string(pub), host)
return pub
}
func generateTLSCertPair(t *testing.T, host string) ([]byte, []byte) {
t.Helper()
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("generating RSA key failed: %s", err)
}
notBefore := time.Now()
notAfter := notBefore.Add(time.Hour)
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
t.Fatalf("generating serial number failed: %s", err)
}
template := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
CommonName: host,
Organization: []string{"testing"},
},
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
SignatureAlgorithm: x509.SHA1WithRSA,
BasicConstraintsValid: true,
IsCA: true,
}
if host != "" {
template.IPAddresses = []net.IP{net.ParseIP(host)}
}
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
if err != nil {
t.Fatalf("creating certificate failed: %s", err)
}
pemPubBytes := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
privBytes, err := x509.MarshalPKCS8PrivateKey(priv)
if err != nil {
t.Fatalf("marshalling private key failed: %v", err)
}
pemPrivBytes := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: privBytes})
return pemPubBytes, pemPrivBytes
}
// ValidateAllocatorResponse validates the response returned by the allcoator
func ValidateAllocatorResponse(t *testing.T, resp *pb.AllocationResponse) {
t.Helper()
if !assert.NotNil(t, resp) {
return
}
assert.Greater(t, len(resp.Ports), 0)
assert.NotEmpty(t, resp.GameServerName)
assert.NotEmpty(t, resp.Address)
assert.NotEmpty(t, resp.Addresses)
assert.NotEmpty(t, resp.NodeName)
assert.NotEmpty(t, resp.Metadata.Labels)
assert.NotEmpty(t, resp.Metadata.Annotations)
}
// DeleteAgonesAllocatorPod deletes a Agones allocator pod
func DeleteAgonesAllocatorPod(ctx context.Context, podName string, framework *e2e.Framework) error {
return DeleteAgonesPod(ctx, podName, "agones-system", framework)
}
// DeleteAgonesPod deletes an Agones pod with the specified namespace and podname
func DeleteAgonesPod(ctx context.Context, podName string, namespace string, framework *e2e.Framework) error {
policy := metav1.DeletePropagationBackground
err := framework.KubeClient.CoreV1().Pods(namespace).Delete(ctx, podName,
metav1.DeleteOptions{PropagationPolicy: &policy})
return err
}
// GetAgonesAllocatorPods returns all the Agones allocator pods
func GetAgonesAllocatorPods(ctx context.Context, framework *e2e.Framework) (*corev1.PodList, error) {
opts := metav1.ListOptions{LabelSelector: labels.Set{"multicluster.agones.dev/role": "allocator"}.String()}
return framework.KubeClient.CoreV1().Pods("agones-system").List(ctx, opts)
}
// GetAllocatorClient creates a client and ensure that it can be connected to
func GetAllocatorClient(ctx context.Context, t *testing.T, framework *e2e.Framework) (pb.AllocationServiceClient, error) {
ip, port := GetAllocatorEndpoint(ctx, t, framework)
requestURL := fmt.Sprintf(allocatorReqURLFmt, ip, port)
tlsCA := RefreshAllocatorTLSCerts(ctx, t, ip, framework)
flt, err := CreateFleet(ctx, framework.Namespace, framework)
if !assert.Nil(t, err) {
return nil, err
}
framework.AssertFleetCondition(t, flt, e2e.FleetReadyCount(flt.Spec.Replicas))
dialOpts, err := CreateRemoteClusterDialOption(ctx, allocatorClientSecretNamespace, allocatorClientSecretName, tlsCA, framework)
if err != nil {
return nil, err
}
conn, err := grpc.Dial(requestURL, dialOpts)
require.NoError(t, err, "Failed grpc.Dial")
go func() {
<-ctx.Done()
conn.Close() // nolint: errcheck
}()
grpcClient := pb.NewAllocationServiceClient(conn)
request := &pb.AllocationRequest{
Namespace: framework.Namespace,
PreferredGameServerSelectors: []*pb.GameServerSelector{{MatchLabels: map[string]string{agonesv1.FleetNameLabel: flt.ObjectMeta.Name}}},
Scheduling: pb.AllocationRequest_Packed,
Metadata: &pb.MetaPatch{Labels: map[string]string{"gslabel": "allocatedbytest"}},
}
var response *pb.AllocationResponse
err = wait.PollUntilContextTimeout(context.Background(), 2*time.Second, 5*time.Minute, true, func(ctx context.Context) (bool, error) {
response, err = grpcClient.Allocate(context.Background(), request)
if err != nil {
logrus.WithError(err).Info("failing Allocate request")
return false, nil
}
ValidateAllocatorResponse(t, response)
return true, nil
})
if err != nil {
return nil, err
}
return grpcClient, nil
}
// CleanupNamespaces cleans up the framework namespace
func CleanupNamespaces(ctx context.Context, framework *e2e.Framework) error {
// list all e2e namespaces
opts := metav1.ListOptions{LabelSelector: labels.Set(e2e.NamespaceLabel).String()}
list, err := framework.KubeClient.CoreV1().Namespaces().List(ctx, opts)
if err != nil {
return err
}
// loop through them, and delete them
for _, ns := range list.Items {
if err := framework.DeleteNamespace(ns.ObjectMeta.Name); err != nil {
cause := errors.Cause(err)
if k8serrors.IsConflict(cause) {
logrus.WithError(cause).Warn("namespace already being deleted")
continue
}
// here just in case we need to catch other errors
logrus.WithField("reason", k8serrors.ReasonForError(cause)).Info("cause for namespace deletion error")
return cause
}
}
return nil
}
// From fleet_test
// defaultFleet returns a default fleet configuration
func defaultFleet(namespace string, framework *e2e.Framework) *agonesv1.Fleet {
gs := framework.DefaultGameServer(namespace)
return fleetWithGameServerSpec(&gs.Spec, namespace)
}
// fleetWithGameServerSpec returns a fleet with specified gameserver spec
func fleetWithGameServerSpec(gsSpec *agonesv1.GameServerSpec, namespace string) *agonesv1.Fleet {
return &agonesv1.Fleet{
ObjectMeta: metav1.ObjectMeta{GenerateName: "simple-fleet-1.0", Namespace: namespace},
Spec: agonesv1.FleetSpec{
Replicas: replicasCount,
Template: agonesv1.GameServerTemplateSpec{
Spec: *gsSpec,
},
},
}
}