-
Notifications
You must be signed in to change notification settings - Fork 18
/
util.go
225 lines (176 loc) · 6.28 KB
/
util.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
/*
SPDX-License-Identifier: Apache-2.0
Copyright Contributors to the Submariner project.
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 test provides test utilities for the Syncer.
package test
import (
"context"
. "github.com/onsi/gomega"
"github.com/submariner-io/admiral/pkg/federate"
"github.com/submariner-io/admiral/pkg/resource"
"github.com/submariner-io/admiral/pkg/test"
"github.com/submariner-io/admiral/pkg/util"
corev1 "k8s.io/api/core/v1"
metaapi "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes/scheme"
)
const (
RemoteNamespace = "remote-ns"
LocalNamespace = "local-ns"
)
func GetResourceAndError[T runtime.Object](resourceInterface dynamic.ResourceInterface, obj T) (T, error) {
u, err := resourceInterface.Get(context.TODO(), resource.MustToMeta(obj).GetName(), metav1.GetOptions{})
if err != nil {
return *new(T), err
}
return resource.MustFromUnstructured(u, obj), nil
}
func GetResource[T runtime.Object](resourceInterface dynamic.ResourceInterface, obj T) T {
ret, err := GetResourceAndError(resourceInterface, obj)
Expect(err).To(Succeed())
return ret
}
func CreateResource[T runtime.Object](resourceInterface dynamic.ResourceInterface, obj T) T {
u := resource.MustToUnstructured(obj)
u.SetResourceVersion("")
created, err := resourceInterface.Create(context.TODO(), u, metav1.CreateOptions{})
Expect(err).To(Succeed())
return resource.MustFromUnstructured(created, obj)
}
func UpdateResource[T runtime.Object](resourceInterface dynamic.ResourceInterface, obj T) T {
u := resource.MustToUnstructured(obj)
err := util.Update[*unstructured.Unstructured](context.Background(), resource.ForDynamic(resourceInterface), u,
util.Replace(u))
Expect(err).To(Succeed())
return GetResource(resourceInterface, obj)
}
func VerifyResource(resourceInterface dynamic.ResourceInterface, expected *corev1.Pod, expNamespace, clusterID string) {
actual := GetResource(resourceInterface, expected)
Expect(actual.GetName()).To(Equal(expected.GetName()))
Expect(actual.GetNamespace()).To(Equal(expNamespace))
Expect(actual.GetAnnotations()).To(Equal(expected.GetAnnotations()))
Expect(actual.GetOwnerReferences()).To(Equal(expected.GetOwnerReferences()))
Expect(actual.Spec).To(Equal(expected.Spec))
Expect(actual.Status).To(Equal(expected.Status))
duplicate := make(map[string]string)
for k, v := range expected.GetLabels() {
duplicate[k] = v
}
if clusterID != "" {
duplicate[federate.ClusterIDLabelKey] = clusterID
}
Expect(actual.GetLabels()).To(Equal(duplicate))
}
func NewPod(namespace string) *corev1.Pod {
return NewPodWithImage(namespace, "nginx")
}
func NewPodWithImage(namespace, imageName string) *corev1.Pod {
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
Namespace: namespace,
Labels: map[string]string{"app": "test"},
Annotations: map[string]string{"foo": "bar"},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Image: imageName,
Name: "httpd",
},
},
},
}
}
func GetRESTMapperAndGroupVersionResourceFor(obj runtime.Object) (metaapi.RESTMapper, *schema.GroupVersionResource) {
restMapper := GetRESTMapperFor(obj)
return restMapper, GetGroupVersionResourceFor(restMapper, obj)
}
func GetRESTMapperFor(objs ...runtime.Object) metaapi.RESTMapper {
gvs := make([]schema.GroupVersion, 0, len(objs))
gvks := make([]schema.GroupVersionKind, 0, len(objs))
for _, obj := range objs {
gvk := GetGroupVersionKindFor(obj)
gvks = append(gvks, gvk)
gvs = append(gvs, gvk.GroupVersion())
}
restMapper := metaapi.NewDefaultRESTMapper(gvs)
for _, gvk := range gvks {
restMapper.Add(gvk, metaapi.RESTScopeNamespace)
}
return restMapper
}
func GetGroupVersionKindFor(obj runtime.Object) schema.GroupVersionKind {
gvks, _, err := scheme.Scheme.ObjectKinds(obj)
Expect(err).To(Succeed())
Expect(gvks).ToNot(BeEmpty())
return gvks[0]
}
func GetGroupVersionResourceFor(restMapper metaapi.RESTMapper, obj runtime.Object) *schema.GroupVersionResource {
gvk := GetGroupVersionKindFor(obj)
mapping, err := restMapper.RESTMapping(gvk.GroupKind(), gvk.Version)
Expect(err).To(Succeed())
return &mapping.Resource
}
func PrepInitialClientObjs(namespace, clusterID string, initObjs ...runtime.Object) []runtime.Object {
newObjs := make([]runtime.Object, 0, len(initObjs))
for _, obj := range initObjs {
raw := resource.MustToUnstructured(obj)
raw.SetUID(uuid.NewUUID())
raw.SetResourceVersion("1")
if namespace != "" {
raw.SetNamespace(namespace)
}
if clusterID != "" {
labels := raw.GetLabels()
if labels == nil {
labels = map[string]string{}
}
labels[federate.ClusterIDLabelKey] = clusterID
raw.SetLabels(labels)
}
newObjs = append(newObjs, raw)
}
return newObjs
}
func SetClusterIDLabel[T runtime.Object](obj T, clusterID string) T {
meta := resource.MustToMeta(obj)
labels := meta.GetLabels()
if labels == nil {
labels = map[string]string{}
}
if clusterID == "" {
delete(labels, federate.ClusterIDLabelKey)
} else {
labels[federate.ClusterIDLabelKey] = clusterID
}
meta.SetLabels(labels)
return obj
}
func AwaitResource(client dynamic.ResourceInterface, name string) *unstructured.Unstructured {
return test.AwaitResource(resource.ForDynamic(client), name)
}
func AwaitAndVerifyResource(client dynamic.ResourceInterface, name string,
verify func(*unstructured.Unstructured) bool,
) *unstructured.Unstructured {
return test.AwaitAndVerifyResource(resource.ForDynamic(client), name, verify)
}
func AwaitNoResource(client dynamic.ResourceInterface, name string) {
test.AwaitNoResource(resource.ForDynamic(client), name)
}