-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathcommon.go
More file actions
78 lines (66 loc) · 2.09 KB
/
Copy pathcommon.go
File metadata and controls
78 lines (66 loc) · 2.09 KB
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
package cel
import (
"crypto/rand"
"fmt"
"testing"
"k8s.io/apimachinery/pkg/runtime"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
ngfAPIv1alpha1 "github.com/nginx/nginx-gateway-fabric/v2/apis/v1alpha1"
ngfAPIv1alpha2 "github.com/nginx/nginx-gateway-fabric/v2/apis/v1alpha2"
)
const (
gatewayKind = "Gateway"
httpRouteKind = "HTTPRoute"
grpcRouteKind = "GRPCRoute"
tcpRouteKind = "TCPRoute"
invalidKind = "InvalidKind"
)
const (
gatewayGroup = "gateway.networking.k8s.io"
invalidGroup = "invalid.networking.k8s.io"
discoveryGroup = "discovery.k8s.io/v1"
)
const (
expectedTargetRefKindError = `TargetRef Kind must be one of: Gateway, HTTPRoute, or GRPCRoute`
expectedTargetRefGroupError = `TargetRef Group must be gateway.networking.k8s.io.`
)
const (
defaultNamespace = "default"
)
const (
testPolicyName = "test-policy"
testTargetRefName = "test-targetRef"
)
// getKubernetesClient returns a client connected to a real Kubernetes cluster.
func getKubernetesClient(t *testing.T) (k8sClient client.Client, err error) {
t.Helper()
// Use controller-runtime to get cluster connection
k8sConfig, err := controllerruntime.GetConfig()
if err != nil {
return nil, err
}
// Set up scheme with NGF types
scheme := runtime.NewScheme()
if err = ngfAPIv1alpha1.AddToScheme(scheme); err != nil {
return nil, err
}
if err = ngfAPIv1alpha2.AddToScheme(scheme); err != nil {
return nil, err
}
// Create a new client with the scheme and return it
return client.New(k8sConfig, client.Options{Scheme: scheme})
}
// randomPrimeNumber generates a random prime number of 64 bits.
// It panics if it fails to generate a random prime number.
func randomPrimeNumber() int64 {
primeNum, err := rand.Prime(rand.Reader, 64)
if err != nil {
panic(fmt.Errorf("failed to generate random prime number: %w", err))
}
return primeNum.Int64()
}
// uniqueResourceName generates a unique resource name by appending a random prime number to the given name.
func uniqueResourceName(name string) string {
return fmt.Sprintf("%s-%d", name, randomPrimeNumber())
}