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

Capture the logs from stderr of custom plugins #442

Merged

Conversation

abansal4032
Copy link
Contributor

The custom plugins : health-checker and log-counter currently log to stderr and the logs are not captured in NPD logs. This change allows to read the stderr from the custom plugin and log the output.

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 16, 2020
@k8s-ci-robot
Copy link
Contributor

Hi @abansal4032. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Jul 16, 2020
stdout, err := cmd.Output()
if err != nil {
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
Copy link
Member

Choose a reason for hiding this comment

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

I have some concern about this, especially stderr. If the stderr is too big, it could potentially cause high memory usage.

Can we implement a limit reader to limit the output size, and discard other outputs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a limit reader for stdout and stderr limiting each to 4k bytes.

@abansal4032 abansal4032 force-pushed the custom-plugin-logs-capture branch from e27b575 to 143e5d4 Compare July 24, 2020 18:05
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jul 24, 2020
@abansal4032 abansal4032 force-pushed the custom-plugin-logs-capture branch from 143e5d4 to ec0fe73 Compare July 25, 2020 06:32
glog.Errorf("Error in starting plugin %q: error - %v", rule.Path, err)
return cpmtypes.Unknown, "Error in starting plugin. Please check the error log"
}

Copy link
Member

Choose a reason for hiding this comment

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

Should we read stdout and stderr at the same time? Or else, the stderr side may potentially block the plugin process when the buffer is full?

And we should probably continue reading the output and discard them after reaching the limit, or else the plugin process may be blocked as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. Reading from stdout and stderr at the same time.
Also added discard once we have read the maxBytes.

@abansal4032 abansal4032 force-pushed the custom-plugin-logs-capture branch 2 times, most recently from 955808e to c44eb5f Compare July 29, 2020 16:15
return cpmtypes.Unknown, "Error in starting plugin. Please check the error log"
}

var wg sync.WaitGroup
Copy link
Member

Choose a reason for hiding this comment

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

nit:

var (
  stdout []byte
  stderr []byte
  ...
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

// log the stderr from the plugin
if len(stderr) != 0 {
glog.Infof("Start logs from plugin %q \n %s\n", rule.Path, string(stderr))
glog.Infof("End logs from plugin %q\n", rule.Path)
Copy link
Member

Choose a reason for hiding this comment

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

Do we need new line for this log line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the redundant line. Did not realize Infof appends that automatically.

stderr, stderrErr = readFromReader(stderrPipe, maxCustomPluginBufferBytes)
wg.Done()
}()
wg.Wait()
Copy link
Member

Choose a reason for hiding this comment

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

It seems that this may block forever.

In theory, it should not happen, because the command should be stopped after timeout, and the IO should be closed.

However, I'm not completely sure about that. Maybe we can call wg.Wait after Wait the command to stop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The documentation for stdoutpipe specifies: https://golang.org/src/os/exec/exec.go?s=17733:17782#L609
It is incorrect to call Wait before all reads from the pipe have completed.

Having the wg.Wait() after will call exec.Wait before we are done reading from pipes?

@Random-Liu
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 29, 2020
@abansal4032 abansal4032 force-pushed the custom-plugin-logs-capture branch 2 times, most recently from 899d0f7 to d0ab163 Compare July 29, 2020 18:45
@abansal4032 abansal4032 force-pushed the custom-plugin-logs-capture branch from d0ab163 to 6acf5b1 Compare July 29, 2020 18:57
@Random-Liu
Copy link
Member

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 29, 2020
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: abansal4032, Random-Liu

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 29, 2020
@k8s-ci-robot k8s-ci-robot merged commit f3ab10e into kubernetes:master Jul 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants