-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Add skaffold internal error and return that instead of user cancelled #6846
Merged
tejal29
merged 10 commits into
GoogleContainerTools:main
from
tejal29:fix_user_cancelled_sc
Nov 15, 2021
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fda2a58
fix: Add skaffold internal error and return that instead of user canc…
tejal29 3bf856f
change logic to detect skaffold deplpy status
tejal29 a991781
fix lint
tejal29 f6cdeed
rever intended change
tejal29 5e5a547
fix tests
tejal29 60127d3
fix race condition between deployment recorded as successful and pod …
tejal29 0b072af
fix lint
tejal29 1d2c886
fix struct lint
tejal29 221c085
code review comments
tejal29 05a8b52
fix lint size of struct
tejal29 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,9 +67,10 @@ const ( | |
) | ||
|
||
type counter struct { | ||
total int | ||
pending int32 | ||
failed int32 | ||
total int | ||
pending int32 | ||
failed int32 | ||
cancelled int32 | ||
} | ||
|
||
type Config interface { | ||
|
@@ -220,17 +221,19 @@ func (s *monitor) statusCheck(ctx context.Context, out io.Writer) (proto.StatusC | |
|
||
ctx, cancel := context.WithCancel(ctx) | ||
defer cancel() | ||
var exitStatusCode proto.StatusCode | ||
|
||
for _, d := range resources { | ||
wg.Add(1) | ||
go func(r *resource.Resource) { | ||
defer wg.Done() | ||
// keep updating the resource status until it fails/succeeds/times out | ||
pollResourceStatus(ctx, s.cfg, r) | ||
rcCopy := c.markProcessed(r.Status().Error()) | ||
rcCopy := c.markProcessed(ctx, r.StatusCode()) | ||
s.printStatusCheckSummary(out, r, rcCopy) | ||
// if one deployment fails, cancel status checks for all deployments. | ||
if r.Status().Error() != nil && r.StatusCode() != proto.StatusCode_STATUSCHECK_USER_CANCELLED { | ||
// if one resource fails, cancel status checks for all resources. | ||
if resourceFailed(r.StatusCode()) { | ||
exitStatusCode = r.StatusCode() | ||
cancel() | ||
} | ||
}(d) | ||
|
@@ -243,8 +246,7 @@ func (s *monitor) statusCheck(ctx context.Context, out io.Writer) (proto.StatusC | |
|
||
// Wait for all deployment statuses to be fetched | ||
wg.Wait() | ||
cancel() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a |
||
return getSkaffoldDeployStatus(c, resources) | ||
return getSkaffoldDeployStatus(c, exitStatusCode) | ||
} | ||
|
||
func getStandalonePods(ctx context.Context, client kubernetes.Interface, ns string, l *label.DefaultLabeller, deadlineDuration time.Duration) ([]*resource.Resource, error) { | ||
|
@@ -382,18 +384,21 @@ func pollResourceStatus(ctx context.Context, cfg kubectl.Config, r *resource.Res | |
} | ||
} | ||
|
||
func getSkaffoldDeployStatus(c *counter, rs []*resource.Resource) (proto.StatusCode, error) { | ||
func getSkaffoldDeployStatus(c *counter, sc proto.StatusCode) (proto.StatusCode, error) { | ||
// return overall code cancelled if status check for all resources was cancelled | ||
if int(c.cancelled) == c.total && c.total > 0 { | ||
return proto.StatusCode_STATUSCHECK_USER_CANCELLED, fmt.Errorf("status check cancelled") | ||
} | ||
// return success if no failures find. | ||
if c.failed == 0 { | ||
return proto.StatusCode_STATUSCHECK_SUCCESS, nil | ||
} | ||
// construct an error message and return appropriate error code | ||
err := fmt.Errorf("%d/%d deployment(s) failed", c.failed, c.total) | ||
for _, r := range rs { | ||
if r.StatusCode() != proto.StatusCode_STATUSCHECK_SUCCESS && | ||
r.StatusCode() != proto.StatusCode_STATUSCHECK_USER_CANCELLED { | ||
return r.StatusCode(), err | ||
} | ||
if sc == proto.StatusCode_STATUSCHECK_SUCCESS || sc == 0 { | ||
return proto.StatusCode_STATUSCHECK_INTERNAL_ERROR, err | ||
} | ||
return proto.StatusCode_STATUSCHECK_USER_CANCELLED, err | ||
return sc, err | ||
} | ||
|
||
func getDeadline(d int) time.Duration { | ||
|
@@ -483,8 +488,12 @@ func newCounter(i int) *counter { | |
} | ||
} | ||
|
||
func (c *counter) markProcessed(err error) counter { | ||
if err != nil && err != context.Canceled { | ||
func (c *counter) markProcessed(ctx context.Context, sc proto.StatusCode) counter { | ||
if resourceCancelled(sc) { | ||
log.Entry(ctx).Debug("marking resource status check cancelled", sc) | ||
atomic.AddInt32(&c.cancelled, 1) | ||
} else if resourceFailed(sc) { | ||
log.Entry(ctx).Debugf("marking resource failed due to error code %s", sc) | ||
atomic.AddInt32(&c.failed, 1) | ||
} | ||
atomic.AddInt32(&c.pending, -1) | ||
|
@@ -493,12 +502,21 @@ func (c *counter) markProcessed(err error) counter { | |
|
||
func (c *counter) copy() counter { | ||
return counter{ | ||
total: c.total, | ||
pending: c.pending, | ||
failed: c.failed, | ||
total: c.total, | ||
pending: c.pending, | ||
failed: c.failed, | ||
cancelled: c.cancelled, | ||
} | ||
} | ||
|
||
func resourceFailed(sc proto.StatusCode) bool { | ||
return sc != proto.StatusCode_STATUSCHECK_SUCCESS && sc != proto.StatusCode_STATUSCHECK_USER_CANCELLED | ||
} | ||
|
||
func resourceCancelled(sc proto.StatusCode) bool { | ||
return sc == proto.StatusCode_STATUSCHECK_USER_CANCELLED | ||
} | ||
|
||
type NoopMonitor struct { | ||
status.NoopMonitor | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move the logic here and in
getSkaffoldDeployStatus()
intocounter
? This scattering of logic is awkward:markProcessed
can check ifctx.Err() == context.Canceled
to determine if the user cancelledmarkProcessed
return(counter, failed bool)
to replace theresourceFailed()
check on line 235counter
return c.result()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't use
markProcessed can check if ctx.Err() == context.Canceled to determine if the user cancelled
since, we fail fast and cancel context if a deployment failed.So at this point, its difficult to detect if a user hit cntrl C Vs, code cancelling all the status check for all resources.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had suggestions 2-4 implemented but then the unit tests were becoming more complex. Maybe the
counter
need to be renamed todeployState
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, so i remember, why we cant have counter store the
exitStatusCode
. The counter gets updated in a go routine.The exit code is stored in
resources
. It makes sense to declare another mutex to update the exit status code safely.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok,
I am addressed 1 and 2 from the suggestions.
For 3, 4:
checkStatus
and updated the code to only capture first failure.getDeployStatus
as is.