Repository: kubernetes
Issue: Crash in fetchGroupVersionResources
Labels: kind/bug,sig/api-machinery,help wanted,triage/accepted

### What happened?

Crash 

Observed a panic: "invalid memory address or nil pointer dereference" (runtime error: invalid memory address or nil pointer dereference)goroutine 462 [running]:k8s.io/apimachinery/pkg/util/runtime.logPanic(0x29dde20, 0x39c6810) /go/pkg/mod/k8s.io/apimachinery@v0.20.4/pkg/util/runtime/runtime.go:74 
0x99k8s.io/apimachinery/pkg/util/runtime.HandleCrash(0x0, 0x0, 0x0) /go/pkg/mod/k8s.io/apimachinery@v0.20.4/pkg/util/runtime/runtime.go:48 
0x89panic(0x29dde20, 0x39c6810) /usr/local/go/src/runtime/panic.go:969 
0x1c7os.OpenFile(0xc000c67b80, 0x9e, 0x0, 0x0, 0x3a3ece0, 0x0, 0x9e) /usr/local/go/src/os/file.go:331 
0xa2os.Open(...) /usr/local/go/src/os/file.go:307k8s.io/client-go/discovery/cached/disk.(*CachedDiscoveryClient).getCachedFile(0xc001c7df00, 0xc000c67b80, 0x9e, 0x0, 0x0, 0x0, 0x0, 0x0) /go/pkg/mod/k8s.io/client-go@v0.20.4/discovery/cached/disk/cached_discovery.go:146 
0x138k8s.io/client-go/discovery/cached/disk.(*CachedDiscoveryClient).ServerResourcesForGroupVersion(0xc001c7df00, 0xc001a716e0, 0x1c, 0x2be9976, 0x1, 0xc00052db70) /go/pkg/mod/k8s.io/client-go@v0.20.4/discovery/cached/disk/cached_discovery.go:66 
0x10ck8s.io/client-go/discovery.fetchGroupVersionResources.func1(0xc00052de80, 0x2e85aa0, 0xc001c7df00, 0xc001a703e0, 0x14, 0xc00052db70, 0x7, 0xc00052de90, 0xc0003e3f20, 0xc0003e3e90) /go/pkg/mod/k8s.io/client-go@v0.20.4/discovery/discovery_client.go:361 
0xf2created by k8s.io/client-go/discovery.fetchGroupVersionResources /go/pkg/mod/k8s.io/client-go@v0.20.4/discovery/discovery_client.go:357 


0x28apanic: runtime error: invalid memory address or nil pointer dereference [recovered] panic: runtime error: invalid memory address or nil pointer dereference[signal 0xc0000005 code=0x0 addr=0x0 pc=0x14974e2]goroutine 462 [running]:k8s.io/apimachinery/pkg/util/runtime.HandleCrash(0x0, 0x0, 0x0) /go/pkg/mod/k8s.io/apimachinery@v0.20.4/pkg/util/runtime/runtime.go:55 
0x10cpanic(0x29dde20, 0x39c6810) /usr/local/go/src/runtime/panic.go:969 
0x1c7os.OpenFile(0xc000c67b80, 0x9e, 0x0, 0x0, 0x3a3ece0, 0x0, 0x9e) /usr/local/go/src/os/file.go:331 
0xa2os.Open(...) /usr/local/go/src/os/file.go:307k8s.io/client-go/discovery/cached/disk.(*CachedDiscoveryClient).getCachedFile(0xc001c7df00, 0xc000c67b80, 0x9e, 0x0, 0x0, 0x0, 0x0, 0x0) /go/pkg/mod/k8s.io/client-go@v0.20.4/discovery/cached/disk/cached_discovery.go:146 
0x138k8s.io/client-go/discovery/cached/disk.(*CachedDiscoveryClient).ServerResourcesForGroupVersion(0xc001c7df00, 0xc001a716e0, 0x1c, 0x2be9976, 0x1, 0xc00052db70) /go/pkg/mod/k8s.io/client-go@v0.20.4/discovery/cached/disk/cached_discovery.go:66 
0x10ck8s.io/client-go/discovery.fetchGroupVersionResources.func1(0xc00052de80, 0x2e85aa0, 0xc001c7df00, 0xc001a703e0, 0x14, 0xc00052db70, 0x7, 0xc00052de90, 0xc0003e3f20, 0xc0003e3e90) /go/pkg/mod/k8s.io/client-go@v0.20.4/discovery/discovery_client.go:361 
0xf2created by k8s.io/client-go/discovery.fetchGroupVersionResources /go/pkg/mod/k8s.io/client-go@v0.20.4/discovery/discovery_client.go:357 
0x28a


### What did you expect to happen?

Expected this to work fine.

### How can we reproduce it (as minimally and precisely as possible)?

Not sure, seems to be a race condition.

### Anything else we need to know?

_No response_

### Kubernetes version

<details>

```console
$ kubectl version
# paste output here
```
Client Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.3", GitCommit:"ca643a4d1f7bfe34773c74f79527be4afd95bf39", GitTreeState:"clean", BuildDate:"2021-07-15T21:04:39Z", GoVersion:"go1.16.6", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.14", GitCommit:"0f77da5bd4809927e15d1658fb4aa8f13ad890a5", GitTreeState:"clean", BuildDate:"2022-06-15T14:11:36Z", GoVersion:"go1.16.15", Compiler:"gc", Platform:"linux/amd64"}
</details>


### Cloud provider

<details>
Azure
</details>


### OS version

<details>

```console
# On Linux:
$ cat /etc/os-release
# paste output here
$ uname -a
# paste output here

# On Windows:
C:\> wmic os get Caption, Version, BuildNumber, OSArchitecture
# paste output here
```

</details>


### Install tools

<details>

</details>


### Container runtime (CRI) and version (if applicable)

<details>

</details>


### Related plugins (CNI, CSI, ...) and versions (if applicable)

<details>

</details>