/
k8s_apis.go
226 lines (193 loc) · 6.3 KB
/
k8s_apis.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
/*
Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
*/
package k8s
import (
"context"
"time"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
servicemeshapi "github.com/oracle/oci-service-operator/apis/servicemesh.oci/v1beta1"
meshCommons "github.com/oracle/oci-service-operator/pkg/servicemanager/servicemesh/utils/commons"
)
// K8sAPIs supports common k8s operations for each kind of resource.
// It will be added to the test framework to facilitate tests.
type K8sAPIs interface {
Get(ctx context.Context, key types.NamespacedName, obj client.Object) error
Create(ctx context.Context, obj client.Object) error
Update(ctx context.Context, oldObj client.Object, newObj client.Object) error
Delete(ctx context.Context, obj client.Object) error
WaitUntilDeleted(ctx context.Context, obj client.Object) error
}
type defaultK8sAPIs struct {
k8sClient client.Client
}
func NewDefaultK8sAPIs(k8sClient client.Client) K8sAPIs {
return &defaultK8sAPIs{k8sClient: k8sClient}
}
func (h *defaultK8sAPIs) Get(ctx context.Context, key types.NamespacedName, obj client.Object) error {
err := h.k8sClient.Get(ctx, key, obj)
if err != nil {
return err
}
return nil
}
func (h *defaultK8sAPIs) Create(ctx context.Context, obj client.Object) error {
err := h.k8sClient.Create(ctx, obj)
if err != nil {
return err
}
if _, err := h.waitUntilActive(ctx, obj); err != nil {
return err
}
return nil
}
func (h *defaultK8sAPIs) Update(ctx context.Context, newObj client.Object, oldObj client.Object) error {
err := h.k8sClient.Patch(ctx, newObj, client.MergeFrom(oldObj))
if err != nil {
return err
}
if _, err := h.waitUntilActive(ctx, newObj); err != nil {
return err
}
return nil
}
func (h *defaultK8sAPIs) Delete(ctx context.Context, obj client.Object) error {
err := h.k8sClient.Delete(ctx, obj)
if err != nil {
return err
}
if err := h.WaitUntilDeleted(ctx, obj); err != nil {
return err
}
return nil
}
func (h *defaultK8sAPIs) waitUntilActive(ctx context.Context, obj client.Object) (client.Object, error) {
key := newNamespacedName(obj)
observedObj := obj
return observedObj, wait.PollImmediateUntil(meshCommons.PollInterval, func() (bool, error) {
// Sometimes there's a delay in the resource showing up
// We need to make sure the resource is in k8s before we send test requests to the reconciler
for i := 0; i < 5; i++ {
if err := h.k8sClient.Get(ctx, key, observedObj); err != nil {
if i >= 5 {
return false, err
}
}
time.Sleep(100 * time.Millisecond)
}
if IsValidMeshResource(obj.GetObjectKind().GroupVersionKind().Kind) {
if !isActive(obj) {
return false, nil
}
}
return true, nil
}, ctx.Done())
}
func (h *defaultK8sAPIs) WaitUntilDeleted(ctx context.Context, obj client.Object) error {
key := newNamespacedName(obj)
observedObj := obj
return wait.PollImmediateUntil(meshCommons.PollInterval, func() (bool, error) {
if err := h.k8sClient.Get(ctx, key, observedObj); err != nil {
if kerrors.IsNotFound(err) {
return true, nil
}
return false, err
}
return false, nil
}, ctx.Done())
}
func newNamespacedName(obj metav1.Object) types.NamespacedName {
return types.NamespacedName{
Namespace: obj.GetNamespace(),
Name: obj.GetName(),
}
}
func isActive(obj client.Object) bool {
if string(meshCommons.Mesh) == obj.GetObjectKind().GroupVersionKind().Kind {
mesh := obj.(*servicemeshapi.Mesh)
for _, condition := range mesh.Status.Conditions {
if condition.Status == metav1.ConditionTrue {
return true
}
}
return false
}
if string(meshCommons.VirtualServiceRouteTable) == obj.GetObjectKind().GroupVersionKind().Kind {
virtualServiceRouteTable := obj.(*servicemeshapi.VirtualServiceRouteTable)
for _, condition := range virtualServiceRouteTable.Status.Conditions {
if condition.Status == metav1.ConditionTrue {
return true
}
}
return false
}
if string(meshCommons.VirtualService) == obj.GetObjectKind().GroupVersionKind().Kind {
virtualService := obj.(*servicemeshapi.VirtualService)
for _, condition := range virtualService.Status.Conditions {
if condition.Status == metav1.ConditionTrue {
return true
}
}
return false
}
if string(meshCommons.VirtualDeploymentBinding) == obj.GetObjectKind().GroupVersionKind().Kind {
virtualDeploymentBinding := obj.(*servicemeshapi.VirtualDeploymentBinding)
for _, condition := range virtualDeploymentBinding.Status.Conditions {
if condition.Status == metav1.ConditionTrue {
return true
}
}
return false
}
if string(meshCommons.VirtualDeployment) == obj.GetObjectKind().GroupVersionKind().Kind {
virtualDeployment := obj.(*servicemeshapi.VirtualDeployment)
for _, condition := range virtualDeployment.Status.Conditions {
if condition.Status == metav1.ConditionTrue {
return true
}
}
return false
}
if string(meshCommons.AccessPolicy) == obj.GetObjectKind().GroupVersionKind().Kind {
accessPolicy := obj.(*servicemeshapi.AccessPolicy)
for _, condition := range accessPolicy.Status.Conditions {
if condition.Status == metav1.ConditionTrue {
return true
}
}
return false
}
if string(meshCommons.IngressGatewayRouteTable) == obj.GetObjectKind().GroupVersionKind().Kind {
ingressGatewayRouteTable := obj.(*servicemeshapi.IngressGatewayRouteTable)
for _, condition := range ingressGatewayRouteTable.Status.Conditions {
if condition.Status == metav1.ConditionTrue {
return true
}
}
return false
}
if string(meshCommons.IngressGatewayDeployment) == obj.GetObjectKind().GroupVersionKind().Kind {
ingressGatewayDeployment := obj.(*servicemeshapi.IngressGatewayDeployment)
for _, condition := range ingressGatewayDeployment.Status.Conditions {
if condition.Status == metav1.ConditionTrue {
return true
}
}
return false
}
if string(meshCommons.IngressGateway) == obj.GetObjectKind().GroupVersionKind().Kind {
ingressGateway := obj.(*servicemeshapi.IngressGateway)
for _, condition := range ingressGateway.Status.Conditions {
if condition.Status == metav1.ConditionTrue {
return true
}
}
return false
}
return false
}