Skip to content
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 nil pointer error when collecting agent supportbundle fails #4306

Merged
merged 1 commit into from
Oct 20, 2022

Conversation

tnqn
Copy link
Member

@tnqn tnqn commented Oct 17, 2022

The code should only schedule file cleanup when the returned error is empty (meaning the returned supportbundle is not nil), otherwise the supportbundle file would never be deleted and the program would panic when there is any error encountered during supportbundle collection.

#2598 tried to fix it but did the opposite.

Signed-off-by: Quan Tian qtian@vmware.com

Based on #4304 and #4295's testing code.

@tnqn tnqn added action/backport Indicates a PR that requires backports. action/release-note Indicates a PR that should be included in release notes. labels Oct 17, 2022
antoninbas
antoninbas previously approved these changes Oct 17, 2022
Copy link
Contributor

@antoninbas antoninbas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I only reviewed the last commit

supportBundleStorage := supportbundle.NewAgentStorage(ovsctl.NewClient(aq.GetNodeConfig().OVSBridge), aq, npq, v4Enabled, v6Enabled)
iptables, err := iptables.New(v4Enabled, v6Enabled)
if err != nil {
return fmt.Errorf("failed to new iptables client: %v", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe "create" or "instantiate" since "new" is not a verb

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will update

require.NoError(t, err)

var collectedBundle *system.SupportBundle
assert.Eventually(t, func() bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is neat. Is this new?
should we switch to assert.Eventually for all new unit tests instead of wait.Poll?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 1.4.0. Yes, I think it can replace wait.Poll in tests.

fakeAgentQuerier.EXPECT().GetAgentInfo(&clusterinformationv1beta1.AntreaAgentInfo{}, false)
fakeIPTables.EXPECT().Save()
storage := NewAgentStorage(fakeOVSCtl, fakeAgentQuerier, fakeNetworkPolicyQuerier, fakeIPTables)
_, err := storage.SupportBundle.Create(context.TODO(), &system.SupportBundle{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I feel like it makes a bit more sense for unit test to define ctx := context.Background() at the beginning of the test case and use that context for every function that requires it. I feel like Background is more logical than TODO for tests, given that we don't intend to ever replace this context.TODO() with something else.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some tests for rest API customize context to set namespace:

ctx := request.WithNamespace(context.TODO(), tt.npNamespace)

Some tests customize context to get a chance to cancel it:

ctx, cancelFunc := context.WithCancel(context.Background())

You mean creating a global background and use it for all tests, and create child context from it if needed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not global, but for a given test case if that test case requires a context object:

func MyTest(t *testing.T) {
        ctx := context.Bakground()
        obj1.req(ctx, ...)
        obj2.req(ctx, ...)
        ctx, cancelFunc := context.WithCancel(ctx)
        defer cancelFunc()
        obj3.req(ctx, ...)
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated, thanks

@codecov
Copy link

codecov bot commented Oct 17, 2022

Codecov Report

Merging #4306 (5e96c8c) into main (c491ead) will decrease coverage by 0.70%.
The diff coverage is n/a.

❗ Current head 5e96c8c differs from pull request most recent head b9d47cd. Consider uploading reports for the commit b9d47cd to get more accurate results

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4306      +/-   ##
==========================================
- Coverage   66.28%   65.57%   -0.71%     
==========================================
  Files         369      364       -5     
  Lines       53270    52925     -345     
==========================================
- Hits        35309    34706     -603     
- Misses      15369    15652     +283     
+ Partials     2592     2567      -25     
Flag Coverage Δ
integration-tests 34.40% <0.00%> (-0.16%) ⬇️
kind-e2e-tests 48.86% <0.00%> (+0.39%) ⬆️
unit-tests 46.70% <0.00%> (-2.73%) ⬇️
Impacted Files Coverage Δ
...kg/apiserver/registry/stats/multicastgroup/rest.go 11.59% <0.00%> (-79.32%) ⬇️
...piserver/registry/stats/networkpolicystats/rest.go 57.81% <0.00%> (-37.02%) ⬇️
...stry/stats/antreaclusternetworkpolicystats/rest.go 63.07% <0.00%> (-36.93%) ⬇️
...gistry/networkpolicy/networkpolicy/subresources.go 47.05% <0.00%> (-35.30%) ⬇️
...er/registry/stats/antreanetworkpolicystats/rest.go 61.42% <0.00%> (-33.89%) ⬇️
pkg/agent/flowexporter/exporter/certificate.go 27.77% <0.00%> (-22.23%) ⬇️
...piserver/registry/controlplane/egressgroup/rest.go 66.66% <0.00%> (-20.52%) ⬇️
...server/registry/networkpolicy/addressgroup/rest.go 66.66% <0.00%> (-20.52%) ⬇️
...erver/registry/networkpolicy/networkpolicy/rest.go 66.66% <0.00%> (-20.52%) ⬇️
...rver/registry/networkpolicy/appliedtogroup/rest.go 66.66% <0.00%> (-20.52%) ⬇️
... and 55 more

@tnqn
Copy link
Member Author

tnqn commented Oct 19, 2022

/test-all

@tnqn tnqn requested a review from antoninbas October 19, 2022 16:54
antoninbas
antoninbas previously approved these changes Oct 19, 2022
Copy link
Contributor

@antoninbas antoninbas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

The code should only schedule file cleanup when the returned error is
empty (meaning the returned supportbundle is not nil), otherwise the
supportbundle file would never be deleted and the program would panic
when there is any error encountered during supportbundle collection.

antrea-io#2598 tried to fix it but did
the opposite.

Signed-off-by: Quan Tian <qtian@vmware.com>
@tnqn
Copy link
Member Author

tnqn commented Oct 20, 2022

/test-all

Copy link
Contributor

@wenyingd wenyingd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tnqn tnqn merged commit 5e722c2 into antrea-io:main Oct 20, 2022
@tnqn tnqn deleted the fix-agent-supportbundle branch October 20, 2022 09:58
hongliangl pushed a commit to hongliangl/antrea that referenced this pull request Oct 22, 2022
…ea-io#4306)

The code should only schedule file cleanup when the returned error is
empty (meaning the returned supportbundle is not nil), otherwise the
supportbundle file would never be deleted and the program would panic
when there is any error encountered during supportbundle collection.

antrea-io#2598 tried to fix it but did
the opposite.

Signed-off-by: Quan Tian <qtian@vmware.com>
heanlan pushed a commit to heanlan/antrea that referenced this pull request Mar 29, 2023
…ea-io#4306)

The code should only schedule file cleanup when the returned error is
empty (meaning the returned supportbundle is not nil), otherwise the
supportbundle file would never be deleted and the program would panic
when there is any error encountered during supportbundle collection.

antrea-io#2598 tried to fix it but did
the opposite.

Signed-off-by: Quan Tian <qtian@vmware.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
action/backport Indicates a PR that requires backports. action/release-note Indicates a PR that should be included in release notes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants