Commit Graph

32 Commits

Author SHA1 Message Date
Maciej Szulik 69682b75e5
Manually read verbosity before kubectl command construction
kubectl command construction is slowly getting more functionality which
sometimes requires to log certain actions. Currently we parse the
verbosity only when actually running the command, so all of construction
code is not able to use -v=5. This commit adds the manual parsing and
loglevel setting berore we even start creating the kubectl command.

Signed-off-by: Maciej Szulik <soltysh@gmail.com>
2025-05-08 16:47:29 +02:00
Patrick Ohly a5d2d6fec3 cli: let kubectl handle error printing
cli.Run was an attempt to elliminate error handling in Kubernetes
commands. However, it had to rely on heuristics that are not necessarily right
for all commands.

kubectl is one example which has its own error printing code that should be
used in all cases after a command failure. It now gets used also for
`--warnings-as-errors`. Previously, that caused the following message to be
logged at the end:

  E0110 16:56:01.987555  202060 run.go:120] "command failed" err="1 warning received"

Now it ends with:

 error: 1 warning received
2022-01-10 17:09:30 +01:00
Patrick Ohly 00e4a599f6 command lines: always show flags with hyphens
All Kubernetes commands should show flags with hyphens in their help text even
when the flag originally was defined with underscore. Converting a command to
this style is not breaking its command line API because the old-style parameter
with underscore is accepted as alias.

The easiest solution to achieve this is to set normalization shortly before
running the command in the new central cli.Run or the few places where that
function isn't used yet.

There may be some texts which depends on normalization at flag definition time,
like the --logging-format usage warning. Those get generated assuming that
hyphens will be used.
2021-09-30 13:46:49 +02:00
Patrick Ohly 21d1bcd6b8 initialize logging after flag parsing
It wasn't documented that InitLogs already uses the log flush frequency, so
some commands have called it before parsing (for example, kubectl in the
original code for logs.go). The flag never had an effect in such commands.

Fixing this turned into a major refactoring of how commands set up flags and
run their Cobra command:

- component-base/logs: implicitely registering flags during package init is an
  anti-pattern that makes it impossible to use the package in commands which
  want full control over their command line. Logging flags must be added
  explicitly now, something that the new cli.Run does automatically.

- component-base/logs: AddFlags would have crashed in kubectl-convert if it
  had been called because it relied on the global pflag.CommandLine. This
  has been fixed and kubectl-convert now has the same --log-flush-frequency
  flag as other commands.

- component-base/logs/testinit: an exception are tests where flag.CommandLine has
  to be used. This new package can be imported to add flags to that
  once per test program.

- Normalization of the klog command line flags was inconsistent. Some commands
  unintentionally didn't normalize to the recommended format with hyphens. This
  gets fixed for sample programs, but not for production programs because
  it would be a breaking change.

This refactoring has the following user-visible effects:

- The validation error for `go run ./cmd/kube-apiserver --logging-format=json
  --add-dir-header` now references `add-dir-header` instead of `add_dir_header`.

- `staging/src/k8s.io/cloud-provider/sample` uses flags with hyphen instead of
  underscore.

- `--log-flush-frequency` is not listed anymore in the --logging-format flag's
  `non-default formats don't honor these flags` usage text because it will also
  work for non-default formats once it is needed.

- `cmd/kubelet`: the description of `--logging-format` uses hyphens instead of
  underscores for the flags, which now matches what the command is using.

- `staging/src/k8s.io/component-base/logs/example/cmd`: added logging flags.

- `apiextensions-apiserver` no longer prints a useless stack trace for `main`
  when command line parsing raises an error.
2021-09-30 13:46:49 +02:00
Maciej Szulik 9b8247e5dd
Move the remaining kubectl bits to k8s.io/kubectl 2020-11-06 12:04:39 +01:00
Kubernetes Prow Robot dd8a000a77
Merge pull request #79493 from odinuge/kube-cmd-double-print
Remove duplicate error messages from cli commands
2019-06-28 13:57:41 -07:00
Odin Ugedal 7caf51daa4
Remove duplicate error messages from cli commands
Since we never use the cobras "SilenceErrors" or "SilenceUsage",
a command executed with "cmd.Execute()" will never return an error
without printing it.

The current behavior results in all error messages being printed twice:

Example:

$ kubectl abc
Error: unknown command "abc" for "kubectl"
Run 'kubectl --help' for usage.
unknown command "abc" for "kubectl"

This applies to all cli commands using Cobra. To verify, follow the code
path of the Execute function:

https://github.com/spf13/cobra/blob/c439c4fa0937/command.go#L793
Signed-off-by: Odin Ugedal <odin@ugedal.com>
2019-06-27 21:55:14 +02:00
Mike Dame a710a83aa9 Move pkg/util/logs to staging and update ref 2019-06-27 15:15:55 -04:00
Marek Counts 7744f90830 Moved flag and globalflag
Moved all flag code from `staging/src/k8s.io/apiserver/pkg/util/[flag|globalflag]` to `component-base/cli/[flag|globalflag]` except for the term function because of unwanted dependencies.
2019-02-15 10:28:13 -05:00
Mikhail Mazurskiy 3a243090a5
Simplify random seed initialization
There is no need to set the time zone as the result does not
depend on it
2018-10-11 21:01:15 +11:00
xuzhonghu 46d8cf23ef kubectl use its own logs 2018-05-22 11:35:38 +08:00
Mike Danese e8d405a0ed auth: reregister auth providers
Fixes: 5d721bff81

That PR unregistered auth providers for kubectl and probably elsewhere.
2018-02-26 09:08:41 -08:00
David Eads e48847e6ce switch hyper to cobra 2018-01-24 11:02:19 -05:00
David Eads d7ddcca231 low hanging fruit for using cobra commands 2018-01-17 13:43:43 -05:00
huzhifeng 2b3a871df5
Add fmt.printf for other need
Signed-off-by: huzhifeng <huzhifeng@douyu.tv>
2017-08-18 16:07:56 +08:00
Lucas Käldström 88ea80b572 Remove GOMAXPROCS() calls because they are unnecessary 2016-07-19 11:08:21 +03:00
David McMahon ef0c9f0c5b Remove "All rights reserved" from all the headers. 2016-06-29 17:47:36 -07:00
Brendan Burns 19c582c2b1 Add kubectl to the hyperkube binary 2015-11-12 22:18:16 -08:00
Mike Danese 8e33cbfa28 rewrite go imports 2015-08-05 17:30:03 -07:00
Eric Paris 6b3a6e6b98 Make copyright ownership statement generic
Instead of saying "Google Inc." (which is not always correct) say "The
Kubernetes Authors", which is generic.
2015-05-01 17:49:56 -04:00
deads2k 6344cf3c3a refactor to move kubectl.cmd.Factory to kubect/cmd/util 2015-04-07 14:43:50 -04:00
Andy Goldstein 5bd0e9ab05 Add streaming command execution & port forwarding
Add streaming command execution & port forwarding via HTTP connection
upgrades (currently using SPDY).
2015-02-20 09:57:02 -05:00
Daniel Smith fcddefa184 Set GOMAXPROCS. Print errors on stderr. 2015-02-19 10:30:31 -08:00
deads2k 500bb3a7fa make kubectl factory flag binding optional 2015-01-20 18:12:24 -05:00
Clayton Coleman 831557408b kubectl is double printing errors
cobra/command prints errs that are returned by cmd.Execute(), so
printing it twice does not accomplish anything.
2015-01-14 20:11:51 -05:00
Clayton Coleman 8a4f225941 Make expandResourceShortcuts part of RESTMapper on client 2015-01-09 11:59:11 -05:00
deads2k 480635bb72 Revert "Revert "add kubeconfig types""
This reverts commit 02dbad7094.
2015-01-08 09:42:30 -05:00
Brendan Burns 02dbad7094 Revert "add kubeconfig types" 2015-01-07 12:39:37 -08:00
deads2k 0e688dc271 add kubeconfig file 2015-01-07 13:09:10 -05:00
deads2k 2dbfb80349 add utility for binding flags and building api server clients 2014-11-26 07:50:51 -05:00
Clayton Coleman 09cfa364c5 Refactor Get and Describe to allow extension of types
Get should use ResourceMapper, allow Printer to be abstracted,
and extract Describe as *Describer types.
2014-11-04 10:44:56 -05:00
Sam Ghods 4b220f8b0a kubectl: kubecfg rewrite for better modularity and improved UX 2014-10-15 15:29:54 -07:00