diff --git a/pkg/endpoints/handlers/create.go b/pkg/endpoints/handlers/create.go index e40e4288a..62a80ad49 100644 --- a/pkg/endpoints/handlers/create.go +++ b/pkg/endpoints/handlers/create.go @@ -77,7 +77,7 @@ func createHandler(r rest.NamedCreater, scope RequestScope, admit admission.Inte scope.err(err, w, req) return } - decoder := scope.Serializer.DecoderToVersion(s.Serializer, schema.GroupVersion{Group: gv.Group, Version: runtime.APIVersionInternal}) + decoder := scope.Serializer.DecoderToVersion(s.Serializer, scope.HubGroupVersion) body, err := readBody(req) if err != nil { diff --git a/pkg/endpoints/handlers/patch.go b/pkg/endpoints/handlers/patch.go index 4163b1826..5c7ecb74e 100644 --- a/pkg/endpoints/handlers/patch.go +++ b/pkg/endpoints/handlers/patch.go @@ -118,9 +118,10 @@ func PatchResource(r rest.Patcher, scope RequestScope, admit admission.Interface return } gv := scope.Kind.GroupVersion() + codec := runtime.NewCodec( scope.Serializer.EncoderForVersion(s.Serializer, gv), - scope.Serializer.DecoderToVersion(s.Serializer, schema.GroupVersion{Group: gv.Group, Version: runtime.APIVersionInternal}), + scope.Serializer.DecoderToVersion(s.Serializer, scope.HubGroupVersion), ) userInfo, _ := request.UserFrom(ctx) @@ -163,6 +164,8 @@ func PatchResource(r rest.Patcher, scope RequestScope, admit admission.Interface kind: scope.Kind, resource: scope.Resource, + hubGroupVersion: scope.HubGroupVersion, + createValidation: rest.AdmissionToValidateObjectFunc(admit, staticAdmissionAttributes), updateValidation: rest.AdmissionToValidateObjectUpdateFunc(admit, staticAdmissionAttributes), admissionCheck: admissionCheck, @@ -218,6 +221,8 @@ type patcher struct { resource schema.GroupVersionResource kind schema.GroupVersionKind + hubGroupVersion schema.GroupVersion + // Validation functions createValidation rest.ValidateObjectFunc updateValidation rest.ValidateObjectUpdateFunc @@ -315,9 +320,8 @@ func (p *smpPatcher) applyPatchToCurrentObject(currentObject runtime.Object) (ru if err := strategicPatchObject(p.defaulter, currentVersionedObject, p.patchJS, versionedObjToUpdate, p.schemaReferenceObj); err != nil { return nil, err } - // Convert the object back to unversioned (aka internal version). - gvk := p.kind.GroupKind().WithVersion(runtime.APIVersionInternal) - return p.unsafeConvertor.ConvertToVersion(versionedObjToUpdate, gvk.GroupVersion()) + // Convert the object back to the hub version + return p.unsafeConvertor.ConvertToVersion(versionedObjToUpdate, p.hubGroupVersion) } // strategicPatchObject applies a strategic merge patch of to diff --git a/pkg/endpoints/handlers/rest.go b/pkg/endpoints/handlers/rest.go index 8b3ca9d62..daa7c76cc 100644 --- a/pkg/endpoints/handlers/rest.go +++ b/pkg/endpoints/handlers/rest.go @@ -65,6 +65,9 @@ type RequestScope struct { Subresource string MetaGroupVersion schema.GroupVersion + + // HubGroupVersion indicates what version objects read from etcd or incoming requests should be converted to for in-memory handling. + HubGroupVersion schema.GroupVersion } func (scope *RequestScope) err(err error, w http.ResponseWriter, req *http.Request) { diff --git a/pkg/endpoints/handlers/rest_test.go b/pkg/endpoints/handlers/rest_test.go index b190f8d29..66d9e9c2c 100644 --- a/pkg/endpoints/handlers/rest_test.go +++ b/pkg/endpoints/handlers/rest_test.go @@ -367,6 +367,7 @@ func (tc *patchTestCase) Run(t *testing.T) { kind := examplev1.SchemeGroupVersion.WithKind("Pod") resource := examplev1.SchemeGroupVersion.WithResource("pods") schemaReferenceObj := &examplev1.Pod{} + hubVersion := example.SchemeGroupVersion for _, patchType := range []types.PatchType{types.JSONPatchType, types.MergePatchType, types.StrategicMergePatchType} { // This needs to be reset on each iteration. @@ -439,6 +440,8 @@ func (tc *patchTestCase) Run(t *testing.T) { kind: kind, resource: resource, + hubGroupVersion: hubVersion, + createValidation: rest.ValidateAllObjectFunc, updateValidation: admissionValidation, admissionCheck: admissionMutation, diff --git a/pkg/endpoints/handlers/update.go b/pkg/endpoints/handlers/update.go index 19d23e1f2..3c0139d3d 100644 --- a/pkg/endpoints/handlers/update.go +++ b/pkg/endpoints/handlers/update.go @@ -89,8 +89,9 @@ func UpdateResource(r rest.Updater, scope RequestScope, admit admission.Interfac } defaultGVK := scope.Kind original := r.New() + trace.Step("About to convert to expected version") - decoder := scope.Serializer.DecoderToVersion(s.Serializer, schema.GroupVersion{Group: defaultGVK.Group, Version: runtime.APIVersionInternal}) + decoder := scope.Serializer.DecoderToVersion(s.Serializer, scope.HubGroupVersion) obj, gvk, err := decoder.Decode(body, &defaultGVK, original) if err != nil { err = transformDecodeError(scope.Typer, err, original, gvk, body) diff --git a/pkg/endpoints/installer.go b/pkg/endpoints/installer.go index 19bb5e55a..bbf905d0d 100644 --- a/pkg/endpoints/installer.go +++ b/pkg/endpoints/installer.go @@ -506,6 +506,8 @@ func (a *APIInstaller) registerResourceHandlers(path string, storage rest.Storag Subresource: subresource, Kind: fqKindToRegister, + HubGroupVersion: schema.GroupVersion{Group: fqKindToRegister.Group, Version: runtime.APIVersionInternal}, + MetaGroupVersion: metav1.SchemeGroupVersion, } if a.group.MetaGroupVersion != nil {