-
Notifications
You must be signed in to change notification settings - Fork 115
/
crud.go
310 lines (276 loc) · 10 KB
/
crud.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
package helper
import (
"context"
"fmt"
"log"
"time"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/dynamic"
routev1 "github.com/openshift/api/route/v1"
userv1 "github.com/openshift/api/user/v1"
routemonitorv1alpha1 "github.com/openshift/route-monitor-operator/api/v1alpha1"
kv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func RouteMonitorResource(h *H) dynamic.NamespaceableResourceInterface {
return h.Dynamic().Resource(schema.GroupVersionResource{
Group: "monitoring.openshift.io", Version: "v1alpha1", Resource: "routemonitors",
})
}
func GetRouteMonitor(ctx context.Context, name string, ns string, h *H) (*routemonitorv1alpha1.RouteMonitor, error) {
ucObj, err := RouteMonitorResource(h).Namespace(ns).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("error retrieving RouteMonitor: %w", err)
}
var routeMonitor routemonitorv1alpha1.RouteMonitor
err = runtime.DefaultUnstructuredConverter.FromUnstructured(ucObj.UnstructuredContent(), &routeMonitor)
if err != nil {
// This, however, is probably error-worthy because it means our RouteMonitor
// has been messed with or something odd's occurred
return nil, fmt.Errorf("error parsing RouteMonitor into object: %w", err)
}
return &routeMonitor, nil
}
func UpdateRouteMonitor(ctx context.Context, routeMonitor *routemonitorv1alpha1.RouteMonitor, namespace string, h *H) error {
rawObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(routeMonitor.DeepCopy())
obj := &unstructured.Unstructured{rawObj} // warning about unjeyed fields here
if err != nil {
return fmt.Errorf("can't convert RouteMonitor to unstructured resource: %w", err)
}
_, err = RouteMonitorResource(h).Namespace(namespace).Update(ctx, obj, metav1.UpdateOptions{})
if err != nil {
log.Printf("Could not issue create command")
return err
}
return nil
}
func CreateRouteMonitor(ctx context.Context, routeMonitor *routemonitorv1alpha1.RouteMonitor, namespace string, h *H) error {
rawObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(routeMonitor.DeepCopy())
if err != nil {
return fmt.Errorf("can't convert UpgradeConfig to unstructured resource: %w", err)
}
obj := &unstructured.Unstructured{rawObj} // warning about unjeyed fields here
newObj, err := RouteMonitorResource(h).Namespace(namespace).Create(ctx, obj, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("can't create RouteMonitor, returned obj %v: %w", newObj, err)
}
// Wait for the pod to create.
err = wait.PollUntilContextTimeout(ctx, 5*time.Second, 1*time.Minute, false, func(ctx context.Context) (bool, error) {
if _, err := RouteMonitorResource(h).Namespace(namespace).Get(ctx, routeMonitor.Name, metav1.GetOptions{}); err != nil {
return false, nil
}
return true, nil
})
return err
}
func DeleteRouteMonitor(ctx context.Context, nsName types.NamespacedName, waitForDelete bool, h *H) error {
namespace, name := nsName.Namespace, nsName.Name
err := RouteMonitorResource(h).Namespace(namespace).Delete(ctx, name, metav1.DeleteOptions{})
if err != nil {
return fmt.Errorf("failed to delete namespace '%s': %w", namespace, err)
}
// Deleting a namespace can take a while. If desired, wait for the namespace to delete before returning.
if waitForDelete {
err = wait.PollUntilContextTimeout(ctx, 2*time.Second, 1*time.Minute, false, func(ctx context.Context) (bool, error) {
rmo, err := RouteMonitorResource(h).Namespace(namespace).Get(ctx, name, metav1.GetOptions{})
// not sure on that
if rmo != nil && err == nil {
return false, nil
}
return true, nil
})
}
return err
}
func SampleRouteMonitor(name string, ns string, h *H) *routemonitorv1alpha1.RouteMonitor {
routeMonitor := routemonitorv1alpha1.RouteMonitor{
// This is required for ToUnstructured to work correctly (test and see output)
TypeMeta: metav1.TypeMeta{
Kind: "RouteMonitor",
APIVersion: "monitoring.openshift.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: name,
},
Spec: routemonitorv1alpha1.RouteMonitorSpec{
Slo: routemonitorv1alpha1.SloSpec{
TargetAvailabilityPercent: "99.95",
},
Route: routemonitorv1alpha1.RouteMonitorRouteSpec{
Namespace: ns,
Name: name,
},
},
}
return &routeMonitor
}
func ClusterRouteMonitorResource(h *H) dynamic.NamespaceableResourceInterface {
return h.Dynamic().Resource(schema.GroupVersionResource{
Group: "monitoring.openshift.io", Version: "v1alpha1", Resource: "clusterurlmonitors",
})
}
func CreateRoute(ctx context.Context, route *routev1.Route, namespace string, h *H) error {
uwm, err := h.Route().RouteV1().Routes(namespace).Create(ctx, route, metav1.CreateOptions{})
if err != nil {
log.Printf("Could not issue create command")
return err
}
// Wait for the pod to create.
err = wait.PollUntilContextTimeout(ctx, 5*time.Second, 1*time.Minute, false, func(ctx context.Context) (bool, error) {
if _, err := h.Route().RouteV1().Routes(namespace).Get(ctx, uwm.Name, metav1.GetOptions{}); err != nil {
return false, nil
}
return true, nil
})
return err
}
func SampleRoute(name, ns string) *routev1.Route {
return &routev1.Route{
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: name,
},
Spec: routev1.RouteSpec{
To: routev1.RouteTargetReference{
Name: name,
},
TLS: &routev1.TLSConfig{Termination: "edge"},
},
Status: routev1.RouteStatus{},
}
}
func CreatePod(ctx context.Context, pod *kv1.Pod, namespace string, h *H) error {
uwm, err := h.Kube().CoreV1().Pods(namespace).Create(ctx, pod, metav1.CreateOptions{})
if err != nil {
log.Printf("Could not issue create command")
return err
}
// Wait for the pod to create.
err = wait.PollUntilContextTimeout(ctx, 5*time.Second, 1*time.Minute, false, func(ctx context.Context) (bool, error) {
if _, err := h.Kube().CoreV1().Pods(namespace).Get(ctx, uwm.Name, metav1.GetOptions{}); err != nil {
return false, nil
}
return true, nil
})
return err
}
func SamplePod(name, namespace, imageName string) *kv1.Pod {
return &kv1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: kv1.PodSpec{
Containers: []kv1.Container{
{
Name: "test",
Image: imageName,
},
},
},
}
}
func SampleService(port int32, targetPort int, serviceName, serviceNamespace string, prometheusName string) *kv1.Service {
service := &kv1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: prometheusName,
Namespace: serviceNamespace,
Labels: map[string]string{prometheusName: serviceName},
},
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
Spec: kv1.ServiceSpec{
Ports: []kv1.ServicePort{
{
Port: port,
Protocol: kv1.ProtocolTCP,
TargetPort: intstr.FromInt(targetPort),
Name: "web",
},
},
Selector: map[string]string{prometheusName: serviceName},
},
}
return service
}
func CreateService(ctx context.Context, svc *kv1.Service, h *H) error {
uwm, err := h.Kube().CoreV1().Services(svc.Namespace).Create(ctx, svc, metav1.CreateOptions{})
if err != nil {
log.Printf("Could not issue create command")
return err
}
// Wait for the pod to create.
err = wait.PollUntilContextTimeout(ctx, 5*time.Second, 1*time.Minute, false, func(ctx context.Context) (bool, error) {
if _, err := h.Kube().CoreV1().Services(uwm.Namespace).Get(ctx, uwm.Name, metav1.GetOptions{}); err != nil {
return false, nil
}
return true, nil
})
return err
}
func CreateNamespace(ctx context.Context, namespace string, h *H) (*kv1.Namespace, error) {
// If the namespace already exists, we don't need to create it. Just return.
ns, err := h.Kube().CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
if ns != nil && ns.Status.Phase != "Terminating" && err == nil {
return ns, err
}
log.Printf("Creating namespace for namespace validation webhook (%s)", namespace)
ns = &kv1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
},
}
h.Kube().CoreV1().Namespaces().Create(ctx, ns, metav1.CreateOptions{})
// Wait for the namespace to create. This is usually pretty quick.
err = wait.PollUntilContextTimeout(ctx, 5*time.Second, 2*time.Minute, false, func(ctx context.Context) (bool, error) {
if _, err := h.Kube().CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{}); err != nil {
return false, nil
}
return true, nil
})
return ns, err
}
func DeleteNamespace(ctx context.Context, namespace string, waitForDelete bool, h *H) error {
log.Printf("Deleting namespace (%s)", namespace)
err := h.Kube().CoreV1().Namespaces().Delete(ctx, namespace, metav1.DeleteOptions{})
if err != nil {
return fmt.Errorf("failed to delete namespace '%s': %w", namespace, err)
}
// Deleting a namespace can take a while. If desired, wait for the namespace to delete before returning.
if waitForDelete {
err = wait.PollUntilContextTimeout(ctx, 2*time.Second, 1*time.Minute, false, func(ctx context.Context) (bool, error) {
ns, _ := h.Kube().CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
if ns != nil && ns.Status.Phase == "Terminating" {
return false, nil
}
return true, nil
})
}
return err
}
func CreateUser(ctx context.Context, userName string, identities []string, groups []string, h *H) (*userv1.User, error) {
user := &userv1.User{
ObjectMeta: metav1.ObjectMeta{
Name: userName,
},
Identities: identities,
Groups: groups,
}
return h.User().UserV1().Users().Create(ctx, user, metav1.CreateOptions{})
}
func AddUserToGroup(ctx context.Context, userName string, groupName string, h *H) (result *userv1.Group, err error) {
group, err := h.User().UserV1().Groups().Get(ctx, groupName, metav1.GetOptions{})
if err != nil {
return &userv1.Group{}, err
}
group.Users = append(group.Users, userName)
return h.User().UserV1().Groups().Update(ctx, group, metav1.UpdateOptions{})
}