Use metav1.*Options in genericapiserver

Treat DeleteOptions as unversioned in metainternalversion for decoding
of bodies from older clients. Use the metav1 Options structs from
generic api server and the appropriate codec.
This commit is contained in:
Clayton Coleman 2017-01-24 17:49:40 -05:00
parent ff67d8218e
commit a591242061
No known key found for this signature in database
GPG Key ID: 3D16906B4F1C5CB3
4 changed files with 23 additions and 10 deletions

View File

@ -1994,7 +1994,7 @@ func TestDeleteWithOptionsQuery(t *testing.T) {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
} }
if res.StatusCode != http.StatusOK { if res.StatusCode != http.StatusOK {
t.Errorf("unexpected response: %s %#v", request.URL, res) t.Fatalf("unexpected response: %s %#v", request.URL, res)
s, err := ioutil.ReadAll(res.Body) s, err := ioutil.ReadAll(res.Body)
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
@ -2002,7 +2002,7 @@ func TestDeleteWithOptionsQuery(t *testing.T) {
t.Logf(string(s)) t.Logf(string(s))
} }
if simpleStorage.deleted != ID { if simpleStorage.deleted != ID {
t.Errorf("Unexpected delete: %s, expected %s", simpleStorage.deleted, ID) t.Fatalf("Unexpected delete: %s, expected %s", simpleStorage.deleted, ID)
} }
simpleStorage.deleteOptions.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{}) simpleStorage.deleteOptions.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{})
if !api.Semantic.DeepEqual(simpleStorage.deleteOptions, item) { if !api.Semantic.DeepEqual(simpleStorage.deleteOptions, item) {

View File

@ -140,7 +140,7 @@ func GetResource(r rest.Getter, e rest.Exporter, scope RequestScope) restful.Rou
options := metav1.GetOptions{} options := metav1.GetOptions{}
if values := req.Request.URL.Query(); len(values) > 0 { if values := req.Request.URL.Query(); len(values) > 0 {
exports := metav1.ExportOptions{} exports := metav1.ExportOptions{}
if err := scope.ParameterCodec.DecodeParameters(values, schema.GroupVersion{Version: "v1"}, &exports); err != nil { if err := metainternalversion.ParameterCodec.DecodeParameters(values, scope.MetaGroupVersion, &exports); err != nil {
return nil, err return nil, err
} }
if exports.Export { if exports.Export {
@ -149,7 +149,7 @@ func GetResource(r rest.Getter, e rest.Exporter, scope RequestScope) restful.Rou
} }
return e.Export(ctx, name, exports) return e.Export(ctx, name, exports)
} }
if err := scope.ParameterCodec.DecodeParameters(values, schema.GroupVersion{Version: "v1"}, &options); err != nil { if err := metainternalversion.ParameterCodec.DecodeParameters(values, scope.MetaGroupVersion, &options); err != nil {
return nil, err return nil, err
} }
} }
@ -818,13 +818,15 @@ func DeleteResource(r rest.GracefulDeleter, allowsOptions bool, scope RequestSco
return return
} }
if len(body) > 0 { if len(body) > 0 {
s, err := negotiation.NegotiateInputSerializer(req.Request, scope.Serializer) s, err := negotiation.NegotiateInputSerializer(req.Request, metainternalversion.Codecs)
if err != nil { if err != nil {
scope.err(err, res.ResponseWriter, req.Request) scope.err(err, res.ResponseWriter, req.Request)
return return
} }
defaultGVK := scope.Kind.GroupVersion().WithKind("DeleteOptions") // For backwards compatibility, we need to allow existing clients to submit per group DeleteOptions
obj, _, err := scope.Serializer.DecoderToVersion(s.Serializer, defaultGVK.GroupVersion()).Decode(body, &defaultGVK, options) // It is also allowed to pass a body with meta.k8s.io/v1.DeleteOptions
defaultGVK := scope.MetaGroupVersion.WithKind("DeleteOptions")
obj, _, err := metainternalversion.Codecs.DecoderToVersion(s.Serializer, defaultGVK.GroupVersion()).Decode(body, &defaultGVK, options)
if err != nil { if err != nil {
scope.err(err, res.ResponseWriter, req.Request) scope.err(err, res.ResponseWriter, req.Request)
return return
@ -835,7 +837,7 @@ func DeleteResource(r rest.GracefulDeleter, allowsOptions bool, scope RequestSco
} }
} else { } else {
if values := req.Request.URL.Query(); len(values) > 0 { if values := req.Request.URL.Query(); len(values) > 0 {
if err := scope.ParameterCodec.DecodeParameters(values, scope.Kind.GroupVersion(), options); err != nil { if err := metainternalversion.ParameterCodec.DecodeParameters(values, scope.MetaGroupVersion, options); err != nil {
scope.err(err, res.ResponseWriter, req.Request) scope.err(err, res.ResponseWriter, req.Request)
return return
} }

View File

@ -29,8 +29,8 @@ const GroupName = "meta.k8s.io"
// Scheme is the registry for any type that adheres to the meta API spec. // Scheme is the registry for any type that adheres to the meta API spec.
var scheme = runtime.NewScheme() var scheme = runtime.NewScheme()
// Codecs provides access to encoding and decoding for the scheme // Codecs provides access to encoding and decoding for the scheme.
var codecs = serializer.NewCodecFactory(scheme) var Codecs = serializer.NewCodecFactory(scheme)
// SchemeGroupVersion is group version used to register these objects // SchemeGroupVersion is group version used to register these objects
var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: runtime.APIVersionInternal} var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: runtime.APIVersionInternal}
@ -61,9 +61,16 @@ func addToGroupVersion(scheme *runtime.Scheme, groupVersion schema.GroupVersion)
Convert_internalversion_ListOptions_To_v1_ListOptions, Convert_internalversion_ListOptions_To_v1_ListOptions,
Convert_v1_ListOptions_To_internalversion_ListOptions, Convert_v1_ListOptions_To_internalversion_ListOptions,
) )
// ListOptions is the only options struct which needs conversion (it exposes labels and fields
// as selectors for convenience). The other types have only a single representation today.
scheme.AddKnownTypes(SchemeGroupVersion, scheme.AddKnownTypes(SchemeGroupVersion,
&ListOptions{}, &ListOptions{},
&metav1.GetOptions{},
&metav1.ExportOptions{},
&metav1.DeleteOptions{},
) )
// Allow delete options to be decoded across all version in this scheme (we may want to be more clever than this)
scheme.AddUnversionedTypes(SchemeGroupVersion, &metav1.DeleteOptions{})
metav1.AddToGroupVersion(scheme, metav1.SchemeGroupVersion) metav1.AddToGroupVersion(scheme, metav1.SchemeGroupVersion)
return nil return nil
} }

View File

@ -42,6 +42,7 @@ func AddToGroupVersion(scheme *runtime.Scheme, groupVersion schema.GroupVersion)
schema.GroupVersion{Group: groupVersion.Group, Version: runtime.APIVersionInternal}.WithKind(WatchEventKind), schema.GroupVersion{Group: groupVersion.Group, Version: runtime.APIVersionInternal}.WithKind(WatchEventKind),
&InternalEvent{}, &InternalEvent{},
) )
// Supports legacy code paths, most callers should use metav1.ParameterCodec for now
scheme.AddKnownTypes(groupVersion, scheme.AddKnownTypes(groupVersion,
&ListOptions{}, &ListOptions{},
&ExportOptions{}, &ExportOptions{},
@ -65,5 +66,8 @@ var ParameterCodec = runtime.NewParameterCodec(scheme)
func init() { func init() {
scheme.AddUnversionedTypes(SchemeGroupVersion, scheme.AddUnversionedTypes(SchemeGroupVersion,
&ListOptions{}, &ListOptions{},
&ExportOptions{},
&GetOptions{},
&DeleteOptions{},
) )
} }