@@ -50,9 +50,16 @@ func (r *reconciledResource) key() kubeutil.ResourceKey {
5050type SyncContext interface {
5151 // Terminate terminates sync operation. The method is asynchronous: it starts deletion is related K8S resources
5252 // such as in-flight resource hooks, updates operation status, and exists without waiting for resource completion.
53+ // Deprecated: use TerminateContext instead
5354 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 )
5458 // Executes next synchronization step and updates operation status.
59+ // Deprecated: use SyncContext instead
5560 Sync ()
61+ // Executes next synchronization step and updates operation status.
62+ SyncContext (ctx context.Context )
5663 // Returns current sync operation state and information about resources synchronized so far.
5764 GetState () (common.OperationPhase , string , []common.ResourceSyncResult )
5865}
@@ -75,12 +82,20 @@ func WithPermissionValidator(validator common.PermissionValidator) SyncOpt {
7582}
7683
7784// WithHealthOverride sets specified health override
85+ // Deprecated: use WithHealthOverrideContext instead
7886func WithHealthOverride (override health.HealthOverride ) SyncOpt {
7987 return func (ctx * syncContext ) {
8088 ctx .healthOverride = override
8189 }
8290}
8391
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+
8499// WithInitialState sets sync operation initial state
85100func WithInitialState (phase common.OperationPhase , message string , results []common.ResourceSyncResult , startedAt metav1.Time ) SyncOpt {
86101 return func (ctx * syncContext ) {
@@ -308,11 +323,17 @@ const (
308323)
309324
310325// 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 ) {
312327 phase := common .OperationSucceeded
313328 message := obj .GetName () + " created"
314329
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+ }
316337 if err != nil {
317338 return "" , "" , err
318339 }
@@ -333,18 +354,19 @@ func (sc *syncContext) getOperationPhase(obj *unstructured.Unstructured) (common
333354}
334355
335356type 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
348370
349371 dryRun bool
350372 skipDryRun bool
@@ -403,8 +425,19 @@ func (sc *syncContext) setRunningPhase(tasks []*syncTask, isPendingDeletion bool
403425 }
404426}
405427
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
407430func (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 ) {
408441 sc .log .WithValues ("skipHooks" , sc .skipHooks , "started" , sc .started ()).Info ("Syncing" )
409442 tasks , ok := sc .getSyncTasks ()
410443 if ! ok {
@@ -441,15 +474,21 @@ func (sc *syncContext) Sync() {
441474 }) {
442475 if task .isHook () {
443476 // update the hook's result
444- operationState , message , err := sc .getOperationPhase (task .liveObj )
477+ operationState , message , err := sc .getOperationPhase (ctx , task .liveObj )
445478 if err != nil {
446479 sc .setResourceResult (task , "" , common .OperationError , fmt .Sprintf ("failed to get resource health: %v" , err ))
447480 } else {
448481 sc .setResourceResult (task , "" , operationState , message )
449482 }
450483 } else {
451484 // 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+ }
453492 if err == nil {
454493 sc .log .WithValues ("task" , task , "healthStatus" , healthStatus ).V (1 ).Info ("attempting to update health of running task" )
455494 if healthStatus == nil {
@@ -1176,8 +1215,17 @@ func (sc *syncContext) hasCRDOfGroupKind(group string, kind string) bool {
11761215 return false
11771216}
11781217
1179- // terminate looks for any running jobs/workflow hooks and deletes the resource
1218+ // Deprecated: use TerminateContext instead
11801219func (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 ) {
11811229 terminateSuccessful := true
11821230 sc .log .V (1 ).Info ("terminating" )
11831231 tasks , _ := sc .getSyncTasks ()
@@ -1190,7 +1238,7 @@ func (sc *syncContext) Terminate() {
11901238 terminateSuccessful = false
11911239 continue
11921240 }
1193- phase , msg , err := sc .getOperationPhase (task .liveObj )
1241+ phase , msg , err := sc .getOperationPhase (ctx , task .liveObj )
11941242 if err != nil {
11951243 sc .setOperationPhase (common .OperationError , fmt .Sprintf ("Failed to get hook health: %v" , err ))
11961244 return
0 commit comments