Files
kubernetes/pkg/kubectl/cmd
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
..
2017-10-15 18:18:13 -07:00
2018-02-21 08:15:22 -05:00
2017-08-16 12:27:36 -07:00
2018-03-26 19:33:42 +02:00
2018-02-21 08:15:22 -05:00
2018-01-05 21:04:44 -06:00
2018-03-13 16:21:30 -04:00
2017-12-25 16:23:38 +08:00
2017-12-25 16:23:38 +08:00
2017-12-25 16:23:38 +08:00
2017-12-25 16:23:38 +08:00
2017-12-25 16:23:38 +08:00
2017-12-25 16:23:38 +08:00
2018-03-26 15:40:40 +08:00
2018-03-01 16:10:04 +08:00
2018-03-17 15:08:26 +08:00
2018-01-25 16:10:31 +01:00
2017-12-25 16:23:38 +08:00
2017-08-16 12:27:36 -07:00
2017-12-25 16:23:38 +08:00
2017-12-25 16:23:38 +08:00
2017-08-16 12:27:36 -07:00