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
This commit is contained in:
Patrick Ohly
2021-12-15 10:44:39 +01:00
parent 8fc7a9bce9
commit a5d2d6fec3
3 changed files with 59 additions and 39 deletions

View File

@@ -17,10 +17,9 @@ limitations under the License.
package main
import (
"os"
"k8s.io/component-base/cli"
"k8s.io/kubectl/pkg/cmd"
"k8s.io/kubectl/pkg/cmd/util"
// Import to initialize client auth plugins.
_ "k8s.io/client-go/plugin/pkg/client/auth"
@@ -28,6 +27,8 @@ import (
func main() {
command := cmd.NewDefaultKubectlCommand()
code := cli.Run(command)
os.Exit(code)
if err := cli.RunNoErrOutput(command); err != nil {
// Pretty-print the error and exit with an error.
util.CheckErr(err)
}
}