Repository: kubernetes/autoscaler (Go)

Issue:
vpa-updater fails in `CrashLoopBackoff` with NPE when Pods have `ownerReference` to a controller which doesn't have a `/scale` subresource in VPA 1.1.0
**Which component are you using?**: vertical-pod-autoscaler

<!--
Which autoscaling component hosted in this repository (cluster-autoscaler, vertical-pod-autoscaler, addon-resizer, helm charts) is the bug in?
-->

**What version of the component are you using?**: 1.1.0

**What did you expect to happen?**: vpa-updater doing its job

<!--
What behaviour did you expect to see?
-->

**What happened instead?**: vpa-updater in `CrashLoopBackoff` because a Pod was found which has an `ownerReference` to a Controller which doesn't implement the `/scale` subresource.

Here is the VPA log (note I've patched the `vpa-updater` to include a line of debug output to catch the `nil` controller being returned by `ctrlFetcher.FindTopMostWellKnownOrScalable(k)`

```
│ I0415 16:03:40.193431       1 updater.go:246] Rate limit disabled                                                                                                                                                                                                                     │
│ I0415 16:03:40.699405       1 api.go:94] Initial VPA synced successfully                                                                                                                                                                                                              │
│ E0415 16:04:40.715558       1 api.go:153] fail to get pod controller: pod=kube-controller-manager-gardener-local-control-plane err=Unhandled targetRef v1 / Node / gardener-local-control-plane, last error node is not a valid owner                                                 │
│ E0415 16:04:40.717288       1 api.go:157] got nil controller: pod=machine-shoot--local--e2e-default-local2-5699c-htb7b                                                                                                                                                                │
│ panic: runtime error: invalid memory address or nil pointer dereference                                                                                                                                                                                                               │
│ [signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x10b46b8]                                                                                                                                                                                                              │
│                                                                                                                                                                                                                                                                                       │
│ goroutine 1 [running]:                                                                                                                                                                                                                                                                │
│ k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/vpa.GetControllingVPAForPod(0x40009d4008, {0x4000175800, 0x1c, 0x0?}, {0x17a91a0, 0x40000b4460})                                                                                                                                  │
│     /gopath/src/k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/vpa/api.go:167 +0x3b8                                                                                                                                                                                             │
│ k8s.io/autoscaler/vertical-pod-autoscaler/pkg/updater/logic.(*updater).RunOnce(0x4000170a50, {0x17c4990, 0x40001777a0})                                                                                                                                                               │
│     /gopath/src/k8s.io/autoscaler/vertical-pod-autoscaler/pkg/updater/logic/updater.go:175 +0x884                                                                                                                                                                                     │
│ main.main()                                                                                                                                                                                                                                                                           │
│     /gopath/src/k8s.io/autoscaler/vertical-pod-autoscaler/pkg/updater/main.go:127 +0x6bc
```

Pod `machine-shoot--local--e2e-default-local2-5699c-htb7b ` has this `ownerReference` to the kind `machine`, [which is a CRD that _doesn't_ implement `/scale`](https://github.com/gardener/machine-controller-manager/blob/master/kubernetes/crds/machine.sapcloud.io_machines.yaml#L284-L285)

```
  ownerReferences:
  - apiVersion: machine.sapcloud.io/v1alpha1
    controller: true
    kind: Machine
    name: shoot--local--e2e-default-local2-5699c-htb7b
```

<!--
What behaviour did see instead?
-->

**How to reproduce it (as minimally and precisely as possible)**: 

<!--
If possible, provide a recipe for reproducing the error.
A detailed sequence of steps describing what to do to observe the issue is good.
A complete runnable bash shell script is best.
-->

**Anything else we need to know?**: This is a pretty big problem for any user of the VPA. vpa-updater doesn't just iterate over Pods under VPA control,[ it iterates _over all Pods_](https://github.com/kubernetes/autoscaler/blob/b5cd900da89cf582c6487da3dba8d8a814933233/vertical-pod-autoscaler/pkg/updater/logic/updater.go#L165-L175), therefore chances are pretty big that there might be Pods owned by a resource that doesn't define `/scale`. **This will most likely break lots of existing systems.**

<!--
Is there anything else you think we should know? Configuration of the component (be careful what you post here if so)? Relevant logs?
-->


Discussion: /cc @dbenque @kwiesmueller 
/area vertical-pod-autoscaler



Fix (patch):
diff --git a/vertical-pod-autoscaler/pkg/utils/vpa/api.go b/vertical-pod-autoscaler/pkg/utils/vpa/api.go
index d6b9fbc22e5e..529d76961727 100644
--- a/vertical-pod-autoscaler/pkg/utils/vpa/api.go
+++ b/vertical-pod-autoscaler/pkg/utils/vpa/api.go
@@ -153,6 +153,9 @@ func GetControllingVPAForPod(pod *core.Pod, vpas []*VpaWithSelector, ctrlFetcher
 		klog.Errorf("fail to get pod controller: pod=%s err=%s", pod.Name, err.Error())
 		return nil
 	}
+	if parentController == nil {
+		return nil
+	}
 
 	var controlling *VpaWithSelector
 	var controllingVpa *vpa_types.VerticalPodAutoscaler