/
common.go
120 lines (107 loc) · 2.98 KB
/
common.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
package manifests
import (
"fmt"
"github.com/Azure/aks-app-routing-operator/api/v1alpha1"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
policyv1 "k8s.io/api/policy/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
ManagedByKey = "app.kubernetes.io/managed-by"
// ManagedByVal is the value for the ManagedByKey label on all resources directly managed by our e2e tester
ManagedByVal = "app-routing-operator-e2e"
)
var (
scheme = runtime.NewScheme()
)
func init() {
// add any types used in this package
batchv1.AddToScheme(scheme)
corev1.AddToScheme(scheme)
metav1.AddMetaToScheme(scheme)
appsv1.AddToScheme(scheme)
policyv1.AddToScheme(scheme)
rbacv1.AddToScheme(scheme)
v1alpha1.AddToScheme(scheme)
}
// MarshalJson converts an object to json
func MarshalJson(obj client.Object) ([]byte, error) {
codec := serializer.NewCodecFactory(scheme).LegacyCodec(scheme.PreferredVersionAllGroups()...)
yml, err := runtime.Encode(codec, obj)
if err != nil {
return nil, fmt.Errorf("encoding object: %w", err)
}
return yml, nil
}
func setGroupKindVersion(obj client.Object) {
gvks, _, err := scheme.ObjectKinds(obj)
if err != nil {
return
}
if len(gvks) == 0 {
return
}
obj.GetObjectKind().SetGroupVersionKind(gvks[0])
}
// newGoDeployment creates a new basic Go deployment with a single main.go file from contents
func newGoDeployment(contents, namespace, name string) *appsv1.Deployment {
command := []string{
"/bin/sh",
"-c",
"mkdir source && cd source && go mod init source && echo '" + contents + "' > main.go && go mod tidy && go run main.go",
}
return &appsv1.Deployment{
TypeMeta: metav1.TypeMeta{
Kind: "Deployment",
APIVersion: "apps/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{
ManagedByKey: ManagedByVal,
},
},
Spec: appsv1.DeploymentSpec{
Replicas: to.Ptr(int32(1)),
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"app": name},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"app": name},
Annotations: map[string]string{},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{{
Name: "container",
Image: "mcr.microsoft.com/oss/go/microsoft/golang:1.20",
Command: command,
}},
},
},
},
}
}
// UncollisionedNs returns a namespace with a guaranteed unique name after creating the namespace
func UncollisionedNs() *corev1.Namespace {
return &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
Kind: "Namespace",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
GenerateName: "app-routing-e2e-",
Labels: map[string]string{
ManagedByKey: ManagedByVal,
},
},
}
}