Repository: kubernetes/kubernetes (language: go)

Issue:
API emulation versioning seems break Cohabitating Resources overwriting
### What happened?

I have an extension apiserver, and I was trying to upgrading the dependency `k8s.io/apiserver`.  In our code, we have a serval resources has its `Cohabitating Resources`, and we expect the storage version is their `Cohabitating Resources`.

for example, we have a resource `machine` in both `raw` and `core` group, and they are wired up by `AddCohabitatingResources` method, and expected encoded as `raw` to storage.
```
storageFactory.AddCohabitatingResources(raw.Resource("machines"), core.Resource("machines"))
```

However, this seems broken after upgrade to v1.31, so I did some invertgation by myself, and I find the storage version is changed by `BackwardCompatibileStorageEncodingFor`  here:
https://github.com/kubernetes/kubernetes/blob/403301bfdf2c7312591077827abd2e72f445a53a/staging/src/k8s.io/apiserver/pkg/server/storage/storage_factory.go#L244-L257

In old behavior `StorageEncodingFor` honor `chosenStorageResource`, but now `BackwardCompatibileStorageEncodingFor` don't.

### What did you expect to happen?

storage version semantic of `Cohabitation Resources` would be honored.

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

there is not easy approach, need to create an extension api server and find out. 

### Anything else we need to know?

_No response_

### Kubernetes version

<details>

Client Version: v1.30.1
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
Server Version: v1.31.0-beta.0.25+a70cb76847ada1-dirty

</details>


### Cloud provider

<details>
NONE
</details>


### OS version

<details>

Kind Cluster run on Darwin

Darwin ReficuldeMacBook-Pro.local 23.0.0 Darwin Kernel Version 23.0.0: Fri Sep 15 14:41:34 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T8103 arm64

</details>


### Install tools

<details>
kind
</details>


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

<details>
</details>


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

<details>

</details>



Discussion / hints:
This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the `triage/accepted` label and provide further guidance.

The `triage/accepted` label can be added by org members by writing `/triage accepted` in a comment.


<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes-sigs/prow](https://github.com/kubernetes-sigs/prow/issues/new?title=Prow%20issue:) repository.
</details>
/sig api-machinery
PTAL @siyuanfoundation 

cc @jpbetz

Fix patch:
diff --git a/staging/src/k8s.io/apiserver/pkg/server/storage/resource_encoding_config.go b/staging/src/k8s.io/apiserver/pkg/server/storage/resource_encoding_config.go
index d73c8e62c61a8..f53fdb832c486 100644
--- a/staging/src/k8s.io/apiserver/pkg/server/storage/resource_encoding_config.go
+++ b/staging/src/k8s.io/apiserver/pkg/server/storage/resource_encoding_config.go
@@ -130,13 +130,24 @@ func emulatedStorageVersion(binaryVersionOfResource schema.GroupVersion, example
 	gvks, _, err := scheme.ObjectKinds(example)
 	if err != nil {
 		return schema.GroupVersion{}, err
-	} else if len(gvks) == 0 {
-		// Probably shouldn't happen if err is non-nil
+	}
+
+	var gvk schema.GroupVersionKind
+	for _, item := range gvks {
+		if item.Group != binaryVersionOfResource.Group {
+			continue
+		}
+
+		gvk = item
+		break
+	}
+
+	if len(gvk.Kind) == 0 {
 		return schema.GroupVersion{}, fmt.Errorf("object %T has no GVKs registered in scheme", example)
 	}
 
 	// VersionsForGroupKind returns versions in priority order
-	versions := scheme.VersionsForGroupKind(schema.GroupKind{Group: gvks[0].Group, Kind: gvks[0].Kind})
+	versions := scheme.VersionsForGroupKind(schema.GroupKind{Group: gvk.Group, Kind: gvk.Kind})
 
 	compatibilityVersion := effectiveVersion.MinCompatibilityVersion()
 
@@ -148,7 +159,7 @@ func emulatedStorageVersion(binaryVersionOfResource schema.GroupVersion, example
 		gvk := schema.GroupVersionKind{
 			Group:   gv.Group,
 			Version: gv.Version,
-			Kind:    gvks[0].Kind,
+			Kind:    gvk.Kind,
 		}
 
 		exampleOfGVK, err := scheme.New(gvk)
diff --git a/staging/src/k8s.io/apiserver/pkg/server/storage/storage_factory.go b/staging/src/k8s.io/apiserver/pkg/server/storage/storage_factory.go
index ad01c5a5d8940..f4ccc62f65c9f 100644
--- a/staging/src/k8s.io/apiserver/pkg/server/storage/storage_factory.go
+++ b/staging/src/k8s.io/apiserver/pkg/server/storage/storage_factory.go
@@ -245,7 +245,7 @@ func (s *DefaultStorageFactory) NewConfig(groupResource schema.GroupResource, ex
 
 	var err error
 	if backwardCompatibleInterface, ok := s.ResourceEncodingConfig.(CompatibilityResourceEncodingConfig); ok {
-		codecConfig.StorageVersion, err = backwardCompatibleInterface.BackwardCompatibileStorageEncodingFor(groupResource, example)
+		codecConfig.StorageVersion, err = backwardCompatibleInterface.BackwardCompatibileStorageEncodingFor(chosenStorageResource, example)
 		if err != nil {
 			return nil, err
 		}


Test patch: