Skip to content

Initialize klog with (go)flags and add these flags to pflag as well #71

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

Merged

Conversation

christoph-jerolimov
Copy link
Contributor

@christoph-jerolimov christoph-jerolimov commented Oct 29, 2021

Changes

Just initialize klog with go flags and add these flags to pflag as well.

Fixes #7

With this change its possible to enable k8s api communication, for example:

go run cmd/shp/main.go -v=10 br list

I1029 19:15:17.183378  141455 loader.go:379] Config loaded from file: ...kubeconfig
I1029 19:15:17.184151  141455 round_trippers.go:425] curl -k -v -XGET  -H "Authorization: Bearer <masked>" -H "Accept: application/json, */*" -H "User-Agent: main/v0.0.0 (linux/amd64) kubernetes/$Format" 'https://api.crc.testing:6443/apis/shipwright.io/v1alpha1/namespaces/default/buildruns'
I1029 19:15:17.195693  141455 round_trippers.go:445] GET https://api.crc.testing:6443/apis/shipwright.io/v1alpha1/namespaces/default/buildruns 200 OK in 11 milliseconds

...

NAME                    STATUS
s2i-nodejs-build-rjtz9  Failed

Help output shows klog attributes as well as all subcommands. See -v and all these --log* arguments.

go run cmd/shp/main.go --help

Command-line client for Shipwright's Build API.

Usage:
  shp [command] [resource] [flags]
  shp [command]

Available Commands:
  build       Manage Builds
  buildrun    Manage BuildRuns
  completion  generate the autocompletion script for the specified shell
  help        Help about any command

Flags:
      --add_dir_header                   If true, adds the file directory to the header of the log messages
      --alsologtostderr                  log to standard error as well as files
      --as string                        Username to impersonate for the operation
      --as-group stringArray             Group to impersonate for the operation, this flag can be repeated to specify multiple groups.
      --cache-dir string                 Default cache directory (default "/home/christoph/.kube/cache")
      --certificate-authority string     Path to a cert file for the certificate authority
      --client-certificate string        Path to a client certificate file for TLS
      --client-key string                Path to a client key file for TLS
      --cluster string                   The name of the kubeconfig cluster to use
      --context string                   The name of the kubeconfig context to use
  -h, --help                             help for shp
      --insecure-skip-tls-verify         If true, the server's certificate will not be checked for validity. This will make your HTTPS connections insecure
      --kubeconfig string                Path to the kubeconfig file to use for CLI requests.
      --log_backtrace_at traceLocation   when logging hits line file:N, emit a stack trace (default :0)
      --log_dir string                   If non-empty, write log files in this directory
      --log_file string                  If non-empty, use this log file
      --log_file_max_size uint           Defines the maximum size a log file can grow to. Unit is megabytes. If the value is 0, the maximum file size is unlimited. (default 1800)
      --logtostderr                      log to standard error instead of files (default true)
  -n, --namespace string                 If present, the namespace scope for this CLI request
      --one_output                       If true, only write logs to their native severity level (vs also writing to each lower severity level
      --request-timeout string           The length of time to wait before giving up on a single server request. Non-zero values should contain a corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout requests. (default "0")
  -s, --server string                    The address and port of the Kubernetes API server
      --skip_headers                     If true, avoid header prefixes in the log messages
      --skip_log_headers                 If true, avoid headers when opening log files
      --stderrthreshold severity         logs at or above this threshold go to stderr (default 2)
      --tls-server-name string           Server name to use for server certificate validation. If it is not provided, the hostname used to contact the server is used
      --token string                     Bearer token for authentication to the API server
      --user string                      The name of the kubeconfig user to use
  -v, --v Level                          number for the log level verbosity
      --vmodule moduleSpec               comma-separated list of pattern=N settings for file-filtered logging

Use "shp [command] --help" for more information about a command.

Submitter Checklist

  • Includes tests if functionality changed/was added
  • Includes docs if changes are user-facing
  • Set a kind label on this PR
  • Release notes block has been filled in, or marked NONE

See the contributor guide
for details on coding conventions, github and prow interactions, and the code review process.

Release Notes

Initialize klog, this enable users to customize k8s api logging with -v and --log* arguments.

Copy link
Member

@adambkaplan adambkaplan left a comment

Choose a reason for hiding this comment

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

@jerolimov and I did a live coding session where we experimented with other kubernetes libraries to initialize klog's flags. Unfortunately this didn't work - the functions here are the best we can do. I have added a few stylistic suggestions to make the code more "go-like".

@jerolimov I think you can also add an e2e test to verify we are adding the desired flags. We use the Bats framework for running the shp command line as if it were run by an end user. You can an example of this in the run-follow test. The bats assert libraries will probably help as well.

@adambkaplan
Copy link
Member

/kind feature

@openshift-ci openshift-ci bot added the kind/feature Categorizes issue or PR as related to a new feature. label Oct 29, 2021
@christoph-jerolimov
Copy link
Contributor Author

Thanks @adambkaplan for the feedback. I've updated the PR with "go style" function names and godocs, and rebased it as well.

I've also added some bat e2e tests for the --help output in general and esp. for the -v=10 case.

Please take a new look. Thanks. 🙂

The last envvars.bats tests fails for me on main and with this PR, so I comment out the last 3 asserts in envvars.bats (only locally).

➜  cli git:(init-klog) ✗ make build test-e2e
go build -v -mod=vendor -o _output/shp ./cmd/shp/...
./test/e2e/bats/core/bin/bats --recursive test/e2e/*.bats
 ✓ shp binary can be executed
 ✓ shp --help lists all available commands
 ✓ shp --help lists some common flags
 ✓ shp --help lists also logging flags
 ✓ shp [build/buildrun] create should not error when a name is specified
 ✓ shp -v=10 build list can log the kubernetes api communication
 ✓ shp environment variable lifecycle
 ✓ shp build run follow verification

8 tests, 0 failures

/cc @coreydaley this PR doesn't update any output from the shp. So the list output (feature request: it should show the states in colors!) or errors doesn't add any timestamps. Only if you enable additional logging it shows this output with the timestamps. Sorry that I didn't picked up your earlier work, didn't know that PR on the hack event on Friday.

Copy link
Member

@adambkaplan adambkaplan left a comment

Choose a reason for hiding this comment

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

/approve

Looks good to me.

As an FYI, there has been improvement upstream in the latest version of k8s.io/component-base that simplifies initialization of klog.
We should consider refactoring to it with our next kube rebase.

See https://github.com/kubernetes/kubernetes/blob/master/staging/src/k8s.io/component-base/cli/run.go

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Nov 12, 2021

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: adambkaplan

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

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 12, 2021
@christoph-jerolimov
Copy link
Contributor Author

@adambkaplan how do we get this merged? :)

@adambkaplan
Copy link
Member

/assign @otaviof

Copy link
Member

@otaviof otaviof left a comment

Choose a reason for hiding this comment

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

Good additions to the CLI, @jerolimov! Thanks.

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Nov 30, 2021
@openshift-merge-robot openshift-merge-robot merged commit e36a2ea into shipwright-io:main Nov 30, 2021
@SaschaSchwarze0 SaschaSchwarze0 added this to the release-v0.7.0 milestone Dec 13, 2021
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. hackday kind/feature Categorizes issue or PR as related to a new feature. lgtm Indicates that a PR is ready to be merged. release-note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support verbose/granular output levels
5 participants