-
Notifications
You must be signed in to change notification settings - Fork 18
/
servicemesh_servicemanager.go
469 lines (422 loc) · 18.4 KB
/
servicemesh_servicemanager.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
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 manager
import (
"context"
"errors"
"github.com/oracle/oci-go-sdk/v65/common"
sdk "github.com/oracle/oci-go-sdk/v65/servicemesh"
api "github.com/oracle/oci-service-operator/api/v1beta1"
servicemeshapi "github.com/oracle/oci-service-operator/apis/servicemesh.oci/v1beta1"
"github.com/oracle/oci-service-operator/pkg/core"
"github.com/oracle/oci-service-operator/pkg/loggerutil"
"github.com/oracle/oci-service-operator/pkg/servicemanager"
meshCommons "github.com/oracle/oci-service-operator/pkg/servicemanager/servicemesh/utils/commons"
meshConversions "github.com/oracle/oci-service-operator/pkg/servicemanager/servicemesh/utils/conversions"
meshErrors "github.com/oracle/oci-service-operator/pkg/servicemanager/servicemesh/utils/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
type Resource interface {
GetResource(ctx context.Context, object client.Object, details *ResourceDetails) error
CreateResource(ctx context.Context, object client.Object, details *ResourceDetails) (bool, error)
UpdateResource(ctx context.Context, object client.Object, details *ResourceDetails) error
DeleteResource(ctx context.Context, object client.Object) error
ChangeCompartment(ctx context.Context, object client.Object, details *ResourceDetails) error
}
type Finalizer interface {
GetFinalizer() string
Finalize(ctx context.Context, object client.Object) error
}
type Status interface {
GetServiceMeshStatus(object client.Object) (*servicemeshapi.ServiceMeshStatus, error)
GetConditionStatus(details *ResourceDetails) metav1.ConditionStatus
UpdateStatus(object client.Object, details *ResourceDetails) (bool, error)
GetTimeUpdated(details *ResourceDetails) *common.SDKTime
}
type Verify interface {
VerifyEntityType(object runtime.Object) (client.Object, error)
VerifyResourceStatus(details *ResourceDetails) (bool, error)
}
type Dependencies interface {
ResolveDependencies(ctx context.Context, object client.Object, details *ResourceDetails) error
}
type SdkOperations interface {
BuildSdk(object client.Object, details *ResourceDetails) error
HasCompartmentIdChanged(object client.Object, details *ResourceDetails) (bool, error)
GetLifecycleState(details *ResourceDetails) string
GetMessage(details *ResourceDetails) string
HasSdk(details *ResourceDetails) bool
}
type CustomResourceHandler interface {
Resource
Finalizer
Status
Verify
Dependencies
SdkOperations
}
type MeshDetails struct {
SdkMesh *sdk.Mesh
BuildSdkMesh *sdk.Mesh
}
type VirtualServiceDetails struct {
MeshId *api.OCID
SdkVs *sdk.VirtualService
BuildSdkVs *sdk.VirtualService
}
type AccessPolicyDetails struct {
Dependencies *meshConversions.AccessPolicyDependencies
SdkAp *sdk.AccessPolicy
BuildSdkAp *sdk.AccessPolicy
}
type VirtualDeploymentDetails struct {
VsRef *meshCommons.ResourceRef
SdkVd *sdk.VirtualDeployment
BuildSdkVd *sdk.VirtualDeployment
}
type VirtualServiceRouteTableDetails struct {
SdkVsrt *sdk.VirtualServiceRouteTable
BuildSdkVsrt *sdk.VirtualServiceRouteTable
Dependencies *meshConversions.VSRTDependencies
}
type IngressGatewayDetails struct {
MeshId *api.OCID
SdkIg *sdk.IngressGateway
BuildSdkIg *sdk.IngressGateway
}
type IngressGatewayRouteTableDetails struct {
SdkIgrt *sdk.IngressGatewayRouteTable
BuildSdkIgrt *sdk.IngressGatewayRouteTable
Dependencies *meshConversions.IGRTDependencies
}
type ResourceDetails struct {
MeshDetails MeshDetails
VsDetails VirtualServiceDetails
VdDetails VirtualDeploymentDetails
VsrtDetails VirtualServiceRouteTableDetails
ApDetails AccessPolicyDetails
IgDetails IngressGatewayDetails
IgrtDetails IngressGatewayRouteTableDetails
OpcRetryToken *string
}
type ServiceMeshServiceManager struct {
client client.Client
log loggerutil.OSOKLogger
handler CustomResourceHandler
}
func NewServiceMeshServiceManager(client client.Client, log loggerutil.OSOKLogger, handler CustomResourceHandler) *ServiceMeshServiceManager {
return &ServiceMeshServiceManager{
client: client,
log: log,
handler: handler,
}
}
func (c *ServiceMeshServiceManager) CreateOrUpdate(ctx context.Context, obj runtime.Object, req ctrl.Request) (servicemanager.OSOKResponse, error) {
object, err := c.handler.VerifyEntityType(obj)
if err != nil {
return meshErrors.GetOsokResponseByHandlingReconcileError(err)
}
resourceDetails := &ResourceDetails{}
resolveDependenciesErr := c.handler.ResolveDependencies(ctx, object, resourceDetails)
c.UpdateServiceMeshDependenciesActiveStatus(ctx, object, resolveDependenciesErr)
if resolveDependenciesErr != nil {
return meshErrors.GetOsokResponseByHandlingReconcileError(resolveDependenciesErr)
}
c.log.InfoLogWithFixedMessage(ctx, "dependencies resolved")
getResourceErr := c.handler.GetResource(ctx, object, resourceDetails)
if getResourceErr != nil {
_ = c.UpdateServiceMeshConfiguredStatus(ctx, object, getResourceErr)
return meshErrors.GetOsokResponseByHandlingReconcileError(getResourceErr)
}
// Verify
// For states like Creating, Updating, Deleting commons.UnknownStatus error is thrown
// Don't proceed with the reconcile for states like Deleted and Failed
validResource, verifyResourceErr := c.handler.VerifyResourceStatus(resourceDetails)
if !validResource {
status := c.handler.GetConditionStatus(resourceDetails)
message := c.handler.GetMessage(resourceDetails)
updateErr := c.UpdateServiceMeshCondition(ctx, object, status, string(meshCommons.LifecycleStateChanged), message, servicemeshapi.ServiceMeshActive)
if updateErr != nil {
return meshErrors.GetOsokResponseByHandlingReconcileError(updateErr)
}
return meshErrors.GetOsokResponseByHandlingReconcileError(verifyResourceErr)
}
// Build object
buildSdkErr := c.handler.BuildSdk(object, resourceDetails)
if buildSdkErr != nil {
return meshErrors.GetOsokResponseByHandlingReconcileError(buildSdkErr)
}
// Reuses the opcRetryToken if the previous request fails, otherwise create a new opcRetryToken
opcRetryToken, err := c.getOpcRetryToken(object)
if err != nil {
return meshErrors.GetOsokResponseByHandlingReconcileError(err)
}
resourceDetails.OpcRetryToken = opcRetryToken
if !c.handler.HasSdk(resourceDetails) {
c.UpdateOpcRetryToken(ctx, object, resourceDetails.OpcRetryToken)
}
resourceCreated, resourceCreationErr := c.handler.CreateResource(ctx, object, resourceDetails)
hasChanged := false
hasResourceUpdatedInCp := c.hasResourceUpdatedInCp(object, resourceDetails)
if !resourceCreated {
_ = c.UpdateServiceMeshConfiguredStatus(ctx, object, resourceCreationErr)
if resourceCreationErr != nil {
c.log.InfoLogWithFixedMessage(ctx, "cp error while creating resource", "error", resourceCreationErr)
if !meshErrors.IsNetworkErrorOrInternalError(resourceCreationErr) {
// Clears the opcRetryToken if the request didn't fail for network or internal error
_ = c.UpdateOpcRetryToken(ctx, object, nil)
}
return meshErrors.GetOsokResponseByHandlingReconcileError(resourceCreationErr)
}
} else if c.hasSpecChanged(object) || hasResourceUpdatedInCp {
hasChanged, err = c.handler.HasCompartmentIdChanged(object, resourceDetails)
if err != nil {
return meshErrors.GetOsokResponseByHandlingReconcileError(err)
}
if hasChanged {
err = c.handler.ChangeCompartment(ctx, object, resourceDetails)
} else {
err = c.handler.UpdateResource(ctx, object, resourceDetails)
}
_ = c.UpdateServiceMeshConfiguredStatus(ctx, object, err)
if err != nil {
return meshErrors.GetOsokResponseByHandlingReconcileError(err)
}
}
// Clears the opcRetryToken when control plane request retry happens and the request goes through successfully
_ = c.UpdateOpcRetryToken(ctx, object, nil)
resourceDetails.OpcRetryToken = nil
updateMeshConfiguredErr := c.UpdateServiceMeshConfiguredStatus(ctx, object, nil)
if updateMeshConfiguredErr != nil {
return meshErrors.GetOsokResponseByHandlingReconcileError(updateMeshConfiguredErr)
}
return c.UpdateK8s(ctx, object, resourceDetails, hasChanged, hasResourceUpdatedInCp)
}
func (c *ServiceMeshServiceManager) hasSpecChanged(object client.Object) bool {
// metadata.generation field is updated if and only if the value at the .spec subpath changes.
// If the spec does not change, .metadata.generation is not updated
// Update only if metadata.generation differs, else it will reconcile forever
// This condition ensures we don't do an update call to CP everytime it reconciles
// Additionally, sync resource from k8s to CP periodically for every commons.RequeueSyncDuration, should change only the status subresource
serviceMeshStatus, err := c.handler.GetServiceMeshStatus(object)
if err != nil {
return false
}
existingCondition := meshCommons.GetServiceMeshCondition(serviceMeshStatus, servicemeshapi.ServiceMeshActive)
if existingCondition == nil || object.GetGeneration() != existingCondition.ObservedGeneration {
return true
}
return false
}
func (c *ServiceMeshServiceManager) hasResourceUpdatedInCp(object client.Object, details *ResourceDetails) bool {
serviceMeshStatus, err := c.handler.GetServiceMeshStatus(object)
if err != nil {
return false
}
if !isResourceActiveWithStatusTrue(serviceMeshStatus) {
return false
}
if serviceMeshStatus.LastUpdatedTime == nil {
return false
}
cpTime := c.handler.GetTimeUpdated(details)
operatorTime := serviceMeshStatus.LastUpdatedTime
return DoTimeStampsDiffer(cpTime, operatorTime)
}
func DoTimeStampsDiffer(cpTimeUpdated *common.SDKTime, operatorTimeUpdated *metav1.Time) bool {
if operatorTimeUpdated == nil && cpTimeUpdated == nil {
return false
}
if operatorTimeUpdated == nil || cpTimeUpdated == nil {
return true
}
return cpTimeUpdated.Time.Unix() != operatorTimeUpdated.Time.Unix()
}
// UpdateServiceMeshCondition updates the status of the resource
// Called when there is an error from CP
// Additionally, also called when the state is not Active
// We haven't fully synced the spec, hence using the existing generation from the status for generation field
func (c *ServiceMeshServiceManager) UpdateServiceMeshCondition(ctx context.Context, obj client.Object, status metav1.ConditionStatus, reason string, message string, meshConditionType servicemeshapi.ServiceMeshConditionType) error {
oldObj := obj.DeepCopyObject()
serviceMeshStatus, err := c.handler.GetServiceMeshStatus(obj)
if err != nil {
return err
}
existingCondition := meshCommons.GetServiceMeshCondition(serviceMeshStatus, meshConditionType)
var generation int64
if existingCondition == nil {
generation = 1
} else if status == metav1.ConditionUnknown {
// do not update the generation for unknown condition status
generation = existingCondition.ObservedGeneration
} else {
generation = obj.GetGeneration()
}
if !meshCommons.UpdateServiceMeshCondition(serviceMeshStatus, meshConditionType, status, reason, message, generation) {
return nil
}
return c.client.Status().Patch(ctx, obj, client.MergeFrom(oldObj))
}
func (c *ServiceMeshServiceManager) UpdateServiceMeshConfiguredStatus(ctx context.Context, obj client.Object, err error) error {
if err == nil {
return c.UpdateServiceMeshCondition(ctx, obj, metav1.ConditionTrue, string(meshCommons.Successful), string(meshCommons.ResourceConfigured), servicemeshapi.ServiceMeshConfigured)
}
if common.IsNetworkError(err) {
return c.UpdateServiceMeshCondition(ctx, obj, metav1.ConditionUnknown, string(meshCommons.ConnectionError), err.Error(), servicemeshapi.ServiceMeshConfigured)
}
if serviceError, ok := err.(common.ServiceError); ok {
return c.UpdateServiceMeshCondition(ctx, obj, meshErrors.GetMeshConfiguredConditionStatus(serviceError), serviceError.GetCode(), meshErrors.GetErrorMessage(serviceError), servicemeshapi.ServiceMeshConfigured)
}
return nil
}
func (c *ServiceMeshServiceManager) UpdateServiceMeshActiveStatus(ctx context.Context, obj client.Object, err error) {
if common.IsNetworkError(err) {
_ = c.UpdateServiceMeshCondition(ctx, obj, metav1.ConditionUnknown, string(meshCommons.ConnectionError), err.Error(), servicemeshapi.ServiceMeshActive)
return
}
if serviceError, ok := err.(common.ServiceError); ok {
_ = c.UpdateServiceMeshCondition(ctx, obj, meshErrors.GetConditionStatus(serviceError), serviceError.GetCode(), meshErrors.GetErrorMessage(serviceError), servicemeshapi.ServiceMeshActive)
}
}
func (c *ServiceMeshServiceManager) UpdateK8s(ctx context.Context, obj client.Object, details *ResourceDetails, hasCompartmentChanged bool, hasResourceUpdatedInCp bool) (servicemanager.OSOKResponse, error) {
oldObject := obj.DeepCopyObject()
needsUpdate, err := c.handler.UpdateStatus(obj, details)
if err != nil {
return meshErrors.GetOsokResponseByHandlingReconcileError(err)
}
serviceMeshStatus, err := c.handler.GetServiceMeshStatus(obj)
if err != nil {
return meshErrors.GetOsokResponseByHandlingReconcileError(err)
}
status := c.handler.GetConditionStatus(details)
// This is a special case where both CompartmentId of the resource are updated
// Keep the status as unknown and do NOT update the generation till all the updates are propagated to CP
// if resource is deleted or failed (i.e. status = false) do not update to cp
if hasCompartmentChanged && status != metav1.ConditionFalse {
status = metav1.ConditionUnknown
serviceMeshCondition := meshCommons.GetServiceMeshCondition(serviceMeshStatus, servicemeshapi.ServiceMeshActive)
generation := serviceMeshCondition.ObservedGeneration
if hasResourceUpdatedInCp {
generation = serviceMeshCondition.ObservedGeneration - 1
}
if meshCommons.UpdateServiceMeshCondition(serviceMeshStatus, servicemeshapi.ServiceMeshActive, status, string(meshCommons.GetReason(status)), string(meshCommons.ResourceChangeCompartment), generation) {
needsUpdate = true
}
} else if meshCommons.UpdateServiceMeshCondition(serviceMeshStatus, servicemeshapi.ServiceMeshActive, status, string(meshCommons.GetReason(status)), string(meshCommons.GetMessage(c.handler.GetLifecycleState(details))), obj.GetGeneration()) {
needsUpdate = true
}
if needsUpdate {
if err := c.client.Status().Patch(ctx, obj, client.MergeFrom(oldObject)); err != nil {
return meshErrors.GetOsokResponseByHandlingReconcileError(err)
}
}
// Requeue request if state is unknown
if status == metav1.ConditionUnknown {
return meshErrors.GetOsokResponseByHandlingReconcileError(meshErrors.NewRequeueOnError(errors.New(meshCommons.UnknownStatus)))
}
// For other states, requeue every RequeueSyncDuration
return servicemanager.OSOKResponse{IsSuccessful: true, ShouldRequeue: true, RequeueDuration: meshCommons.RequeueSyncDuration}, nil
}
func (c *ServiceMeshServiceManager) UpdateServiceMeshDependenciesActiveStatus(ctx context.Context, object client.Object, err error) {
if err == nil {
_ = c.UpdateServiceMeshCondition(ctx, object, metav1.ConditionTrue, string(meshCommons.Successful), string(meshCommons.DependenciesResolved), servicemeshapi.ServiceMeshDependenciesActive)
return
}
if common.IsNetworkError(err) {
_ = c.UpdateServiceMeshCondition(ctx, object, metav1.ConditionUnknown, string(meshCommons.ConnectionError), err.Error(), servicemeshapi.ServiceMeshDependenciesActive)
return
}
if serviceError, ok := err.(common.ServiceError); ok {
_ = c.UpdateServiceMeshCondition(ctx, object, meshErrors.GetConditionStatus(serviceError), serviceError.GetCode(), meshErrors.GetErrorMessage(serviceError), servicemeshapi.ServiceMeshDependenciesActive)
} else {
_ = c.UpdateServiceMeshCondition(ctx, object, meshCommons.GetConditionStatusFromK8sError(err), string(meshCommons.DependenciesNotResolved), err.Error(), servicemeshapi.ServiceMeshDependenciesActive)
}
}
func (c *ServiceMeshServiceManager) GetCrdStatus(obj runtime.Object) (*api.OSOKStatus, error) {
return nil, nil
}
func (c *ServiceMeshServiceManager) Delete(ctx context.Context, obj runtime.Object) (bool, error) {
object, err := c.handler.VerifyEntityType(obj)
if err != nil {
return false, err
}
if core.HasFinalizer(object, c.handler.GetFinalizer()) {
c.log.InfoLogWithFixedMessage(ctx, "Checking dependencies")
err := c.handler.Finalize(ctx, object)
c.UpdateServiceMeshDependenciesActiveStatus(ctx, object, err)
if err != nil {
return false, err
}
if err = c.removeFinalizer(ctx, object, c.handler.GetFinalizer()); err != nil {
return false, err
}
}
if core.HasFinalizer(object, core.OSOKFinalizerName) {
c.log.InfoLogWithFixedMessage(ctx, "Attempting to delete the resource in the control plane")
err := c.handler.DeleteResource(ctx, object)
if err != nil {
c.UpdateServiceMeshActiveStatus(ctx, object, err)
return false, err
}
}
return true, nil
}
func (c *ServiceMeshServiceManager) removeFinalizer(ctx context.Context, obj client.Object, finalizer string) error {
oldObj := obj.DeepCopyObject()
needsUpdate := false
if controllerutil.ContainsFinalizer(obj, finalizer) {
controllerutil.RemoveFinalizer(obj, finalizer)
needsUpdate = true
}
if !needsUpdate {
return nil
}
return c.client.Patch(ctx, obj, client.MergeFrom(oldObj))
}
func (c *ServiceMeshServiceManager) UpdateOpcRetryToken(ctx context.Context, obj client.Object, opcRetryToken *string) error {
oldObj := obj.DeepCopyObject()
needsUpdate := false
serviceMeshStatus, err := c.handler.GetServiceMeshStatus(obj)
if err != nil {
return err
}
if serviceMeshStatus.OpcRetryToken == nil && opcRetryToken == nil {
return nil
}
if serviceMeshStatus.OpcRetryToken == nil || opcRetryToken == nil || *serviceMeshStatus.OpcRetryToken != *opcRetryToken {
serviceMeshStatus.OpcRetryToken = opcRetryToken
needsUpdate = true
}
if !needsUpdate {
return nil
}
return c.client.Status().Patch(ctx, obj, client.MergeFrom(oldObj))
}
func (c *ServiceMeshServiceManager) getOpcRetryToken(obj client.Object) (*string, error) {
serviceMeshStatus, err := c.handler.GetServiceMeshStatus(obj)
if err != nil {
return nil, err
}
if serviceMeshStatus.OpcRetryToken != nil {
return serviceMeshStatus.OpcRetryToken, nil
}
opcRetryToken := common.RetryToken()
return &opcRetryToken, nil
}
func isResourceActiveWithStatusTrue(serviceMeshStatus *servicemeshapi.ServiceMeshStatus) bool {
if serviceMeshStatus.Conditions == nil {
return false
}
for _, condition := range serviceMeshStatus.Conditions {
if condition.Status != metav1.ConditionTrue && condition.Type == servicemeshapi.ServiceMeshActive {
return false
}
}
return true
}