@@ -72,11 +72,18 @@ func (m *ResourceManager) Wait(objects []*unstructured.Unstructured, opts WaitOp
7272 return m .WaitForSet (objectsMeta , opts )
7373}
7474
75- // WaitForSet checks if the given set of FmtObjMetadata has been fully reconciled.
75+ // WaitForSet checks if the given ObjMetadataSet has been fully reconciled.
7676func (m * ResourceManager ) WaitForSet (set object.ObjMetadataSet , opts WaitOptions ) error {
77+ return m .WaitForSetWithContext (context .Background (), set , opts )
78+ }
79+
80+ // WaitForSetWithContext checks if the given ObjMetadataSet has been fully reconciled.
81+ // The provided context can be used to cancel the operation.
82+ func (m * ResourceManager ) WaitForSetWithContext (ctx context.Context , set object.ObjMetadataSet , opts WaitOptions ) error {
7783 statusCollector := collector .NewResourceStatusCollector (set )
84+ canceledInternally := false
7885
79- ctx , cancel := context .WithTimeout (context . Background () , opts .Timeout )
86+ ctx , cancel := context .WithTimeout (ctx , opts .Timeout )
8087 defer cancel ()
8188
8289 pollingOpts := polling.PollOptions {
@@ -110,6 +117,7 @@ func (m *ResourceManager) WaitForSet(set object.ObjMetadataSet, opts WaitOptions
110117 desired := status .CurrentStatus
111118 aggStatus := aggregator .AggregateStatus (rss , desired )
112119 if aggStatus == desired || (opts .FailFast && countFailed > 0 ) {
120+ canceledInternally = true
113121 cancel ()
114122 return
115123 }
@@ -118,6 +126,11 @@ func (m *ResourceManager) WaitForSet(set object.ObjMetadataSet, opts WaitOptions
118126
119127 <- done
120128
129+ // If the context was cancelled externally, return early.
130+ if ! canceledInternally && errors .Is (ctx .Err (), context .Canceled ) {
131+ return ctx .Err ()
132+ }
133+
121134 if statusCollector .Error != nil {
122135 return statusCollector .Error
123136 }
@@ -127,15 +140,9 @@ func (m *ResourceManager) WaitForSet(set object.ObjMetadataSet, opts WaitOptions
127140 switch {
128141 case rs == nil || lastStatus [id ] == nil :
129142 errs = append (errs , fmt .Sprintf ("can't determine status for %s" , utils .FmtObjMetadata (id )))
130- case lastStatus [id ].Status == status .FailedStatus :
131- var builder strings.Builder
132- builder .WriteString (fmt .Sprintf ("%s status: '%s'" ,
133- utils .FmtObjMetadata (rs .Identifier ), lastStatus [id ].Status ))
134- if rs .Error != nil {
135- builder .WriteString (fmt .Sprintf (": %s" , rs .Error ))
136- }
137- errs = append (errs , builder .String ())
138- case errors .Is (ctx .Err (), context .DeadlineExceeded ) && lastStatus [id ].Status != status .CurrentStatus :
143+ case lastStatus [id ].Status == status .FailedStatus ,
144+ errors .Is (ctx .Err (), context .DeadlineExceeded ) &&
145+ lastStatus [id ].Status != status .CurrentStatus :
139146 var builder strings.Builder
140147 builder .WriteString (fmt .Sprintf ("%s status: '%s'" ,
141148 utils .FmtObjMetadata (rs .Identifier ), lastStatus [id ].Status ))
0 commit comments