@@ -50,9 +50,16 @@ func (r *reconciledResource) key() kubeutil.ResourceKey {
50
50
type SyncContext interface {
51
51
// Terminate terminates sync operation. The method is asynchronous: it starts deletion is related K8S resources
52
52
// such as in-flight resource hooks, updates operation status, and exists without waiting for resource completion.
53
+ // Deprecated: use TerminateContext instead
53
54
Terminate ()
55
+ // TerminateContext terminates sync operation. The method is asynchronous: it starts deletion is related K8S resources
56
+ // such as in-flight resource hooks, updates operation status, and exists without waiting for resource completion.
57
+ TerminateContext (ctx context.Context )
54
58
// Executes next synchronization step and updates operation status.
59
+ // Deprecated: use SyncContext instead
55
60
Sync ()
61
+ // Executes next synchronization step and updates operation status.
62
+ SyncContext (ctx context.Context )
56
63
// Returns current sync operation state and information about resources synchronized so far.
57
64
GetState () (common.OperationPhase , string , []common.ResourceSyncResult )
58
65
}
@@ -75,12 +82,20 @@ func WithPermissionValidator(validator common.PermissionValidator) SyncOpt {
75
82
}
76
83
77
84
// WithHealthOverride sets specified health override
85
+ // Deprecated: use WithHealthOverrideContext instead
78
86
func WithHealthOverride (override health.HealthOverride ) SyncOpt {
79
87
return func (ctx * syncContext ) {
80
88
ctx .healthOverride = override
81
89
}
82
90
}
83
91
92
+ // WithHealthOverrideContext sets specified health override
93
+ func WithHealthOverrideContext (override health.HealthOverrideContext ) SyncOpt {
94
+ return func (ctx * syncContext ) {
95
+ ctx .healthOverrideContext = override
96
+ }
97
+ }
98
+
84
99
// WithInitialState sets sync operation initial state
85
100
func WithInitialState (phase common.OperationPhase , message string , results []common.ResourceSyncResult , startedAt metav1.Time ) SyncOpt {
86
101
return func (ctx * syncContext ) {
@@ -308,11 +323,17 @@ const (
308
323
)
309
324
310
325
// getOperationPhase returns a health status from a _live_ unstructured object
311
- func (sc * syncContext ) getOperationPhase (obj * unstructured.Unstructured ) (common.OperationPhase , string , error ) {
326
+ func (sc * syncContext ) getOperationPhase (ctx context. Context , obj * unstructured.Unstructured ) (common.OperationPhase , string , error ) {
312
327
phase := common .OperationSucceeded
313
328
message := obj .GetName () + " created"
314
329
315
- resHealth , err := health .GetResourceHealth (obj , sc .healthOverride )
330
+ var resHealth * health.HealthStatus
331
+ var err error
332
+ if sc .healthOverrideContext != nil {
333
+ resHealth , err = health .GetResourceHealthContext (ctx , obj , sc .healthOverrideContext )
334
+ } else if sc .healthOverride != nil {
335
+ resHealth , err = health .GetResourceHealth (obj , sc .healthOverride )
336
+ }
316
337
if err != nil {
317
338
return "" , "" , err
318
339
}
@@ -333,18 +354,19 @@ func (sc *syncContext) getOperationPhase(obj *unstructured.Unstructured) (common
333
354
}
334
355
335
356
type syncContext struct {
336
- healthOverride health.HealthOverride
337
- permissionValidator common.PermissionValidator
338
- resources map [kubeutil.ResourceKey ]reconciledResource
339
- hooks []* unstructured.Unstructured
340
- config * rest.Config
341
- rawConfig * rest.Config
342
- dynamicIf dynamic.Interface
343
- disco discovery.DiscoveryInterface
344
- extensionsclientset * clientset.Clientset
345
- kubectl kubeutil.Kubectl
346
- resourceOps kubeutil.ResourceOperations
347
- namespace string
357
+ healthOverride health.HealthOverride
358
+ healthOverrideContext health.HealthOverrideContext
359
+ permissionValidator common.PermissionValidator
360
+ resources map [kubeutil.ResourceKey ]reconciledResource
361
+ hooks []* unstructured.Unstructured
362
+ config * rest.Config
363
+ rawConfig * rest.Config
364
+ dynamicIf dynamic.Interface
365
+ disco discovery.DiscoveryInterface
366
+ extensionsclientset * clientset.Clientset
367
+ kubectl kubeutil.Kubectl
368
+ resourceOps kubeutil.ResourceOperations
369
+ namespace string
348
370
349
371
dryRun bool
350
372
skipDryRun bool
@@ -403,8 +425,19 @@ func (sc *syncContext) setRunningPhase(tasks []*syncTask, isPendingDeletion bool
403
425
}
404
426
}
405
427
406
- // sync has performs the actual apply or hook based sync
428
+ // Sync has performs the actual apply or hook based sync
429
+ // Deprecated: use SyncContext instead
407
430
func (sc * syncContext ) Sync () {
431
+ sc .SyncContext (context .Background ())
432
+ }
433
+
434
+ // SyncContext has performs the actual apply or hook based sync
435
+ func (sc * syncContext ) SyncContext (ctx context.Context ) {
436
+ sc .sync (ctx )
437
+ }
438
+
439
+ // sync has performs the actual apply or hook based sync
440
+ func (sc * syncContext ) sync (ctx context.Context ) {
408
441
sc .log .WithValues ("skipHooks" , sc .skipHooks , "started" , sc .started ()).Info ("Syncing" )
409
442
tasks , ok := sc .getSyncTasks ()
410
443
if ! ok {
@@ -441,15 +474,21 @@ func (sc *syncContext) Sync() {
441
474
}) {
442
475
if task .isHook () {
443
476
// update the hook's result
444
- operationState , message , err := sc .getOperationPhase (task .liveObj )
477
+ operationState , message , err := sc .getOperationPhase (ctx , task .liveObj )
445
478
if err != nil {
446
479
sc .setResourceResult (task , "" , common .OperationError , fmt .Sprintf ("failed to get resource health: %v" , err ))
447
480
} else {
448
481
sc .setResourceResult (task , "" , operationState , message )
449
482
}
450
483
} else {
451
484
// this must be calculated on the live object
452
- healthStatus , err := health .GetResourceHealth (task .liveObj , sc .healthOverride )
485
+ var healthStatus * health.HealthStatus
486
+ var err error
487
+ if sc .healthOverrideContext != nil {
488
+ healthStatus , err = health .GetResourceHealthContext (ctx , task .liveObj , sc .healthOverrideContext )
489
+ } else if sc .healthOverride != nil {
490
+ healthStatus , err = health .GetResourceHealth (task .liveObj , sc .healthOverride )
491
+ }
453
492
if err == nil {
454
493
sc .log .WithValues ("task" , task , "healthStatus" , healthStatus ).V (1 ).Info ("attempting to update health of running task" )
455
494
if healthStatus == nil {
@@ -1176,8 +1215,17 @@ func (sc *syncContext) hasCRDOfGroupKind(group string, kind string) bool {
1176
1215
return false
1177
1216
}
1178
1217
1179
- // terminate looks for any running jobs/workflow hooks and deletes the resource
1218
+ // Deprecated: use TerminateContext instead
1180
1219
func (sc * syncContext ) Terminate () {
1220
+ sc .TerminateContext (context .Background ())
1221
+ }
1222
+
1223
+ func (sc * syncContext ) TerminateContext (ctx context.Context ) {
1224
+ sc .terminate (ctx )
1225
+ }
1226
+
1227
+ // terminate looks for any running jobs/workflow hooks and deletes the resource
1228
+ func (sc * syncContext ) terminate (ctx context.Context ) {
1181
1229
terminateSuccessful := true
1182
1230
sc .log .V (1 ).Info ("terminating" )
1183
1231
tasks , _ := sc .getSyncTasks ()
@@ -1190,7 +1238,7 @@ func (sc *syncContext) Terminate() {
1190
1238
terminateSuccessful = false
1191
1239
continue
1192
1240
}
1193
- phase , msg , err := sc .getOperationPhase (task .liveObj )
1241
+ phase , msg , err := sc .getOperationPhase (ctx , task .liveObj )
1194
1242
if err != nil {
1195
1243
sc .setOperationPhase (common .OperationError , fmt .Sprintf ("Failed to get hook health: %v" , err ))
1196
1244
return
0 commit comments