k3s/pkg/kubectl
Kubernetes Submit Queue 8f7eb4e6ac
Merge pull request #61713 from mountkin/fix-cli-run-panic
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

kubectl: fix a panic when createGeneratedObject failed

**What this PR does / why we need it**:
This PR fixed two bugs in the `kubectl run` command.
The first commit fixed a panic that can be reproduced with the following two commands:
```bash
kubectl run --image=alpine hello -- sleep 1d
kubectl run --image=alpine --stdin --tty --attach hello -- /bin/sh
```
When the panic happens, the stack trace is as the following: 
```
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x12f92e6]

goroutine 1 [running]:
k8s.io/kubernetes/pkg/kubectl/cmd.RunRun(0x1890d20, 0xc4207f6210, 0x184e700, 0xc42000c010, 0x184e720, 0xc42000c018, 0x184e720, 0xc42000c020, 0xc42052ef00, 0xc42054f980, ...)
	/home/star/go/src/k8s.io/kubernetes/_output/local/go/src/k8s.io/kubernetes/pkg/kubectl/cmd/run.go:328 +0x10b6
k8s.io/kubernetes/pkg/kubectl/cmd.NewCmdRun.func1(0xc42052ef00, 0xc42054f980, 0x2, 0x8)
	/home/star/go/src/k8s.io/kubernetes/_output/local/go/src/k8s.io/kubernetes/pkg/kubectl/cmd/run.go:105 +0x144
...
```
The reason for the panic is that the original code didn't handle the error returned by `createGeneratedObject` correctly. See https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/cmd/run.go#L281
If the function `createGeneratedObject` fails, the returned `runObject` will be `nil`.  At https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/cmd/run.go#L328 a nil pointer dereference will happen.

The second problem this PR fixed is a resource leak if both `--rm` and `--expose` flags are provided. The original code used a map (https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/cmd/run.go#L280) to trace the created objects. But at https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/cmd/run.go#L296 the code will overwrite the previously created object with the same key. So at https://github.com/kubernetes/kubernetes/blob/master/pkg/kubectl/cmd/run.go#L348 only the service will be deleted. The deployment will be left behind.

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
Fixes #

**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```
/kind bug
/sig cli
2018-03-30 13:19:31 -07:00
..
apply Merge pull request #60947 from fanzhangio/replace 2018-03-30 11:53:07 -07:00
apps Autogenerated: hack/update-bazel.sh 2018-02-16 13:43:01 -08:00
categories Autogenerated: hack/update-bazel.sh 2018-02-16 13:43:01 -08:00
cmd Merge pull request #61713 from mountkin/fix-cli-run-panic 2018-03-30 13:19:31 -07:00
explain escape literal percent sign when formatting 2018-03-23 12:59:50 +08:00
metricsutil kubectl: Use metrics-server for kubectl top commands 2018-01-22 21:45:06 +01:00
plugins Autogenerated: hack/update-bazel.sh 2018-02-16 13:43:01 -08:00
proxy Autogenerated: hack/update-bazel.sh 2018-02-16 13:43:01 -08:00
resource Merge pull request #58787 from Lion-Wei/kubectl-3 2018-03-27 04:08:15 -07:00
scheme remove unneeded factory codec methods 2018-02-21 15:20:20 -05:00
util Add missing binaryData field to the ConfigMap Hash 2018-03-13 22:08:06 -04:00
validation Autogenerated: hack/update-bazel.sh 2018-02-16 13:43:01 -08:00
.import-restrictions Move scheduler code out of plugin directory. 2018-01-05 15:05:01 -08:00
BUILD removes custom scalers from kubectl 2018-03-26 19:33:42 +02:00
OWNERS
apply.go remove internal version api from apply 2017-12-11 13:49:39 +05:30
autoscale.go refactor kubectl autoscale to use the new generator 2017-11-20 14:12:20 +08:00
autoscale_test.go refactor kubectl autoscale to use the new generator 2017-11-20 14:12:20 +08:00
bash_comp_utils.go
clusterrolebinding.go
clusterrolebinding_test.go
conditions.go Remove pkg/client/unversioned 2018-02-21 13:08:11 +01:00
configmap.go Add support for binary file in configmap 2018-01-23 07:28:34 -05:00
configmap_test.go Add support for binary file in configmap 2018-01-23 07:28:34 -05:00
delete.go make reapers tolerate 404s on scaling down 2018-03-28 08:25:21 -04:00
delete_test.go make reapers tolerate 404s on scaling down 2018-03-28 08:25:21 -04:00
deployment.go
deployment_test.go
doc.go
env_file.go fix all the typos across the project 2018-02-11 11:04:14 +08:00
env_file_test.go
generate.go
generate_test.go
history.go Update versioned portions of kubectl to use apps/v1 with DaemonSet 2018-02-23 08:06:42 -08:00
history_test.go Refactor HistoryViewerFor to use Visitor design pattern 2017-11-09 08:11:19 +00:00
interfaces.go
kubectl.go Document that endpoints is only plural in resource aliases 2018-03-14 16:02:51 -07:00
namespace.go Remove kubectl create namespace dependencies on kubernetes/pkg/api 2017-10-25 20:26:11 +08:00
namespace_test.go Remove unused code in UT files in pkg/ 2018-01-15 16:02:35 +08:00
pdb.go Print type representation on errors when checking parameter types 2017-11-01 10:36:17 +01:00
pdb_test.go Print type representation on errors when checking parameter types 2017-11-01 10:36:17 +01:00
priorityclass.go add create subcommand for priorityclass 2017-11-07 15:03:01 +08:00
priorityclass_test.go add create subcommand for priorityclass 2017-11-07 15:03:01 +08:00
quota.go switch some commands to use its own scheme 2017-10-27 18:31:42 -07:00
quota_test.go switch some commands to use its own scheme 2017-10-27 18:31:42 -07:00
rolebinding.go switch some commands to use its own scheme 2017-10-27 18:31:42 -07:00
rolebinding_test.go switch some commands to use its own scheme 2017-10-27 18:31:42 -07:00
rollback.go Update versioned portions of kubectl to use apps/v1 with DaemonSet 2018-02-23 08:06:42 -08:00
rollback_test.go Replace type switches in Rollback with Visitor pattern 2017-11-19 17:46:57 +00:00
rolling_updater.go removes custom scalers from kubectl 2018-03-26 19:33:42 +02:00
rolling_updater_test.go Remove unused code in UT files in pkg/ 2018-01-15 16:02:35 +08:00
rollout_status.go use extensions client with explicit version 2017-11-22 21:18:14 +08:00
rollout_status_test.go Replace some occurances of kubernetes internal api types in kubectl 2017-11-08 18:27:45 +01:00
run.go Merge pull request #54554 from zjj2wry/set-dep 2017-11-11 12:44:30 -08:00
run_test.go
scale.go make reapers tolerate 404s on scaling down 2018-03-28 08:25:21 -04:00
scale_test.go make reapers tolerate 404s on scaling down 2018-03-28 08:25:21 -04:00
secret.go switch some commands to use its own scheme 2017-10-27 18:31:42 -07:00
secret_for_docker_registry.go add --from-file flag to docker-registry secret 2018-03-21 11:13:11 -04:00
secret_for_docker_registry_test.go Fix problem accessing private docker registries 2017-12-20 12:37:40 -05:00
secret_for_tls.go switch some commands to use its own scheme 2017-10-27 18:31:42 -07:00
secret_for_tls_test.go switch some commands to use its own scheme 2017-10-27 18:31:42 -07:00
secret_test.go switch some commands to use its own scheme 2017-10-27 18:31:42 -07:00
service.go remove dependency from service generator 2017-10-31 15:35:36 +08:00
service_basic.go Make sure the ports is valid when parsing portString 2017-11-13 20:46:05 +08:00
service_basic_test.go Make sure the ports is valid when parsing portString 2017-11-13 20:46:05 +08:00
service_test.go remove dependency from service generator 2017-10-31 15:35:36 +08:00
serviceaccount.go switch some commands to use its own scheme 2017-10-27 18:31:42 -07:00
serviceaccount_test.go switch some commands to use its own scheme 2017-10-27 18:31:42 -07:00
sorting_printer.go
sorting_printer_test.go pkg/api/legacyscheme: fixup imports 2017-10-18 17:23:55 +02:00